| OLD | NEW |
| 1 part of sprites; | 1 part of sprites; |
| 2 | 2 |
| 3 typedef void ActionCallback(); | 3 typedef void ActionCallback(); |
| 4 | 4 |
| 5 abstract class Action { | 5 abstract class Action { |
| 6 Object _tag; | 6 Object _tag; |
| 7 bool _finished = false; | 7 bool _finished = false; |
| 8 | 8 |
| 9 void step(double dt); | 9 void step(double dt); |
| 10 void update(double t) { | 10 void update(double t) { |
| 11 } | 11 } |
| 12 | 12 |
| 13 double get duration => 0.0; | 13 double get duration => 0.0; |
| 14 } | 14 } |
| 15 | 15 |
| 16 abstract class ActionInterval extends Action { | 16 abstract class ActionInterval extends Action { |
| 17 double _duration; | 17 double _duration; |
| 18 | 18 |
| 19 bool _firstTick = true; | 19 bool _firstTick = true; |
| 20 double _elapsed = 0.0; | 20 double _elapsed = 0.0; |
| 21 | 21 |
| 22 @override |
| 22 double get duration => _duration; | 23 double get duration => _duration; |
| 23 | 24 |
| 24 ActionInterval([this._duration = 0.0]); | 25 ActionInterval([this._duration = 0.0]); |
| 25 | 26 |
| 27 @override |
| 26 void step(double dt) { | 28 void step(double dt) { |
| 27 if (_firstTick) { | 29 if (_firstTick) { |
| 28 _firstTick = false; | 30 _firstTick = false; |
| 29 } else { | 31 } else { |
| 30 _elapsed += dt; | 32 _elapsed += dt; |
| 31 } | 33 } |
| 32 | 34 |
| 33 double t; | 35 double t; |
| 34 if (this._duration == 0.0) { | 36 if (this._duration == 0.0) { |
| 35 t = 1.0; | 37 t = 1.0; |
| 36 } else { | 38 } else { |
| 37 t = (_elapsed / _duration).clamp(0.0, 1.0); | 39 t = (_elapsed / _duration).clamp(0.0, 1.0); |
| 38 } | 40 } |
| 39 | 41 |
| 40 update(t); | 42 update(t); |
| 41 | 43 |
| 42 if (t >= 1.0) _finished = true; | 44 if (t >= 1.0) _finished = true; |
| 43 } | 45 } |
| 44 } | 46 } |
| 45 | 47 |
| 46 class ActionRepeat extends ActionInterval { | 48 class ActionRepeat extends ActionInterval { |
| 47 final int numRepeats; | 49 final int numRepeats; |
| 48 final ActionInterval action; | 50 final ActionInterval action; |
| 49 | 51 |
| 50 ActionRepeat(this.action, this.numRepeats) { | 52 ActionRepeat(this.action, this.numRepeats) { |
| 51 _duration = action.duration * numRepeats; | 53 _duration = action.duration * numRepeats; |
| 52 } | 54 } |
| 53 | 55 |
| 56 @override |
| 54 void update(double t) { | 57 void update(double t) { |
| 55 action.update((t * numRepeats.toDouble()) % numRepeats.toDouble()); | 58 action.update((t * numRepeats.toDouble()) % numRepeats.toDouble()); |
| 56 } | 59 } |
| 57 } | 60 } |
| 58 | 61 |
| 59 class ActionSequence extends ActionInterval { | 62 class ActionSequence extends ActionInterval { |
| 60 Action _a; | 63 Action _a; |
| 61 Action _b; | 64 Action _b; |
| 62 double _split; | 65 double _split; |
| 63 | 66 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 75 | 78 |
| 76 // Calculate split and duration | 79 // Calculate split and duration |
| 77 _duration = _a.duration + _b.duration; | 80 _duration = _a.duration + _b.duration; |
| 78 if (_duration > 0) { | 81 if (_duration > 0) { |
| 79 _split = _a.duration / _duration; | 82 _split = _a.duration / _duration; |
| 80 } else { | 83 } else { |
| 81 _split = 1.0; | 84 _split = 1.0; |
| 82 } | 85 } |
| 83 } | 86 } |
| 84 | 87 |
| 88 @override |
| 85 void update(double t) { | 89 void update(double t) { |
| 86 if (t < _split) { | 90 if (t < _split) { |
| 87 // Play first action | 91 // Play first action |
| 88 double ta; | 92 double ta; |
| 89 if (_split > 0.0) { | 93 if (_split > 0.0) { |
| 90 ta = (t / _split).clamp(0.0, 1.0); | 94 ta = (t / _split).clamp(0.0, 1.0); |
| 91 } else { | 95 } else { |
| 92 ta = 1.0; | 96 ta = 1.0; |
| 93 } | 97 } |
| 94 _a.update(ta); | 98 _a.update(ta); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 105 } else { | 109 } else { |
| 106 tb = 1.0; | 110 tb = 1.0; |
| 107 } | 111 } |
| 108 _b.update(tb); | 112 _b.update(tb); |
| 109 } | 113 } |
| 110 } | 114 } |
| 111 } | 115 } |
| 112 | 116 |
| 113 abstract class ActionInstant extends Action { | 117 abstract class ActionInstant extends Action { |
| 114 | 118 |
| 119 @override |
| 115 void step(double dt) { | 120 void step(double dt) { |
| 116 } | 121 } |
| 117 | 122 |
| 123 @override |
| 118 void update(double t) { | 124 void update(double t) { |
| 119 fire(); | 125 fire(); |
| 120 _finished = true; | 126 _finished = true; |
| 121 } | 127 } |
| 122 | 128 |
| 123 void fire(); | 129 void fire(); |
| 124 } | 130 } |
| 125 | 131 |
| 126 class ActionCallFunction extends ActionInstant { | 132 class ActionCallFunction extends ActionInstant { |
| 127 ActionCallback _function; | 133 ActionCallback _function; |
| 128 | 134 |
| 129 ActionCallFunction(this._function); | 135 ActionCallFunction(this._function); |
| 130 | 136 |
| 137 @override |
| 131 void fire() { | 138 void fire() { |
| 132 _function(); | 139 _function(); |
| 133 } | 140 } |
| 134 } | 141 } |
| 135 | 142 |
| 136 class ActionRemoveNode extends ActionInstant { | 143 class ActionRemoveNode extends ActionInstant { |
| 137 Node _node; | 144 Node _node; |
| 138 | 145 |
| 139 ActionRemoveNode(this._node); | 146 ActionRemoveNode(this._node); |
| 140 | 147 |
| 148 @override |
| 141 void fire() { | 149 void fire() { |
| 142 _node.removeFromParent(); | 150 _node.removeFromParent(); |
| 143 } | 151 } |
| 144 } | 152 } |
| 145 | 153 |
| 146 class ActionRepeatForever extends Action { | 154 class ActionRepeatForever extends Action { |
| 147 final ActionInterval action; | 155 final ActionInterval action; |
| 148 double _elapsedInAction = 0.0; | 156 double _elapsedInAction = 0.0; |
| 149 | 157 |
| 150 ActionRepeatForever(this.action); | 158 ActionRepeatForever(this.action); |
| 151 | 159 |
| 160 @override |
| 152 step(double dt) { | 161 step(double dt) { |
| 153 _elapsedInAction += dt; | 162 _elapsedInAction += dt; |
| 154 while (_elapsedInAction > action.duration) { | 163 while (_elapsedInAction > action.duration) { |
| 155 _elapsedInAction -= action.duration; | 164 _elapsedInAction -= action.duration; |
| 156 } | 165 } |
| 157 _elapsedInAction = Math.max(_elapsedInAction, 0.0); | 166 _elapsedInAction = Math.max(_elapsedInAction, 0.0); |
| 158 | 167 |
| 159 double t; | 168 double t; |
| 160 if (action._duration == 0.0) { | 169 if (action._duration == 0.0) { |
| 161 t = 1.0; | 170 t = 1.0; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 191 int aDelta = endVal.alpha - startVal.alpha; | 200 int aDelta = endVal.alpha - startVal.alpha; |
| 192 int rDelta = endVal.red - startVal.red; | 201 int rDelta = endVal.red - startVal.red; |
| 193 int gDelta = endVal.green - startVal.green; | 202 int gDelta = endVal.green - startVal.green; |
| 194 int bDelta = endVal.blue - startVal.blue; | 203 int bDelta = endVal.blue - startVal.blue; |
| 195 _delta = new _ColorDiff(aDelta, rDelta, gDelta, bDelta); | 204 _delta = new _ColorDiff(aDelta, rDelta, gDelta, bDelta); |
| 196 } else { | 205 } else { |
| 197 assert(false); | 206 assert(false); |
| 198 } | 207 } |
| 199 } | 208 } |
| 200 | 209 |
| 210 @override |
| 201 void update(double t) { | 211 void update(double t) { |
| 202 var newVal; | 212 var newVal; |
| 203 | 213 |
| 204 if (startVal is Point) { | 214 if (startVal is Point) { |
| 205 // Point | 215 // Point |
| 206 double xStart = startVal.x; | 216 double xStart = startVal.x; |
| 207 double yStart = startVal.y; | 217 double yStart = startVal.y; |
| 208 double xDelta = _delta.x; | 218 double xDelta = _delta.x; |
| 209 double yDelta = _delta.y; | 219 double yDelta = _delta.y; |
| 210 newVal = new Point(xStart + xDelta * t, yStart + yDelta * t); | 220 newVal = new Point(xStart + xDelta * t, yStart + yDelta * t); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 } | 280 } |
| 271 | 281 |
| 272 class _ColorDiff { | 282 class _ColorDiff { |
| 273 final int alpha; | 283 final int alpha; |
| 274 final int red; | 284 final int red; |
| 275 final int green; | 285 final int green; |
| 276 final int blue; | 286 final int blue; |
| 277 | 287 |
| 278 _ColorDiff(this.alpha, this.red, this.green, this.blue); | 288 _ColorDiff(this.alpha, this.red, this.green, this.blue); |
| 279 } | 289 } |
| OLD | NEW |