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

Unified Diff: sky/sdk/example/game/lib/action.dart

Issue 1224773003: Adds ActionSequence to sprites and improved explosions in game demo (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sky/sdk/example/game/lib/game_demo_world.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/sdk/example/game/lib/action.dart
diff --git a/sky/sdk/example/game/lib/action.dart b/sky/sdk/example/game/lib/action.dart
index 400bcd8b13dda63e1b84b1b01a66a540b89a1e7a..f0763c02c01bd09d23b335c1f60d7455d95d6dbd 100644
--- a/sky/sdk/example/game/lib/action.dart
+++ b/sky/sdk/example/game/lib/action.dart
@@ -8,6 +8,8 @@ abstract class Action {
void step(double dt);
void update(double t) {
}
+
+ double get duration => 0.0;
}
abstract class ActionInterval extends Action {
@@ -53,17 +55,93 @@ class ActionRepeat extends ActionInterval {
}
}
-// TODO: Implement
class ActionSequence extends ActionInterval {
- final List<ActionInterval> actions;
+ Action _a;
+ Action _b;
+ double _split;
+
+ ActionSequence(List<Action> actions) {
+ assert(actions.length >= 2);
+
+ if (actions.length == 2) {
+ // Base case
+ _a = actions[0];
+ _b = actions[1];
+ } else {
+ _a = actions[0];
+ _b = new ActionSequence(actions.sublist(1));
+ }
+
+ // Calculate split and duration
+ _duration = _a.duration + _b.duration;
+ if (_duration > 0) {
+ _split = _a.duration / _duration;
+ } else {
+ _split = 1.0;
+ }
+ }
- ActionSequence(this.actions) {
- for (Action action in actions) {
- _duration += action._duration;
+ void update(double t) {
+ if (t < _split) {
+ // Play first action
+ double ta;
+ if (_split > 0.0) {
+ ta = (t / _split).clamp(0.0, 1.0);
+ } else {
+ ta = 1.0;
+ }
+ _a.update(ta);
+ } else if (t >= 1.0) {
+ // Make sure everything is finished
+ if (!_a._finished) _a.update(1.0);
+ if (!_b._finished) _b.update(1.0);
+ } else {
+ // Play second action, but first make sure the first has finished
+ if (!_a._finished) _a.update(1.0);
+ double tb;
+ if (_split < 1.0) {
+ tb = (1.0 - (1.0 - t) / (1.0 - _split)).clamp(0.0, 1.0);
+ } else {
+ tb = 1.0;
+ }
+ _b.update(tb);
}
}
}
+abstract class ActionInstant extends Action {
+
+ void step(double dt) {
+ }
+
+ void update(double t) {
+ fire();
+ _finished = true;
+ }
+
+ void fire();
+}
+
+class ActionCallFunction extends ActionInstant {
+ Function _function;
+
+ ActionCallFunction(this._function);
+
+ void fire() {
+ _function();
+ }
+}
+
+class ActionRemoveFromParent extends ActionInstant {
+ Node _node;
+
+ ActionRemoveFromParent(this._node);
+
+ void fire() {
+ _node.removeFromParent();
+ }
+}
+
class ActionRepeatForever extends Action {
final ActionInterval action;
double _elapsedInAction = 0.0;
@@ -140,6 +218,7 @@ class ActionController {
ActionController();
void run(Action action) {
+ action.update(0.0);
_actions.add(action);
}
« no previous file with comments | « no previous file | sky/sdk/example/game/lib/game_demo_world.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698