implemented the timers and cleaned up game code a bit

This commit is contained in:
Lucas Pleß 2012-06-05 01:26:47 +02:00
parent 06a0fe0b46
commit bb0645491d
5 changed files with 173 additions and 103 deletions

View File

@ -1,14 +1,16 @@
package de.ctdo.crashtest.game; package de.ctdo.crashtest.game;
/**
* @author: lucas
* @date: 01.06.12 14:17
*/
public interface IStatemachine { public interface IStatemachine {
void addStateChangedListener(StateChangeListener listener); void addListener(StatemachineListener listener);
void reset(); void reset();
Statemachine.state getCurrentState(); Statemachine.state getCurrentState();
void setNewState(Statemachine.state newState); void setNewState(Statemachine.state newState);
int getStateChangeCounter(); int getStateChangeCounter();
void handleInput(char input); void handleInput(char input);
int getTimerSecondsLast();
void startTimer(int seconds);
void stopTimer();
void pauseTimer(boolean pause);
} }

View File

@ -1,7 +1,11 @@
package de.ctdo.crashtest.game; package de.ctdo.crashtest.game;
import de.ctdo.crashtest.log.Logger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class Statemachine implements IStatemachine { public class Statemachine implements IStatemachine {
public enum state { public enum state {
@ -13,8 +17,8 @@ public class Statemachine implements IStatemachine {
TABLE_GAME_FOUR, TABLE_GAME_FOUR,
TABLE_GAME_FIVE, TABLE_GAME_FIVE,
TABLE_GAME_SIX, TABLE_GAME_SIX,
TABLE_GAME_SEVEN, TABLE_GAME_DONE,
TABLE_FINISH, ROKET_STARTED,
ROKET_DONE ROKET_DONE
} }
@ -25,15 +29,24 @@ public class Statemachine implements IStatemachine {
private final char TABLE_TWO = 'H'; private final char TABLE_TWO = 'H';
private final char TABLE_THREE = 'I'; private final char TABLE_THREE = 'I';
private final char ROKET_INPUT = 'B'; private final char ROKET_INPUT = 'B';
private final List<StateChangeListener> stateChangeListenerList = new ArrayList<StateChangeListener>(); private final List<StatemachineListener> statemachineListenerList;
private long lastHandleInput = 0; private Timer timer;
private int stateChangeCounter = 0;
private state currentState = state.IDLE; private long lastHandleInput;
private int stateChangeCounter;
private state currentState;
private int timertSecondsLast;
public Statemachine() {
currentState = state.IDLE;
statemachineListenerList = new ArrayList<StatemachineListener>();
}
@Override @Override
public void addStateChangedListener(StateChangeListener listener) { public void addListener(StatemachineListener listener) {
stateChangeListenerList.add(listener); statemachineListenerList.add(listener);
} }
@Override @Override
@ -57,6 +70,7 @@ public class Statemachine implements IStatemachine {
public void reset() { public void reset() {
stateChangeCounter = 0; stateChangeCounter = 0;
currentState = state.IDLE; currentState = state.IDLE;
stopTimer();
onStateChanged(); onStateChanged();
} }
@ -68,7 +82,7 @@ public class Statemachine implements IStatemachine {
if( newState != currentState ) { if( newState != currentState ) {
stateChangeCounter++; stateChangeCounter++;
System.out.println("newState = " + newState); Logger.sLog("newState = " + newState);
currentState = newState; currentState = newState;
onStateChanged(); onStateChanged();
@ -77,16 +91,50 @@ public class Statemachine implements IStatemachine {
lastHandleInput = System.currentTimeMillis(); 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 * notifies all listeners about the changed state
*/ */
private void onStateChanged() { private void onStateChanged() {
for(StateChangeListener listener: stateChangeListenerList) { for(StatemachineListener listener: statemachineListenerList) {
listener.stateChanged(currentState); 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 * 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) { if(input == RESET) {
retVal = state.IDLE; retVal = state.IDLE;
} else { } else {
switch (currentState) { switch (currentState) {
case IDLE: case IDLE:
if(input == LIGHT_BARRIER) { if(input == LIGHT_BARRIER) {
@ -146,17 +194,17 @@ public class Statemachine implements IStatemachine {
break; break;
case TABLE_GAME_SIX: case TABLE_GAME_SIX:
if(input == TABLE_THREE) { if(input == TABLE_THREE) {
retVal = state.TABLE_GAME_SEVEN; retVal = state.TABLE_GAME_DONE;
} else if (input == TABLE_TWO) { } else if (input == TABLE_TWO) {
retVal = state.TABLE_GAME_ONE; retVal = state.TABLE_GAME_ONE;
} }
break; break;
case TABLE_GAME_SEVEN: case TABLE_GAME_DONE:
if(input == BLUE_BUTTON) { if(input == BLUE_BUTTON) {
retVal = state.TABLE_FINISH; retVal = state.ROKET_STARTED;
} }
break; break;
case TABLE_FINISH: case ROKET_STARTED:
if(input == ROKET_INPUT) { if(input == ROKET_INPUT) {
retVal = state.ROKET_DONE; retVal = state.ROKET_DONE;
} }
@ -165,4 +213,28 @@ public class Statemachine implements IStatemachine {
return retVal; 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);
}
} }

View File

@ -5,6 +5,7 @@ package de.ctdo.crashtest.game;
* Date: 10.05.12 * Date: 10.05.12
* Time: 13:37 * Time: 13:37
*/ */
public interface StateChangeListener { public interface StatemachineListener {
void stateChanged(Statemachine.state newState); void stateChanged(Statemachine.state newState);
void timerTick(int tsecondsLeft);
} }

View File

@ -6,20 +6,13 @@ import de.ctdo.crashtest.irc.*;
import de.ctdo.crashtest.mpd.IMPDController; import de.ctdo.crashtest.mpd.IMPDController;
import de.ctdo.crashtest.mpd.MPDController; import de.ctdo.crashtest.mpd.MPDController;
public class TheGame implements StateChangeListener, GuiEventListener, IRCEventListener { public class TheGame implements StatemachineListener, GuiEventListener, IRCEventListener {
private IGuiControl guiControl; private IGuiControl guiControl;
private IIrcClient ircClient; private IIrcClient ircClient;
private IStatemachine machine; private IStatemachine machine;
private IBuntiClient bunti; private IBuntiClient bunti;
private IMPDController mpdController; private IMPDController mpdController;
private int timertSeconds = 0;
private int timertSecondsLast = 0;
private int timeSpentTableGame = 0;
private int timeSpentRokets = 0;
public TheGame(IGuiControl guiControl) { public TheGame(IGuiControl guiControl) {
this.guiControl = guiControl; this.guiControl = guiControl;
@ -34,7 +27,7 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL
private void initGame() { private void initGame() {
guiControl.addListener(this); guiControl.addListener(this);
ircClient.addListener(this); ircClient.addListener(this);
machine.addStateChangedListener(this); machine.addListener(this);
machine.reset(); machine.reset();
} }
@ -51,71 +44,111 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL
switch (newState) { switch (newState) {
case IDLE: case IDLE:
mpdController.setVolume(45); machine.stopTimer();
mpdController.playSong("start", "mix"); mpdController.playSong("start", "mix");
mpdController.setVolume(45);
bunti.setPar56(0,0,0); bunti.setPar56(0,0,0);
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
guiControl.showCountDown(false); guiControl.showCountDown(false);
break; break;
case ENTERED_ROOM: case ENTERED_ROOM:
mpdController.setVolume(70);
mpdController.playSong("Modem", "Dial_In_and_HF Longplay"); mpdController.playSong("Modem", "Dial_In_and_HF Longplay");
mpdController.setVolume(70);
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
bunti.setPar56(20,0,100); bunti.setPar56(20,0,100);
guiControl.showCountDown(true);
machine.startTimer(60*8);
break; break;
case TABLE_GAME_ONE: case TABLE_GAME_ONE:
mpdController.setVolume(70); mpdController.setVolume(70);
mpdController.playSong("K2", "Der Berg Ruft"); mpdController.playSong("K2", "Der Berg Ruft");
bunti.setLampel(true,false,false); bunti.setLampel(true,false,false);
guiControl.showCountDown(true);
bunti.setPar56(255,0,100); bunti.setPar56(255,0,100);
guiControl.showCountDown(true);
break; break;
case TABLE_GAME_TWO: case TABLE_GAME_TWO:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,0,100); bunti.setPar56(255,0,100);
guiControl.showCountDown(true);
break; break;
case TABLE_GAME_THREE: case TABLE_GAME_THREE:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,35,0); bunti.setPar56(255,35,0);
mpdController.setVolume(60);
mpdController.playSong("Mo-Do","9 Eins Zwei Polizei"); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_FOUR: case TABLE_GAME_FOUR:
mpdController.playSong("Mo-Do","9 Eins Zwei Polizei");
mpdController.setVolume(60);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,55,0); bunti.setPar56(255,55,0);
guiControl.showCountDown(true);
break; break;
case TABLE_GAME_FIVE: case TABLE_GAME_FIVE:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,75,0); bunti.setPar56(255,75,0);
guiControl.showCountDown(true);
break; break;
case TABLE_GAME_SIX: case TABLE_GAME_SIX:
mpdController.playSong("Zlatko & Jürgen","Großer Bruder");
mpdController.setVolume(60);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,100,0); bunti.setPar56(255,100,0);
mpdController.setVolume(60);
mpdController.playSong("Zlatko & Jürgen","Großer Bruder"); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_SEVEN: case TABLE_GAME_DONE:
bunti.setLampel(false,false,true); bunti.setLampel(false,false,true);
bunti.setPar56(255,100,0); bunti.setPar56(255,100,0);
guiControl.showCountDown(true);
machine.pauseTimer(true);
sayScore(); sayScore();
break; 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.setLampel(false,false,true);
bunti.setPar56(0, 255, 0); bunti.setPar56(0, 255, 0);
ircClient.say("table game complete, r0kets now"); 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; break;
case ROKET_DONE: case ROKET_DONE:
mpdController.playSong("Coldplay", "Amsterdam");
mpdController.setVolume(50);
bunti.setLampel(true,true,true); bunti.setLampel(true,true,true);
bunti.setPar56(255, 196, 0); bunti.setPar56(255, 196, 0);
mpdController.setVolume(50);
mpdController.playSong("Coldplay", "Amsterdam"); guiControl.showCountDown(true);
machine.pauseTimer(true);
break; 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 * Event listener for keyPress events from the GUI
@ -133,7 +166,9 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL
*/ */
@Override @Override
public void windowClosing() { 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); handleHelpCommand(message);
} else if(message.equals("reset")) { } else if(message.equals("reset")) {
machine.reset(); machine.reset();
ircClient.say("reset done");
} else if(message.startsWith("state")) { } else if(message.startsWith("state")) {
handleStateCommand(message); handleStateCommand(message);
} else if(message.startsWith("timer")) { } else if(message.startsWith("timer")) {
@ -169,12 +203,11 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL
String timeStr = params.substring("start".length()).trim(); String timeStr = params.substring("start".length()).trim();
if(timeStr.length() > 0) { if(timeStr.length() > 0) {
int time = 0;
try { try {
time = Integer.parseInt(timeStr); int time = Integer.parseInt(timeStr);
guiControl.showCountDown(true); guiControl.showCountDown(true);
machine.startTimer(time);
ircClient.say("got it, starting with " + time + " seconds"); ircClient.say("got it, starting with " + time + " seconds");
} }
catch (NumberFormatException e) { catch (NumberFormatException e) {
@ -184,23 +217,20 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL
ircClient.say("invalid parameter"); ircClient.say("invalid parameter");
} }
} else if(params.startsWith("stop")) { } else if(params.startsWith("stop")) {
machine.stopTimer();
guiControl.showCountDown(false); guiControl.showCountDown(false);
ircClient.say("timer stopped"); ircClient.say("timer stopped");
} else if(params.startsWith("pause")) { } else if(params.startsWith("pause")) {
machine.pauseTimer(true);
} else if(params.startsWith("resume")) { } else if(params.startsWith("resume")) {
machine.pauseTimer(false);
} }
} }
private void handleStateCommand(final String message) { private void handleStateCommand(final String message) {
if(message.equals("state")) { if(message.equals("state")) {
ircClient.say(machine.getCurrentState().name()); ircClient.say(machine.getCurrentState().name());
} else { } else {
String params = message.substring("state".length()).trim().toLowerCase(); String params = message.substring("state".length()).trim().toLowerCase();
Boolean ok = false; Boolean ok = false;
@ -215,16 +245,9 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL
} }
if(!ok) ircClient.say("ehm, impossibruu!"); 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) { private void handleHelpCommand(final String message) {
if(message.equals("help")) { if(message.equals("help")) {
ircClient.say("commands: help, reset, state, timer, wall, extra, score"); 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 + "");
}
} }

View File

@ -45,23 +45,6 @@ public class MainGui extends JFrame implements IGuiControl {
setVisible(true); 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) { private void setCountDownText(final int tseconds) {
int mins = tseconds / 600; int mins = tseconds / 600;
int secs = tseconds % 600 / 10 ; int secs = tseconds % 600 / 10 ;
@ -105,30 +88,6 @@ public class MainGui extends JFrame implements IGuiControl {
extraField.setText(" "); 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 @Override
public void setWall(final String message) { public void setWall(final String message) {
final Runnable runnable = new Runnable() { final Runnable runnable = new Runnable() {
@ -175,7 +134,6 @@ public class MainGui extends JFrame implements IGuiControl {
}; };
SwingUtilities.invokeLater(runnable); SwingUtilities.invokeLater(runnable);
} }
@Override @Override