From b77e4a7c77d9608be715e943f282af1f6ef45596 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 19 May 2014 20:40:34 +0200 Subject: Implement Scene --- src/esieequest/controller/Callback.java | 15 ++++ src/esieequest/controller/commands/NewCommand.java | 23 ++++-- src/esieequest/model/events/Quest.java | 2 +- src/esieequest/model/events/Scene.java | 48 ++++++++++++ src/esieequest/view/Viewable.java | 9 +++ src/esieequest/view/app/UserInterface.java | 91 ++++++++++------------ src/esieequest/view/text/TextInterface.java | 9 ++- src/esieequest/view/web/WebInterface.java | 27 ++++++- 8 files changed, 164 insertions(+), 60 deletions(-) create mode 100644 src/esieequest/controller/Callback.java create mode 100644 src/esieequest/model/events/Scene.java diff --git a/src/esieequest/controller/Callback.java b/src/esieequest/controller/Callback.java new file mode 100644 index 0000000..ef03b72 --- /dev/null +++ b/src/esieequest/controller/Callback.java @@ -0,0 +1,15 @@ +package esieequest.controller; + +/** + * 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/controller/commands/NewCommand.java b/src/esieequest/controller/commands/NewCommand.java index 3f0b4b9..7283989 100644 --- a/src/esieequest/controller/commands/NewCommand.java +++ b/src/esieequest/controller/commands/NewCommand.java @@ -1,7 +1,9 @@ package esieequest.controller.commands; +import esieequest.controller.Callback; import esieequest.model.Game; import esieequest.model.Text; +import esieequest.model.events.Scene; import esieequest.view.Viewable; /** @@ -22,13 +24,22 @@ public class NewCommand implements Executable { game.newGame(true, challengeMode); - view.enable(); - - view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer() - .getCurrentDirection(), game.getPlayer().getCurrentSide(), game.getPlayer() - .canGoBack()); view.echo(Text.WELCOME.toString()); - } + Scene.INTRO.setCallback(new Callback() { + @Override + public void call() { + view.updateQuest(game.getPlayer().getCurrentQuest()); + + view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer() + .getCurrentDirection(), game.getPlayer().getCurrentSide(), game.getPlayer() + .canGoBack()); + view.enable(); + } + }); + + view.playScene(Scene.INTRO); + + } } diff --git a/src/esieequest/model/events/Quest.java b/src/esieequest/model/events/Quest.java index 54cbb4a..ac523e2 100644 --- a/src/esieequest/model/events/Quest.java +++ b/src/esieequest/model/events/Quest.java @@ -13,7 +13,7 @@ public enum Quest { WHAT_HAPPENED("What happened?!?!"), FIND_ATHANASE("Find Athanase"), - FEED_ATHAnASE("Feed Athanase"), + FEED_ATHANASE("Feed Athanase"), FIND_DISK("Find the Disk"), FIND_TRANSPONDER("Find the Transponder"), ACTIVATE_TRANSPONDER("Activate the Transponder"), diff --git a/src/esieequest/model/events/Scene.java b/src/esieequest/model/events/Scene.java new file mode 100644 index 0000000..eebd9f6 --- /dev/null +++ b/src/esieequest/model/events/Scene.java @@ -0,0 +1,48 @@ +package esieequest.model.events; + +import lombok.Getter; +import lombok.Setter; +import esieequest.controller.Callback; + +/** + * Represents an animated Scene that can be played. + * + * @author Pacien TRAN-GIRARD + */ +public enum Scene { + + // @formatter:off + + INTRO("Testing is the future...", "...", 2500), + END("...and the future starts with you!", "...", 2500), + + ; + + // @formatter:on + + @Getter + private final String title; + + @Getter + private final String text; + + @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/view/Viewable.java b/src/esieequest/view/Viewable.java index dc12973..845b4ea 100644 --- a/src/esieequest/view/Viewable.java +++ b/src/esieequest/view/Viewable.java @@ -2,6 +2,7 @@ package esieequest.view; import esieequest.controller.GameEngine; import esieequest.model.events.Quest; +import esieequest.model.events.Scene; import esieequest.model.items.Inventory; import esieequest.model.map.Direction; import esieequest.model.map.Room; @@ -74,4 +75,12 @@ public interface Viewable { */ public void updateInventory(final Inventory inventory); + /** + * Plays the given Scene. + * + * @param scene + * the Scene to play + */ + public void playScene(final Scene scene); + } diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index 0b0a44b..cb8d6aa 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -14,9 +14,7 @@ import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.net.URL; import java.util.HashMap; -import java.util.HashSet; import java.util.Map.Entry; -import java.util.Set; import javax.swing.AbstractAction; import javax.swing.JButton; @@ -36,6 +34,7 @@ import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.Text; import esieequest.model.events.Quest; +import esieequest.model.events.Scene; import esieequest.model.items.Inventory; import esieequest.model.items.Item; import esieequest.model.map.Direction; @@ -67,11 +66,10 @@ abstract class UserInterface implements Viewable, ActionListener { private JPanel questPanel; private JPanel gamePanel; - private final Set gameButtons; - private JButton newButton; private JButton soundButton; private JPanel filePanel; + private JButton loadButton; private JButton saveButton; private JPanel imagePanel; @@ -89,8 +87,6 @@ abstract class UserInterface implements Viewable, ActionListener { private JTextPane infoTextPane; private JTextField inputField; - private final Set controlButtons; - private JButton forwardButton; private JButton inventoryButton; private JButton actionButton; @@ -102,9 +98,6 @@ abstract class UserInterface implements Viewable, ActionListener { * The default constructor. */ public UserInterface() { - this.gameButtons = new HashSet<>(); - this.controlButtons = new HashSet<>(); - this.buildUI(); this.setActionListener(this); this.bindKeys(); @@ -141,28 +134,24 @@ abstract class UserInterface implements Viewable, ActionListener { this.newButton.setToolTipText(Text.NEW_GAME_TOOLTIP.toString()); this.newButton.setActionCommand(Command.NEW.name()); this.gamePanel.add(this.newButton); - this.gameButtons.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.gameButtons.add(this.soundButton); this.filePanel = new JPanel(); this.menuPanel.add(this.filePanel, BorderLayout.EAST); - final JButton loadButton = new JButton(Text.LOAD_GAME_BUTTON.toString()); - loadButton.setToolTipText(Text.LOAD_GAME_TOOLTIP.toString()); - loadButton.setActionCommand(Command.LOAD.name()); - this.filePanel.add(loadButton); - this.gameButtons.add(loadButton); + 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); - this.gameButtons.add(this.saveButton); // central illustration panel this.imagePanel = new JPanel(); @@ -211,6 +200,9 @@ abstract class UserInterface implements Viewable, ActionListener { this.userPanel.add(this.controlPanel, BorderLayout.EAST); this.controlPanel.setLayout(new BorderLayout(0, 0)); + final Font font = new Font("Dialog", Font.BOLD, 19); + final Dimension squareButton = new Dimension(50, 50); + this.topControlPanel = new JPanel(); this.controlPanel.add(this.topControlPanel, BorderLayout.NORTH); this.topControlPanel.setLayout(new BorderLayout(0, 0)); @@ -218,19 +210,22 @@ abstract class UserInterface implements Viewable, ActionListener { this.forwardButton = new JButton(Text.GO_FORWARD_BUTTON.toString()); this.forwardButton.setToolTipText(Text.GO_FORWARD_TOOLTIP.toString()); this.forwardButton.setActionCommand(Command.FORWARD.name()); + this.forwardButton.setFont(font); + this.forwardButton.setPreferredSize(squareButton); this.topControlPanel.add(this.forwardButton, BorderLayout.CENTER); - this.controlButtons.add(this.forwardButton); this.inventoryButton = new JButton(Text.INVENTORY_BUTTON.toString()); this.inventoryButton.setToolTipText(Text.INVENTORY_TOOLTIP.toString()); + this.inventoryButton.setFont(font); + this.inventoryButton.setPreferredSize(squareButton); this.topControlPanel.add(this.inventoryButton, BorderLayout.WEST); - this.controlButtons.add(this.inventoryButton); this.actionButton = new JButton(Text.ACTION_BUTTON.toString()); this.actionButton.setToolTipText(Text.ACTION_TOOLTIP.toString()); this.actionButton.setActionCommand(Command.DO.name()); + this.actionButton.setFont(font); + this.actionButton.setPreferredSize(squareButton); this.topControlPanel.add(this.actionButton, BorderLayout.EAST); - this.controlButtons.add(this.actionButton); this.bottomControlPanel = new JPanel(); this.controlPanel.add(this.bottomControlPanel, BorderLayout.SOUTH); @@ -239,44 +234,26 @@ abstract class UserInterface implements Viewable, ActionListener { this.backButton = new JButton(Text.GO_BACK_BUTTON.toString()); this.backButton.setToolTipText(Text.GO_BACK_TOOLTIP.toString()); this.backButton.setActionCommand(Command.BACK.name()); + this.backButton.setFont(font); + this.backButton.setPreferredSize(squareButton); this.bottomControlPanel.add(this.backButton, BorderLayout.CENTER); - this.controlButtons.add(this.backButton); this.leftButton = new JButton(Text.TURN_LEFT_BUTTON.toString()); this.leftButton.setToolTipText(Text.TURN_LEFT_TOOLTIP.toString()); this.leftButton.setActionCommand(Command.TURN.name() + Text.COMMAND_SEPARATOR.toString() + Orientation.LEFT.name()); + this.leftButton.setFont(font); + this.leftButton.setPreferredSize(squareButton); this.bottomControlPanel.add(this.leftButton, BorderLayout.WEST); - this.controlButtons.add(this.leftButton); this.rightButton = new JButton(Text.TURN_RIGHT_BUTTON.toString()); this.rightButton.setToolTipText(Text.TURN_RIGHT_TOOLTIP.toString()); this.rightButton.setActionCommand(Command.TURN.name() + Text.COMMAND_SEPARATOR.toString() + Orientation.RIGHT.name()); + this.rightButton.setFont(font); + this.rightButton.setPreferredSize(squareButton); this.bottomControlPanel.add(this.rightButton, BorderLayout.EAST); - this.controlButtons.add(this.rightButton); - - final Font font = new Font("Dialog", Font.BOLD, 19); - final Dimension squareButton = new Dimension(50, 50); - for (final JButton controlButton : this.controlButtons) { - controlButton.setFont(font); - controlButton.setPreferredSize(squareButton); - } - - } - /** - * Sets the action listener for the given JButtons. - * - * @param buttons - * the JButton Set - * @param actionListener - * the action listener - */ - private void setActionListener(final Set buttons, final ActionListener actionListener) { - for (final JButton button : buttons) { - button.addActionListener(actionListener); - } } /** @@ -287,8 +264,15 @@ abstract class UserInterface implements Viewable, ActionListener { */ private void setActionListener(final ActionListener actionListener) { this.inputField.addActionListener(actionListener); - this.setActionListener(this.gameButtons, actionListener); - this.setActionListener(this.controlButtons, actionListener); + this.newButton.addActionListener(actionListener); + this.soundButton.addActionListener(actionListener); + this.loadButton.addActionListener(actionListener); + this.forwardButton.addActionListener(actionListener); + this.inventoryButton.addActionListener(actionListener); + this.actionButton.addActionListener(actionListener); + this.backButton.addActionListener(actionListener); + this.leftButton.addActionListener(actionListener); + this.rightButton.addActionListener(actionListener); } /** @@ -373,8 +357,13 @@ abstract class UserInterface implements Viewable, ActionListener { private void setControlsState(final boolean state) { this.inputField.setEnabled(state); this.saveButton.setEnabled(state); - for (final JButton vButton : this.controlButtons) { - vButton.setEnabled(state); + this.inventoryButton.setEnabled(state); + this.leftButton.setEnabled(state); + this.rightButton.setEnabled(state); + if (!state) { + this.forwardButton.setEnabled(state); + this.backButton.setEnabled(state); + this.actionButton.setEnabled(state); } } @@ -523,4 +512,10 @@ abstract class UserInterface implements Viewable, ActionListener { this.openInventory(); } + + @Override + public void playScene(final Scene scene) { + scene.getCallback().call(); + } + } diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 76ee308..c735640 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -4,6 +4,7 @@ import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.Text; import esieequest.model.events.Quest; +import esieequest.model.events.Scene; import esieequest.model.items.Inventory; import esieequest.model.map.Direction; import esieequest.model.map.Room; @@ -59,14 +60,13 @@ abstract class TextInterface implements Viewable { @Override public void show() { - this.enable(); + this.gameEngine.interpret(Command.NEW.name()); } @Override public void enable() { if (!this.running) { this.running = true; - this.gameEngine.interpret(Command.NEW.name()); this.run(); } } @@ -97,4 +97,9 @@ abstract class TextInterface implements Viewable { this.gameEngine.interpret(Command.INVENTORY.name()); } + @Override + public void playScene(final Scene scene) { + scene.getCallback().call(); + } + } diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 0d5f201..0bd5776 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -11,6 +11,7 @@ import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Event.NativePreviewEvent; import com.google.gwt.user.client.Event.NativePreviewHandler; +import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.FlowPanel; @@ -25,6 +26,7 @@ import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.Text; import esieequest.model.events.Quest; +import esieequest.model.events.Scene; import esieequest.model.items.Inventory; import esieequest.model.items.Item; import esieequest.model.map.Direction; @@ -253,11 +255,13 @@ class WebInterface extends Composite implements Viewable { this.inputField.setEnabled(state); this.saveButton.setEnabled(state); this.inventoryButton.setEnabled(state); - this.actionButton.setEnabled(state); - this.forwardButton.setEnabled(state); - this.backButton.setEnabled(state); this.leftButton.setEnabled(state); this.rightButton.setEnabled(state); + if (!state) { + this.forwardButton.setEnabled(state); + this.backButton.setEnabled(state); + this.actionButton.setEnabled(state); + } } /** @@ -390,4 +394,21 @@ class WebInterface extends Composite implements Viewable { this.openInventory(); } + @Override + public void playScene(final Scene scene) { + this.disable(); + + this.setQuestLabel(scene.getTitle()); + this.echo(scene.getText()); + this.setIllustration(scene.name()); + + new Timer() { + @Override + public void run() { + scene.getCallback().call(); + } + }.schedule(scene.getDuration()); + + } + } -- cgit v1.2.3