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;
/**
* @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);
}

View File

@ -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<StateChangeListener> stateChangeListenerList = new ArrayList<StateChangeListener>();
private final List<StatemachineListener> 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<StatemachineListener>();
}
@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
@ -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);
}
}

View File

@ -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);
}

View File

@ -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 + "");
}
}

View File

@ -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