Skip to content

Commit

Permalink
Add music
Browse files Browse the repository at this point in the history
  • Loading branch information
Arinerron committed Mar 31, 2017
1 parent e4015e3 commit dc68798
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
2 changes: 2 additions & 0 deletions res/map.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ CONFIG:
# - background.color - The color of the background, as string
# - spawn.x - The X coordinate for spawning
# - spawn.y - The Y coordinate for spawning
# - music.file - The wav file to play, without the extension. filepath in res/mus/
# Note that the coordinates MUST be integers. A tile is 20x20, so if you want to
# set the spawn to third tile over, it would be 20*3, which equals 60.
#
background.color=#000000
spawn.x=23
spawn.y=10
music.file=chip
#
# The configuration ends with the empty line (ex, the one below).

Expand Down
Binary file added res/mus/chip.wav
Binary file not shown.
Binary file added res/mus/derp.wav
Binary file not shown.
8 changes: 7 additions & 1 deletion res/tileset.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#
# Valid case-insensitive commands include:
# - kill; - Kill the player
# - sound=<file>; - Play an audio file. Do not include .wav extension. Path is relative to res/mus/
# - dither=<boolean>; - Enable/Disable dithering while on tile
# - filter=<int>; - Sets the percent darkness to apply
# - teleport=<int/string>; - teleports to the comma separated x,y coordinates. If there is a + or - sign before one of the coordinates, it will teleport relatively. Ex. teleport=5,+1 will go to x=5 y=y+1
Expand All @@ -92,7 +93,7 @@ L tiles/lava fluid nojump dangerous default animation={500,tiles/lava,tiles/lava
C tiles/dirt checkpoint
_ tiles/ice speed=0.4 acceleration=0.0008 nojump slippery particle={color:#A1CAFF,count:1,iteration:35,lifetime:400,front:true,xacceleration:[-0.02|0.02],yacceleration:[-0.02|0.02]}
G tiles/gravel acceleration=0.1 speed=0.5 particle={color:#A9A9A9,count:5,front:false,iteration:35,lifetime:800,xacceleration:[-0.001|0.001],yacceleration:[-0.001|0.001]}
X tiles/gravel teleport=+0,+22
X tiles/gravel event={onentry:b} teleport=+0,+22
~ tiles/test solid

# Path tiles
Expand All @@ -117,6 +118,11 @@ a (
particle={color:#5B2D00,count:1,iteration:25,lifetime:500,xacceleration:[-0.001|0.001],yacceleration:[-0.001|0.001],front:false}
)

b (
a;a;a;a;a;a;a;a;a;a;a;
sound=derp;
)

[entities]

# Default entities
94 changes: 93 additions & 1 deletion src/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.*;
import java.text.*;
import javax.imageio.*;
import javax.sound.sampled.*;
import tileeditor.*;

public class Game extends JPanel {
Expand Down Expand Up @@ -109,6 +110,7 @@ public class Game extends JPanel {
public BufferedImage particles_back = null; // optimize by rendering particles in a separate thread
public BufferedImage particles_front = null;
public String commands = "";
public SoundPlayer mainplayer = null;

public static void main(String[] args) {
if(args.length != 0 && (args[0].equalsIgnoreCase("--tileeditor") || args[0].equalsIgnoreCase("-e")))
Expand Down Expand Up @@ -564,7 +566,13 @@ else if(yacceleration < -speed)
}
}}).start();

// audio playing thread
new Thread(new Runnable() {@Override public void run() {
if(mainplayer != null)
mainplayer.loop();
}}).start();

// F3 statistics-updating thread
new java.util.Timer().scheduleAtFixedRate(new TimerTask() {@Override public void run() {
fps = frames;
frames = 0;
Expand All @@ -573,7 +581,7 @@ else if(yacceleration < -speed)

particlecount = particles.size();
}}, 1000, 1000);

respawn();
}

Expand Down Expand Up @@ -712,6 +720,11 @@ public void loadMap(String name) throws Exception {
this.setSpawn(this.spawnx, -tilesize * Double.parseDouble(val), true);
this.y = this.spawny;
break;
case "music.file":
if(mainplayer != null)
mainplayer.dispose();
this.mainplayer = new SoundPlayer(new File("../res/mus/" + val + ".wav"));
break;
default:
System.out.println("Unknown config option \"" + key + "\" for map file \"" + name + "\".");
break;
Expand Down Expand Up @@ -1393,6 +1406,9 @@ public void executeFunction(String config, String funcname) { // TODO: Make an e
case "dither":
eightbit = Boolean.parseBoolean(val);
break;
case "sound":
new SoundPlayer(new File("../res/mus/" + val + ".wav")).play();
break;
case "filter":
tile.filter = (int)((Double.parseDouble(val)) * 2.54);
tile.filterset = true;
Expand Down Expand Up @@ -1592,6 +1608,82 @@ public Event(String name, Tile tile) {
}
}

class SoundPlayer {
private File file = null;
private Clip clip = null;
private FloatControl controls = null;
private long position = 0;

public SoundPlayer(File file) {
this.file = file;

try {
this.clip = AudioSystem.getClip();
this.clip.open(AudioSystem.getAudioInputStream(file.toURI().toURL().openStream()));
this.controls = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
} catch (Exception e) {
System.err.println("Failed to initialize sound player for file " + file.getAbsolutePath());
e.printStackTrace();
}
}

public void play() {
this.clip.setMicrosecondPosition(position);
this.clip.start();
}

public void stop() {
this.position = 0;
this.clip.stop();
this.clip.flush();
}

public void restart() {
stop();
play();
}

public void loop() {
this.stop();
this.clip.loop(Clip.LOOP_CONTINUOUSLY);
}

public void pause() {
this.position = clip.getMicrosecondPosition();

this.clip.stop();
this.clip.flush();
}

// call this when done with audio
public void dispose() {
try {
this.clip.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
this.clip = null;
}
}

// 0f-1f I think?
public void setVolume(float volume) {
this.controls.setValue((float) Math.min(this.controls.getMaximum(), Math.max(this.controls.getMinimum(), volume)));
}

public float getVolume() {
return this.controls.getValue();
}

public boolean isPlaying() {
return this.clip != null && this.clip.isRunning();
}

public File getFile() {
return this.file;
}
}

class Colors {
// all of the colors in the images
public static final int[] colors = new int[] {
Expand Down

0 comments on commit dc68798

Please sign in to comment.