Skip to content

Commit

Permalink
Added sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
theKidOfArcrania committed Jan 21, 2018
1 parent 0dadf50 commit 4749fde
Show file tree
Hide file tree
Showing 32 changed files with 155 additions and 38 deletions.
Binary file added Classic.mtp
Binary file not shown.
6 changes: 4 additions & 2 deletions Convertor/src/turtle/editor/TMXTestLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import turtle.ui.MainApp;

import java.io.File;
import java.util.UUID;

/**
* @author Henry Wang
Expand Down Expand Up @@ -36,10 +37,11 @@ public static void main(String args[]) throws Exception {

System.setProperty("user.dir", lvlFile.getParent());

Launcher.testing = new LevelPack("Testing");
LevelPack pack = Launcher.testing = new LevelPack("Testing");
pack.setLevelPackID(new UUID(0, 0));

Level lvl = TMXToMTP.loadLevel(lvlFile);
Launcher.testing.addLevel(lvl);
pack.addLevel(lvl);

Application.launch(Launcher.class);
}
Expand Down
1 change: 1 addition & 0 deletions Main/Main.iml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
Binary file added Main/res/turtle/comp/blowing.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/click.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/explosion.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/grass.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/splash.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/steam.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/tap.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/unlock.wav
Binary file not shown.
Binary file added Main/res/turtle/comp/whip.wav
Binary file not shown.
File renamed without changes.
1 change: 1 addition & 0 deletions Main/src/turtle/comp/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Bucket extends Mover {
public boolean die(Component attacker) {
if (attacker instanceof Water) {
if (!isFilled()) {
playSound(Sounds.SPLASH);
setFilled(true);
((Water) attacker).transformToSand();
}
Expand Down
1 change: 1 addition & 0 deletions Main/src/turtle/comp/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void setLinked(Location linkedLocation) {
@Override
public boolean interact(Actor other) {
Grid parent = getParentGrid();
playSound(Sounds.CLICK);
if (parent != null && parent.isValidLocation(linkedLocation)) {
Cell factory = parent.getCellAt(linkedLocation);
if (factory instanceof Factory) {
Expand Down
2 changes: 2 additions & 0 deletions Main/src/turtle/comp/Cannon.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ private void shoot() {
return;
}

playSound(Sounds.EXPLOSION);

Direction heading = getHeading();
Location loc = new Location(getHeadLocation());
heading.traverse(loc);
Expand Down
1 change: 1 addition & 0 deletions Main/src/turtle/comp/Door.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public boolean checkInteract(Actor other) {
if (other instanceof Player) {
for (Item itm : ((Player) other).getPocket()) {
if (itm instanceof Key && ((Key) itm).getColor() == getColor()) {
playSound(Sounds.UNLOCK);
die(this);
return true;
}
Expand Down
1 change: 1 addition & 0 deletions Main/src/turtle/comp/Exit.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void updateFrame(long frame) {
super.updateFrame(frame);
if (winner != null && winner.getTrailingLocation().equals(
getHeadLocation())) {
//TODO: sound
winner.win();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Main/src/turtle/comp/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Factory extends Cell {
*/
public static final int DEFAULT_IMAGE = 70;
private static final int FACTORY_OFFSET_IMAGE = DEFAULT_IMAGE;
private static final double RATIO_CLONE_IMG = .8;
private static final double RATIO_CLONE_IMG = .7;
private static final long serialVersionUID = -8544196441607182765L;

