Commit 65289031 authored by Viktor Lidholt's avatar Viktor Lidholt

Merge pull request #862 from vlidholt/master

Moves GameObjectFactory and PlayerState to their own files in demo game
parents 3c1672a8 d90ccd3a
......@@ -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';
......@@ -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<String,SoundEffect> 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;
}
}
}
part of game;
enum GameObjectType {
asteroidBig,
asteroidSmall,
movingEnemy,
coin,
}
class GameObjectFactory {
GameObjectFactory(this.sheet, this.sounds, this.level, this.playerState);
SpriteSheet sheet;
Map<String,SoundEffect> 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);
}
}
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;
}
}
}
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