| OLD | NEW |
| 1 part of sprites; | 1 part of sprites; |
| 2 | 2 |
| 3 typedef void PointSetter(Point point); | 3 typedef void PointSetter(Point point); |
| 4 | 4 |
| 5 abstract class Action { | 5 abstract class Action { |
| 6 bool _finished = false; | 6 bool _finished = false; |
| 7 | 7 |
| 8 void step(double dt); | 8 void step(double dt); |
| 9 void update(double t) { | 9 void update(double t) { |
| 10 } | 10 } |
| 11 |
| 12 double get duration => 0.0; |
| 11 } | 13 } |
| 12 | 14 |
| 13 abstract class ActionInterval extends Action { | 15 abstract class ActionInterval extends Action { |
| 14 double _duration; | 16 double _duration; |
| 15 | 17 |
| 16 bool _firstTick = true; | 18 bool _firstTick = true; |
| 17 double _elapsed = 0.0; | 19 double _elapsed = 0.0; |
| 18 | 20 |
| 19 double get duration => _duration; | 21 double get duration => _duration; |
| 20 | 22 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 46 | 48 |
| 47 ActionRepeat(this.action, this.numRepeats) { | 49 ActionRepeat(this.action, this.numRepeats) { |
| 48 _duration = action.duration * numRepeats; | 50 _duration = action.duration * numRepeats; |
| 49 } | 51 } |
| 50 | 52 |
| 51 void update(double t) { | 53 void update(double t) { |
| 52 action.update((t * numRepeats.toDouble()) % numRepeats.toDouble()); | 54 action.update((t * numRepeats.toDouble()) % numRepeats.toDouble()); |
| 53 } | 55 } |
| 54 } | 56 } |
| 55 | 57 |
| 56 // TODO: Implement | |
| 57 class ActionSequence extends ActionInterval { | 58 class ActionSequence extends ActionInterval { |
| 58 final List<ActionInterval> actions; | 59 Action _a; |
| 60 Action _b; |
| 61 double _split; |
| 59 | 62 |
| 60 ActionSequence(this.actions) { | 63 ActionSequence(List<Action> actions) { |
| 61 for (Action action in actions) { | 64 assert(actions.length >= 2); |
| 62 _duration += action._duration; | 65 |
| 66 if (actions.length == 2) { |
| 67 // Base case |
| 68 _a = actions[0]; |
| 69 _b = actions[1]; |
| 70 } else { |
| 71 _a = actions[0]; |
| 72 _b = new ActionSequence(actions.sublist(1)); |
| 63 } | 73 } |
| 74 |
| 75 // Calculate split and duration |
| 76 _duration = _a.duration + _b.duration; |
| 77 if (_duration > 0) { |
| 78 _split = _a.duration / _duration; |
| 79 } else { |
| 80 _split = 1.0; |
| 81 } |
| 82 } |
| 83 |
| 84 void update(double t) { |
| 85 if (t < _split) { |
| 86 // Play first action |
| 87 double ta; |
| 88 if (_split > 0.0) { |
| 89 ta = (t / _split).clamp(0.0, 1.0); |
| 90 } else { |
| 91 ta = 1.0; |
| 92 } |
| 93 _a.update(ta); |
| 94 } else if (t >= 1.0) { |
| 95 // Make sure everything is finished |
| 96 if (!_a._finished) _a.update(1.0); |
| 97 if (!_b._finished) _b.update(1.0); |
| 98 } else { |
| 99 // Play second action, but first make sure the first has finished |
| 100 if (!_a._finished) _a.update(1.0); |
| 101 double tb; |
| 102 if (_split < 1.0) { |
| 103 tb = (1.0 - (1.0 - t) / (1.0 - _split)).clamp(0.0, 1.0); |
| 104 } else { |
| 105 tb = 1.0; |
| 106 } |
| 107 _b.update(tb); |
| 108 } |
| 109 } |
| 110 } |
| 111 |
| 112 abstract class ActionInstant extends Action { |
| 113 |
| 114 void step(double dt) { |
| 115 } |
| 116 |
| 117 void update(double t) { |
| 118 fire(); |
| 119 _finished = true; |
| 120 } |
| 121 |
| 122 void fire(); |
| 123 } |
| 124 |
| 125 class ActionCallFunction extends ActionInstant { |
| 126 Function _function; |
| 127 |
| 128 ActionCallFunction(this._function); |
| 129 |
| 130 void fire() { |
| 131 _function(); |
| 132 } |
| 133 } |
| 134 |
| 135 class ActionRemoveFromParent extends ActionInstant { |
| 136 Node _node; |
| 137 |
| 138 ActionRemoveFromParent(this._node); |
| 139 |
| 140 void fire() { |
| 141 _node.removeFromParent(); |
| 64 } | 142 } |
| 65 } | 143 } |
| 66 | 144 |
| 67 class ActionRepeatForever extends Action { | 145 class ActionRepeatForever extends Action { |
| 68 final ActionInterval action; | 146 final ActionInterval action; |
| 69 double _elapsedInAction = 0.0; | 147 double _elapsedInAction = 0.0; |
| 70 | 148 |
| 71 ActionRepeatForever(this.action); | 149 ActionRepeatForever(this.action); |
| 72 | 150 |
| 73 step(double dt) { | 151 step(double dt) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 } | 211 } |
| 134 } | 212 } |
| 135 | 213 |
| 136 class ActionController { | 214 class ActionController { |
| 137 | 215 |
| 138 List<Action> _actions = []; | 216 List<Action> _actions = []; |
| 139 | 217 |
| 140 ActionController(); | 218 ActionController(); |
| 141 | 219 |
| 142 void run(Action action) { | 220 void run(Action action) { |
| 221 action.update(0.0); |
| 143 _actions.add(action); | 222 _actions.add(action); |
| 144 } | 223 } |
| 145 | 224 |
| 146 void step(double dt) { | 225 void step(double dt) { |
| 147 for (int i = _actions.length - 1; i >= 0; i--) { | 226 for (int i = _actions.length - 1; i >= 0; i--) { |
| 148 Action action = _actions[i]; | 227 Action action = _actions[i]; |
| 149 action.step(dt); | 228 action.step(dt); |
| 150 | 229 |
| 151 if (action._finished) { | 230 if (action._finished) { |
| 152 _actions.removeAt(i); | 231 _actions.removeAt(i); |
| 153 } | 232 } |
| 154 } | 233 } |
| 155 } | 234 } |
| 156 } | 235 } |
| OLD | NEW |