Commit e239e4c9 authored by Viktor Lidholt's avatar Viktor Lidholt

Adds counting of coins in demo game

parent 65289031
......@@ -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();
}
}
}
......
......@@ -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();
}
}
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<Point> 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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment