From 55f1693fb6517544fee7d5bcebeddf3081090cf0 Mon Sep 17 00:00:00 2001 From: Agetian Date: Fri, 23 Nov 2012 14:43:47 +0000 Subject: [PATCH] - Sound System: converted tap/untap sound playback to an event, preliminary support for synced sounds using the event model. --- .gitattributes | 1 + src/main/java/forge/Card.java | 5 +- .../java/forge/game/event/SetTappedEvent.java | 17 ++++ .../java/forge/sound/EventVisualizer.java | 21 ++++- .../java/forge/sound/SoundEffectType.java | 82 +++++++++++-------- 5 files changed, 85 insertions(+), 41 deletions(-) create mode 100644 src/main/java/forge/game/event/SetTappedEvent.java diff --git a/.gitattributes b/.gitattributes index 037cd0dd25f..a822c2ad696 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12952,6 +12952,7 @@ src/main/java/forge/game/event/Event.java -text src/main/java/forge/game/event/LandPlayedEvent.java -text src/main/java/forge/game/event/PoisonCounterEvent.java -text src/main/java/forge/game/event/RemoveCounterEvent.java -text +src/main/java/forge/game/event/SetTappedEvent.java -text src/main/java/forge/game/event/SpellResolvedEvent.java -text src/main/java/forge/game/event/package-info.java -text src/main/java/forge/game/limited/BoosterDeck.java -text diff --git a/src/main/java/forge/Card.java b/src/main/java/forge/Card.java index 453a0ff44b3..f8e85a63431 100644 --- a/src/main/java/forge/Card.java +++ b/src/main/java/forge/Card.java @@ -56,6 +56,7 @@ import forge.card.trigger.TriggerType; import forge.card.trigger.ZCTrigger; import forge.game.GlobalRuleChange; import forge.game.event.AddCounterEvent; +import forge.game.event.SetTappedEvent; import forge.game.phase.Combat; import forge.game.player.ComputerUtil; import forge.game.player.Player; @@ -4901,7 +4902,7 @@ public class Card extends GameEntity implements Comparable { this.setTapped(true); // Play the Tap sound - Singletons.getControl().getSoundSystem().play(SoundEffectType.Tap); + Singletons.getModel().getGame().getEvents().post(new SetTappedEvent(true)); } /** @@ -4917,7 +4918,7 @@ public class Card extends GameEntity implements Comparable { Singletons.getModel().getGame().getTriggerHandler().runTrigger(TriggerType.Untaps, runParams); // Play the Untap sound - Singletons.getControl().getSoundSystem().play(SoundEffectType.Untap); + Singletons.getModel().getGame().getEvents().post(new SetTappedEvent(false)); } for (final Command var : this.untapCommandList) { diff --git a/src/main/java/forge/game/event/SetTappedEvent.java b/src/main/java/forge/game/event/SetTappedEvent.java new file mode 100644 index 00000000000..eed9db14e70 --- /dev/null +++ b/src/main/java/forge/game/event/SetTappedEvent.java @@ -0,0 +1,17 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package forge.game.event; + +/** + * + * @author Agetian + */ +public class SetTappedEvent extends Event { + public final boolean Tapped; + + public SetTappedEvent(boolean tapped) { + Tapped = tapped; + } +} diff --git a/src/main/java/forge/sound/EventVisualizer.java b/src/main/java/forge/sound/EventVisualizer.java index 103e96598e3..a562e11fac8 100644 --- a/src/main/java/forge/sound/EventVisualizer.java +++ b/src/main/java/forge/sound/EventVisualizer.java @@ -12,6 +12,7 @@ import forge.game.event.Event; import forge.game.event.LandPlayedEvent; import forge.game.event.PoisonCounterEvent; import forge.game.event.RemoveCounterEvent; +import forge.game.event.SetTappedEvent; import forge.game.event.SpellResolvedEvent; /** @@ -49,6 +50,9 @@ public class EventVisualizer { return null; } } + if (evt instanceof SetTappedEvent) { + return getSoundEffectForTapState(((SetTappedEvent) evt).Tapped); + } return fromMap; } @@ -90,10 +94,23 @@ public class EventVisualizer { return null; } + /** + * Plays the sound corresponding to the change of the card's tapped state + * (when a card is tapped or untapped). + * + * @param tapped_state if true, the "tap" sound is played; otherwise, the + * "untap" sound is played + * @return the sound effect type + */ + public static SoundEffectType getSoundEffectForTapState(boolean tapped_state) { + return tapped_state ? SoundEffectType.Tap : SoundEffectType.Untap; + } + /** * Plays the sound corresponding to the land type when the land is played. * * @param land the land card that was played + * @return the sound effect type */ public static SoundEffectType getSoundEffectForLand(final Card land) { if (land == null) { @@ -136,9 +153,7 @@ public class EventVisualizer { * Play a specific sound effect based on card's name. * * @param c the card to play the sound effect for. - * @return true if the special effect was found and played, otherwise - * false (in which case the type-based FX will be played, if - * applicable). + * @return the sound effect type */ private static SoundEffectType getSpecificCardEffect(final Card c) { // Implement sound effects for specific cards here, if necessary. diff --git a/src/main/java/forge/sound/SoundEffectType.java b/src/main/java/forge/sound/SoundEffectType.java index b0d81bda865..6c513b4dcb1 100644 --- a/src/main/java/forge/sound/SoundEffectType.java +++ b/src/main/java/forge/sound/SoundEffectType.java @@ -20,10 +20,13 @@ package forge.sound; /** * Sounds (enumeration) - all sounds in the game must be declared here. Once - * declared, the sound can be played from anywhere in the code using - * Sounds.soundName.play(). The sounds are only preloaded once, so there is no - * memory overhead for playing the sound multiple times. + * loaded, the sound can be played from the code by posting the appropriate + * event to the event bus via Singletons.getModel().getGame().getEvents().post. * + * The second parameter specifies whether a sound needs to be synced with other + * similar sounds (when there's a chance of multiple instances of the same sound + * generated in quick succession, so that the slowdown can be avoided) or not. + * * Currently, if the file does not exist, it is not a fatal error. No sound is * played in that case, a simple message is generated on the debug console * during preloading. @@ -33,41 +36,43 @@ package forge.sound; public enum SoundEffectType { // Sounds must be listed in alphabetic order. - AddCounter("add_counter.wav"), - Artifact("artifact.wav"), - ArtifactCreature("artifact_creature.wav"), - BlackLand("black_land.wav"), - BlueLand("blue_land.wav"), - Creature("creature.wav"), - Damage("damage.wav"), - Destroy("destroy.wav"), - Discard("discard.wav"), - Draw("draw.wav"), - Enchantment("enchant.wav"), - EndOfTurn("end_of_turn.wav"), - Equip("equip.wav"), - FlipCoin("flip_coin.wav"), - GreenLand("green_land.wav"), - Instant("instant.wav"), - LifeLoss("life_loss.wav"), - LoseDuel("lose_duel.wav"), - ManaBurn("mana_burn.wav"), - OtherLand("other_land.wav"), - Planeswalker("planeswalker.wav"), - Poison("poison.wav"), - RedLand("red_land.wav"), - Regen("regeneration.wav"), - RemoveCounter("remove_counter.wav"), - Sacrifice("sacrifice.wav"), - Sorcery("sorcery.wav"), - Shuffle("shuffle.wav"), - Tap("tap.wav"), - Untap("untap.wav"), - WhiteLand("white_land.wav"), - WinDuel("win_duel.wav"); + AddCounter("add_counter.wav", false), + Artifact("artifact.wav", false), + ArtifactCreature("artifact_creature.wav", false), + BlackLand("black_land.wav", false), + BlueLand("blue_land.wav", false), + Creature("creature.wav", false), + Damage("damage.wav", true), + Destroy("destroy.wav", true), + Discard("discard.wav", false), + Draw("draw.wav", false), + Enchantment("enchant.wav", false), + EndOfTurn("end_of_turn.wav", false), + Equip("equip.wav", false), + FlipCoin("flip_coin.wav", false), + GreenLand("green_land.wav", false), + Instant("instant.wav", false), + LifeLoss("life_loss.wav", true), + LoseDuel("lose_duel.wav", false), + ManaBurn("mana_burn.wav", false), + OtherLand("other_land.wav", false), + Planeswalker("planeswalker.wav", false), + Poison("poison.wav", false), + RedLand("red_land.wav", false), + Regen("regeneration.wav", false), + RemoveCounter("remove_counter.wav", false), + Sacrifice("sacrifice.wav", false), + Sorcery("sorcery.wav", false), + Shuffle("shuffle.wav", false), + Tap("tap.wav", false), + Untap("untap.wav", false), + WhiteLand("white_land.wav", false), + WinDuel("win_duel.wav", false); private final String resourceFileName; + private final boolean isSynced; + /** * @return the resourceFileName */ @@ -77,8 +82,13 @@ public enum SoundEffectType { /** * @param filename * name of the sound file associated with the entry. + * @param isSoundSynced + * determines if only one instance of the sound can be played + * at a time (the sound is synced with the other sounds of the + * same kind). */ - SoundEffectType(final String filename) { + SoundEffectType(final String filename, final boolean isSoundSynced) { resourceFileName = filename; + isSynced = isSoundSynced; } }