From e239e4c9938af5b1fb98b43d18eec6ce025d83ad Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Wed, 26 Aug 2015 15:35:19 -0700 Subject: [PATCH] Adds counting of coins in demo game --- examples/game/lib/game_demo_node.dart | 4 +- examples/game/lib/game_objects.dart | 10 +++ examples/game/lib/player_state.dart | 90 +++++++++++++++++++++------ 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/examples/game/lib/game_demo_node.dart b/examples/game/lib/game_demo_node.dart index 48cf262819..b55eeecaa0 100644 --- a/examples/game/lib/game_demo_node.dart +++ b/examples/game/lib/game_demo_node.dart @@ -39,7 +39,7 @@ class GameDemoNode extends NodeWithSize { _gameScreen.addChild(_level); // Add heads up display - _playerState = new PlayerState(_spritesUI); + _playerState = new PlayerState(_spritesUI, _spritesGame); addChild(_playerState); _objectFactory = new GameObjectFactory(_spritesGame, _sounds, _level, _playerState); @@ -165,7 +165,7 @@ class GameDemoNode extends NodeWithSize { } else if (node is GameObject && node.canBeCollected) { if (node.collidingWith(_level.ship)) { // The ship ran over something collectable - node.removeFromParent(); + node.collect(); } } } diff --git a/examples/game/lib/game_objects.dart b/examples/game/lib/game_objects.dart index cdf19933a2..02904dbf2a 100644 --- a/examples/game/lib/game_objects.dart +++ b/examples/game/lib/game_objects.dart @@ -53,6 +53,10 @@ abstract class GameObject extends Node { } } + void collect() { + removeFromParent(); + } + void addDamage(double d) { if (!canBeDamaged) return; @@ -263,6 +267,7 @@ class Coin extends PowerUp { _sprt = new Sprite(f.sheet["shield.png"]); _sprt.transferMode = sky.TransferMode.plus; _sprt.size = new Size(15.0, 15.0); + _sprt.colorOverlay = new Color(0xffffff00); addChild(_sprt); radius = 7.5; @@ -274,4 +279,9 @@ class Coin extends PowerUp { } Sprite _sprt; + + void collect() { + f.playerState.addCoin(this); + super.collect(); + } } diff --git a/examples/game/lib/player_state.dart b/examples/game/lib/player_state.dart index 331517e73d..90be2a3b01 100644 --- a/examples/game/lib/player_state.dart +++ b/examples/game/lib/player_state.dart @@ -1,10 +1,72 @@ part of game; class PlayerState extends Node { - SpriteSheet sheet; - Sprite sprtBgScore; + PlayerState(this._sheetUI, this._sheetGame) { + // Score display + Sprite sprtBgScore = new Sprite(_sheetUI["scoreboard.png"]); + sprtBgScore.pivot = new Point(1.0, 0.0); + sprtBgScore.scale = 0.35; + sprtBgScore.position = new Point(310.0, 10.0); + addChild(sprtBgScore); + + _scoreDisplay = new ScoreDisplay(_sheetUI); + _scoreDisplay.position = new Point(-13.0, 49.0); + sprtBgScore.addChild(_scoreDisplay); + + // Coin display + Sprite sprtBgCoins = new Sprite(_sheetUI["scoreboard.png"]); + sprtBgCoins.pivot = new Point(1.0, 0.0); + sprtBgCoins.scale = 0.35; + sprtBgCoins.position = new Point(170.0, 10.0); + addChild(sprtBgCoins); + + _coinDisplay = new ScoreDisplay(_sheetUI); + _coinDisplay.position = new Point(-13.0, 49.0); + sprtBgCoins.addChild(_coinDisplay); + } + + final SpriteSheet _sheetUI; + final SpriteSheet _sheetGame; + + ScoreDisplay _scoreDisplay; + ScoreDisplay _coinDisplay; + + int get score => _scoreDisplay.score; + + set score(int score) { + _scoreDisplay.score = score; + } + + int get coins => _coinDisplay.score; + + void addCoin(Coin c) { + // Animate coin to the top of the screen + Point startPos = convertPointFromNode(Point.origin, c); + Point finalPos = new Point(10.0, 10.0); + Point middlePos = new Point((startPos.x + finalPos.x) / 2.0 + 50.0, + (startPos.y + finalPos.y) / 2.0); + + List path = [startPos, middlePos, finalPos]; + + Sprite sprt = new Sprite(_sheetGame["shield.png"]); + sprt.size = new Size(15.0, 15.0); + sprt.transferMode = sky.TransferMode.plus; + sprt.colorOverlay = new Color(0xffffff00); + ActionSpline spline = new ActionSpline((a) => sprt.position = a, path, 0.5); + spline.tension = 0.25; + sprt.actions.run(new ActionSequence([ + spline, + new ActionRemoveNode(sprt), + new ActionCallFunction(() { _coinDisplay.score += 1; }) + ])); + + addChild(sprt); + } +} + +class ScoreDisplay extends Node { + ScoreDisplay(this._sheetUI); - bool _dirtyScore = true; int _score = 0; int get score => _score; @@ -14,29 +76,21 @@ class PlayerState extends Node { _dirtyScore = true; } - PlayerState(this.sheet) { - position = new Point(310.0, 10.0); - scale = 0.6; + SpriteSheet _sheetUI; - sprtBgScore = new Sprite(sheet["scoreboard.png"]); - sprtBgScore.pivot = new Point(1.0, 0.0); - sprtBgScore.scale = 0.6; - addChild(sprtBgScore); - } + bool _dirtyScore = true; void update(double dt) { - // Update score if (_dirtyScore) { - - sprtBgScore.removeAllChildren(); + removeAllChildren(); String scoreStr = _score.toString(); - double xPos = -50.0; + double xPos = -37.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); + Sprite numSprt = new Sprite(_sheetUI["number_$numStr.png"]); + numSprt.position = new Point(xPos, 0.0); + addChild(numSprt); xPos -= 37.0; } _dirtyScore = false;