diff --git a/examples/game/lib/game_demo.dart b/examples/game/lib/game_demo.dart index 6e53c6a028..699c03a63d 100644 --- a/examples/game/lib/game_demo.dart +++ b/examples/game/lib/game_demo.dart @@ -12,5 +12,7 @@ part 'explosions.dart'; part 'flash.dart'; part 'game_demo_node.dart'; part 'game_objects.dart'; +part 'game_object_factory.dart'; +part 'player_state.dart'; part 'repeated_image.dart'; part 'star_field.dart'; diff --git a/examples/game/lib/game_demo_node.dart b/examples/game/lib/game_demo_node.dart index 77730ffcc7..48cf262819 100644 --- a/examples/game/lib/game_demo_node.dart +++ b/examples/game/lib/game_demo_node.dart @@ -256,97 +256,3 @@ class Level extends Node { return position.y; } } - -enum GameObjectType { - asteroidBig, - asteroidSmall, - movingEnemy, - coin, -} - -class GameObjectFactory { - GameObjectFactory(this.sheet, this.sounds, this.level, this.playerState); - - SpriteSheet sheet; - Map sounds; - Level level; - PlayerState playerState; - - 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) - obj = new AsteroidBig(this); - else if (type == GameObjectType.asteroidSmall) - obj = new AsteroidSmall(this); - else if (type == GameObjectType.movingEnemy) - obj = new MovingEnemy(this); - else if (type == GameObjectType.coin) - obj = new Coin(this); - - obj.position = pos; - obj.setupActions(); - - level.addChild(obj); - } -} - -class PlayerState extends Node { - SpriteSheet sheet; - Sprite sprtBgScore; - - bool _dirtyScore = true; - int _score = 0; - - int get score => _score; - - set score(int score) { - _score = score; - _dirtyScore = true; - } - - PlayerState(this.sheet) { - position = new Point(310.0, 10.0); - scale = 0.6; - - sprtBgScore = new Sprite(sheet["scoreboard.png"]); - sprtBgScore.pivot = new Point(1.0, 0.0); - sprtBgScore.scale = 0.6; - addChild(sprtBgScore); - } - - void update(double dt) { - // Update score - if (_dirtyScore) { - - sprtBgScore.removeAllChildren(); - - String scoreStr = _score.toString(); - double xPos = -50.0; - for (int i = scoreStr.length - 1; i >= 0; i--) { - String numStr = scoreStr.substring(i, i + 1); - Sprite numSprt = new Sprite(sheet["number_$numStr.png"]); - numSprt.position = new Point(xPos, 49.0); - sprtBgScore.addChild(numSprt); - xPos -= 37.0; - } - _dirtyScore = false; - } - } -} diff --git a/examples/game/lib/game_object_factory.dart b/examples/game/lib/game_object_factory.dart new file mode 100644 index 0000000000..97a102f00a --- /dev/null +++ b/examples/game/lib/game_object_factory.dart @@ -0,0 +1,51 @@ +part of game; + +enum GameObjectType { + asteroidBig, + asteroidSmall, + movingEnemy, + coin, +} + +class GameObjectFactory { + GameObjectFactory(this.sheet, this.sounds, this.level, this.playerState); + + SpriteSheet sheet; + Map sounds; + Level level; + PlayerState playerState; + + 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) + obj = new AsteroidBig(this); + else if (type == GameObjectType.asteroidSmall) + obj = new AsteroidSmall(this); + else if (type == GameObjectType.movingEnemy) + obj = new MovingEnemy(this); + else if (type == GameObjectType.coin) + obj = new Coin(this); + + obj.position = pos; + obj.setupActions(); + + level.addChild(obj); + } +} diff --git a/examples/game/lib/player_state.dart b/examples/game/lib/player_state.dart new file mode 100644 index 0000000000..331517e73d --- /dev/null +++ b/examples/game/lib/player_state.dart @@ -0,0 +1,45 @@ +part of game; + +class PlayerState extends Node { + SpriteSheet sheet; + Sprite sprtBgScore; + + bool _dirtyScore = true; + int _score = 0; + + int get score => _score; + + set score(int score) { + _score = score; + _dirtyScore = true; + } + + PlayerState(this.sheet) { + position = new Point(310.0, 10.0); + scale = 0.6; + + sprtBgScore = new Sprite(sheet["scoreboard.png"]); + sprtBgScore.pivot = new Point(1.0, 0.0); + sprtBgScore.scale = 0.6; + addChild(sprtBgScore); + } + + void update(double dt) { + // Update score + if (_dirtyScore) { + + sprtBgScore.removeAllChildren(); + + String scoreStr = _score.toString(); + double xPos = -50.0; + for (int i = scoreStr.length - 1; i >= 0; i--) { + String numStr = scoreStr.substring(i, i + 1); + Sprite numSprt = new Sprite(sheet["number_$numStr.png"]); + numSprt.position = new Point(xPos, 49.0); + sprtBgScore.addChild(numSprt); + xPos -= 37.0; + } + _dirtyScore = false; + } + } +}