Commit 8c958f97 authored by Viktor Lidholt's avatar Viktor Lidholt

Refactors code in demo game to make it simpler

parent 20809bc9
part of game; part of game;
enum GameObjectType {
asteroidBig,
asteroidSmall,
asteroidPowerUp,
enemyScout,
enemyDestroyer,
coin,
}
class GameObjectFactory { class GameObjectFactory {
GameObjectFactory(this.sheet, this.sounds, this.level, this.playerState); GameObjectFactory(this.sheet, this.sounds, this.level, this.playerState);
...@@ -19,11 +10,17 @@ class GameObjectFactory { ...@@ -19,11 +10,17 @@ class GameObjectFactory {
void addAsteroids(int numAsteroids, double yPos, double distribution) { void addAsteroids(int numAsteroids, double yPos, double distribution) {
for (int i = 0; i < numAsteroids; i++) { for (int i = 0; i < numAsteroids; i++) {
GameObjectType type = (randomDouble() < distribution) ? GameObjectType.asteroidBig : GameObjectType.asteroidSmall; GameObject obj;
if (i == 0) type = GameObjectType.asteroidPowerUp; if (i == 0)
obj = new AsteroidPowerUp(this);
else if (randomDouble() < distribution)
obj = new AsteroidBig(this);
else
obj = new AsteroidSmall(this);
Point pos = new Point(randomSignedDouble() * 160.0, Point pos = new Point(randomSignedDouble() * 160.0,
yPos + _chunkSpacing * randomDouble()); yPos + _chunkSpacing * randomDouble());
addGameObject(type, pos); addGameObject(obj, pos);
} }
} }
...@@ -31,31 +28,17 @@ class GameObjectFactory { ...@@ -31,31 +28,17 @@ class GameObjectFactory {
for (int i = 0; i < numEnemies; i++) { for (int i = 0; i < numEnemies; i++) {
double spacing = math.max(_chunkSpacing / (numEnemies + 1.0), 80.0); double spacing = math.max(_chunkSpacing / (numEnemies + 1.0), 80.0);
double y = yPos + _chunkSpacing / 2.0 - (numEnemies - 1) * spacing / 2.0 + i * spacing; double y = yPos + _chunkSpacing / 2.0 - (numEnemies - 1) * spacing / 2.0 + i * spacing;
addGameObject(GameObjectType.enemyScout, new Point(0.0, y)); addGameObject(new EnemyScout(this), new Point(0.0, y));
} }
} }
void addEnemyDestroyerSwarm(int numEnemies, double yPos) { void addEnemyDestroyerSwarm(int numEnemies, double yPos) {
for (int i = 0; i < numEnemies; i++) { for (int i = 0; i < numEnemies; i++) {
addGameObject(GameObjectType.enemyDestroyer, new Point(randomSignedDouble() * 120.0 , yPos + _chunkSpacing * randomDouble())); addGameObject(new EnemyDestroyer(this), new Point(randomSignedDouble() * 120.0 , yPos + _chunkSpacing * randomDouble()));
} }
} }
void addGameObject(GameObjectType type, Point pos) { void addGameObject(GameObject obj, 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.asteroidPowerUp)
obj = new AsteroidPowerUp(this);
else if (type == GameObjectType.enemyScout)
obj = new EnemyScout(this);
else if (type == GameObjectType.enemyDestroyer)
obj = new EnemyDestroyer(this);
else if (type == GameObjectType.coin)
obj = new Coin(this);
obj.position = pos; obj.position = pos;
obj.setupActions(); obj.setupActions();
...@@ -64,10 +47,10 @@ class GameObjectFactory { ...@@ -64,10 +47,10 @@ class GameObjectFactory {
void addBossFight(int l, double yPos) { void addBossFight(int l, double yPos) {
EnemyBoss boss = new EnemyBoss(this); EnemyBoss boss = new EnemyBoss(this);
boss.position = new Point(0.0, yPos + _chunkSpacing / 2.0); Point pos = new Point(0.0, yPos + _chunkSpacing / 2.0);
boss.setupActions();
addGameObject(boss, pos);
level.addChild(boss);
playerState.boss = boss; playerState.boss = boss;
} }
} }
...@@ -44,9 +44,7 @@ abstract class GameObject extends Node { ...@@ -44,9 +44,7 @@ abstract class GameObject extends Node {
Collectable powerUp = createPowerUp(); Collectable powerUp = createPowerUp();
if (powerUp != null) { if (powerUp != null) {
powerUp.position = position; f.addGameObject(powerUp, position);
powerUp.setupActions();
parent.addChild(powerUp);
} }
removeFromParent(); removeFromParent();
......
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