|
| 1 | +package engine; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.UUID; |
| 6 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 7 | + |
| 8 | +import engine.events.Event; |
| 9 | +import engine.time.LocalTimeline; |
| 10 | +import processing.core.PConstants; |
| 11 | +import processing.core.PVector; |
| 12 | + |
| 13 | +public class Replay extends EngineObject { |
| 14 | + |
| 15 | + private CopyOnWriteArrayList<Event> history = new CopyOnWriteArrayList<>(); |
| 16 | + private CopyOnWriteArrayList<Event> startState = new CopyOnWriteArrayList<>(); |
| 17 | + private LocalTimeline replayTimeline = new LocalTimeline(Rectangles.globalTimeline, 1); |
| 18 | + private boolean recording = false; |
| 19 | + private boolean playing = false; |
| 20 | + private Object lock = new Object(); |
| 21 | + |
| 22 | + |
| 23 | + public CopyOnWriteArrayList<Event> getHistory() { |
| 24 | + return history; |
| 25 | + } |
| 26 | + |
| 27 | + public void setHistory(CopyOnWriteArrayList<Event> history) { |
| 28 | + this.history = history; |
| 29 | + } |
| 30 | + |
| 31 | + public LocalTimeline getReplayTimeline() { |
| 32 | + return replayTimeline; |
| 33 | + } |
| 34 | + |
| 35 | + public void setReplayTimeline(LocalTimeline replayTimeline) { |
| 36 | + this.replayTimeline = replayTimeline; |
| 37 | + } |
| 38 | + |
| 39 | + public boolean isRecording() { |
| 40 | + return recording; |
| 41 | + } |
| 42 | + |
| 43 | + public void setRecording(boolean recording) { |
| 44 | + this.recording = recording; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void handleEvent(Event e) { |
| 49 | + switch(e.getType()) { |
| 50 | + case(Event.EVENT_INPUT): |
| 51 | + switch((int) e.getData().get("keyCode")) { |
| 52 | + case('R'): |
| 53 | + synchronized (this.lock) { |
| 54 | + if (!this.recording && !this.playing) { |
| 55 | + this.recording = true; |
| 56 | + this.replayTimeline.start(); |
| 57 | + this.startState.clear(); |
| 58 | + this.history.clear(); |
| 59 | + System.out.println("Started Recording"); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + break; |
| 64 | + case('S'): |
| 65 | + synchronized (this.lock) { |
| 66 | + if (!this.playing) { |
| 67 | + this.recording = false; |
| 68 | + this.playing = true; |
| 69 | + Rectangles.physicsTimeline.pause(); |
| 70 | + // Make start events time after history events, then queue them |
| 71 | + for (GameObj obj : Rectangles.movObjects) { |
| 72 | + HashMap<String, Object> data = new HashMap<>(); |
| 73 | + // Setting time to 0 here, change when stop recording |
| 74 | + Event s = new Event(Event.EVENT_MOVEMENT, this.replayTimeline.getCurrentTime(), data); |
| 75 | + data.put("x", obj.getPy().getLocation().x); |
| 76 | + data.put("y", obj.getPy().getLocation().y); |
| 77 | + data.put("caller", obj.getUUID()); |
| 78 | + this.history.add(s); |
| 79 | + } |
| 80 | + HashMap<String, Object> data = new HashMap<>(); |
| 81 | + // Setting time to 0 here, change when stop recording |
| 82 | + Event end = new Event(Event.EVENT_END_REPLAY, this.replayTimeline.getCurrentTime(), data); |
| 83 | + data.put("caller", Rectangles.player.getUUID()); |
| 84 | + this.history.add(end); |
| 85 | + Rectangles.eventTimeline = new LocalTimeline(Rectangles.globalTimeline, 8); |
| 86 | + Rectangles.eventManager.getEventQueue().addAll(this.history); |
| 87 | + System.out.println("Started Replay"); |
| 88 | + } |
| 89 | + } |
| 90 | + break; |
| 91 | + case('1'): |
| 92 | + Rectangles.eventTimeline = new LocalTimeline(Rectangles.globalTimeline, 8); |
| 93 | + break; |
| 94 | + case('2'): |
| 95 | + Rectangles.eventTimeline = new LocalTimeline(Rectangles.globalTimeline, 2); |
| 96 | + |
| 97 | + break; |
| 98 | + case('3'): |
| 99 | + Rectangles.eventTimeline = new LocalTimeline(Rectangles.globalTimeline, 1); |
| 100 | + break; |
| 101 | + default: |
| 102 | + break; |
| 103 | + } |
| 104 | + case (Event.EVENT_MOVEMENT): |
| 105 | + synchronized (this.lock) { |
| 106 | + if (this.recording && !this.playing) { |
| 107 | + Event copy = new Event(e); |
| 108 | + copy.setTime(this.replayTimeline.getCurrentTime()); |
| 109 | + copy.getData().put("isReplay", true); |
| 110 | + this.history.add(copy); |
| 111 | + } |
| 112 | + } |
| 113 | + break; |
| 114 | + case(Event.EVENT_END_REPLAY): |
| 115 | + synchronized (this.lock) { |
| 116 | + this.playing = false; |
| 117 | + Rectangles.eventTimeline = new LocalTimeline(Rectangles.globalTimeline, 2); |
| 118 | + Rectangles.physicsTimeline.unpause(); |
| 119 | + } |
| 120 | + break; |
| 121 | + default: |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public boolean isPlaying() { |
| 127 | + return this.playing; |
| 128 | + } |
| 129 | +} |
0 commit comments