Merge pull request #862 from vlidholt/master
Moves GameObjectFactory and PlayerState to their own files in demo game
This commit is contained in:
commit
6528903150
@ -12,5 +12,7 @@ part 'explosions.dart';
|
|||||||
part 'flash.dart';
|
part 'flash.dart';
|
||||||
part 'game_demo_node.dart';
|
part 'game_demo_node.dart';
|
||||||
part 'game_objects.dart';
|
part 'game_objects.dart';
|
||||||
|
part 'game_object_factory.dart';
|
||||||
|
part 'player_state.dart';
|
||||||
part 'repeated_image.dart';
|
part 'repeated_image.dart';
|
||||||
part 'star_field.dart';
|
part 'star_field.dart';
|
||||||
|
@ -256,97 +256,3 @@ class Level extends Node {
|
|||||||
return position.y;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
51
examples/game/lib/game_object_factory.dart
Normal file
51
examples/game/lib/game_object_factory.dart
Normal file
@ -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<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);
|
||||||
|
}
|
||||||
|
}
|
45
examples/game/lib/player_state.dart
Normal file
45
examples/game/lib/player_state.dart
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user