/**
Expand Down
5 changes: 4 additions & 1 deletion Main/src/turtle/comp/Fire.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public boolean pass(Actor visitor) {
if (smoking) {
return true;
}
visitor.die(this);
if (visitor.die(this)) {
playSound(Sounds.STEAM);
Sounds.GRASS.play();
}
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion Main/src/turtle/comp/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public boolean checkInteract(Actor other) {
*/
@Override
public boolean interact(Actor other) {
return other instanceof Player && super.interact(other);
if (other instanceof Player) {
//TODO: sound
return super.interact(other);
}
return false;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions Main/src/turtle/comp/Grass.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public void updateFrame(long frame) {
if (dr + dc <= 1) {
fading = 0;
animateFrames(TRANSFORM_FRAMES, false);
if (!Sounds.GRASS.isPlaying())
playSound(Sounds.GRASS);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Main/src/turtle/comp/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public abstract class Item extends Actor {
@Override
public boolean interact(Actor other) {
if (other instanceof Player) {
//if (getCurrentClip() == null)
playSound(Sounds.TAP);
Player p = (Player) other;
if (p.collectItem(this)) {
getParentGrid().removeActor(this);
Expand Down
32 changes: 32 additions & 0 deletions Main/src/turtle/comp/Sounds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package turtle.comp;

import javafx.scene.media.AudioClip;


import static java.lang.ClassLoader.getSystemResource;

/**
* Contains all the default sounds that can be used for various interactions
* @author Henry Wang
*/
public interface Sounds {
AudioClip BLOWING = loadClip("turtle/comp/blowing.wav");
AudioClip CLICK = loadClip("turtle/comp/click.wav");
AudioClip EXPLOSION = loadClip("turtle/comp/explosion.wav");
AudioClip GRASS = loadClip("turtle/comp/grass.wav");
AudioClip SPLASH = loadClip("turtle/comp/splash.wav");
AudioClip STEAM = loadClip("turtle/comp/steam.wav");
AudioClip TAP = loadClip("turtle/comp/tap.wav");
AudioClip UNLOCK = loadClip("turtle/comp/unlock.wav");
AudioClip WHIP = loadClip("turtle/comp/whip.wav");

/**
* Loads an audio clip from the classpath
* @param res the classpath of the audio clip
* @return the loaded audio clip
*/
static AudioClip loadClip(String res) {

return new AudioClip(getSystemResource(res).toExternalForm());
}
}
5 changes: 3 additions & 2 deletions Main/src/turtle/comp/Trap.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Trap extends Actor {
*/
@Override
public boolean checkInteract(Actor other) {
return true;
return !(other instanceof Trap);
}

/**
Expand All @@ -53,6 +53,7 @@ public boolean checkInteract(Actor other) {
@Override
public boolean interact(Actor other) {
if (other.die(this)) {
playSound(Sounds.WHIP);
die(this);
}
return true;
Expand All @@ -67,7 +68,7 @@ public boolean interact(Actor other) {
*/
@Override
public DominanceLevel dominanceLevelFor(Actor other) {
return FIXTURE;
return FIXTURE_TRAP;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion Main/src/turtle/comp/Water.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package turtle.comp;

import javafx.animation.Transition;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
import turtle.core.Actor;
import turtle.core.Cell;
import turtle.core.TileSet;
Expand All @@ -26,6 +28,7 @@ public class Water extends Cell {
private static final int MAX_TRANSFORM = TRANSFORM_ANIMATION_FRAME.length *
DEF_ANIMATION_FRAME_CHANGE;
private static final long serialVersionUID = 991189208764206004L;
private static final Duration SPLASH_DELAY = Duration.millis(100);

/**
* Determines the associated attributes with a tile if the tile is related to this object.
Expand Down Expand Up @@ -63,7 +66,9 @@ public Water() {
*/
@Override
public boolean pass(Actor visitor) {
visitor.die(this);
if (visitor.die(this)) {
playSound(Sounds.SPLASH);
}
return true;
}

Expand Down
1 change: 1 addition & 0 deletions Main/src/turtle/core/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class Actor extends Component {
public static final DominanceLevel ENEMY = new DominanceLevel("Enemy", 100);
public static final DominanceLevel MOVER = new DominanceLevel("Mover", 200);
public static final DominanceLevel ITEM = new DominanceLevel("Item", 300);
public static final DominanceLevel FIXTURE_TRAP = new DominanceLevel("Fixture_Trap", 350);
public static final DominanceLevel FIXTURE = new DominanceLevel("Fixture", 400);

private static final long serialVersionUID = -8229684437846026366L;
Expand Down
93 changes: 72 additions & 21 deletions Main/src/turtle/core/Component.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package turtle.core;

import javafx.application.Platform;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.media.AudioClip;
import turtle.attributes.Attributable;
import turtle.attributes.AttributeSet;
import turtle.attributes.NotAttribute;
import turtle.comp.Sounds;

import java.io.IOException;
import java.io.ObjectInputStream;
Expand All @@ -26,6 +29,33 @@
*/
public abstract class Component extends Pane implements Attributable,
Serializable {
public static final int DEFAULT_IMAGE = -1;

public static final int DEF_ANIMATION_FRAME_CHANGE = 4;
public static final TileSet DEFAULT_SET = new TileSet();
public static final int BIG_FRAME = 10;

private static final int INVALID_IMAGE_FRAME = -2;
private static final long serialVersionUID = -65657197093045828L;

private static final int SHUFFLE = 50;
private static AudioClip currentClip = null;

static {
//Initialize all the sounds
for (Field fld : Sounds.class.getFields()) {
try {
Object val = fld.get(null);
if (val instanceof AudioClip) {
((AudioClip) val).play(0);
((AudioClip) val).stop();
}
} catch (IllegalAccessException e) {
throw new Error(e);
}
}
}

/**
* Obtains the default image of this class. If this class does not define
* a default image, this will search up the hierarchy until the field is
Expand Down Expand Up @@ -55,9 +85,9 @@ public static int getDefaultImage(Class<? extends Component> comp) {
return DEFAULT_IMAGE;
}

public static final int DEFAULT_IMAGE = -1;
private static final int INVALID_IMAGE_FRAME = -2;
private static final long serialVersionUID = -65657197093045828L;
public static AudioClip getCurrentClip() {
return (currentClip != null && currentClip.isPlaying()) ? currentClip : null;
}

/**
* Utility method used to shuffle an array.
Expand Down Expand Up @@ -91,11 +121,6 @@ public static void shuffle(Object[] arr, Random rng) {
}
}

public static final int DEF_ANIMATION_FRAME_CHANGE = 4;
public static final TileSet DEFAULT_SET = new TileSet();
public static final int BIG_FRAME = 10;
private static final int SHUFFLE = 50;

private final Location headLoc;
private final Location trailLoc;
private final AttributeSet<Component> attributes;
Expand Down Expand Up @@ -128,19 +153,6 @@ protected Component() {
attributes = new AttributeSet<>(this);
}

/**
* Initializes the tile-set and image-view. This should ONLY be called
* exactly once in the component's lifetime.
*
* @param ts the tileset to initialize to.
*/
private void initTileSet(TileSet ts) {
this.ts = ts;
currentImage = INVALID_IMAGE_FRAME;
img = new ImageView(ts.getImageSet());
this.getChildren().add(img);
}

/**
* Animates component through a series of frames at the default
* change rate (change once per 4 frames).
Expand Down Expand Up @@ -291,6 +303,45 @@ protected void layoutChildren() {
VPos.CENTER);
}

/**
* Plays an audio clip sound, stopping the previous sound effect.
* @param sound the sound clip to play
*/
protected void playSound(AudioClip sound) {
if (!Platform.isFxApplicationThread()) {
throw new IllegalStateException("Must be on Fx thread");
}

if (getParentGrid() == null || !getParentGrid().isPlaying()) {
return;
}

//if (currentClip != null && currentClip.isPlaying()) {
// currentClip.stop();
//}
if (sound.isPlaying()) {
sound.stop();
}

//TODO: configure volume.

sound.play();
currentClip = sound;
}

/**
* Initializes the tile-set and image-view. This should ONLY be called
* exactly once in the component's lifetime.
*
* @param ts the tileset to initialize to.
*/
private void initTileSet(TileSet ts) {
this.ts = ts;
currentImage = INVALID_IMAGE_FRAME;
img = new ImageView(ts.getImageSet());
this.getChildren().add(img);
}

/**
* Obtains next step of incrementing a value
*
Expand Down
9 changes: 9 additions & 0 deletions Main/src/turtle/core/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Grid extends Pane implements Serializable {
private int timeLeft;
private Direction lastMove;
private final Recording recording;
private boolean playing;


/**
Expand Down Expand Up @@ -181,6 +182,14 @@ public int getTimeLeft() {
return timeLeft;
}

public boolean isPlaying() {
return playing;
}

public void setPlaying(boolean playing) {
this.playing = playing;
}

/**
* Setter method for remaining time
*
Expand Down
Loading

0 comments on commit 4749fde

Please sign in to comment.