building on statemachine

This commit is contained in:
Lucas Ple 2012-05-10 11:06:16 +02:00 committed by Lucas Pleß
parent 85b2d9699a
commit 3c05a00c1a
2 changed files with 86 additions and 6 deletions

View File

@ -1,7 +1,13 @@
package de.ctdo.crashtest; package de.ctdo.crashtest;
public class Statemachine { public class Statemachine {
public enum STATES { private final char BLUE_BUTTON = 'd';
private final char LIGHT_BARRIER = 'k';
private final char TABLE_ONE = 'c';
private final char TABLE_TWO = 'v';
private final char TABLE_THREE = 'b';
public enum state {
IDLE, IDLE,
ENTERED_ROOM, ENTERED_ROOM,
TABLE_GAME_ONE, TABLE_GAME_ONE,
@ -11,20 +17,93 @@ public class Statemachine {
TABLE_GAME_FIVE, TABLE_GAME_FIVE,
TABLE_GAME_SIX, TABLE_GAME_SIX,
TABLE_GAME_SEVEN, TABLE_GAME_SEVEN,
TABLE_GAME_EIGHT,
FINISH, FINISH,
} }
private STATES currentState = STATES.IDLE; private state currentState = state.IDLE;
public STATES getCurrentState() { public state getCurrentState() {
return currentState; return currentState;
} }
public void handleInput(char input) { public void handleInput(char input) {
state newState = getNewState(input);
System.out.println("newState = " + newState);
currentState = newState;
}
private state getNewState(char input) {
state retVal = currentState;
switch (currentState) {
case IDLE:
if(input == LIGHT_BARRIER) {
retVal = state.ENTERED_ROOM;
}
break;
case ENTERED_ROOM:
if(input == TABLE_ONE) {
retVal = state.TABLE_GAME_ONE;
}
break;
case TABLE_GAME_ONE:
if(input == TABLE_TWO) {
retVal = state.TABLE_GAME_TWO;
} else {
retVal = state.TABLE_GAME_ONE;
}
break;
case TABLE_GAME_TWO:
if(input == TABLE_THREE) {
retVal = state.TABLE_GAME_THREE;
} else {
retVal = state.TABLE_GAME_ONE;
}
break;
case TABLE_GAME_THREE:
if(input == TABLE_TWO) {
retVal = state.TABLE_GAME_FOUR;
} else {
retVal = state.TABLE_GAME_ONE;
}
break;
case TABLE_GAME_FOUR:
if(input == TABLE_THREE) {
retVal = state.TABLE_GAME_FIVE;
} else {
retVal = state.TABLE_GAME_ONE;
}
break;
case TABLE_GAME_FIVE:
if(input == TABLE_ONE) {
retVal = state.TABLE_GAME_SIX;
} else {
retVal = state.TABLE_GAME_ONE;
}
break;
case TABLE_GAME_SIX:
if(input == TABLE_THREE) {
retVal = state.TABLE_GAME_SEVEN;
} else {
retVal = state.TABLE_GAME_ONE;
}
break;
case TABLE_GAME_SEVEN:
if(input == BLUE_BUTTON) {
retVal = state.FINISH;
} else {
retVal = state.TABLE_GAME_ONE;
}
break;
}
return retVal;
} }
} }

View File

@ -9,7 +9,7 @@ public class SteuerungFrame extends JFrame {
Statemachine machine = new Statemachine(); Statemachine machine = new Statemachine();
public SteuerungFrame() { public SteuerungFrame() {
setType(Type.UTILITY); //setType(Type.UTILITY);
setBackground(Color.black); setBackground(Color.black);
setBounds(200,200, 200, 200); setBounds(200,200, 200, 200);
@ -17,7 +17,8 @@ public class SteuerungFrame extends JFrame {
addKeyListener(new KeyAdapter() { addKeyListener(new KeyAdapter() {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
System.out.println(e.getKeyChar()); //System.out.println(e.getKeyChar());
machine.handleInput(e.getKeyChar());
} }
}); });