From a7c5aa3462b22f902b74521b37c3d99802d9d161 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 30 May 2014 18:40:20 +0200 Subject: Fix and refactor Scene Callback-s --- src/esieequest/engine/Callback.java | 15 --------------- src/esieequest/engine/GameEngine.java | 8 +++++++- src/esieequest/engine/commands/NewCommand.java | 6 ++---- src/esieequest/game/characters/Sumobot.java | 8 +++++++- src/esieequest/game/items/Disk.java | 9 +++------ src/esieequest/game/states/Callback.java | 15 +++++++++++++++ src/esieequest/game/states/Scene.java | 12 ------------ src/esieequest/ui/Viewable.java | 5 ++++- src/esieequest/ui/rich/UserInterface.java | 9 ++++++--- src/esieequest/ui/text/TextInterface.java | 5 +++-- src/esieequest/ui/web/WebInterface.java | 19 +++++++++++++------ 11 files changed, 60 insertions(+), 51 deletions(-) delete mode 100644 src/esieequest/engine/Callback.java create mode 100644 src/esieequest/game/states/Callback.java diff --git a/src/esieequest/engine/Callback.java b/src/esieequest/engine/Callback.java deleted file mode 100644 index 91d8451..0000000 --- a/src/esieequest/engine/Callback.java +++ /dev/null @@ -1,15 +0,0 @@ -package esieequest.engine; - -/** - * The Callback interface. - * - * @author Pacien TRAN-GIRARD - */ -public interface Callback { - - /** - * Will be run after the execution of the function. - */ - public void call(); - -} diff --git a/src/esieequest/engine/GameEngine.java b/src/esieequest/engine/GameEngine.java index 8c1ac95..573d3a8 100644 --- a/src/esieequest/engine/GameEngine.java +++ b/src/esieequest/engine/GameEngine.java @@ -7,6 +7,7 @@ import esieequest.game.characters.MovingCharacter; import esieequest.game.items.Beamer; import esieequest.game.items.Item; import esieequest.game.map.Room; +import esieequest.game.states.Callback; import esieequest.game.states.Scene; import esieequest.ui.Viewable; @@ -104,7 +105,12 @@ public class GameEngine { final boolean trapped = inDeadEnd && !canGoBack && !canTeleport; if (trapped) { - this.view.playScene(Scene.GAME_LOST); + this.view.playScene(Scene.GAME_LOST, new Callback() { + @Override + public void call() { + return; + } + }); this.view.echo("You are trapped. You losed."); } diff --git a/src/esieequest/engine/commands/NewCommand.java b/src/esieequest/engine/commands/NewCommand.java index 49c0225..dea279a 100644 --- a/src/esieequest/engine/commands/NewCommand.java +++ b/src/esieequest/engine/commands/NewCommand.java @@ -1,8 +1,8 @@ package esieequest.engine.commands; -import esieequest.engine.Callback; import esieequest.game.Game; import esieequest.game.Text; +import esieequest.game.states.Callback; import esieequest.game.states.Scene; import esieequest.ui.Viewable; @@ -22,7 +22,7 @@ public class NewCommand implements Executable { view.echo(Text.WELCOME.toString()); - Scene.INTRO.setCallback(new Callback() { + view.playScene(Scene.INTRO, new Callback() { @Override public void call() { view.updateQuest(game.getPlayer().getCurrentQuest()); @@ -35,7 +35,5 @@ public class NewCommand implements Executable { } }); - view.playScene(Scene.INTRO); - } } diff --git a/src/esieequest/game/characters/Sumobot.java b/src/esieequest/game/characters/Sumobot.java index f650c32..5f38e67 100644 --- a/src/esieequest/game/characters/Sumobot.java +++ b/src/esieequest/game/characters/Sumobot.java @@ -6,6 +6,7 @@ import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.map.Direction; import esieequest.game.map.Room; +import esieequest.game.states.Callback; import esieequest.game.states.Scene; import esieequest.ui.Viewable; @@ -50,7 +51,12 @@ public class Sumobot extends FollowingCharacter { return; } - view.playScene(Scene.GAME_LOST); + view.playScene(Scene.GAME_LOST, new Callback() { + @Override + public void call() { + return; + } + }); view.echo(this.getRandomMessage() + Text.NEW_LINE + Sumobot.KILLED_MESSAGE); } diff --git a/src/esieequest/game/items/Disk.java b/src/esieequest/game/items/Disk.java index f558956..096aa48 100644 --- a/src/esieequest/game/items/Disk.java +++ b/src/esieequest/game/items/Disk.java @@ -1,9 +1,9 @@ package esieequest.game.items; -import esieequest.engine.Callback; import esieequest.game.Game; import esieequest.game.map.Direction; import esieequest.game.map.Room; +import esieequest.game.states.Callback; import esieequest.game.states.Scene; import esieequest.ui.Viewable; @@ -33,20 +33,17 @@ public class Disk extends SimpleItem { view.disableInput(); - Scene.RUN_CONSOLE.setCallback(new Callback() { + view.playScene(Scene.RUN_CONSOLE, new Callback() { @Override public void call() { - Scene.END.setCallback(new Callback() { + view.playScene(Scene.END, new Callback() { @Override public void call() { view.echo("Congratulations. The simple fact that you are standing here reading this sentence means you made a glorious contribution to science!"); } }); - view.playScene(Scene.END); } }); - - view.playScene(Scene.RUN_CONSOLE); } else { view.echo("Can't use this item here."); } diff --git a/src/esieequest/game/states/Callback.java b/src/esieequest/game/states/Callback.java new file mode 100644 index 0000000..1fc0ebb --- /dev/null +++ b/src/esieequest/game/states/Callback.java @@ -0,0 +1,15 @@ +package esieequest.game.states; + +/** + * The Callback interface. + * + * @author Pacien TRAN-GIRARD + */ +public interface Callback { + + /** + * Will be run after the execution of the function. + */ + public void call(); + +} diff --git a/src/esieequest/game/states/Scene.java b/src/esieequest/game/states/Scene.java index 46d6884..b802e5e 100644 --- a/src/esieequest/game/states/Scene.java +++ b/src/esieequest/game/states/Scene.java @@ -1,8 +1,6 @@ package esieequest.game.states; import lombok.Getter; -import lombok.Setter; -import esieequest.engine.Callback; /** * Represents an animated Scene that can be played. @@ -31,20 +29,10 @@ public enum Scene { @Getter private final int duration; - @Getter - @Setter - private Callback callback; - Scene(final String title, final String text, final int duration) { this.title = title; this.text = text; this.duration = duration; - this.callback = new Callback() { - @Override - public void call() { - return; - } - }; } } diff --git a/src/esieequest/ui/Viewable.java b/src/esieequest/ui/Viewable.java index cc6e091..f45f1aa 100644 --- a/src/esieequest/ui/Viewable.java +++ b/src/esieequest/ui/Viewable.java @@ -5,6 +5,7 @@ import esieequest.game.items.Inventory; import esieequest.game.map.Direction; import esieequest.game.map.Room; import esieequest.game.map.Side; +import esieequest.game.states.Callback; import esieequest.game.states.Quest; import esieequest.game.states.Scene; @@ -80,8 +81,10 @@ public interface Viewable { * * @param scene * the Scene to play + * @param callback + * the Callback to call once the scene finished playing */ - public void playScene(final Scene scene); + public void playScene(final Scene scene, final Callback callback); /** * Stops the music currently playing. diff --git a/src/esieequest/ui/rich/UserInterface.java b/src/esieequest/ui/rich/UserInterface.java index 6d4c3d4..e8fbbc7 100644 --- a/src/esieequest/ui/rich/UserInterface.java +++ b/src/esieequest/ui/rich/UserInterface.java @@ -50,6 +50,7 @@ import esieequest.game.map.Direction; import esieequest.game.map.Orientation; import esieequest.game.map.Room; import esieequest.game.map.Side; +import esieequest.game.states.Callback; import esieequest.game.states.Quest; import esieequest.game.states.Scene; import esieequest.ui.Viewable; @@ -630,8 +631,10 @@ abstract class UserInterface implements Viewable, ActionListener { } @Override - public void playScene(final Scene scene) { - this.skipScene(); + public void playScene(final Scene scene, final Callback callback) { + if (this.scenePlaying) { + this.timerTask.cancel(); + } this.disableInput(); @@ -645,7 +648,7 @@ abstract class UserInterface implements Viewable, ActionListener { this.timerTask = new TimerTask() { @Override public void run() { - scene.getCallback().call(); + callback.call(); UserInterface.this.scenePlaying = false; } }; diff --git a/src/esieequest/ui/text/TextInterface.java b/src/esieequest/ui/text/TextInterface.java index 0ff2363..7341f7e 100644 --- a/src/esieequest/ui/text/TextInterface.java +++ b/src/esieequest/ui/text/TextInterface.java @@ -13,6 +13,7 @@ import esieequest.game.items.Inventory; import esieequest.game.map.Direction; import esieequest.game.map.Room; import esieequest.game.map.Side; +import esieequest.game.states.Callback; import esieequest.game.states.Quest; import esieequest.game.states.Scene; import esieequest.ui.Viewable; @@ -104,8 +105,8 @@ abstract class TextInterface implements Viewable { } @Override - public void playScene(final Scene scene) { - scene.getCallback().call(); + public void playScene(final Scene scene, final Callback callback) { + callback.call(); } @Override diff --git a/src/esieequest/ui/web/WebInterface.java b/src/esieequest/ui/web/WebInterface.java index 6f2459d..c407d47 100644 --- a/src/esieequest/ui/web/WebInterface.java +++ b/src/esieequest/ui/web/WebInterface.java @@ -33,6 +33,7 @@ import esieequest.game.map.Direction; import esieequest.game.map.Orientation; import esieequest.game.map.Room; import esieequest.game.map.Side; +import esieequest.game.states.Callback; import esieequest.game.states.Quest; import esieequest.game.states.Scene; import esieequest.ui.Viewable; @@ -117,6 +118,13 @@ class WebInterface extends Composite implements Viewable { this.bindKeys(); this.bindButtons(); + this.timer = new Timer() { + @Override + public void run() { + return; + } + }; + this.storage = Storage.getLocalStorageIfSupported(); this.audio = Audio.createIfSupported(); @@ -322,9 +330,6 @@ class WebInterface extends Composite implements Viewable { * Skips the currently player Scene. */ private void skipScene() { - if (this.timer == null) { - return; - } if (this.timer.isRunning()) { this.timer.cancel(); this.timer.run(); @@ -423,8 +428,10 @@ class WebInterface extends Composite implements Viewable { } @Override - public void playScene(final Scene scene) { - this.skipScene(); + public void playScene(final Scene scene, final Callback callback) { + if (this.timer.isRunning()) { + this.timer.cancel(); + } this.disableInput(); @@ -436,7 +443,7 @@ class WebInterface extends Composite implements Viewable { this.timer = new Timer() { @Override public void run() { - scene.getCallback().call(); + callback.call(); } }; this.timer.schedule(scene.getDuration()); -- cgit v1.2.3