From bc62a243f54aea646ed38624084f3747f663e8db Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 25 Aug 2015 10:14:26 -0700 Subject: [PATCH 1/5] Removes old code from demo game --- examples/game/lib/game_demo_node.dart | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/examples/game/lib/game_demo_node.dart b/examples/game/lib/game_demo_node.dart index c819ab5820..e9ada2a8d3 100644 --- a/examples/game/lib/game_demo_node.dart +++ b/examples/game/lib/game_demo_node.dart @@ -483,29 +483,6 @@ class GameObjectFactory { } } -// class MovingObstacle extends Obstacle { -// MovingObstacle(SpriteSheet sheet, Map effects, ObstacleType type) : super (sheet, effects, type); -// -// void setupAction() { -// actions.stopAll(); -// -// List offsets = [ -// new Offset(-160.0, 160.0), -// new Offset(-80.0, -160.0), -// new Offset(0.0, 160.0), -// new Offset(80.0, -160.0), -// new Offset(160.0, 160.0)]; -// -// List points = []; -// for (Offset offset in offsets) { -// points.add(position + offset); -// } -// -// ActionSpline spline = new ActionSpline((a) => position = a, points, 4.0); -// actions.run(new ActionRepeatForever(spline)); -// } -// } - class StarField extends NodeWithSize { sky.Image _image; SpriteSheet _spriteSheet; From dc3159316e11cc6cb81dab3e947bcb2446016b2f Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 25 Aug 2015 14:45:44 -0700 Subject: [PATCH 2/5] Makes the spline tension editable in sprites --- examples/game/lib/action_spline.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/game/lib/action_spline.dart b/examples/game/lib/action_spline.dart index 61e7835fa2..5b4a351530 100644 --- a/examples/game/lib/action_spline.dart +++ b/examples/game/lib/action_spline.dart @@ -24,11 +24,11 @@ class ActionSpline extends ActionInterval { final Function setter; final List points; + double tension = 0.5; double _dt; void update(double t) { - double tension = 0.5; int p; double lt; From dcd17aefd6f2b229bd71c92f3582251449559418 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 25 Aug 2015 14:47:56 -0700 Subject: [PATCH 3/5] Adds constraint for rotating a node towards another node --- examples/game/lib/constraint.dart | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/examples/game/lib/constraint.dart b/examples/game/lib/constraint.dart index 9909ba84a5..dcd3e66618 100644 --- a/examples/game/lib/constraint.dart +++ b/examples/game/lib/constraint.dart @@ -18,6 +18,7 @@ double _dampenRotation(double src, double dst, double dampening) { class ConstraintRotationToMovement { ConstraintRotationToMovement([this.baseRotation = 0.0, this.dampening]); + final double dampening; final double baseRotation; @@ -41,3 +42,29 @@ class ConstraintRotationToMovement { node.rotation = _dampenRotation(node.rotation, target, dampening); } } + +class ConstraintRotationToNode { + ConstraintRotationToNode(this.targetNode, [this.baseRotation, this.dampening]); + + final Node targetNode; + final double baseRotation; + final double dampening; + + void constrain(Node node, double dt) { + Offset offset; + + if (targetNode.parent == node.parent) { + offset = targetNode.position - node.position; + } else { + offset = node.convertPointToBoxSpace(Point.origin) + - targetNode.convertPointToBoxSpace(Point.origin); + } + + double target = degrees(GameMath.atan2(offset.dy, offset.dx)) + baseRotation; + + if (dampening == null) + node.rotation = target; + else + node.rotation = _dampenRotation(node.rotation, target, dampening); + } +} From dec2689c078ec1f11425f61a6857d7831ac327c5 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 25 Aug 2015 14:49:54 -0700 Subject: [PATCH 4/5] Adds swarms of enemies to demo game --- examples/game/lib/game_demo.dart | 1 + examples/game/lib/game_demo_node.dart | 83 ++++++++++++++++++++------- 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/examples/game/lib/game_demo.dart b/examples/game/lib/game_demo.dart index d5f0090164..1a7ce7d558 100644 --- a/examples/game/lib/game_demo.dart +++ b/examples/game/lib/game_demo.dart @@ -1,6 +1,7 @@ library game; import 'dart:async'; +import 'dart:math' as math; import 'dart:sky' as sky; import 'sprites.dart'; diff --git a/examples/game/lib/game_demo_node.dart b/examples/game/lib/game_demo_node.dart index e9ada2a8d3..67bc68d9d5 100644 --- a/examples/game/lib/game_demo_node.dart +++ b/examples/game/lib/game_demo_node.dart @@ -3,6 +3,9 @@ part of game; final double _gameSizeWidth = 320.0; double _gameSizeHeight = 320.0; +final double _chunkSpacing = 640.0; +final int _chunksPerLevel = 5; + final bool _drawDebug = false; class GameDemoNode extends NodeWithSize { @@ -164,7 +167,6 @@ class GameDemoNode extends NodeWithSize { } int _chunk = 0; - double _chunkSpacing = 640.0; void addObjects() { @@ -181,21 +183,24 @@ class GameDemoNode extends NodeWithSize { if (chunk == 0) { // Leave the first chunk empty return; - } else if (chunk == 1) { - addLevelAsteroids(10, yPos, 0.0); - } else { - addLevelAsteroids(9 + chunk, yPos, 0.5); } - } - void addLevelAsteroids(int numAsteroids, double yPos, double distribution) { - for (int i = 0; i < numAsteroids; i++) { - GameObjectType type = (randomDouble() < distribution) ? GameObjectType.asteroidBig : GameObjectType.asteroidSmall; - Point pos = new Point(randomSignedDouble() * 160.0, - yPos + _chunkSpacing * randomDouble()); - _objectFactory.addGameObject(type, pos); + chunk -= 1; + + int level = chunk ~/ _chunksPerLevel; + int part = chunk % _chunksPerLevel; + + if (part == 0) { + _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); + } else if (part == 1) { + _objectFactory.addSwarm(4 + level * 2, yPos); + } else if (part == 2) { + _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); + } else if (part == 3) { + _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); + } else if (part == 4) { + _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); } - _objectFactory.addGameObject(GameObjectType.movingEnemy, new Point(0.0, yPos + 160.0)); } void fire() { @@ -434,20 +439,41 @@ class MovingEnemy extends Obstacle { constraints = [new ConstraintRotationToMovement(0.0, 0.5)]; } + final double _swirlSpacing = 80.0; + + _addRandomSquare(List offsets, double x, double y) { + double xMove = (randomDouble() < 0.5) ? _swirlSpacing : -_swirlSpacing; + double yMove = (randomDouble() < 0.5) ? _swirlSpacing : -_swirlSpacing; + + if (randomDouble() < 0.5) { + offsets.add(new Offset(x, y)); + offsets.add(new Offset(xMove + x, y)); + offsets.add(new Offset(xMove + x, yMove + y)); + offsets.add(new Offset(x, yMove + y)); + offsets.add(new Offset(x, y)); + } else { + offsets.add(new Offset(x, y)); + offsets.add(new Offset(x, y + yMove)); + offsets.add(new Offset(xMove + x, yMove + y)); + offsets.add(new Offset(xMove + x, y)); + offsets.add(new Offset(x, y)); + } + } + void setupActions() { - List offsets = [ - new Offset(-160.0, 160.0), - new Offset(-80.0, -160.0), - new Offset(0.0, 160.0), - new Offset(80.0, -160.0), - new Offset(160.0, 160.0)]; + + List offsets = []; + _addRandomSquare(offsets, -_swirlSpacing, 0.0); + _addRandomSquare(offsets, _swirlSpacing, 0.0); + offsets.add(new Offset(-_swirlSpacing, 0.0)); List points = []; for (Offset offset in offsets) { points.add(position + offset); } - ActionSpline spline = new ActionSpline((a) => position = a, points, 4.0); + ActionSpline spline = new ActionSpline((a) => position = a, points, 6.0); + spline.tension = 0.7; actions.run(new ActionRepeatForever(spline)); } @@ -467,6 +493,23 @@ class GameObjectFactory { Map sounds; Level level; + void addAsteroids(int numAsteroids, double yPos, double distribution) { + for (int i = 0; i < numAsteroids; i++) { + GameObjectType type = (randomDouble() < distribution) ? GameObjectType.asteroidBig : GameObjectType.asteroidSmall; + Point pos = new Point(randomSignedDouble() * 160.0, + yPos + _chunkSpacing * randomDouble()); + addGameObject(type, pos); + } + } + + void addSwarm(int numEnemies, double yPos) { + for (int i = 0; i < numEnemies; i++) { + double spacing = math.max(_chunkSpacing / (numEnemies + 1.0), 80.0); + double y = yPos + _chunkSpacing / 2.0 - (numEnemies - 1) * spacing / 2.0 + i * spacing; + addGameObject(GameObjectType.movingEnemy, new Point(0.0, y)); + } + } + void addGameObject(GameObjectType type, Point pos) { GameObject obj; if (type == GameObjectType.asteroidBig) From 59817111f0722176d253d619bad7886b945649e6 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 25 Aug 2015 15:30:45 -0700 Subject: [PATCH 5/5] Fixes nits --- examples/game/lib/constraint.dart | 13 +++++------ examples/game/lib/game_demo_node.dart | 32 +++++++++++++++------------ examples/game/lib/util.dart | 4 ++++ 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/examples/game/lib/constraint.dart b/examples/game/lib/constraint.dart index dcd3e66618..0833e54cd3 100644 --- a/examples/game/lib/constraint.dart +++ b/examples/game/lib/constraint.dart @@ -8,6 +8,9 @@ abstract class Constraint { } double _dampenRotation(double src, double dst, double dampening) { + if (dampening == null) + return dst; + double delta = dst - src; while (delta > 180.0) delta -= 360; while (delta < -180) delta += 360; @@ -36,10 +39,7 @@ class ConstraintRotationToMovement { Offset offset = node.position - _lastPosition; double target = degrees(GameMath.atan2(offset.dy, offset.dx)) + baseRotation; - if (dampening == null) - node.rotation = target; - else - node.rotation = _dampenRotation(node.rotation, target, dampening); + node.rotation = _dampenRotation(node.rotation, target, dampening); } } @@ -62,9 +62,6 @@ class ConstraintRotationToNode { double target = degrees(GameMath.atan2(offset.dy, offset.dx)) + baseRotation; - if (dampening == null) - node.rotation = target; - else - node.rotation = _dampenRotation(node.rotation, target, dampening); + node.rotation = _dampenRotation(node.rotation, target, dampening); } } diff --git a/examples/game/lib/game_demo_node.dart b/examples/game/lib/game_demo_node.dart index 67bc68d9d5..df2bb234c9 100644 --- a/examples/game/lib/game_demo_node.dart +++ b/examples/game/lib/game_demo_node.dart @@ -394,7 +394,7 @@ abstract class Asteroid extends Obstacle { void setupActions() { // Rotate obstacle int direction = 1; - if (randomDouble() < 0.5) direction = -1; + if (randomBool()) direction = -1; ActionTween rotate = new ActionTween( (a) => _sprt.rotation = a, 0.0, 360.0 * direction, 5.0 + 5.0 * randomDouble()); @@ -442,21 +442,25 @@ class MovingEnemy extends Obstacle { final double _swirlSpacing = 80.0; _addRandomSquare(List offsets, double x, double y) { - double xMove = (randomDouble() < 0.5) ? _swirlSpacing : -_swirlSpacing; - double yMove = (randomDouble() < 0.5) ? _swirlSpacing : -_swirlSpacing; + double xMove = (randomBool()) ? _swirlSpacing : -_swirlSpacing; + double yMove = (randomBool()) ? _swirlSpacing : -_swirlSpacing; - if (randomDouble() < 0.5) { - offsets.add(new Offset(x, y)); - offsets.add(new Offset(xMove + x, y)); - offsets.add(new Offset(xMove + x, yMove + y)); - offsets.add(new Offset(x, yMove + y)); - offsets.add(new Offset(x, y)); + if (randomBool()) { + offsets.addAll([ + new Offset(x, y), + new Offset(xMove + x, y), + new Offset(xMove + x, yMove + y), + new Offset(x, yMove + y), + new Offset(x, y) + ]); } else { - offsets.add(new Offset(x, y)); - offsets.add(new Offset(x, y + yMove)); - offsets.add(new Offset(xMove + x, yMove + y)); - offsets.add(new Offset(xMove + x, y)); - offsets.add(new Offset(x, y)); + offsets.addAll([ + new Offset(x, y), + new Offset(x, y + yMove), + new Offset(xMove + x, yMove + y), + new Offset(xMove + x, y), + new Offset(x, y) + ]); } } diff --git a/examples/game/lib/util.dart b/examples/game/lib/util.dart index 71edbad109..5ff5d00a34 100644 --- a/examples/game/lib/util.dart +++ b/examples/game/lib/util.dart @@ -17,6 +17,10 @@ int randomInt(int max) { return _random.nextInt(max); } +bool randomBool() { + return _random.nextDouble() < 0.5; +} + // atan2 class _Atan2Constants {