Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 part of sprites; | |
| 2 | |
| 3 typedef void PointSetter(Point point); | |
| 4 | |
| 5 abstract class Action { | |
| 6 bool _finished = false; | |
| 7 | |
| 8 void step(double dt); | |
| 9 void update(double t); | |
| 10 } | |
| 11 | |
| 12 abstract class ActionInterval extends Action { | |
| 13 double _duration; | |
| 14 | |
| 15 bool _firstTick = true; | |
| 16 double _elapsed = 0.0; | |
| 17 | |
| 18 double get duration => _duration; | |
| 19 | |
| 20 ActionInterval([this._duration = 0.0]); | |
| 21 | |
| 22 void step(double dt) { | |
| 23 if (_firstTick) { | |
| 24 _firstTick = false; | |
| 25 } else { | |
| 26 _elapsed += dt; | |
| 27 } | |
| 28 | |
| 29 double t; | |
| 30 if (this._duration == 0.0) { | |
| 31 t = 1.0; | |
| 32 } else { | |
| 33 t = _clamp(_elapsed / _duration, 0.0, 1.0); | |
| 34 } | |
| 35 | |
| 36 update(t); | |
| 37 | |
| 38 if (t >= 1.0) _finished = true; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 class ActionRepeat extends ActionInterval { | |
| 43 final int numRepeats; | |
| 44 final ActionInterval action; | |
| 45 | |
| 46 ActionRepeat(this.action, this.numRepeats) { | |
| 47 _duration = action.duration * numRepeats; | |
| 48 } | |
| 49 | |
| 50 void update(double t) { | |
| 51 action.update((t * numRepeats.toDouble()) % numRepeats.toDouble()); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 // TODO: Implement | |
| 56 class ActionSequence extends ActionInterval { | |
| 57 final List<ActionInterval> actions; | |
| 58 | |
| 59 ActionSequence(this.actions) { | |
| 60 for (Action action in actions) { | |
| 61 _duration += action._duration; | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 class ActionRepeatForever extends Action { | |
| 67 final ActionInterval action; | |
| 68 double _elapsedInAction = 0.0; | |
| 69 | |
| 70 ActionRepeatForever(this.action); | |
| 71 | |
| 72 step(double dt) { | |
| 73 _elapsedInAction += dt; | |
| 74 while (_elapsedInAction > action.duration) { | |
| 75 _elapsedInAction -= action.duration; | |
| 76 } | |
| 77 _elapsedInAction = Math.max(_elapsedInAction, 0.0); | |
| 78 | |
| 79 double t; | |
| 80 if (action._duration == 0.0) { | |
| 81 t = 1.0; | |
| 82 } else { | |
| 83 t = _clamp(_elapsedInAction / action._duration, 0.0, 1.0); | |
| 84 } | |
| 85 | |
| 86 action.update(t); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 class ActionTween extends ActionInterval { | |
| 91 final Function setter; | |
| 92 final startVal; | |
| 93 final endVal; | |
| 94 | |
| 95 var _delta; | |
| 96 | |
| 97 ActionTween(this.setter, this.startVal, this.endVal, double duration) : super( duration) { | |
| 98 _computeDelta(); | |
| 99 } | |
| 100 | |
| 101 void _computeDelta() { | |
| 102 if (startVal is Point) { | |
| 103 double xStart = startVal.x; | |
| 104 double yStart = startVal.y; | |
| 105 double xEnd = endVal.x; | |
| 106 double yEnd = endVal.y; | |
| 107 _delta = new Point(xEnd - xStart, yEnd - yStart); | |
| 108 } else if (startVal is double) { | |
| 109 _delta = endVal - startVal; | |
| 110 } else { | |
| 111 assert(false); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void update(double t) { | |
| 116 var newVal; | |
| 117 | |
| 118 if (startVal is Point) { | |
| 119 double xStart = startVal.x; | |
| 120 double yStart = startVal.y; | |
| 121 double xDelta = _delta.x; | |
| 122 double yDelta = _delta.y; | |
| 123 | |
| 124 newVal = new Point(xStart + xDelta * t, yStart + yDelta * t); | |
| 125 } else if (startVal is double) { | |
| 126 newVal = startVal + _delta * t; | |
| 127 } else { | |
| 128 assert(false); | |
| 129 } | |
| 130 | |
| 131 setter(newVal); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 class ActionController { | |
| 136 | |
| 137 List<Action> _actions = []; | |
| 138 | |
| 139 ActionController(); | |
| 140 | |
| 141 void run(Action action) { | |
| 142 _actions.add(action); | |
| 143 } | |
| 144 | |
| 145 void step(double dt) { | |
| 146 for (int i = _actions.length - 1; i >= 0; i--) { | |
| 147 Action action = _actions[i]; | |
| 148 action.step(dt); | |
| 149 | |
| 150 if (action._finished) { | |
|
kulakowski
2015/07/01 22:18:43
How many actions can be pending? If it's a bunch,
| |
| 151 _actions.removeAt(i); | |
| 152 } | |
| 153 } | |
| 154 } | |
| 155 } | |
| OLD | NEW |