Skip to content

Commit ea1de4b

Browse files
committed
serielization of ActionCallbackQueue
1 parent 99e4ca1 commit ea1de4b

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

blade-engine/src/com/bladecoder/engine/actions/ActionCallbackQueue.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21-
import com.bladecoder.engine.actions.ActionCallback;
21+
import com.badlogic.gdx.utils.Json;
22+
import com.badlogic.gdx.utils.Json.Serializable;
23+
import com.badlogic.gdx.utils.JsonValue;
24+
import com.bladecoder.engine.util.ActionCallbackSerialization;
2225

2326
/**
2427
* This is a queue to group all cb that must be triggered in the next iteration.
@@ -29,27 +32,56 @@
2932
* @author rgarcia
3033
*
3134
*/
32-
public class ActionCallbackQueue {
35+
public class ActionCallbackQueue implements Serializable{
3336
private static final List<ActionCallback> queue = new ArrayList<ActionCallback>();
37+
private static final List<ActionCallback> runQueue = new ArrayList<ActionCallback>();
3438

3539
public static void add(ActionCallback cb) {
3640
queue.add(cb);
3741
}
3842

43+
/**
44+
* Resume all cb's in the 'queue'.
45+
*
46+
* To do that, we copy all elements in the 'runQueue' and clean the 'queue' because
47+
* cb.resume() can trigger more cb's
48+
*/
3949
public static void run() {
4050
if(!queue.isEmpty()) {
41-
ActionCallback[] array = queue.toArray(new ActionCallback[queue.size()]);
42-
51+
runQueue.addAll(queue);
4352
queue.clear();
4453

45-
for(ActionCallback cb: array)
54+
for(ActionCallback cb: runQueue)
4655
cb.resume();
56+
57+
runQueue.clear();
4758
}
4859
}
4960

5061
public static void clear() {
5162
queue.clear();
5263
}
5364

54-
// TODO: SAVE AND RESUME QUEUE
65+
@Override
66+
public void write(Json json) {
67+
ArrayList<String> q = new ArrayList<String>();
68+
for(ActionCallback cb: queue) {
69+
q.add(ActionCallbackSerialization.find(cb));
70+
}
71+
72+
json.writeValue("queue", q);
73+
}
74+
75+
@SuppressWarnings("unchecked")
76+
@Override
77+
public void read (Json json, JsonValue jsonData) {
78+
ArrayList<String> q = json.readValue("queue", ArrayList.class, String.class,
79+
jsonData);
80+
81+
queue.clear();
82+
83+
for(String s: q) {
84+
queue.add(ActionCallbackSerialization.find(s));
85+
}
86+
}
5587
}

blade-engine/src/com/bladecoder/engine/model/Scene.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ public void read(Json json, JsonValue jsonData) {
688688
actors = json.readValue("actors", HashMap.class, BaseActor.class,
689689
jsonData);
690690
player = json.readValue("player", String.class, jsonData);
691+
692+
bgActors.clear();
693+
dynamicActors.clear();
694+
fgActors.clear();
691695

692696
for (BaseActor a : actors.values()) {
693697
a.setScene(this);

0 commit comments

Comments
 (0)