From dbda2725ff770954cc79f98b8229754684b7a807 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 3 Sep 2015 14:55:14 -0700 Subject: [PATCH 1/3] Adds coins when a boss explodes in demo game --- examples/game/lib/game_objects.dart | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/game/lib/game_objects.dart b/examples/game/lib/game_objects.dart index 80e15d9624..0246979180 100644 --- a/examples/game/lib/game_objects.dart +++ b/examples/game/lib/game_objects.dart @@ -468,7 +468,25 @@ class EnemyBoss extends Obstacle { void destroy() { f.playerState.boss = null; + + NodeWithSize screen = f.playerState.parent; + screen.addChild(new Flash(screen.size, 1.0)); super.destroy(); + + // Add coins + for (int i = 0; i < 20; i++) { + Coin coin = new Coin(f); + Point pos = new Point( + randomSignedDouble() * 160, + position.y + randomSignedDouble() * 160.0); + f.addGameObject(coin, pos); + } + } + + Explosion createExplosion() { + ExplosionBig explo = new ExplosionBig(f.sheet); + explo.scale = 1.5; + return explo; } set damage(double d) { From e749bea18c6cfd4f3cd5f6acd22474116246b3a0 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 3 Sep 2015 15:35:47 -0700 Subject: [PATCH 2/3] Adds constraint for aligning with another node in sprites --- packages/flutter_sprites/lib/constraint.dart | 31 ++++++++++++++++++++ packages/flutter_sprites/lib/util.dart | 4 +++ 2 files changed, 35 insertions(+) diff --git a/packages/flutter_sprites/lib/constraint.dart b/packages/flutter_sprites/lib/constraint.dart index fbdbbd6f5e..5c21d78165 100644 --- a/packages/flutter_sprites/lib/constraint.dart +++ b/packages/flutter_sprites/lib/constraint.dart @@ -70,3 +70,34 @@ class ConstraintRotationToNode extends Constraint { node.rotation = _dampenRotation(node.rotation, target, dampening); } } + +class ConstraintPositionToNode extends Constraint { + ConstraintPositionToNode(this.targetNode, {this.dampening, this.offset: Offset.zero}); + + final Node targetNode; + final Offset offset; + final double dampening; + + void constrain(Node node, double dt) { + Point targetPosition; + + if (targetNode.spriteBox != node.spriteBox || node.parent == null) { + // The target node is in another sprite box or has been removed + return; + } + + if (targetNode.parent == node.parent) { + targetPosition = targetNode.position; + } else { + targetPosition = node.parent.convertPointFromNode(Point.origin, targetNode); + } + + if (offset != null) + targetPosition += offset; + + if (dampening == null) + node.position = targetPosition; + else + node.position = GameMath.filterPoint(node.position, targetPosition, dampening); + } +} diff --git a/packages/flutter_sprites/lib/util.dart b/packages/flutter_sprites/lib/util.dart index 81c1a9e03d..f92b73966d 100644 --- a/packages/flutter_sprites/lib/util.dart +++ b/packages/flutter_sprites/lib/util.dart @@ -101,4 +101,8 @@ class GameMath { static double filter (double a, double b, double filterFactor) { return (a * (1-filterFactor)) + b * filterFactor; } + + static Point filterPoint(Point a, Point b, double filterFactor) { + return new Point(filter(a.x, b.x, filterFactor), filter(a.y, b.y, filterFactor)); + } } From eb3f30ab1f6846378ccf8e6b25464d4db6bc2de5 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 3 Sep 2015 15:36:26 -0700 Subject: [PATCH 3/3] Refactors boss power bar to use new constraint in demo game --- examples/game/lib/game_objects.dart | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/game/lib/game_objects.dart b/examples/game/lib/game_objects.dart index 0246979180..dd136be184 100644 --- a/examples/game/lib/game_objects.dart +++ b/examples/game/lib/game_objects.dart @@ -426,9 +426,13 @@ class EnemyBoss extends Obstacle { constraints = [new ConstraintRotationToNode(f.level.ship, dampening: 0.05)]; _powerBar = new PowerBar(new Size(60.0, 10.0)); - _powerBar.position = new Point(-80.0, 0.0); _powerBar.pivot = new Point(0.5, 0.5); - addChild(_powerBar); + f.level.addChild(_powerBar); + _powerBar.constraints = [new ConstraintPositionToNode( + this, + dampening: 0.5, + offset: new Offset(0.0, -70.0) + )]; } Sprite _sprt; @@ -446,8 +450,6 @@ class EnemyBoss extends Obstacle { _countDown = 60 + randomInt(120); } - - _powerBar.rotation = -rotation; } void fire(double r) { @@ -468,7 +470,9 @@ class EnemyBoss extends Obstacle { void destroy() { f.playerState.boss = null; + _powerBar.removeFromParent(); + // Flash the screen NodeWithSize screen = f.playerState.parent; screen.addChild(new Flash(screen.size, 1.0)); super.destroy();