summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/Ctrl.java
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-04-13 23:44:53 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-04-13 23:48:54 +0200
commit95d0131cb9733d273b81bf63a8778dc2b8e0a414 (patch)
treebe0bca04dc39f66ce1ffbeb5f22e191123e5facf /core/src/ch/asynk/rustanddust/game/Ctrl.java
parent513938e9b1731308535ef45336239f9209e2e396 (diff)
downloadRustAndDust-keep.zip
RustAndDust-keep.tar.gz
WIPkeep
Diffstat (limited to 'core/src/ch/asynk/rustanddust/game/Ctrl.java')
-rw-r--r--core/src/ch/asynk/rustanddust/game/Ctrl.java480
1 files changed, 246 insertions, 234 deletions
diff --git a/core/src/ch/asynk/rustanddust/game/Ctrl.java b/core/src/ch/asynk/rustanddust/game/Ctrl.java
index 6ec7184..a5807d0 100644
--- a/core/src/ch/asynk/rustanddust/game/Ctrl.java
+++ b/core/src/ch/asynk/rustanddust/game/Ctrl.java
@@ -18,32 +18,49 @@ import ch.asynk.rustanddust.game.State.StateType;
import ch.asynk.rustanddust.game.states.StateCommon;
import ch.asynk.rustanddust.game.states.StateSelect;
import ch.asynk.rustanddust.game.states.StateMove;
-import ch.asynk.rustanddust.game.states.StateRotate;
import ch.asynk.rustanddust.game.states.StatePromote;
import ch.asynk.rustanddust.game.states.StateEngage;
-import ch.asynk.rustanddust.game.states.StateBreak;
import ch.asynk.rustanddust.game.states.StateAnimation;
-import ch.asynk.rustanddust.game.states.StateReinforcement;
import ch.asynk.rustanddust.game.states.StateDeployment;
-import ch.asynk.rustanddust.game.states.StateWithdraw;
+// import ch.asynk.rustanddust.game.states.StateReinforcement;
import ch.asynk.rustanddust.game.states.StateReplay;
public abstract class Ctrl implements Disposable
{
- public enum EventType
+
+ private static final boolean debugCtrl = true;
+
+ public enum MsgType
{
- STATE_CHANGE,
- HUD_ANSWER,
+ OK,
+ CANCEL,
+ PROMOTE,
ANIMATIONS_DONE,
+ UNIT_DOCK_SELECT,
UNIT_DOCK_TOGGLE,
- UNIT_DOCK_SELECT;
+ UNIT_DEPLOYED,
+ }
+
+ public enum EventType
+ {
+ ORDER,
+ ORDER_DONE,
+ STATE_CHANGE,
+ ANIMATED_STATE_CHANGE,
+ REPLAY_STEP,
+ REPLAY_DONE,
+ TURN_DONE,
+ // ACTION_DONE,
+ ACTION_ABORTED,
+ EXIT_BATTLE,
}
class Event
{
public EventType type;
public Object data;
- public boolean status;
+ @Override
+ public String toString() { return String.format("Event : %s - %s", type, (data == null) ? "" : data); }
}
public final RustAndDust game;
@@ -61,20 +78,17 @@ public abstract class Ctrl implements Disposable
protected boolean synched;
private int depth;
+ private Order lastOrder;
+
private final State selectState;
- private final State pathState;
- private final State rotateState;
+ private final State moveState;
private final State promoteState;
private final State engageState;
- private final State breakState;
private final State animationState;
- private final State reinforcementState;
private final State deploymentState;
- private final State withdrawState;
+ // private final State reinforcementState;
private final State replayState;
- private int animationCount = 0;
-
private State state;
private StateType stateType;
private StateType stateAfterAnimation;
@@ -108,16 +122,15 @@ public abstract class Ctrl implements Disposable
this.synched = false;
this.depth = 0;
+ this.lastOrder = null;
+
this.selectState = new StateSelect();
- this.pathState = new StateMove();
- this.rotateState = new StateRotate();
+ this.moveState = new StateMove();
this.promoteState = new StatePromote();
this.engageState = new StateEngage();
- this.breakState = new StateBreak();
this.animationState = new StateAnimation();
- this.reinforcementState = new StateReinforcement();
this.deploymentState = new StateDeployment();
- this.withdrawState = new StateWithdraw();
+ // this.reinforcementState = new StateReinforcement();
this.replayState = new StateReplay();
this.stateType = StateType.LOADING;
@@ -129,12 +142,11 @@ public abstract class Ctrl implements Disposable
hud.update();
this.state = selectState;
- this.stateType = StateType.DONE;
- this.stateAfterAnimation = StateType.DONE;
-
- setState(battle.getState());
+ this.stateType = StateType.WAIT_EVENT;
+ this.stateAfterAnimation = StateType.WAIT_EVENT;
if (synched) {
+ setState(battle.getState());
this.hud.notify(battle.toString(), 2, Position.MIDDLE_CENTER, false);
return;
}
@@ -152,7 +164,6 @@ public abstract class Ctrl implements Disposable
setState(StateType.REPLAY);
break;
}
-
}
@Override
@@ -174,6 +185,7 @@ public abstract class Ctrl implements Disposable
protected void load(Marshal.Mode mode, String payload)
{
+ if (payload == null) return;
JsonValue root = new JsonReader().parse(payload);
battle.load(mode, root);
}
@@ -199,9 +211,7 @@ public abstract class Ctrl implements Disposable
public void touchDown(float hudX, float hudY, float mapX, float mapY)
{
- boolean inAnimation = (this.stateType == StateType.ANIMATION);
-
- if (!blockHud && hud.hit(hudX, hudY, inAnimation))
+ if (!blockHud && hud.hit(hudX, hudY, inAnimation()))
return;
touchedHex = (blockMap ? null : map.getHexAt(mapX, mapY));
@@ -213,34 +223,73 @@ public abstract class Ctrl implements Disposable
state.touch(touchedHex);
}
- // EVENTS
+ // MESSAGES
- private Event getEvent()
+ public void sendMsg(MsgType msgType)
{
- Event evt = freeEvents.pop();
- if (evt == null)
- evt = new Event();
- return evt;
+ sendMsg(msgType, null);
}
- public void postDone() { post(StateType.DONE); }
- public void postAbort() { post(StateType.ABORT); }
+ public void sendMsg(MsgType msgType, Object data)
+ {
+ switch(msgType) {
+ case ANIMATIONS_DONE:
+ animationsDone();
+ break;
+ case UNIT_DOCK_TOGGLE:
+ unitDockToggle();
+ break;
+ case UNIT_DEPLOYED:
+ deploymentState.processMsg(msgType, data);
+ break;
+ default:
+ if (!this.state.processMsg(msgType, data))
+ RustAndDust.error(String.format("%s does not handle msg : %s %s", this.state, msgType, data));
+ break;
+ }
+ }
+
+ // EVENTS
+
+ public void postReplayDone() { postEvent(EventType.REPLAY_DONE); }
+ public void postTurnDone() { postEvent(EventType.TURN_DONE); }
+ // public void postActionDone() { postEvent(EventType.ACTION_DONE); }
+ public void postActionAborted() { postEvent(EventType.ACTION_ABORTED); }
public void post(StateType stateType)
{
- Event evt = getEvent();
- evt.type = EventType.STATE_CHANGE;
- evt.data = stateType;
- events.enqueue(evt);
+ postEvent(EventType.STATE_CHANGE, stateType);
+ }
+
+ // public void postTransitionTo(StateType stateType)
+ // {
+ // postEvent(EventType.ANIMATED_STATE_CHANGE, stateType);
+ // }
+
+ // public void postTransitionToDone()
+ // {
+ // postEvent(EventType.ANIMATED_STATE_CHANGE, StateType.WAIT_EVENT);
+ // postEvent(EventType.ACTION_DONE);
+ // }
+
+ // public void postTransitionToAborted()
+ // {
+ // postEvent(EventType.ANIMATED_STATE_CHANGE, StateType.WAIT_EVENT);
+ // postEvent(EventType.ACTION_ABORTED);
+ // }
+
+ public void postOrder(Order order)
+ {
+ postOrder(order, null);
}
- public void postAnswer(Hud.OkCancelAction what, boolean status)
+ public void postOrder(Order order, StateType stateType)
{
- Event evt = getEvent();
- evt.type = EventType.HUD_ANSWER;
- evt.data = what;
- evt.status = status;
- events.enqueue(evt);
+ postEvent(EventType.ORDER, order);
+ // FIXME maybe use postActionDone()
+ if (order.type != Order.OrderType.END)
+ postEvent(EventType.ANIMATED_STATE_CHANGE, StateType.WAIT_EVENT);
+ postEvent(EventType.ORDER_DONE, stateType);
}
public void postEvent(EventType type)
@@ -250,7 +299,9 @@ public abstract class Ctrl implements Disposable
public void postEvent(EventType type, Object data)
{
- Event evt = getEvent();
+ Event evt = freeEvents.pop();
+ if (evt == null)
+ evt = new Event();
evt.type = type;
evt.data = data;
events.enqueue(evt);
@@ -258,25 +309,43 @@ public abstract class Ctrl implements Disposable
public void processEvent()
{
- if (events.size() <= 0)
+ if ((events.size() <= 0) || inAnimation())
return;
Event evt = events.dequeue();
+ RustAndDust.debug(evt.toString());
switch(evt.type) {
+ case ORDER:
+ lastOrder = (Order) evt.data;
+ map.execute(lastOrder);
+ break;
+ case ORDER_DONE:
+ orderDone((StateType) evt.data);
+ break;
case STATE_CHANGE:
setState((StateType) evt.data);
break;
- case HUD_ANSWER:
- handleHudAnswer(evt);
+ case ANIMATED_STATE_CHANGE:
+ stateAfterAnimation = (StateType) evt.data;
+ setState(StateType.ANIMATION);
break;
- case ANIMATIONS_DONE:
- animationsDone();
+ case REPLAY_STEP:
+ replayStep((boolean) evt.data);
break;
- case UNIT_DOCK_TOGGLE:
- unitDockToggle();
+ case REPLAY_DONE:
+ replayDone();
+ break;
+ case TURN_DONE:
+ turnDone();
+ break;
+ // case ACTION_DONE:
+ // actionDone();
+ // break;
+ case ACTION_ABORTED:
+ abortAction();
break;
- case UNIT_DOCK_SELECT:
- unitDockSelect((Unit) evt.data);
+ case EXIT_BATTLE:
+ exitBattle();
break;
default:
RustAndDust.error(String.format("Unhandled Event Type : %s %s", evt.type, evt.data));
@@ -284,243 +353,186 @@ public abstract class Ctrl implements Disposable
freeEvents.push(evt);
}
- // State callbacks
-
- public void setAfterAnimationState(StateType after)
- {
- stateAfterAnimation = after;
- }
-
- // Event handlers
-
- private void handleHudAnswer(Event evt)
+ private boolean inAnimation()
{
- switch((Hud.OkCancelAction) evt.data) {
- case EXIT_BOARD:
- if (evt.status) setState(StateType.DONE);
- else setState(StateType.ABORT);
- break;
- case ABORT_TURN:
- if (evt.status) {
- this.state.abort();
- turnDone();
- }
- break;
- case END_DEPLOYMENT:
- if (evt.status) {
- this.state.execute();
- turnDone();
- }
- break;
- case QUIT_BATTLE:
- if (evt.status)
- game.switchToMenu();
- break;
-
- }
+ return (this.stateType == StateType.ANIMATION);
}
private void animationsDone()
{
+ if (debugCtrl) RustAndDust.debug("ANIMATIONS DONE");
if (hud.dialogActive())
hud.notifyAnimationsDone();
if (stateType == StateType.ANIMATION) {
+ sendMsg(MsgType.OK, null);
StateType tmp = stateAfterAnimation;
- stateAfterAnimation = StateType.DONE;
+ stateAfterAnimation = StateType.WAIT_EVENT;
setState(tmp);
}
}
- private void unitDockSelect(Unit unit)
+ private void replayStep(boolean burnAp)
{
- if ((stateType == StateType.DEPLOYMENT) || (stateType == StateType.REINFORCEMENT))
- state.touch(null);
+ if (debugCtrl) RustAndDust.debug("REPLAY STEP --> burn down 1 AP " + burnAp);
+ if (burnAp)
+ battle.getPlayer().burnDownOneAp();
+ map.actionDone();
+ hud.update();
}
- private void unitDockToggle()
+ private void replayDone()
{
- if (this.stateType == StateType.SELECT)
- setState(StateType.REINFORCEMENT);
- else if (this.stateType == StateType.REINFORCEMENT)
- setState(StateType.SELECT);
+ if (debugCtrl) RustAndDust.debug("REPLAY DONE --> check turn change");
+ if (battle.getPlayer().apExhausted())
+ postTurnDone();
+ else if (!battle.getPlayer().canDoSomething())
+ postTurnDone();
+ else
+ post(battle.getState());
}
- //
+ private void orderDone(StateType nextState)
+ {
+ if (debugCtrl) RustAndDust.debug("ORDER DONE");
+
+ if (lastOrder.cost == 0) {
+ post((nextState == null) ? battle.getState() : nextState);
+ return;
+ }
+
+ battle.getPlayer().burnDownOneAp();
+ hud.notify("1 Action Point burnt");
+ hud.update();
+
+ if (battle.getPlayer().apExhausted()) {
+ hud.notify("No more Action Points");
+ postTurnDone();
+ } else if (!battle.getPlayer().canDoSomething()) {
+ hud.notify("No available Actions");
+ postTurnDone();
+ } else {
+ post((nextState == null) ? battle.getState() : nextState);
+ }
+ }
+
+ // private void actionDone()
+ // {
+ // if (debugCtrl) RustAndDust.debug("ACTION DONE");
+ // if (battle.actionDone()) {
+ // hud.notify("1 Action Point burnt");
+ // hud.update();
+ // }
+
+ // if (battle.getPlayer().apExhausted()) {
+ // hud.notify("No more Action Points");
+ // postTurnDone();
+ // } else if (!battle.getPlayer().canDoSomething()) {
+ // hud.notify("No available Actions");
+ // postTurnDone();
+ // } else {
+ // post(battle.getState());
+ // }
+ // }
private void turnDone()
{
- if (battle.turnDone())
+ if (debugCtrl) RustAndDust.debug("TURN DONE");
+ if (battle.turnDone()) {
hud.victory(battle.getPlayer(), battle.getOpponent());
+ // FIXME BATTLE OVER
+ }
else {
if (battle.getPlayer().hasReinforcement())
hud.notify("You have reinforcement", 2, Position.MIDDLE_CENTER, true);
hud.update();
if (!battle.getPlayer().canDoSomething()) {
hud.notify("No available Actions");
- setState(StateType.TURN_OVER);
- } else
- setState(battle.getState());
+ // FIXME DOUBLE TURN DONE
+ postTurnDone();
+ } else {
+ post(battle.getState());
+ }
}
}
- //
-
- private void setState(StateType nextState)
+ private void abortAction()
{
- depth += 1;
- if (depth > 1)
- RustAndDust.error(String.format("***!!!*** STATE DEPTH : %d", depth));
-
- if (nextState == StateType.ABORT)
- nextState = abortAction();
- else if (nextState == StateType.DONE) {
- nextState = complete();
- }
-
- if (stateType == StateType.ANIMATION) {
- this.blockMap = hud.dialogActive();
- if (nextState == StateType.REPLAY)
- completeReplayStep();
- }
-
- hud.playerInfo.blockEndOfTurn(nextState != StateType.SELECT);
-
- if (nextState == stateType)
- RustAndDust.debug(String.format("***!!!*** STATE LOOP : %s", stateType));
-
- this.state.leaveFor(nextState);
-
- this.state = getNextState(nextState);
-
- StateType tmp = stateType;
- stateType = nextState;
-
- this.state.enterFrom(tmp);
-
- if (nextState == StateType.TURN_OVER)
- turnDone();
- depth -= 1;
+ if (debugCtrl) RustAndDust.debug("ABORT ACTION");
+ // sendMsg(MsgType.CANCEL); this will loop
+ post(battle.getState());
}
- private StateType complete()
+ private void exitBattle()
{
- switch(stateType) {
- case DEPLOYMENT:
- return completeDeployment();
- case REPLAY:
- return completeReplay();
- default:
- return completeAction();
- }
+ if (debugCtrl) RustAndDust.debug("EXIT BATTLE");
+ System.err.println("FIXME exitBattle NOT IMPLEMENTED YET");
}
- private StateType completeDeployment()
+ private void unitDockToggle()
{
- if (battle.isDeploymentDone())
- hud.askEndDeployment();
- battle.actionDone();
- return StateType.DEPLOYMENT;
+ // if (this.stateType == StateType.SELECT)
+ // post(StateType.REINFORCEMENT);
+ // else if (this.stateType == StateType.REINFORCEMENT) {
+ // sendMsg(MsgType.OK);
+ // post(StateType.SELECT);
+ // }
}
- private StateType abortAction()
+ //
+
+ private void setState(StateType nextState)
{
- hud.notify("Action canceled");
- StateType nextState = this.state.abort();
+ if (stateType == nextState) {
+ RustAndDust.error(String.format("***!!!*** STATE LOOP : %d", depth));
+ return;
+ }
- if (nextState == StateType.ABORT)
- nextState = battle.getState();
+ if (nextState == StateType.WAIT_EVENT) {
+ stateType = nextState;
+ if (debugCtrl) RustAndDust.debug("WAIT_EVENT");
+ return;
+ }
- return nextState;
- }
+ depth += 1;
+ if (depth > 1)
+ RustAndDust.error(String.format("***!!!*** STATE DEPTH : %d", depth));
- private StateType completeAction()
- {
- StateType nextState = this.state.execute();
+ // FIXME handle corner cases (is ok here ?)
- if (nextState == StateType.DONE) {
- if (battle.actionDone()) {
- hud.notify("1 Action Point burnt");
- hud.update();
- }
- if (battle.getPlayer().apExhausted()) {
- hud.notify("No more Action Points");
- nextState = StateType.TURN_OVER;
- } else if (!battle.getPlayer().canDoSomething()) {
- hud.notify("No available Actions");
- nextState = StateType.TURN_OVER;
- } else
- nextState = battle.getState();
+ if (nextState == StateType.DEPLOYMENT) {
+ if (battle.isDeploymentDone())
+ hud.askEndDeployment();
}
- return nextState;
- }
+ //
- private StateType completeReplay()
- {
- if (battle.getPlayer().apExhausted()) {
- return StateType.TURN_OVER;
- } else if (!battle.getPlayer().canDoSomething()) {
- return StateType.TURN_OVER;
- } else
- return battle.getState();
- }
+ hud.playerInfo.blockEndOfTurn(nextState != StateType.SELECT);
- private void completeReplayStep()
- {
- StateType nextState = replayState.execute();
+ this.state = getNextState(nextState);
+ StateType tmp = stateType;
+ stateType = nextState;
+ this.state.enterFrom(tmp);
- if (nextState == StateType.DONE) {
- battle.getPlayer().burnDownOneAp();
- hud.update();
- }
+ depth -= 1;
}
private State getNextState(StateType nextState)
{
- RustAndDust.debug("Ctrl", String.format(" %s -> %s : %s", stateType, nextState, battle.getPlayer()));
-
- State state = this.state;
+ RustAndDust.debug("Ctrl", String.format("%s -> %s : %s", stateType, nextState, battle.getPlayer()));
switch(nextState) {
- case SELECT:
- state = selectState;
- break;
- case MOVE:
- state = pathState;
- break;
- case ROTATE:
- state = rotateState;
- break;
- case PROMOTE:
- state = promoteState;
- break;
- case ENGAGE:
- state = engageState;
- break;
- case BREAK:
- state = breakState;
- break;
- case WITHDRAW:
- state = withdrawState;
- break;
- case ANIMATION:
- state = animationState;
- this.blockMap = true;
- break;
- case REINFORCEMENT:
- state = reinforcementState;
- break;
- case DEPLOYMENT:
- state = deploymentState;
- break;
- case REPLAY:
- state = replayState;
- break;
+ case SELECT: return selectState;
+ case MOVE: return moveState;
+ case PROMOTE: return promoteState;
+ case ENGAGE: return engageState;
+ case ANIMATION: return animationState;
+ case DEPLOYMENT: return deploymentState;
+ // case REINFORCEMENT: return reinforcementState;
+ case REPLAY: return replayState;
default:
RustAndDust.error(String.format("Unhandled State : %s", nextState));
- break;
}
- return state;
+ return this.state;
}
}