Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(920)

Side by Side Diff: sky/sdk/example/game/lib/action.dart

Issue 1223543004: Adds better explosions, shield, and game over (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: git cl web Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sky/sdk/example/game/lib/game_demo.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 part of sprites; 1 part of sprites;
2 2
3 typedef void PointSetter(Point point); 3 typedef void ActionCallback();
4 4
5 abstract class Action { 5 abstract class Action {
6 Object _tag;
6 bool _finished = false; 7 bool _finished = false;
7 8
8 void step(double dt); 9 void step(double dt);
9 void update(double t) { 10 void update(double t) {
10 } 11 }
11 12
12 double get duration => 0.0; 13 double get duration => 0.0;
13 } 14 }
14 15
15 abstract class ActionInterval extends Action { 16 abstract class ActionInterval extends Action {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 117
117 void update(double t) { 118 void update(double t) {
118 fire(); 119 fire();
119 _finished = true; 120 _finished = true;
120 } 121 }
121 122
122 void fire(); 123 void fire();
123 } 124 }
124 125
125 class ActionCallFunction extends ActionInstant { 126 class ActionCallFunction extends ActionInstant {
126 Function _function; 127 ActionCallback _function;
127 128
128 ActionCallFunction(this._function); 129 ActionCallFunction(this._function);
129 130
130 void fire() { 131 void fire() {
131 _function(); 132 _function();
132 } 133 }
133 } 134 }
134 135
135 class ActionRemoveFromParent extends ActionInstant { 136 class ActionRemoveNode extends ActionInstant {
136 Node _node; 137 Node _node;
137 138
138 ActionRemoveFromParent(this._node); 139 ActionRemoveNode(this._node);
139 140
140 void fire() { 141 void fire() {
141 _node.removeFromParent(); 142 _node.removeFromParent();
142 } 143 }
143 } 144 }
144 145
145 class ActionRepeatForever extends Action { 146 class ActionRepeatForever extends Action {
146 final ActionInterval action; 147 final ActionInterval action;
147 double _elapsedInAction = 0.0; 148 double _elapsedInAction = 0.0;
148 149
(...skipping 30 matching lines...) Expand all
179 180
180 void _computeDelta() { 181 void _computeDelta() {
181 if (startVal is Point) { 182 if (startVal is Point) {
182 double xStart = startVal.x; 183 double xStart = startVal.x;
183 double yStart = startVal.y; 184 double yStart = startVal.y;
184 double xEnd = endVal.x; 185 double xEnd = endVal.x;
185 double yEnd = endVal.y; 186 double yEnd = endVal.y;
186 _delta = new Point(xEnd - xStart, yEnd - yStart); 187 _delta = new Point(xEnd - xStart, yEnd - yStart);
187 } else if (startVal is double) { 188 } else if (startVal is double) {
188 _delta = endVal - startVal; 189 _delta = endVal - startVal;
190 } else if (startVal is Color) {
191 int aDelta = endVal.alpha - startVal.alpha;
192 int rDelta = endVal.red - startVal.red;
193 int gDelta = endVal.green - startVal.green;
194 int bDelta = endVal.blue - startVal.blue;
195 _delta = new _ColorDiff(aDelta, rDelta, gDelta, bDelta);
189 } else { 196 } else {
190 assert(false); 197 assert(false);
191 } 198 }
192 } 199 }
193 200
194 void update(double t) { 201 void update(double t) {
195 var newVal; 202 var newVal;
196 203
197 if (startVal is Point) { 204 if (startVal is Point) {
205 // Point
198 double xStart = startVal.x; 206 double xStart = startVal.x;
199 double yStart = startVal.y; 207 double yStart = startVal.y;
200 double xDelta = _delta.x; 208 double xDelta = _delta.x;
201 double yDelta = _delta.y; 209 double yDelta = _delta.y;
202
203 newVal = new Point(xStart + xDelta * t, yStart + yDelta * t); 210 newVal = new Point(xStart + xDelta * t, yStart + yDelta * t);
204 } else if (startVal is double) { 211 } else if (startVal is double) {
212 // Doubles
205 newVal = startVal + _delta * t; 213 newVal = startVal + _delta * t;
214 } else if (startVal is Color) {
215 // Colors
216 int aNew = (startVal.alpha + (_delta.alpha * t).toInt()).clamp(0, 255);
217 int rNew = (startVal.red + (_delta.red * t).toInt()).clamp(0, 255);
218 int gNew = (startVal.green + (_delta.green * t).toInt()).clamp(0, 255);
219 int bNew = (startVal.blue + (_delta.blue * t).toInt()).clamp(0, 255);
220 newVal = new Color.fromARGB(aNew, rNew, gNew, bNew);
206 } else { 221 } else {
222 // Oopses
207 assert(false); 223 assert(false);
208 } 224 }
209 225
210 setter(newVal); 226 setter(newVal);
211 } 227 }
212 } 228 }
213 229
214 class ActionController { 230 class ActionController {
215 231
216 List<Action> _actions = []; 232 List<Action> _actions = [];
217 233
218 ActionController(); 234 ActionController();
219 235
220 void run(Action action) { 236 void run(Action action, [Object tag]) {
237 action._tag = tag;
221 action.update(0.0); 238 action.update(0.0);
222 _actions.add(action); 239 _actions.add(action);
223 } 240 }
224 241
242 void stop(Action action) {
243 _actions.remove(action);
244 }
245
246 void stopWithTag(Object tag) {
247 for (int i = _actions.length - 1; i >= 0; i--) {
248 Action action = _actions[i];
249 if (action._tag == tag) {
250 _actions.removeAt(i);
251 print("removing tag: $tag");
252 }
253 }
254 }
255
256 void stopAll() {
257 _actions.clear();
258 }
259
225 void step(double dt) { 260 void step(double dt) {
226 for (int i = _actions.length - 1; i >= 0; i--) { 261 for (int i = _actions.length - 1; i >= 0; i--) {
227 Action action = _actions[i]; 262 Action action = _actions[i];
228 action.step(dt); 263 action.step(dt);
229 264
230 if (action._finished) { 265 if (action._finished) {
231 _actions.removeAt(i); 266 _actions.removeAt(i);
232 } 267 }
233 } 268 }
234 } 269 }
270 }
271
272 class _ColorDiff {
273 final int alpha;
274 final int red;
275 final int green;
276 final int blue;
277
278 _ColorDiff(this.alpha, this.red, this.green, this.blue);
235 } 279 }
OLDNEW
« no previous file with comments | « no previous file | sky/sdk/example/game/lib/game_demo.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698