From bb0645491d85b6d1a10069ebbe3db75f80c4c9fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Tue, 5 Jun 2012 01:26:47 +0200 Subject: [PATCH] implemented the timers and cleaned up game code a bit --- src/de/ctdo/crashtest/game/IStatemachine.java | 12 +- src/de/ctdo/crashtest/game/Statemachine.java | 102 ++++++++++++--- ...istener.java => StatemachineListener.java} | 3 +- src/de/ctdo/crashtest/game/TheGame.java | 117 ++++++++++++------ src/de/ctdo/crashtest/gui/MainGui.java | 42 ------- 5 files changed, 173 insertions(+), 103 deletions(-) rename src/de/ctdo/crashtest/game/{StateChangeListener.java => StatemachineListener.java} (64%) diff --git a/src/de/ctdo/crashtest/game/IStatemachine.java b/src/de/ctdo/crashtest/game/IStatemachine.java index 8dc9c89..ceb5f3d 100644 --- a/src/de/ctdo/crashtest/game/IStatemachine.java +++ b/src/de/ctdo/crashtest/game/IStatemachine.java @@ -1,14 +1,16 @@ package de.ctdo.crashtest.game; -/** - * @author: lucas - * @date: 01.06.12 14:17 - */ public interface IStatemachine { - void addStateChangedListener(StateChangeListener listener); + void addListener(StatemachineListener listener); void reset(); Statemachine.state getCurrentState(); void setNewState(Statemachine.state newState); int getStateChangeCounter(); void handleInput(char input); + int getTimerSecondsLast(); + + + void startTimer(int seconds); + void stopTimer(); + void pauseTimer(boolean pause); } diff --git a/src/de/ctdo/crashtest/game/Statemachine.java b/src/de/ctdo/crashtest/game/Statemachine.java index 7bb32c6..d2d72a8 100644 --- a/src/de/ctdo/crashtest/game/Statemachine.java +++ b/src/de/ctdo/crashtest/game/Statemachine.java @@ -1,7 +1,11 @@ package de.ctdo.crashtest.game; +import de.ctdo.crashtest.log.Logger; + import java.util.ArrayList; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; public class Statemachine implements IStatemachine { public enum state { @@ -13,8 +17,8 @@ public class Statemachine implements IStatemachine { TABLE_GAME_FOUR, TABLE_GAME_FIVE, TABLE_GAME_SIX, - TABLE_GAME_SEVEN, - TABLE_FINISH, + TABLE_GAME_DONE, + ROKET_STARTED, ROKET_DONE } @@ -25,15 +29,24 @@ public class Statemachine implements IStatemachine { private final char TABLE_TWO = 'H'; private final char TABLE_THREE = 'I'; private final char ROKET_INPUT = 'B'; - private final List stateChangeListenerList = new ArrayList(); + private final List statemachineListenerList; - private long lastHandleInput = 0; - private int stateChangeCounter = 0; - private state currentState = state.IDLE; + private Timer timer; + + private long lastHandleInput; + private int stateChangeCounter; + private state currentState; + private int timertSecondsLast; + + + public Statemachine() { + currentState = state.IDLE; + statemachineListenerList = new ArrayList(); + } @Override - public void addStateChangedListener(StateChangeListener listener) { - stateChangeListenerList.add(listener); + public void addListener(StatemachineListener listener) { + statemachineListenerList.add(listener); } @Override @@ -57,6 +70,7 @@ public class Statemachine implements IStatemachine { public void reset() { stateChangeCounter = 0; currentState = state.IDLE; + stopTimer(); onStateChanged(); } @@ -68,7 +82,7 @@ public class Statemachine implements IStatemachine { if( newState != currentState ) { stateChangeCounter++; - System.out.println("newState = " + newState); + Logger.sLog("newState = " + newState); currentState = newState; onStateChanged(); @@ -77,16 +91,50 @@ public class Statemachine implements IStatemachine { lastHandleInput = System.currentTimeMillis(); } + @Override + public int getTimerSecondsLast() { + return timertSecondsLast / 10; + } + + @Override + public void startTimer(int seconds) { + Logger.sLog("starting timer"); + timertSecondsLast = seconds*10; + scheduleTimer(); + } + + @Override + public void stopTimer() { + Logger.sLog("stopping timer"); + if(timer != null) timer.cancel(); + timertSecondsLast = 0; + } + + @Override + public void pauseTimer(boolean pause) { + Logger.sLog("pausing timer: " + pause); + if(pause) { + if(timer != null) timer.cancel(); + } else { + scheduleTimer(); + } + } + /** * notifies all listeners about the changed state */ private void onStateChanged() { - for(StateChangeListener listener: stateChangeListenerList) { + for(StatemachineListener listener: statemachineListenerList) { listener.stateChanged(currentState); } } + private void onTimerTick() { + for(StatemachineListener listener: statemachineListenerList) { + listener.timerTick(timertSecondsLast); + } + } /** * Calculates the new game state from the current state and new input button @@ -99,7 +147,7 @@ public class Statemachine implements IStatemachine { if(input == RESET) { retVal = state.IDLE; } else { - + switch (currentState) { case IDLE: if(input == LIGHT_BARRIER) { @@ -146,17 +194,17 @@ public class Statemachine implements IStatemachine { break; case TABLE_GAME_SIX: if(input == TABLE_THREE) { - retVal = state.TABLE_GAME_SEVEN; + retVal = state.TABLE_GAME_DONE; } else if (input == TABLE_TWO) { retVal = state.TABLE_GAME_ONE; } break; - case TABLE_GAME_SEVEN: + case TABLE_GAME_DONE: if(input == BLUE_BUTTON) { - retVal = state.TABLE_FINISH; + retVal = state.ROKET_STARTED; } break; - case TABLE_FINISH: + case ROKET_STARTED: if(input == ROKET_INPUT) { retVal = state.ROKET_DONE; } @@ -165,4 +213,28 @@ public class Statemachine implements IStatemachine { return retVal; } + + + private void scheduleTimer() { + if(timer != null) timer.cancel(); + + timer = new Timer(); + + TimerTask timerTask = new TimerTask() { + @Override + public void run() { + timertSecondsLast--; + + onTimerTick(); + + if(timertSecondsLast <= 0) { + if(timer != null) timer.cancel(); + } + } + }; + timer.scheduleAtFixedRate(timerTask, 100, 100); + } + + + } diff --git a/src/de/ctdo/crashtest/game/StateChangeListener.java b/src/de/ctdo/crashtest/game/StatemachineListener.java similarity index 64% rename from src/de/ctdo/crashtest/game/StateChangeListener.java rename to src/de/ctdo/crashtest/game/StatemachineListener.java index 5c00ca1..bb8e3e4 100644 --- a/src/de/ctdo/crashtest/game/StateChangeListener.java +++ b/src/de/ctdo/crashtest/game/StatemachineListener.java @@ -5,6 +5,7 @@ package de.ctdo.crashtest.game; * Date: 10.05.12 * Time: 13:37 */ -public interface StateChangeListener { +public interface StatemachineListener { void stateChanged(Statemachine.state newState); + void timerTick(int tsecondsLeft); } diff --git a/src/de/ctdo/crashtest/game/TheGame.java b/src/de/ctdo/crashtest/game/TheGame.java index 7093355..13c12e2 100644 --- a/src/de/ctdo/crashtest/game/TheGame.java +++ b/src/de/ctdo/crashtest/game/TheGame.java @@ -6,20 +6,13 @@ import de.ctdo.crashtest.irc.*; import de.ctdo.crashtest.mpd.IMPDController; import de.ctdo.crashtest.mpd.MPDController; -public class TheGame implements StateChangeListener, GuiEventListener, IRCEventListener { +public class TheGame implements StatemachineListener, GuiEventListener, IRCEventListener { private IGuiControl guiControl; private IIrcClient ircClient; private IStatemachine machine; private IBuntiClient bunti; private IMPDController mpdController; - private int timertSeconds = 0; - private int timertSecondsLast = 0; - private int timeSpentTableGame = 0; - private int timeSpentRokets = 0; - - - public TheGame(IGuiControl guiControl) { this.guiControl = guiControl; @@ -34,7 +27,7 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL private void initGame() { guiControl.addListener(this); ircClient.addListener(this); - machine.addStateChangedListener(this); + machine.addListener(this); machine.reset(); } @@ -51,71 +44,111 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL switch (newState) { case IDLE: - mpdController.setVolume(45); + machine.stopTimer(); + mpdController.playSong("start", "mix"); + mpdController.setVolume(45); + bunti.setPar56(0,0,0); bunti.setLampel(false,false,false); + guiControl.showCountDown(false); break; case ENTERED_ROOM: - mpdController.setVolume(70); mpdController.playSong("Modem", "Dial_In_and_HF Longplay"); + mpdController.setVolume(70); + bunti.setLampel(false,false,false); bunti.setPar56(20,0,100); + + guiControl.showCountDown(true); + + machine.startTimer(60*8); break; case TABLE_GAME_ONE: mpdController.setVolume(70); mpdController.playSong("K2", "Der Berg Ruft"); + bunti.setLampel(true,false,false); - guiControl.showCountDown(true); bunti.setPar56(255,0,100); + guiControl.showCountDown(true); break; case TABLE_GAME_TWO: bunti.setLampel(false,true,false); bunti.setPar56(255,0,100); + + guiControl.showCountDown(true); break; case TABLE_GAME_THREE: bunti.setLampel(false,true,false); bunti.setPar56(255,35,0); - mpdController.setVolume(60); - mpdController.playSong("Mo-Do","9 Eins Zwei Polizei"); + + guiControl.showCountDown(true); break; case TABLE_GAME_FOUR: + mpdController.playSong("Mo-Do","9 Eins Zwei Polizei"); + mpdController.setVolume(60); + bunti.setLampel(false,true,false); bunti.setPar56(255,55,0); + + guiControl.showCountDown(true); break; case TABLE_GAME_FIVE: bunti.setLampel(false,true,false); bunti.setPar56(255,75,0); + + guiControl.showCountDown(true); break; case TABLE_GAME_SIX: + mpdController.playSong("Zlatko & Jürgen","Großer Bruder"); + mpdController.setVolume(60); + bunti.setLampel(false,true,false); bunti.setPar56(255,100,0); - mpdController.setVolume(60); - mpdController.playSong("Zlatko & Jürgen","Großer Bruder"); + + guiControl.showCountDown(true); break; - case TABLE_GAME_SEVEN: + case TABLE_GAME_DONE: bunti.setLampel(false,false,true); bunti.setPar56(255,100,0); + + guiControl.showCountDown(true); + machine.pauseTimer(true); sayScore(); break; - case TABLE_FINISH: // und roket muss starten + case ROKET_STARTED: + mpdController.playSong("The Underdog Project", "Summer Jam"); + mpdController.setVolume(50); + bunti.setLampel(false,false,true); bunti.setPar56(0, 255, 0); + ircClient.say("table game complete, r0kets now"); - mpdController.setVolume(50); - mpdController.playSong("The Underdog Project", "Summer Jam"); + + guiControl.showCountDown(true); + machine.startTimer(7*60); break; case ROKET_DONE: + mpdController.playSong("Coldplay", "Amsterdam"); + mpdController.setVolume(50); + bunti.setLampel(true,true,true); bunti.setPar56(255, 196, 0); - mpdController.setVolume(50); - mpdController.playSong("Coldplay", "Amsterdam"); + + guiControl.showCountDown(true); + machine.pauseTimer(true); break; } } + @Override + public void timerTick(int tsecondsLeft) { + guiControl.setCountDown(tsecondsLeft); + if(tsecondsLeft == 0) ircClient.say("timer expired"); + } + /** * Event listener for keyPress events from the GUI @@ -133,7 +166,9 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL */ @Override public void windowClosing() { - + machine.reset(); + bunti.setPar56(0xff,0xff,0xff); + ircClient.say("bye"); } /** @@ -146,7 +181,6 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL handleHelpCommand(message); } else if(message.equals("reset")) { machine.reset(); - ircClient.say("reset done"); } else if(message.startsWith("state")) { handleStateCommand(message); } else if(message.startsWith("timer")) { @@ -169,12 +203,11 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL String timeStr = params.substring("start".length()).trim(); if(timeStr.length() > 0) { - int time = 0; - try { - time = Integer.parseInt(timeStr); + int time = Integer.parseInt(timeStr); guiControl.showCountDown(true); + machine.startTimer(time); ircClient.say("got it, starting with " + time + " seconds"); } catch (NumberFormatException e) { @@ -184,23 +217,20 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL ircClient.say("invalid parameter"); } } else if(params.startsWith("stop")) { - + machine.stopTimer(); guiControl.showCountDown(false); ircClient.say("timer stopped"); } else if(params.startsWith("pause")) { - + machine.pauseTimer(true); } else if(params.startsWith("resume")) { - + machine.pauseTimer(false); } - - } private void handleStateCommand(final String message) { if(message.equals("state")) { ircClient.say(machine.getCurrentState().name()); } else { - String params = message.substring("state".length()).trim().toLowerCase(); Boolean ok = false; @@ -215,16 +245,9 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL } if(!ok) ircClient.say("ehm, impossibruu!"); - } } - private void sayScore() { - ircClient.say("stateChangeCounter: " + machine.getStateChangeCounter()); - ircClient.say("timerlast: " + timertSecondsLast / 10); - } - - private void handleHelpCommand(final String message) { if(message.equals("help")) { ircClient.say("commands: help, reset, state, timer, wall, extra, score"); @@ -266,4 +289,18 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL } + + private void sayScore() { + ircClient.say("stateChangeCounter: " + machine.getStateChangeCounter()); +// ircClient.say("timerlast: " + machine.getTimerSecondsLast()); + + int seconds = machine.getTimerSecondsLast(); + int mins = seconds / 60; + int secs = seconds % 60; + ircClient.say(" " + mins + ":" + secs + ""); + + } + + + } diff --git a/src/de/ctdo/crashtest/gui/MainGui.java b/src/de/ctdo/crashtest/gui/MainGui.java index fd69252..74ea890 100644 --- a/src/de/ctdo/crashtest/gui/MainGui.java +++ b/src/de/ctdo/crashtest/gui/MainGui.java @@ -45,23 +45,6 @@ public class MainGui extends JFrame implements IGuiControl { setVisible(true); } -// -// -// private void initTimer() { -// timer = new Timer(100, new ActionListener() { -// @Override -// public void actionPerformed(ActionEvent e) { -// timerSecondsLast--; -// setTimerText(); -// -// if(timerSecondsLast <= 0) { -// timerSecondsLast = 0; -// timer.stop(); -// } -// } -// }); -// } -// private void setCountDownText(final int tseconds) { int mins = tseconds / 600; int secs = tseconds % 600 / 10 ; @@ -105,30 +88,6 @@ public class MainGui extends JFrame implements IGuiControl { extraField.setText(" "); } - -// @Override -// public void startTimer(int seconds) { -// timerSeconds = seconds*10; -// timerSecondsLast = seconds*10; -// timer.start(); -// } -// -// @Override -// public void stopTimer() { -// timer.stop(); -// timerSeconds = 0; -// timerSecondsLast = 0; -// } -// -// @Override -// public void pauseTimer(Boolean pause) { -// if(pause) { -// timer.stop(); -// } else { -// timer.start(); -// } -// } - @Override public void setWall(final String message) { final Runnable runnable = new Runnable() { @@ -175,7 +134,6 @@ public class MainGui extends JFrame implements IGuiControl { }; SwingUtilities.invokeLater(runnable); - } @Override