From 5b7c510ce0751a696a309a60c4fc281ff523da32 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 1 Jun 2014 19:27:12 +0200 Subject: Refactor UI components --- src/esieequest/Main.java | 8 +- src/esieequest/engine/GameEngine.java | 14 +- src/esieequest/engine/audioplayer/AudioPlayer.java | 31 + .../engine/audioplayer/BrowserAudioPlayer.java | 46 ++ .../engine/audioplayer/SysAudioPlayer.java | 77 +++ .../engine/audioplayer/package-info.java | 6 + src/esieequest/engine/commands/AleaCommand.java | 4 +- src/esieequest/engine/commands/BackCommand.java | 4 +- src/esieequest/engine/commands/Command.java | 8 +- src/esieequest/engine/commands/DoCommand.java | 4 +- src/esieequest/engine/commands/DropCommand.java | 4 +- src/esieequest/engine/commands/Executable.java | 4 +- src/esieequest/engine/commands/GoCommand.java | 4 +- src/esieequest/engine/commands/HelpCommand.java | 4 +- .../engine/commands/InventoryCommand.java | 4 +- src/esieequest/engine/commands/LoadCommand.java | 6 +- src/esieequest/engine/commands/LookCommand.java | 4 +- src/esieequest/engine/commands/NewCommand.java | 6 +- src/esieequest/engine/commands/QuitCommand.java | 4 +- src/esieequest/engine/commands/SaveCommand.java | 6 +- src/esieequest/engine/commands/TakeCommand.java | 4 +- src/esieequest/engine/commands/TalkCommand.java | 4 +- .../engine/commands/ToggleSoundCommand.java | 6 +- src/esieequest/engine/commands/TurnCommand.java | 4 +- src/esieequest/engine/commands/UseCommand.java | 4 +- .../engine/datastore/BrowserDataStore.java | 37 ++ src/esieequest/engine/datastore/DataStore.java | 33 + src/esieequest/engine/datastore/FileDataStore.java | 39 ++ src/esieequest/engine/datastore/package-info.java | 6 + src/esieequest/engine/scheduler/Callback.java | 15 + src/esieequest/engine/scheduler/JSScheduler.java | 52 ++ src/esieequest/engine/scheduler/Scheduler.java | 36 ++ src/esieequest/engine/scheduler/SysScheduler.java | 61 ++ src/esieequest/engine/scheduler/package-info.java | 6 + src/esieequest/esieequest.gwt.xml | 2 +- src/esieequest/game/characters/Athanase.java | 4 +- src/esieequest/game/characters/Character.java | 4 +- .../game/characters/SimpleCharacter.java | 4 +- src/esieequest/game/characters/Sumobot.java | 6 +- src/esieequest/game/doors/Door.java | 4 +- src/esieequest/game/doors/LockedDoor.java | 4 +- src/esieequest/game/doors/TrapDoor.java | 4 +- src/esieequest/game/items/Banana.java | 4 +- src/esieequest/game/items/Beamer.java | 4 +- src/esieequest/game/items/Disk.java | 6 +- src/esieequest/game/items/Item.java | 4 +- src/esieequest/game/items/Note.java | 4 +- src/esieequest/game/items/PortableConsole.java | 4 +- src/esieequest/game/items/SimpleItem.java | 4 +- src/esieequest/game/states/Callback.java | 15 - src/esieequest/ui/View.java | 116 ++++ src/esieequest/ui/Viewable.java | 119 ---- src/esieequest/ui/rich/Applet.java | 28 - src/esieequest/ui/rich/RichInterface.java | 85 +++ src/esieequest/ui/rich/UserInterface.java | 707 --------------------- src/esieequest/ui/rich/Window.java | 32 - src/esieequest/ui/rich/app/AppInterface.java | 244 +++++++ src/esieequest/ui/rich/app/Applet.java | 29 + src/esieequest/ui/rich/app/Layout.java | 371 +++++++++++ src/esieequest/ui/rich/app/Window.java | 32 + src/esieequest/ui/rich/app/package-info.java | 5 + src/esieequest/ui/rich/package-info.java | 7 +- src/esieequest/ui/rich/web/Layout.java | 238 +++++++ src/esieequest/ui/rich/web/Layout.ui.xml | 89 +++ src/esieequest/ui/rich/web/Main.java | 26 + src/esieequest/ui/rich/web/WebInterface.java | 164 +++++ src/esieequest/ui/rich/web/package-info.java | 5 + src/esieequest/ui/text/Console.java | 2 +- src/esieequest/ui/text/FileReader.java | 2 +- src/esieequest/ui/text/TextInterface.java | 60 +- src/esieequest/ui/web/Main.java | 26 - src/esieequest/ui/web/WebInterface.java | 491 -------------- src/esieequest/ui/web/WebInterface.ui.xml | 89 --- src/esieequest/ui/web/package-info.java | 5 - 74 files changed, 1954 insertions(+), 1650 deletions(-) create mode 100644 src/esieequest/engine/audioplayer/AudioPlayer.java create mode 100644 src/esieequest/engine/audioplayer/BrowserAudioPlayer.java create mode 100644 src/esieequest/engine/audioplayer/SysAudioPlayer.java create mode 100644 src/esieequest/engine/audioplayer/package-info.java create mode 100644 src/esieequest/engine/datastore/BrowserDataStore.java create mode 100644 src/esieequest/engine/datastore/DataStore.java create mode 100644 src/esieequest/engine/datastore/FileDataStore.java create mode 100644 src/esieequest/engine/datastore/package-info.java create mode 100644 src/esieequest/engine/scheduler/Callback.java create mode 100644 src/esieequest/engine/scheduler/JSScheduler.java create mode 100644 src/esieequest/engine/scheduler/Scheduler.java create mode 100644 src/esieequest/engine/scheduler/SysScheduler.java create mode 100644 src/esieequest/engine/scheduler/package-info.java delete mode 100644 src/esieequest/game/states/Callback.java create mode 100644 src/esieequest/ui/View.java delete mode 100644 src/esieequest/ui/Viewable.java delete mode 100644 src/esieequest/ui/rich/Applet.java create mode 100644 src/esieequest/ui/rich/RichInterface.java delete mode 100644 src/esieequest/ui/rich/UserInterface.java delete mode 100644 src/esieequest/ui/rich/Window.java create mode 100644 src/esieequest/ui/rich/app/AppInterface.java create mode 100644 src/esieequest/ui/rich/app/Applet.java create mode 100644 src/esieequest/ui/rich/app/Layout.java create mode 100644 src/esieequest/ui/rich/app/Window.java create mode 100644 src/esieequest/ui/rich/app/package-info.java create mode 100644 src/esieequest/ui/rich/web/Layout.java create mode 100644 src/esieequest/ui/rich/web/Layout.ui.xml create mode 100644 src/esieequest/ui/rich/web/Main.java create mode 100644 src/esieequest/ui/rich/web/WebInterface.java create mode 100644 src/esieequest/ui/rich/web/package-info.java delete mode 100644 src/esieequest/ui/web/Main.java delete mode 100644 src/esieequest/ui/web/WebInterface.java delete mode 100644 src/esieequest/ui/web/WebInterface.ui.xml delete mode 100644 src/esieequest/ui/web/package-info.java diff --git a/src/esieequest/Main.java b/src/esieequest/Main.java index c755741..dd548f1 100755 --- a/src/esieequest/Main.java +++ b/src/esieequest/Main.java @@ -6,9 +6,9 @@ import java.util.List; import javax.swing.JApplet; import esieequest.engine.GameEngine; -import esieequest.ui.Viewable; -import esieequest.ui.rich.Applet; -import esieequest.ui.rich.Window; +import esieequest.ui.View; +import esieequest.ui.rich.app.Applet; +import esieequest.ui.rich.app.Window; import esieequest.ui.text.Console; import esieequest.ui.text.FileReader; @@ -45,7 +45,7 @@ public class Main extends JApplet { */ public static void main(final String[] args) { final List arguments = Arrays.asList(args); - Viewable view; + View view; if (arguments.contains("--file")) { if (arguments.size() < 2) { diff --git a/src/esieequest/engine/GameEngine.java b/src/esieequest/engine/GameEngine.java index 573d3a8..9211eb0 100644 --- a/src/esieequest/engine/GameEngine.java +++ b/src/esieequest/engine/GameEngine.java @@ -1,15 +1,15 @@ package esieequest.engine; import esieequest.engine.commands.Command; +import esieequest.engine.scheduler.Callback; import esieequest.game.Game; import esieequest.game.Text; 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; +import esieequest.ui.View; /** * The game main controller class. @@ -20,7 +20,7 @@ import esieequest.ui.Viewable; public class GameEngine { private final Game game; - private final Viewable view; + private final View view; /** * Instantiates a game engine with the given model and view. @@ -30,11 +30,11 @@ public class GameEngine { * @param view * the view */ - public GameEngine(final Viewable view, final Game game) { + public GameEngine(final View view, final Game game) { this.game = game; this.view = view; - this.view.setController(this); + this.view.setGameEngine(this); this.view.show(); } @@ -47,7 +47,7 @@ public class GameEngine { * @param challengeMode * the mode */ - public GameEngine(final Viewable view, final boolean challengeMode) { + public GameEngine(final View view, final boolean challengeMode) { this(view, new Game(challengeMode)); } @@ -57,7 +57,7 @@ public class GameEngine { * @param view * the view */ - public GameEngine(final Viewable view) { + public GameEngine(final View view) { this(view, false); } diff --git a/src/esieequest/engine/audioplayer/AudioPlayer.java b/src/esieequest/engine/audioplayer/AudioPlayer.java new file mode 100644 index 0000000..3b65ec5 --- /dev/null +++ b/src/esieequest/engine/audioplayer/AudioPlayer.java @@ -0,0 +1,31 @@ +package esieequest.engine.audioplayer; + +/** + * The audio player adapter interface. + * + * @author Pacien TRAN-GIRARD + */ +public interface AudioPlayer { + + public static final String SOUND_DIR = "resources/audio/"; + public static final String SOUND_EXT = ".ogg"; + + /** + * Sets the current music playing. + * + * @param fileName + * the URL of the audio file + */ + public void play(final String fileName); + + /** + * Stops the music currently playing. + */ + public void stop(); + + /** + * Toggles the sound. + */ + public void toggleMute(); + +} diff --git a/src/esieequest/engine/audioplayer/BrowserAudioPlayer.java b/src/esieequest/engine/audioplayer/BrowserAudioPlayer.java new file mode 100644 index 0000000..0ec4bed --- /dev/null +++ b/src/esieequest/engine/audioplayer/BrowserAudioPlayer.java @@ -0,0 +1,46 @@ +package esieequest.engine.audioplayer; + +import com.google.gwt.media.client.Audio; + +/** + * The browser audio player adapter. Uses the browser's integrated audio player. + * + * @author Pacien TRAN-GIRARD + */ +public class BrowserAudioPlayer implements AudioPlayer { + + private final Audio audio; + + /** + * Creates a new audio player. + */ + public BrowserAudioPlayer() { + this.audio = Audio.createIfSupported(); + } + + @Override + public void play(final String fileName) { + if (this.audio == null) { + return; + } + this.audio.setSrc(AudioPlayer.SOUND_DIR + fileName + AudioPlayer.SOUND_EXT); + this.audio.play(); + } + + @Override + public void stop() { + if (this.audio == null) { + return; + } + this.audio.setMuted(true); + } + + @Override + public void toggleMute() { + if (this.audio == null) { + return; + } + this.audio.setMuted(!this.audio.isMuted()); + } + +} diff --git a/src/esieequest/engine/audioplayer/SysAudioPlayer.java b/src/esieequest/engine/audioplayer/SysAudioPlayer.java new file mode 100644 index 0000000..51e3a88 --- /dev/null +++ b/src/esieequest/engine/audioplayer/SysAudioPlayer.java @@ -0,0 +1,77 @@ +package esieequest.engine.audioplayer; + +import java.io.IOException; + +import org.newdawn.easyogg.OggClip; + +/** + * The standard audio player interface. + * + * @author Pacien TRAN-GIRARD + */ +public class SysAudioPlayer implements AudioPlayer { + + private OggClip audio; + private boolean muted; + + /** + * Creates a new audio player. + */ + public SysAudioPlayer() { + this.muted = false; + } + + /** + * Sets the gain. + * + * PulseAudio does not comply with the JSAPI => pause/play instead of muting + */ + private void setAudioGain() { + if (this.audio == null) { + return; + } + if (this.audio.stopped()) { + return; + } + + if (this.muted) { + // this.audio.setGain(0.0f); + this.audio.pause(); + } else if (this.audio.isPaused()) { + // this.audio.setGain(-1.0f); + this.audio.resume(); + } + } + + @Override + public void play(final String fileName) { + if (this.audio != null) { + // stop() and close() are not working properly... + this.audio.pause(); + } + + try { + this.audio = new OggClip(AudioPlayer.SOUND_DIR + fileName + AudioPlayer.SOUND_EXT); + this.audio.play(); + this.setAudioGain(); + } catch (final IOException e) { + e.printStackTrace(); + } + } + + @Override + public void stop() { + if (this.audio == null) { + return; + } + // stop() and close() are not working properly... + this.audio.pause(); + } + + @Override + public void toggleMute() { + this.muted = !this.muted; + this.setAudioGain(); + } + +} diff --git a/src/esieequest/engine/audioplayer/package-info.java b/src/esieequest/engine/audioplayer/package-info.java new file mode 100644 index 0000000..965e5e1 --- /dev/null +++ b/src/esieequest/engine/audioplayer/package-info.java @@ -0,0 +1,6 @@ +/** + * The audioplayer package. Contains audio player adapters. + * + * @author Pacien TRAN-GIRARD + */ +package esieequest.engine.audioplayer; \ No newline at end of file diff --git a/src/esieequest/engine/commands/AleaCommand.java b/src/esieequest/engine/commands/AleaCommand.java index 1e9fce7..b71288e 100644 --- a/src/esieequest/engine/commands/AleaCommand.java +++ b/src/esieequest/engine/commands/AleaCommand.java @@ -4,7 +4,7 @@ import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.doors.TransporterDoor; import esieequest.game.map.Room; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Provides a way to override the random behaviour of TransporterDoor-s for @@ -15,7 +15,7 @@ import esieequest.ui.Viewable; public class AleaCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { final String forcedRoomName = argument; Room destinationRoom = null; diff --git a/src/esieequest/engine/commands/BackCommand.java b/src/esieequest/engine/commands/BackCommand.java index 0e0e646..59d7d4b 100644 --- a/src/esieequest/engine/commands/BackCommand.java +++ b/src/esieequest/engine/commands/BackCommand.java @@ -2,7 +2,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the user to go back on his steps and go to his previous location. @@ -12,7 +12,7 @@ import esieequest.ui.Viewable; public class BackCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (!argument.isEmpty()) { view.echo(Text.TOO_MANY_ARGUMENTS.toString()); diff --git a/src/esieequest/engine/commands/Command.java b/src/esieequest/engine/commands/Command.java index 23b3ca6..5c44e9b 100644 --- a/src/esieequest/engine/commands/Command.java +++ b/src/esieequest/engine/commands/Command.java @@ -6,7 +6,7 @@ import java.util.List; import esieequest.engine.utils.ListUtils; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * The Command-s the user can use. @@ -42,7 +42,9 @@ public enum Command { TALK(new TalkCommand()), // shortcuts - DO(new DoCommand()); + DO(new DoCommand()), + + ; // @formatter:on @@ -68,7 +70,7 @@ public enum Command { * @param view * the View */ - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { final String arg = argument == null ? "" : argument; this.command.execute(arg, game, view); } diff --git a/src/esieequest/engine/commands/DoCommand.java b/src/esieequest/engine/commands/DoCommand.java index e6b5936..ce491cc 100644 --- a/src/esieequest/engine/commands/DoCommand.java +++ b/src/esieequest/engine/commands/DoCommand.java @@ -3,7 +3,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.map.Side; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A shortcut Command that executes a TALK or a TAKE Command accordingly to the @@ -14,7 +14,7 @@ import esieequest.ui.Viewable; public class DoCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (!argument.isEmpty()) { view.echo(Text.TOO_MANY_ARGUMENTS.toString()); diff --git a/src/esieequest/engine/commands/DropCommand.java b/src/esieequest/engine/commands/DropCommand.java index c25e0a8..24a26e6 100644 --- a/src/esieequest/engine/commands/DropCommand.java +++ b/src/esieequest/engine/commands/DropCommand.java @@ -3,7 +3,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.items.Item; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the user to drop an Item from his inventory (referred by its name) to @@ -14,7 +14,7 @@ import esieequest.ui.Viewable; public class DropCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { final String itemName = argument; diff --git a/src/esieequest/engine/commands/Executable.java b/src/esieequest/engine/commands/Executable.java index 807e3e4..646555f 100644 --- a/src/esieequest/engine/commands/Executable.java +++ b/src/esieequest/engine/commands/Executable.java @@ -1,7 +1,7 @@ package esieequest.engine.commands; import esieequest.game.Game; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * The Executable interface. @@ -20,6 +20,6 @@ public interface Executable { * @param view * the View */ - public void execute(final String argument, final Game game, final Viewable view); + public void execute(final String argument, final Game game, final View view); } diff --git a/src/esieequest/engine/commands/GoCommand.java b/src/esieequest/engine/commands/GoCommand.java index b15c1d4..1cfe7c2 100644 --- a/src/esieequest/engine/commands/GoCommand.java +++ b/src/esieequest/engine/commands/GoCommand.java @@ -4,7 +4,7 @@ import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.doors.Door; import esieequest.game.map.Direction; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the user to move from connected Room-s to Room-s by giving the @@ -15,7 +15,7 @@ import esieequest.ui.Viewable; public class GoCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { Direction direction; diff --git a/src/esieequest/engine/commands/HelpCommand.java b/src/esieequest/engine/commands/HelpCommand.java index 8738a8e..810a24e 100644 --- a/src/esieequest/engine/commands/HelpCommand.java +++ b/src/esieequest/engine/commands/HelpCommand.java @@ -2,7 +2,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Prints the available commands. @@ -12,7 +12,7 @@ import esieequest.ui.Viewable; public class HelpCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (!argument.isEmpty()) { view.echo(Text.TOO_MANY_ARGUMENTS.toString()); diff --git a/src/esieequest/engine/commands/InventoryCommand.java b/src/esieequest/engine/commands/InventoryCommand.java index c427722..9bd72ff 100644 --- a/src/esieequest/engine/commands/InventoryCommand.java +++ b/src/esieequest/engine/commands/InventoryCommand.java @@ -2,7 +2,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Prints the list of the items in the Player's inventory. @@ -12,7 +12,7 @@ import esieequest.ui.Viewable; public class InventoryCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (!argument.isEmpty()) { view.echo(Text.TOO_MANY_ARGUMENTS.toString()); diff --git a/src/esieequest/engine/commands/LoadCommand.java b/src/esieequest/engine/commands/LoadCommand.java index d4368ff..a9f38c2 100644 --- a/src/esieequest/engine/commands/LoadCommand.java +++ b/src/esieequest/engine/commands/LoadCommand.java @@ -6,7 +6,7 @@ import org.json.simple.parser.ParseException; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Loads a saved Game. @@ -16,7 +16,7 @@ import esieequest.ui.Viewable; public class LoadCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (argument.isEmpty()) { view.echo(Text.MISSING_ARGUMENT.toString()); @@ -27,7 +27,7 @@ public class LoadCommand implements Executable { if (argument.startsWith("{")) { jsonString = argument; } else { - jsonString = view.load(argument); + jsonString = view.getDataStore().load(argument); } JSONObject datas = new JSONObject(); diff --git a/src/esieequest/engine/commands/LookCommand.java b/src/esieequest/engine/commands/LookCommand.java index e0d1f1f..12f872b 100644 --- a/src/esieequest/engine/commands/LookCommand.java +++ b/src/esieequest/engine/commands/LookCommand.java @@ -6,7 +6,7 @@ import com.google.common.base.Joiner; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Prints informations about the current location. @@ -16,7 +16,7 @@ import esieequest.ui.Viewable; public class LookCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (!argument.isEmpty()) { view.echo(Text.TOO_MANY_ARGUMENTS.toString()); diff --git a/src/esieequest/engine/commands/NewCommand.java b/src/esieequest/engine/commands/NewCommand.java index dea279a..52749f0 100644 --- a/src/esieequest/engine/commands/NewCommand.java +++ b/src/esieequest/engine/commands/NewCommand.java @@ -1,10 +1,10 @@ package esieequest.engine.commands; +import esieequest.engine.scheduler.Callback; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.game.states.Callback; import esieequest.game.states.Scene; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Creates a new game. @@ -14,7 +14,7 @@ import esieequest.ui.Viewable; public class NewCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { final boolean challengeMode = argument.equals(Text.CHALLENGE_FLAG.toString()); diff --git a/src/esieequest/engine/commands/QuitCommand.java b/src/esieequest/engine/commands/QuitCommand.java index 11fd226..3c3d105 100644 --- a/src/esieequest/engine/commands/QuitCommand.java +++ b/src/esieequest/engine/commands/QuitCommand.java @@ -2,7 +2,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the user to quit the game. @@ -12,7 +12,7 @@ import esieequest.ui.Viewable; public class QuitCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (!argument.isEmpty()) { view.echo(Text.TOO_MANY_ARGUMENTS.toString()); diff --git a/src/esieequest/engine/commands/SaveCommand.java b/src/esieequest/engine/commands/SaveCommand.java index f70dd93..5907fb1 100644 --- a/src/esieequest/engine/commands/SaveCommand.java +++ b/src/esieequest/engine/commands/SaveCommand.java @@ -2,7 +2,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Saves the current Game. @@ -12,7 +12,7 @@ import esieequest.ui.Viewable; public class SaveCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { final String filePath = argument; @@ -22,7 +22,7 @@ public class SaveCommand implements Executable { } final String serialisedGame = game.serialise().toJSONString(); - view.save(filePath, serialisedGame); + view.getDataStore().save(filePath, serialisedGame); } diff --git a/src/esieequest/engine/commands/TakeCommand.java b/src/esieequest/engine/commands/TakeCommand.java index 7a96a23..e705c64 100644 --- a/src/esieequest/engine/commands/TakeCommand.java +++ b/src/esieequest/engine/commands/TakeCommand.java @@ -5,7 +5,7 @@ import esieequest.game.Text; import esieequest.game.items.Item; import esieequest.game.map.Room; import esieequest.game.map.Side; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the player to take an Item from a Room and transfer it to his @@ -16,7 +16,7 @@ import esieequest.ui.Viewable; public class TakeCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { Item item; diff --git a/src/esieequest/engine/commands/TalkCommand.java b/src/esieequest/engine/commands/TalkCommand.java index f63b2c3..e43fc6c 100644 --- a/src/esieequest/engine/commands/TalkCommand.java +++ b/src/esieequest/engine/commands/TalkCommand.java @@ -3,7 +3,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.characters.Character; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the Player to TALK to a Character. @@ -13,7 +13,7 @@ import esieequest.ui.Viewable; public class TalkCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { Character character = null; diff --git a/src/esieequest/engine/commands/ToggleSoundCommand.java b/src/esieequest/engine/commands/ToggleSoundCommand.java index 1e433b2..434875b 100644 --- a/src/esieequest/engine/commands/ToggleSoundCommand.java +++ b/src/esieequest/engine/commands/ToggleSoundCommand.java @@ -2,7 +2,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Mute/unmute the game musics. @@ -12,14 +12,14 @@ import esieequest.ui.Viewable; public class ToggleSoundCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { if (!argument.isEmpty()) { view.echo(Text.TOO_MANY_ARGUMENTS.toString()); return; } - view.toggleSound(); + view.toggleMute(); } diff --git a/src/esieequest/engine/commands/TurnCommand.java b/src/esieequest/engine/commands/TurnCommand.java index c5edaef..0e8e22e 100644 --- a/src/esieequest/engine/commands/TurnCommand.java +++ b/src/esieequest/engine/commands/TurnCommand.java @@ -4,7 +4,7 @@ import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.map.Direction; import esieequest.game.map.Orientation; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the user to turn on himself, that is to say to point to another @@ -15,7 +15,7 @@ import esieequest.ui.Viewable; public class TurnCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { final String direction = argument.toUpperCase(); diff --git a/src/esieequest/engine/commands/UseCommand.java b/src/esieequest/engine/commands/UseCommand.java index 41e215b..985dcf7 100644 --- a/src/esieequest/engine/commands/UseCommand.java +++ b/src/esieequest/engine/commands/UseCommand.java @@ -3,7 +3,7 @@ package esieequest.engine.commands; import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.items.Item; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Allows the player to use an item located in his inventory. @@ -13,7 +13,7 @@ import esieequest.ui.Viewable; public class UseCommand implements Executable { @Override - public void execute(final String argument, final Game game, final Viewable view) { + public void execute(final String argument, final Game game, final View view) { final String itemName = argument.toLowerCase(); diff --git a/src/esieequest/engine/datastore/BrowserDataStore.java b/src/esieequest/engine/datastore/BrowserDataStore.java new file mode 100644 index 0000000..e00cc5c --- /dev/null +++ b/src/esieequest/engine/datastore/BrowserDataStore.java @@ -0,0 +1,37 @@ +package esieequest.engine.datastore; + +import com.google.gwt.storage.client.Storage; + +/** + * The browser datastore adapter. Uses the browser's content storage. + * + * @author Pacien TRAN-GIRARD + */ +public class BrowserDataStore implements DataStore { + + private final Storage contentStorage; + + /** + * Creates a new datastore. + */ + public BrowserDataStore() { + this.contentStorage = Storage.getLocalStorageIfSupported(); + } + + @Override + public String load(final String path) { + if (this.contentStorage == null) { + return null; + } + return this.contentStorage.getItem(path); + } + + @Override + public void save(final String path, final String serialisedGame) { + if (this.contentStorage == null) { + return; + } + this.contentStorage.setItem(path, serialisedGame); + } + +} diff --git a/src/esieequest/engine/datastore/DataStore.java b/src/esieequest/engine/datastore/DataStore.java new file mode 100644 index 0000000..187d5b4 --- /dev/null +++ b/src/esieequest/engine/datastore/DataStore.java @@ -0,0 +1,33 @@ +package esieequest.engine.datastore; + +/** + * The datastore adapter interface. + * + * @author Pacien TRAN-GIRARD + */ +public interface DataStore { + + public static final String SAVE_LABEL = "ESIEEquest Game"; + public static final String SAVE_EXT = "eqg"; + + /** + * Loads a saved game file. + * + * @param path + * the path of the file + * + * @return the serialised Game + */ + public String load(final String path); + + /** + * Saves a game as a file. + * + * @param path + * the path of the file + * @param serialisedGame + * the serialised Game + */ + public void save(final String path, final String serialisedGame); + +} diff --git a/src/esieequest/engine/datastore/FileDataStore.java b/src/esieequest/engine/datastore/FileDataStore.java new file mode 100644 index 0000000..b11437e --- /dev/null +++ b/src/esieequest/engine/datastore/FileDataStore.java @@ -0,0 +1,39 @@ +package esieequest.engine.datastore; + +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * The file datastore adapter. Uses files written on the user's disk. + * + * @author Pacien TRAN-GIRARD + */ +public class FileDataStore implements DataStore { + + @Override + public String load(final String path) { + final Path filePath = Paths.get(path); + String serialisedGame = null; + try { + serialisedGame = new String(Files.readAllBytes(filePath)); + } catch (final IOException e) { + e.printStackTrace(); + } + return serialisedGame; + } + + @Override + public void save(final String path, final String serialisedGame) { + try { + final FileWriter fileWriter = new FileWriter(path); + fileWriter.write(serialisedGame); + fileWriter.close(); + } catch (final IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/esieequest/engine/datastore/package-info.java b/src/esieequest/engine/datastore/package-info.java new file mode 100644 index 0000000..4624260 --- /dev/null +++ b/src/esieequest/engine/datastore/package-info.java @@ -0,0 +1,6 @@ +/** + * The DataStore package. Contains datastores adapters. + * + * @author Pacien TRAN-GIRARD + */ +package esieequest.engine.datastore; \ No newline at end of file diff --git a/src/esieequest/engine/scheduler/Callback.java b/src/esieequest/engine/scheduler/Callback.java new file mode 100644 index 0000000..b8b776b --- /dev/null +++ b/src/esieequest/engine/scheduler/Callback.java @@ -0,0 +1,15 @@ +package esieequest.engine.scheduler; + +/** + * 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/scheduler/JSScheduler.java b/src/esieequest/engine/scheduler/JSScheduler.java new file mode 100644 index 0000000..8a31339 --- /dev/null +++ b/src/esieequest/engine/scheduler/JSScheduler.java @@ -0,0 +1,52 @@ +package esieequest.engine.scheduler; + +import com.google.gwt.user.client.Timer; + +/** + * The Javascript/GWT Timer adapter. + * + * @author Pacien TRAN-GIRARD + */ +public class JSScheduler implements Scheduler { + + private Timer timer; + + @Override + public void schedule(final Callback callback, final int delay) { + this.cancel(); + + this.timer = new Timer() { + @Override + public void run() { + callback.call(); + } + }; + this.timer.schedule(delay); + } + + @Override + public void cancel() { + if (this.timer == null) { + return; + } + this.timer.cancel(); + } + + @Override + public void run() { + if (this.timer == null) { + return; + } + this.cancel(); + this.timer.run(); + } + + @Override + public boolean isRunning() { + if (this.timer == null) { + return false; + } + return this.timer.isRunning(); + } + +} diff --git a/src/esieequest/engine/scheduler/Scheduler.java b/src/esieequest/engine/scheduler/Scheduler.java new file mode 100644 index 0000000..c1b2b1b --- /dev/null +++ b/src/esieequest/engine/scheduler/Scheduler.java @@ -0,0 +1,36 @@ +package esieequest.engine.scheduler; + +/** + * The Scheduler adapter interface. + * + * @author Pacien TRAN-GIRARD + */ +public interface Scheduler { + + /** + * Schedule the execution of the given Callback after the given delay (ms). + * Cancels any previously scheduled task. + * + * @param callback + * the callback + * @param delay + * the delay + */ + public void schedule(final Callback callback, final int delay); + + /** + * Cancels the execution of any previously scheduled task. + */ + public void cancel(); + + /** + * Runs the last previously scheduled task. + */ + public void run(); + + /** + * @return true if a task is planned, false otherwise. + */ + public boolean isRunning(); + +} diff --git a/src/esieequest/engine/scheduler/SysScheduler.java b/src/esieequest/engine/scheduler/SysScheduler.java new file mode 100644 index 0000000..e5a5947 --- /dev/null +++ b/src/esieequest/engine/scheduler/SysScheduler.java @@ -0,0 +1,61 @@ +package esieequest.engine.scheduler; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * The standard Java Timer adapter. + * + * @author Pacien TRAN-GIRARD + */ +public class SysScheduler implements Scheduler { + + private final Timer timer; + private TimerTask timerTask; + + /** + * Creates a new scheduler. + */ + public SysScheduler() { + this.timer = new Timer(); + } + + @Override + public void schedule(final Callback callback, final int time) { + this.cancel(); + + this.timerTask = new TimerTask() { + @Override + public void run() { + callback.call(); + } + }; + this.timer.schedule(this.timerTask, time); + } + + @Override + public void cancel() { + if (this.timerTask == null) { + return; + } + this.timerTask.cancel(); + } + + @Override + public void run() { + if (this.timerTask == null) { + return; + } + this.cancel(); + this.timerTask.run(); + } + + @Override + public boolean isRunning() { + if (this.timerTask == null) { + return false; + } + return this.timerTask.scheduledExecutionTime() == 0L; + } + +} diff --git a/src/esieequest/engine/scheduler/package-info.java b/src/esieequest/engine/scheduler/package-info.java new file mode 100644 index 0000000..8362a2e --- /dev/null +++ b/src/esieequest/engine/scheduler/package-info.java @@ -0,0 +1,6 @@ +/** + * The Scheduler package. Contains schedulers adapters. + * + * @author Pacien TRAN-GIRARD + */ +package esieequest.engine.scheduler; \ No newline at end of file diff --git a/src/esieequest/esieequest.gwt.xml b/src/esieequest/esieequest.gwt.xml index 1175239..3fbdbbc 100644 --- a/src/esieequest/esieequest.gwt.xml +++ b/src/esieequest/esieequest.gwt.xml @@ -23,7 +23,7 @@ so that your app can take advantage of the latest GWT module capabilities. - + diff --git a/src/esieequest/game/characters/Athanase.java b/src/esieequest/game/characters/Athanase.java index f5d321b..51fa3c0 100644 --- a/src/esieequest/game/characters/Athanase.java +++ b/src/esieequest/game/characters/Athanase.java @@ -4,7 +4,7 @@ import esieequest.game.Game; import esieequest.game.items.Inventory; import esieequest.game.items.Item; import esieequest.game.states.Quest; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Athanase the mathgorilla. @@ -21,7 +21,7 @@ public class Athanase extends SimpleCharacter { } @Override - public void talk(final Game game, final Viewable view) { + public void talk(final Game game, final View view) { final Quest currentQuest = game.getPlayer().getCurrentQuest(); final Inventory playersInventory = game.getPlayer().getInventory(); switch (currentQuest) { diff --git a/src/esieequest/game/characters/Character.java b/src/esieequest/game/characters/Character.java index b9dbd9b..4efd6b6 100644 --- a/src/esieequest/game/characters/Character.java +++ b/src/esieequest/game/characters/Character.java @@ -8,7 +8,7 @@ import esieequest.engine.utils.SerialisableObject; import esieequest.game.Game; import esieequest.game.map.Direction; import esieequest.game.map.Room; -import esieequest.ui.Viewable; +import esieequest.ui.View; public enum Character implements SerialisableObject { @@ -34,7 +34,7 @@ public enum Character implements SerialisableObject { return this.character.getName(); } - public void talk(final Game game, final Viewable view) { + public void talk(final Game game, final View view) { this.character.talk(game, view); } diff --git a/src/esieequest/game/characters/SimpleCharacter.java b/src/esieequest/game/characters/SimpleCharacter.java index 1aa02d5..efcdb8d 100644 --- a/src/esieequest/game/characters/SimpleCharacter.java +++ b/src/esieequest/game/characters/SimpleCharacter.java @@ -7,7 +7,7 @@ import org.json.simple.JSONObject; import esieequest.engine.utils.SerialisableObject; import esieequest.game.Game; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A character that can talk with the player. @@ -34,7 +34,7 @@ public abstract class SimpleCharacter implements SerialisableObject { * * @return what the Character says */ - public abstract void talk(final Game game, final Viewable view); + public abstract void talk(final Game game, final View view); @Override public JSONObject serialise() { diff --git a/src/esieequest/game/characters/Sumobot.java b/src/esieequest/game/characters/Sumobot.java index 5f38e67..2f58447 100644 --- a/src/esieequest/game/characters/Sumobot.java +++ b/src/esieequest/game/characters/Sumobot.java @@ -2,13 +2,13 @@ package esieequest.game.characters; import java.util.Random; +import esieequest.engine.scheduler.Callback; 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; +import esieequest.ui.View; /** * A Sumobot from ESIEEspace who says random sentences and moves randomly. @@ -43,7 +43,7 @@ public class Sumobot extends FollowingCharacter { } @Override - public void talk(final Game game, final Viewable view) { + public void talk(final Game game, final View view) { if (!this.isMobile()) { this.setMobile(true); diff --git a/src/esieequest/game/doors/Door.java b/src/esieequest/game/doors/Door.java index 88dc592..74658cc 100644 --- a/src/esieequest/game/doors/Door.java +++ b/src/esieequest/game/doors/Door.java @@ -3,7 +3,7 @@ package esieequest.game.doors; import lombok.Getter; import esieequest.game.Game; import esieequest.game.map.Room; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A door that links two rooms. @@ -35,7 +35,7 @@ public class Door { * * @return if the Door was effectively crossed */ - public boolean cross(final Game game, final Viewable view) { + public boolean cross(final Game game, final View view) { game.getPlayer().goToRoom(this.getDestination()); return true; } diff --git a/src/esieequest/game/doors/LockedDoor.java b/src/esieequest/game/doors/LockedDoor.java index 752d2d8..a9c0769 100644 --- a/src/esieequest/game/doors/LockedDoor.java +++ b/src/esieequest/game/doors/LockedDoor.java @@ -6,7 +6,7 @@ import esieequest.game.Player; import esieequest.game.Text; import esieequest.game.items.Item; import esieequest.game.map.Room; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A door that links two rooms and requires a KeyCard to pass through. @@ -30,7 +30,7 @@ public class LockedDoor extends Door { } @Override - public boolean cross(final Game game, final Viewable view) { + public boolean cross(final Game game, final View view) { if (!this.isAuthorised(game.getPlayer())) { view.echo(Text.DOOR_LOCKED.toString()); return false; diff --git a/src/esieequest/game/doors/TrapDoor.java b/src/esieequest/game/doors/TrapDoor.java index ae1b733..367d192 100644 --- a/src/esieequest/game/doors/TrapDoor.java +++ b/src/esieequest/game/doors/TrapDoor.java @@ -2,7 +2,7 @@ package esieequest.game.doors; import esieequest.game.Game; import esieequest.game.map.Room; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A one-way door that links two rooms and prevent the usage of the 'back' @@ -23,7 +23,7 @@ public class TrapDoor extends Door { } @Override - public boolean cross(final Game game, final Viewable view) { + public boolean cross(final Game game, final View view) { final boolean crossed = super.cross(game, view); game.getPlayer().clearRoomHistory(); return crossed; diff --git a/src/esieequest/game/items/Banana.java b/src/esieequest/game/items/Banana.java index 820b027..81f0b45 100644 --- a/src/esieequest/game/items/Banana.java +++ b/src/esieequest/game/items/Banana.java @@ -1,7 +1,7 @@ package esieequest.game.items; import esieequest.game.Game; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A Banana that can be eaten only by Athanase. @@ -18,7 +18,7 @@ public class Banana extends SimpleItem { } @Override - public void use(final Game game, final Viewable view) { + public void use(final Game game, final View view) { view.echo("Better keep it for later..."); } diff --git a/src/esieequest/game/items/Beamer.java b/src/esieequest/game/items/Beamer.java index e53edfd..246b080 100644 --- a/src/esieequest/game/items/Beamer.java +++ b/src/esieequest/game/items/Beamer.java @@ -8,7 +8,7 @@ import org.json.simple.JSONObject; import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.map.Room; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A beamer is a device that can be charged, and fired. When charged, it @@ -34,7 +34,7 @@ public class Beamer extends SimpleItem { } @Override - public void use(final Game game, final Viewable view) { + public void use(final Game game, final View view) { if (this.room == null) { this.room = game.getPlayer().getCurrentRoom(); view.echo(Text.BEAMER_ROOM_MEMORISED.toString() + this.room.hashCode()); diff --git a/src/esieequest/game/items/Disk.java b/src/esieequest/game/items/Disk.java index 81c1456..2094ffc 100644 --- a/src/esieequest/game/items/Disk.java +++ b/src/esieequest/game/items/Disk.java @@ -1,11 +1,11 @@ package esieequest.game.items; +import esieequest.engine.scheduler.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; +import esieequest.ui.View; /** * A Disk that can be used in conjunction with an installed PortableConsole. @@ -19,7 +19,7 @@ public class Disk extends SimpleItem { } @Override - public void use(final Game game, final Viewable view) { + public void use(final Game game, final View view) { final boolean onRoundabout = game.getPlayer().getCurrentRoom().equals( Room.ENTRANCE_ROUNDABOUT); final boolean pointingSouth = game.getPlayer().getCurrentDirection().equals(Direction.EAST); diff --git a/src/esieequest/game/items/Item.java b/src/esieequest/game/items/Item.java index 9a20f10..d5aad7c 100644 --- a/src/esieequest/game/items/Item.java +++ b/src/esieequest/game/items/Item.java @@ -10,7 +10,7 @@ import esieequest.engine.utils.EnumUtils; import esieequest.engine.utils.SerialisableObject; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; public enum Item implements Mappable, SerialisableObject { @@ -77,7 +77,7 @@ public enum Item implements Mappable, SerialisableObject { * @param view * the View */ - public void use(final Game game, final Viewable view) { + public void use(final Game game, final View view) { this.item.use(game, view); } diff --git a/src/esieequest/game/items/Note.java b/src/esieequest/game/items/Note.java index 92d8182..89fe494 100644 --- a/src/esieequest/game/items/Note.java +++ b/src/esieequest/game/items/Note.java @@ -2,7 +2,7 @@ package esieequest.game.items; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * A note. @@ -27,7 +27,7 @@ public class Note extends SimpleItem { } @Override - public void use(final Game game, final Viewable view) { + public void use(final Game game, final View view) { view.echo(Text.NOTE_PREFIX.toString() + this.text); } diff --git a/src/esieequest/game/items/PortableConsole.java b/src/esieequest/game/items/PortableConsole.java index b7883f1..9c14219 100644 --- a/src/esieequest/game/items/PortableConsole.java +++ b/src/esieequest/game/items/PortableConsole.java @@ -3,7 +3,7 @@ package esieequest.game.items; import esieequest.game.Game; import esieequest.game.map.Direction; import esieequest.game.map.Room; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * The transponder. @@ -20,7 +20,7 @@ public class PortableConsole extends SimpleItem { } @Override - public void use(final Game game, final Viewable view) { + public void use(final Game game, final View view) { final boolean onRoundabout = game.getPlayer().getCurrentRoom().equals( Room.ENTRANCE_ROUNDABOUT); final boolean pointingSouth = game.getPlayer().getCurrentDirection().equals(Direction.EAST); diff --git a/src/esieequest/game/items/SimpleItem.java b/src/esieequest/game/items/SimpleItem.java index 04c38f6..79c445f 100644 --- a/src/esieequest/game/items/SimpleItem.java +++ b/src/esieequest/game/items/SimpleItem.java @@ -8,7 +8,7 @@ import org.json.simple.JSONObject; import esieequest.engine.utils.SerialisableObject; import esieequest.game.Game; import esieequest.game.Text; -import esieequest.ui.Viewable; +import esieequest.ui.View; /** * Represents an item with a description and a weight, stored in a room. Can be @@ -71,7 +71,7 @@ public class SimpleItem implements SerialisableObject { * @param view * the View */ - public void use(final Game game, final Viewable view) { + public void use(final Game game, final View view) { view.echo(Text.NO_USE.toString()); } diff --git a/src/esieequest/game/states/Callback.java b/src/esieequest/game/states/Callback.java deleted file mode 100644 index 1fc0ebb..0000000 --- a/src/esieequest/game/states/Callback.java +++ /dev/null @@ -1,15 +0,0 @@ -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/ui/View.java b/src/esieequest/ui/View.java new file mode 100644 index 0000000..a909bcc --- /dev/null +++ b/src/esieequest/ui/View.java @@ -0,0 +1,116 @@ +package esieequest.ui; + +import lombok.Getter; +import lombok.Setter; +import esieequest.engine.GameEngine; +import esieequest.engine.datastore.DataStore; +import esieequest.engine.scheduler.Callback; +import esieequest.game.items.Inventory; +import esieequest.game.map.Direction; +import esieequest.game.map.Room; +import esieequest.game.map.Side; +import esieequest.game.states.Quest; +import esieequest.game.states.Scene; + +/** + * The view that describes an user interface. + * + * @author Pacien TRAN-GIRARD + */ +public abstract class View { + + @Getter + @Setter + private GameEngine gameEngine; + + @Getter + private final DataStore dataStore; + + /** + * Instantiates a new View. + * + * @param dataStore + * the data store + */ + public View(final DataStore dataStore) { + this.dataStore = dataStore; + } + + /** + * Displays the user interface. + */ + public abstract void show(); + + /** + * Enables the user interface. + */ + public abstract void enableInput(); + + /** + * Disables the user interface. + */ + public abstract void disableInput(); + + /** + * Displays a message. + * + * @param message + * the message + */ + public abstract void echo(final String message); + + /** + * Updates the view to match the current quest. + * + * @param quest + * the current quest to display + */ + public abstract void updateQuest(final Quest quest); + + /** + * Updates the view to match the current location. + * + * @param room + * the room + * @param direction + * the current direction + * @param side + * the side of a room + */ + public abstract void updateLocation(final Room room, final Direction direction, + final Side side, final boolean canGoBack); + + /** + * Updates the view to display the items contained in the inventory + * + * @param inventory + * the Inventory + */ + public abstract void updateInventory(final Inventory inventory); + + /** + * Plays the given Scene. + * + * @param scene + * the Scene to play + * @param callback + * the Callback to call once the scene finished playing + */ + public abstract void playScene(final Scene scene, final Callback callback); + + /** + * Skips the Scene currently playing. + */ + public abstract void skipScene(); + + /** + * Stops the music currently playing. + */ + public abstract void stopMusic(); + + /** + * Toggles the sound. + */ + public abstract void toggleMute(); + +} diff --git a/src/esieequest/ui/Viewable.java b/src/esieequest/ui/Viewable.java deleted file mode 100644 index f45f1aa..0000000 --- a/src/esieequest/ui/Viewable.java +++ /dev/null @@ -1,119 +0,0 @@ -package esieequest.ui; - -import esieequest.engine.GameEngine; -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; - -/** - * The view interface that describes an user interface. - * - * @author Pacien TRAN-GIRARD - */ -public interface Viewable { - - /** - * Sets the controller to use. - * - * @param gameEngine - * the GameEngine controller - */ - public void setController(final GameEngine gameEngine); - - /** - * Displays the user interface. - */ - public void show(); - - /** - * Enables the user interface. - */ - public void enableInput(); - - /** - * Disables the user interface. - */ - public void disableInput(); - - /** - * Displays a message. - * - * @param message - * the message - */ - public void echo(final String message); - - /** - * Updates the view to match the current quest. - * - * @param quest - * the current quest to display - */ - public void updateQuest(final Quest quest); - - /** - * Updates the view to match the current location. - * - * @param room - * the room - * @param direction - * the current direction - * @param side - * the side of a room - */ - public void updateLocation(final Room room, final Direction direction, final Side side, - final boolean canGoBack); - - /** - * Updates the view to display the items contained in the inventory - * - * @param inventory - * the Inventory - */ - public void updateInventory(final Inventory inventory); - - /** - * Plays the given Scene. - * - * @param scene - * the Scene to play - * @param callback - * the Callback to call once the scene finished playing - */ - public void playScene(final Scene scene, final Callback callback); - - /** - * Stops the music currently playing. - */ - public void stopMusic(); - - /** - * Toggles the sound. - */ - public void toggleSound(); - - /** - * Loads a saved game file. - * - * @param path - * the path of the file - * - * @return the serialised Game - */ - public String load(final String path); - - /** - * Saves a game as a file. - * - * @param path - * the path of the file - * @param serialisedGame - * the serialised Game - */ - public void save(final String path, final String serialisedGame); - -} diff --git a/src/esieequest/ui/rich/Applet.java b/src/esieequest/ui/rich/Applet.java deleted file mode 100644 index 1137182..0000000 --- a/src/esieequest/ui/rich/Applet.java +++ /dev/null @@ -1,28 +0,0 @@ -package esieequest.ui.rich; - -import javax.swing.JApplet; - -/** - * The applet view. - * - * @author Pacien TRAN-GIRARD - */ -public class Applet extends UserInterface { - - private final JApplet applet; - - /** - * The constructor. - * - * @param applet - * the JApplet - */ - public Applet(final JApplet applet) { - this.applet = applet; - } - - @Override - public void show() { - this.applet.add(this.getLayout()); - } -} diff --git a/src/esieequest/ui/rich/RichInterface.java b/src/esieequest/ui/rich/RichInterface.java new file mode 100644 index 0000000..89e66c2 --- /dev/null +++ b/src/esieequest/ui/rich/RichInterface.java @@ -0,0 +1,85 @@ +package esieequest.ui.rich; + +import lombok.Getter; +import esieequest.engine.audioplayer.AudioPlayer; +import esieequest.engine.datastore.DataStore; +import esieequest.engine.scheduler.Callback; +import esieequest.engine.scheduler.Scheduler; +import esieequest.game.states.Scene; +import esieequest.ui.View; + +/** + * A rich user interface which can play Scene-s. + * + * @author Pacien TRAN-GIRARD + */ +public abstract class RichInterface extends View { + + private final Scheduler scheduler; + + @Getter + private final AudioPlayer audioPlayer; + + /** + * Instantiates a new rich user interface. + * + * @param dataStore + * the data store + * @param scheduler + * the scheduler + * @param audioPlayer + * the audio player + */ + public RichInterface(final DataStore dataStore, final Scheduler scheduler, + final AudioPlayer audioPlayer) { + super(dataStore); + + this.scheduler = scheduler; + this.audioPlayer = audioPlayer; + } + + /** + * Sets the label. + * + * @param label + * the value of the label + */ + public abstract void setLabel(final String label); + + /** + * Sets the illustration frame. + * + * @param frameName + * the path to the frame to display + */ + public abstract void setFrame(final String frameName); + + @Override + public void playScene(final Scene scene, final Callback callback) { + this.disableInput(); + + this.setLabel(scene.getTitle()); + this.echo(scene.getText()); + this.setFrame(scene.name()); + this.audioPlayer.play(scene.name()); + + this.scheduler.schedule(callback, scene.getDuration()); + } + + @Override + public void skipScene() { + this.scheduler.cancel(); + this.scheduler.run(); + } + + @Override + public void stopMusic() { + this.audioPlayer.stop(); + } + + @Override + public void toggleMute() { + this.audioPlayer.toggleMute(); + } + +} diff --git a/src/esieequest/ui/rich/UserInterface.java b/src/esieequest/ui/rich/UserInterface.java deleted file mode 100644 index 636905d..0000000 --- a/src/esieequest/ui/rich/UserInterface.java +++ /dev/null @@ -1,707 +0,0 @@ -package esieequest.ui.rich; - -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.GridLayout; -import java.awt.Image; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; -import java.awt.event.KeyEvent; -import java.awt.image.BufferedImage; -import java.io.FileWriter; -import java.io.IOException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map.Entry; -import java.util.Timer; -import java.util.TimerTask; - -import javax.swing.AbstractAction; -import javax.swing.JButton; -import javax.swing.JComponent; -import javax.swing.JFileChooser; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JTextField; -import javax.swing.JTextPane; -import javax.swing.KeyStroke; -import javax.swing.border.EmptyBorder; -import javax.swing.filechooser.FileNameExtensionFilter; - -import lombok.Getter; - -import org.newdawn.easyogg.OggClip; - -import com.wordpress.tipsforjava.swing.StretchIcon; - -import esieequest.engine.GameEngine; -import esieequest.engine.commands.Command; -import esieequest.game.Text; -import esieequest.game.items.Inventory; -import esieequest.game.items.Item; -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; - -/** - * The Swing based graphical user interface. - * - * @author Pacien TRAN-GIRARD - * @author BenoƮt LUBRANO DI SBARAGLIONE - */ -abstract class UserInterface implements Viewable, ActionListener { - - private static final String ILLUSTRATION_DIR = "resources/images/"; - private static final String ILLUSTRATION_EXT = ".jpg"; - - private static final String SOUND_DIR = "resources/audio/"; - private static final String SOUND_EXT = ".ogg"; - - private static final String SAVE_LABEL = "ESIEEquest Game"; - private static final String SAVE_EXT = "eqg"; - - private GameEngine gameEngine; - - @Getter - private JPanel layout; - - private JTextPane questTextPane; - - private JLabel imageLabel; - - private JPanel menuPanel; - private JPanel questPanel; - private JPanel gamePanel; - - private JButton newButton; - private JButton soundButton; - private JPanel filePanel; - private JButton loadButton; - private JButton saveButton; - - private JPanel imagePanel; - - private JPanel userPanel; - private JPanel dispPanel; - - private JPanel consolePanel; - private JPanel inventoryPanel; - - private JPanel controlPanel; - private JPanel topControlPanel; - private JPanel bottomControlPanel; - - private JTextPane infoTextPane; - private JTextField inputField; - - private JButton forwardButton; - private JButton inventoryButton; - private JButton actionButton; - private JButton backButton; - private JButton leftButton; - private JButton rightButton; - - private Timer timer; - private TimerTask timerTask; - private boolean scenePlaying; - - private OggClip audio; - private boolean muted; - - /** - * The default constructor. - */ - public UserInterface() { - this.buildUI(); - this.setActionListener(this); - this.bindKeys(); - this.bindFocus(); - this.setControlsState(false); - this.muted = false; - } - - /** - * Creates the interface widgets. - */ - private void buildUI() { - - // main window - this.layout = new JPanel(); - this.layout.setLayout(new BorderLayout(0, 0)); - - // top bar - this.menuPanel = new JPanel(); - this.layout.add(this.menuPanel, BorderLayout.NORTH); - this.menuPanel.setLayout(new BorderLayout(0, 0)); - - this.questPanel = new JPanel(); - this.menuPanel.add(this.questPanel, BorderLayout.CENTER); - - this.questTextPane = new JTextPane(); - this.questTextPane.setEditable(false); - this.questTextPane.setText(Text.DEFAULT_QUEST_TITLE.toString()); - this.questPanel.add(this.questTextPane); - - this.gamePanel = new JPanel(); - this.menuPanel.add(this.gamePanel, BorderLayout.WEST); - - this.newButton = new JButton(Text.NEW_GAME_BUTTON.toString()); - this.newButton.setToolTipText(Text.NEW_GAME_TOOLTIP.toString()); - this.newButton.setActionCommand(Command.NEW.name()); - this.gamePanel.add(this.newButton); - - this.soundButton = new JButton(Text.TOGGLE_SOUND_BUTTON.toString()); - this.soundButton.setToolTipText(Text.TOGGLE_SOUND_TOOLTIP.toString()); - this.soundButton.setActionCommand(Command.SOUND.name()); - this.gamePanel.add(this.soundButton); - - this.filePanel = new JPanel(); - this.menuPanel.add(this.filePanel, BorderLayout.EAST); - - this.loadButton = new JButton(Text.LOAD_GAME_BUTTON.toString()); - this.loadButton.setToolTipText(Text.LOAD_GAME_TOOLTIP.toString()); - this.loadButton.setActionCommand(Command.LOAD.name()); - this.filePanel.add(this.loadButton); - - this.saveButton = new JButton(Text.SAVE_GAME_BUTTON.toString()); - this.saveButton.setToolTipText(Text.SAVE_GAME_TOOLTIP.toString()); - this.saveButton.setActionCommand(Command.SAVE.name()); - this.filePanel.add(this.saveButton); - - // central illustration panel - this.imagePanel = new JPanel(); - this.layout.add(this.imagePanel, BorderLayout.CENTER); - this.imagePanel.setLayout(new BorderLayout(0, 0)); - - this.imageLabel = new JLabel(); - this.imagePanel.add(this.imageLabel); - - // bottom panel - this.userPanel = new JPanel(); - this.layout.add(this.userPanel, BorderLayout.SOUTH); - this.userPanel.setLayout(new BorderLayout(0, 0)); - - // console and inventory - this.dispPanel = new JPanel(); - this.dispPanel.setBorder(new EmptyBorder(5, 5, 5, 0)); - this.userPanel.add(this.dispPanel, BorderLayout.CENTER); - this.dispPanel