From 41ce65d9ae3e75cd374b6f5f99486ee057194a06 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 10 Feb 2009 18:32:04 -0500 Subject: Engine: Added some control to Music MapViewGameState now plays music depending on the variables of its Map, which can tell it to either stop the music, keep the previous music playing or start playing a specified music. --- src/com/fourisland/fourpuzzle/Audio.java | 18 +++++++++++++-- .../fourpuzzle/gamestate/mapview/Map.java | 24 ++++++++++++++++++- .../fourpuzzle/gamestate/mapview/MapMusicType.java | 27 ++++++++++++++++++++++ .../gamestate/mapview/MapViewGameState.java | 11 ++++----- .../gamestate/mapview/event/SpecialEvent.java | 22 ++++++++++++++++++ 5 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/MapMusicType.java (limited to 'src/com') diff --git a/src/com/fourisland/fourpuzzle/Audio.java b/src/com/fourisland/fourpuzzle/Audio.java index 5fe0dfc..3e40e6a 100755 --- a/src/com/fourisland/fourpuzzle/Audio.java +++ b/src/com/fourisland/fourpuzzle/Audio.java @@ -5,7 +5,6 @@ package com.fourisland.fourpuzzle; -import com.fourisland.fourpuzzle.util.MidiParser; import com.fourisland.fourpuzzle.util.ObjectLoader; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -14,6 +13,7 @@ import java.util.logging.Logger; import javax.sound.midi.InvalidMidiDataException; import javax.sound.midi.MidiSystem; import javax.sound.midi.MidiUnavailableException; +import javax.sound.midi.Sequence; import javax.sound.midi.Sequencer; import javax.sound.sampled.Clip; @@ -64,8 +64,16 @@ public class Audio { public static void playMusic(String file, boolean loop, float speed) { + Sequence s = ObjectLoader.getMusic(file); + + if ((seq.getSequence() != null) && (seq.getSequence().equals(s))) + { + return; + } + try { - seq.setSequence(ObjectLoader.getMusic(file)); + seq.setSequence(s); + seq.setTickPosition(0); if (loop) { seq.setLoopCount(seq.LOOP_CONTINUOUSLY); @@ -86,6 +94,12 @@ public class Audio { if (seq != null) { seq.stop(); + + try { + seq.setSequence((Sequence) null); + } catch (InvalidMidiDataException ex) { + Logger.getLogger(Audio.class.getName()).log(Level.SEVERE, null, ex); + } } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java index d02c7ed..bb40b39 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java @@ -182,9 +182,31 @@ public abstract class Map { { return music; } - public void setMusic(String music) + + /** + * Sets the name of the Music file to play when this Map loads + * + * When this function is run, it also sets the MusicType to Specified + * automatically as the only time this function would be used would be when + * the MusicType was Specified. + * + * @param music + */ + protected void setMusic(String music) { this.music = music; + this.musicType = MapMusicType.Specified; + } + + private MapMusicType musicType = MapMusicType.NoChange; + public MapMusicType getMusicType() + { + return musicType; + } + + protected void setMusicType(MapMusicType musicType) + { + this.musicType = musicType; } BufferedImage lowerLayer = null; diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapMusicType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapMusicType.java new file mode 100644 index 0000000..52fd712 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapMusicType.java @@ -0,0 +1,27 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview; + +/** + * + * @author hatkirby + */ +public enum MapMusicType { + /** + * NoMusic represents the instance where no Music should be played + */ + NoMusic, + /** + * NoChange represents the instance where the currently playing Music (or + * silence) should be kept + */ + NoChange, + /** + * Specified represents the instance where a specified Music file should be + * played + */ + Specified +} diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 9924c9e..52a56c4 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -50,18 +50,17 @@ public class MapViewGameState implements GameState { public void initalize() { - //if (!currentMap.getMusic().equals("")) + switch (currentMap.getMusicType()) { - // Audio.playMusic(currentMap.getMusic()); + case NoMusic: Audio.stopMusic(); break; + case NoChange: break; + case Specified: Audio.playMusic(currentMap.getMusic()); break; } } public void deinitalize() { - //if (!currentMap.getMusic().equals("")) - { - Audio.stopMusic(); - } + // Do nothing, yet } public void processInput() diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index 22e464d..7ca08ff 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -298,4 +298,26 @@ public class SpecialEvent { })); } + /** + * Starts playing a Music file + * + * @param filename The name of the Music file to play + * @param loop Whether or not you want this Music to loop + * @param speed The Tempo Factor. If this is 1, the Music will play at + * normal speed. If this is 2, the Music will play at twice the normal + * speed, and so on. + */ + public void PlayMusic(String filename, boolean loop, float speed) + { + Audio.playMusic(filename, loop, speed); + } + + /** + * Stops playing the currently playing Music file + */ + public void StopMusic() + { + Audio.stopMusic(); + } + } -- cgit 1.4.1