summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-03-30 16:23:09 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-03-30 16:23:43 +0200
commitdc56ea3c8f3c0badd225774b576564215abbd90d (patch)
treef0e6fb2af9b8920e531123b9b3aa609f0b540fe0
parent01669f7f8824487d8d436126e0cf752bdb9a88e2 (diff)
downloadRustAndDust-dc56ea3c8f3c0badd225774b576564215abbd90d.zip
RustAndDust-dc56ea3c8f3c0badd225774b576564215abbd90d.tar.gz
Ctrl: add event queue, supports STATE_CHANGE events
-rw-r--r--core/src/ch/asynk/rustanddust/game/Ctrl.java46
1 files changed, 46 insertions, 0 deletions
diff --git a/core/src/ch/asynk/rustanddust/game/Ctrl.java b/core/src/ch/asynk/rustanddust/game/Ctrl.java
index 998a564..4a36b0e 100644
--- a/core/src/ch/asynk/rustanddust/game/Ctrl.java
+++ b/core/src/ch/asynk/rustanddust/game/Ctrl.java
@@ -9,6 +9,8 @@ import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonWriter.OutputType;
import ch.asynk.rustanddust.RustAndDust;
+import ch.asynk.rustanddust.engine.util.IterableQueue;
+import ch.asynk.rustanddust.engine.util.IterableStack;
import ch.asynk.rustanddust.ui.Position;
import ch.asynk.rustanddust.util.Marshal;
import ch.asynk.rustanddust.game.ctrl.Solo;
@@ -26,11 +28,25 @@ import ch.asynk.rustanddust.game.states.StateDeployment;
import ch.asynk.rustanddust.game.states.StateWithdraw;
import ch.asynk.rustanddust.game.states.StateReplay;
+class Event
+{
+ public enum Type
+ {
+ STATE_CHANGE,
+ TURN_END;
+ }
+
+ public Type type;
+ public Object data;
+}
+
public abstract class Ctrl implements Disposable
{
public final RustAndDust game;
public final Battle battle;
+ private final IterableQueue<Event> events = new IterableQueue<Event>(4);
+ private final IterableStack<Event> freeEvents = new IterableStack<Event>(4);
private final StringWriter writer = new StringWriter(2048);
public Map map;
@@ -142,6 +158,36 @@ public abstract class Ctrl implements Disposable
hud.dispose();
map.dispose();
battle.desinit();
+ events.clear();
+ freeEvents.clear();
+ }
+
+ // EVENTS
+
+ public void post(StateType stateType)
+ {
+ Event evt = freeEvents.pop();
+ if (evt == null)
+ evt = new Event();
+ evt.type = Event.Type.STATE_CHANGE;
+ evt.data = stateType;
+ events.enqueue(evt);
+ }
+
+ public void processEvent()
+ {
+ if (events.size() <= 0)
+ return;
+
+ Event evt = events.dequeue();
+ switch(evt.type) {
+ case STATE_CHANGE:
+ setState((StateType) evt.data);
+ break;
+ default:
+ RustAndDust.error(String.format("Unhandled Event Type : %s", evt.type));
+ }
+ freeEvents.push(evt);
}
// JSON