Permalink
Browse files

Update examples to treat the game as an entity.

  • Loading branch information...
richardlord committed Oct 10, 2012
1 parent 81b4944 commit 01c87f42fe947b2683e153757ecebe99f820340a
Showing with 364 additions and 269 deletions.
  1. +12 −19 examples/no-dependencies/asteroids/net/richardlord/asteroids/Asteroids.as
  2. +9 −1 examples/no-dependencies/asteroids/net/richardlord/asteroids/EntityCreator.as
  3. +8 −0 examples/no-dependencies/asteroids/net/richardlord/asteroids/GameConfig.as
  4. +1 −9 examples/no-dependencies/asteroids/net/richardlord/asteroids/components/GameState.as
  5. +10 −0 examples/no-dependencies/asteroids/net/richardlord/asteroids/nodes/GameNode.as
  6. +42 −34 examples/no-dependencies/asteroids/net/richardlord/asteroids/systems/GameManager.as
  7. +10 −11 examples/no-dependencies/asteroids/net/richardlord/asteroids/systems/MovementSystem.as
  8. +0 −1 examples/no-dependencies/asteroids/net/richardlord/asteroids/systems/RenderSystem.as
  9. +9 −1 examples/robotlegs/asteroids/net/richardlord/asteroids/EntityCreator.as
  10. +8 −0 examples/robotlegs/asteroids/net/richardlord/asteroids/GameConfig.as
  11. +9 −11 examples/robotlegs/asteroids/net/richardlord/asteroids/StartAsteroids.as
  12. +1 −9 examples/robotlegs/asteroids/net/richardlord/asteroids/components/GameState.as
  13. +10 −0 examples/robotlegs/asteroids/net/richardlord/asteroids/nodes/GameNode.as
  14. +40 −32 examples/robotlegs/asteroids/net/richardlord/asteroids/systems/GameManager.as
  15. +10 −6 examples/robotlegs/asteroids/net/richardlord/asteroids/systems/MovementSystem.as
  16. +1 −1 examples/robotlegs/asteroids/net/richardlord/asteroids/systems/RenderSystem.as
  17. +12 −14 examples/starling/asteroids/net/richardlord/asteroids/Asteroids.as
  18. +9 −1 examples/starling/asteroids/net/richardlord/asteroids/EntityCreator.as
  19. +8 −0 examples/starling/asteroids/net/richardlord/asteroids/GameConfig.as
  20. +1 −9 examples/starling/asteroids/net/richardlord/asteroids/components/GameState.as
  21. +10 −0 examples/starling/asteroids/net/richardlord/asteroids/nodes/GameNode.as
  22. +42 −34 examples/starling/asteroids/net/richardlord/asteroids/systems/GameManager.as
  23. +10 −11 examples/starling/asteroids/net/richardlord/asteroids/systems/MovementSystem.as
  24. +14 −17 examples/swiftsuspenders/asteroids/net/richardlord/asteroids/Asteroids.as
  25. +9 −1 examples/swiftsuspenders/asteroids/net/richardlord/asteroids/EntityCreator.as
  26. +8 −0 examples/swiftsuspenders/asteroids/net/richardlord/asteroids/GameConfig.as
  27. +1 −9 examples/swiftsuspenders/asteroids/net/richardlord/asteroids/components/GameState.as
  28. +10 −0 examples/swiftsuspenders/asteroids/net/richardlord/asteroids/nodes/GameNode.as
  29. +40 −32 examples/swiftsuspenders/asteroids/net/richardlord/asteroids/systems/GameManager.as
  30. +10 −6 examples/swiftsuspenders/asteroids/net/richardlord/asteroids/systems/MovementSystem.as
@@ -1,9 +1,7 @@
package net.richardlord.asteroids
{
- import flash.display.DisplayObjectContainer;
import net.richardlord.ash.core.Game;
import net.richardlord.ash.tick.FrameTickProvider;
- import net.richardlord.asteroids.components.GameState;
import net.richardlord.asteroids.systems.BulletAgeSystem;
import net.richardlord.asteroids.systems.CollisionSystem;
import net.richardlord.asteroids.systems.GameManager;
@@ -14,51 +12,46 @@ package net.richardlord.asteroids
import net.richardlord.asteroids.systems.SystemPriorities;
import net.richardlord.input.KeyPoll;
-
+ import flash.display.DisplayObjectContainer;
public class Asteroids
{
private var container : DisplayObjectContainer;
private var game : Game;
private var tickProvider : FrameTickProvider;
- private var gameState : GameState;
private var creator : EntityCreator;
private var keyPoll : KeyPoll;
- private var width : Number;
- private var height : Number;
+ private var config : GameConfig;
public function Asteroids( container : DisplayObjectContainer, width : Number, height : Number )
{
this.container = container;
- this.width = width;
- this.height = height;
- prepare();
+ prepare( width, height );
}
- private function prepare() : void
+ private function prepare( width : Number, height : Number ) : void
{
game = new Game();
- gameState = new GameState( width, height );
creator = new EntityCreator( game );
keyPoll = new KeyPoll( container.stage );
+ config = new GameConfig();
+ config.width = width;
+ config.height = height;
- game.addSystem( new GameManager( gameState, creator ), SystemPriorities.preUpdate );
+ game.addSystem( new GameManager( creator, config ), SystemPriorities.preUpdate );
game.addSystem( new MotionControlSystem( keyPoll ), SystemPriorities.update );
game.addSystem( new GunControlSystem( keyPoll, creator ), SystemPriorities.update );
game.addSystem( new BulletAgeSystem( creator ), SystemPriorities.update );
- game.addSystem( new MovementSystem( gameState ), SystemPriorities.move );
+ game.addSystem( new MovementSystem( config ), SystemPriorities.move );
game.addSystem( new CollisionSystem( creator ), SystemPriorities.resolveCollisions );
game.addSystem( new RenderSystem( container ), SystemPriorities.render );
-
- tickProvider = new FrameTickProvider( container );
+
+ creator.createGame();
}
public function start() : void
{
- gameState.level = 0;
- gameState.lives = 3;
- gameState.points = 0;
-
+ tickProvider = new FrameTickProvider( container );
tickProvider.add( game.update );
tickProvider.start();
}
@@ -1,5 +1,6 @@
package net.richardlord.asteroids
{
+ import net.richardlord.asteroids.components.GameState;
import flash.ui.Keyboard;
import net.richardlord.ash.core.Entity;
import net.richardlord.ash.core.Game;
@@ -16,7 +17,6 @@ package net.richardlord.asteroids
import net.richardlord.asteroids.graphics.BulletView;
import net.richardlord.asteroids.graphics.SpaceshipView;
-
public class EntityCreator
{
private var game : Game;
@@ -30,6 +30,14 @@ package net.richardlord.asteroids
{
game.removeEntity( entity );
}
+
+ public function createGame() : Entity
+ {
+ var gameEntity : Entity = new Entity()
+ .add( new GameState() );
+ game.addEntity( gameEntity );
+ return gameEntity;
+ }
public function createAsteroid( radius : Number, x : Number, y : Number ) : Entity
{
@@ -0,0 +1,8 @@
+package net.richardlord.asteroids
+{
+ public class GameConfig
+ {
+ public var width : Number;
+ public var height : Number;
+ }
+}
@@ -2,16 +2,8 @@ package net.richardlord.asteroids.components
{
public class GameState
{
- public var lives : int = 0;
+ public var lives : int = 3;
public var level : int = 0;
public var points : int = 0;
- public var width : Number = 0;
- public var height : Number = 0;
-
- public function GameState( width : Number = 0, height : Number = 0 )
- {
- this.width = width;
- this.height = height;
- }
}
}
@@ -0,0 +1,10 @@
+package net.richardlord.asteroids.nodes
+{
+ import net.richardlord.ash.core.Node;
+ import net.richardlord.asteroids.components.GameState;
+
+ public class GameNode extends Node
+ {
+ public var state : GameState;
+ }
+}
@@ -1,87 +1,95 @@
package net.richardlord.asteroids.systems
{
- import flash.geom.Point;
import net.richardlord.ash.core.Game;
import net.richardlord.ash.core.NodeList;
import net.richardlord.ash.core.System;
import net.richardlord.asteroids.EntityCreator;
- import net.richardlord.asteroids.components.GameState;
+ import net.richardlord.asteroids.GameConfig;
import net.richardlord.asteroids.nodes.AsteroidCollisionNode;
import net.richardlord.asteroids.nodes.BulletCollisionNode;
+ import net.richardlord.asteroids.nodes.GameNode;
import net.richardlord.asteroids.nodes.SpaceshipCollisionNode;
+ import flash.geom.Point;
public class GameManager extends System
{
- private var gameState : GameState;
+ private var config : GameConfig;
private var creator : EntityCreator;
+ private var gameNodes : NodeList;
private var spaceships : NodeList;
private var asteroids : NodeList;
private var bullets : NodeList;
- public function GameManager( gameState : GameState, creator : EntityCreator )
+ public function GameManager( creator : EntityCreator, config : GameConfig )
{
- this.gameState = gameState;
this.creator = creator;
+ this.config = config;
}
override public function addToGame( game : Game ) : void
{
+ gameNodes = game.getNodeList( GameNode );
spaceships = game.getNodeList( SpaceshipCollisionNode );
asteroids = game.getNodeList( AsteroidCollisionNode );
bullets = game.getNodeList( BulletCollisionNode );
}
override public function update( time : Number ) : void
{
- if( spaceships.empty )
+ var node : GameNode;
+ for( node = gameNodes.head; node; node = node.next )
{
- if( gameState.lives > 0 )
+ if( spaceships.empty )
{
- var newSpaceshipPosition : Point = new Point( gameState.width * 0.5, gameState.height * 0.5 );
- var clearToAddSpaceship : Boolean = true;
- for( var asteroid : AsteroidCollisionNode = asteroids.head; asteroid; asteroid = asteroid.next )
+ if( node.state.lives > 0 )
{
- if( Point.distance( asteroid.position.position, newSpaceshipPosition ) <= asteroid.position.collisionRadius + 50 )
+ var newSpaceshipPosition : Point = new Point( config.width * 0.5, config.height * 0.5 );
+ var clearToAddSpaceship : Boolean = true;
+ for( var asteroid : AsteroidCollisionNode = asteroids.head; asteroid; asteroid = asteroid.next )
{
- clearToAddSpaceship = false;
- break;
+ if( Point.distance( asteroid.position.position, newSpaceshipPosition ) <= asteroid.position.collisionRadius + 50 )
+ {
+ clearToAddSpaceship = false;
+ break;
+ }
+ }
+ if( clearToAddSpaceship )
+ {
+ creator.createSpaceship();
+ node.state.lives--;
}
}
- if( clearToAddSpaceship )
+ else
{
- creator.createSpaceship();
- gameState.lives--;
+ // game over
}
}
- else
- {
- // game over
- }
- }
-
- if( asteroids.empty && bullets.empty && !spaceships.empty )
- {
- // next level
- var spaceship : SpaceshipCollisionNode = spaceships.head;
- gameState.level++;
- var asteroidCount : int = 2 + gameState.level;
- for( var i:int = 0; i < asteroidCount; ++i )
+
+ if( asteroids.empty && bullets.empty && !spaceships.empty )
{
- // check not on top of spaceship
- do
+ // next level
+ var spaceship : SpaceshipCollisionNode = spaceships.head;
+ node.state.level++;
+ var asteroidCount : int = 2 + node.state.level;
+ for( var i:int = 0; i < asteroidCount; ++i )
{
- var position : Point = new Point( Math.random() * gameState.width, Math.random() * gameState.height );
+ // check not on top of spaceship
+ do
+ {
+ var position : Point = new Point( Math.random() * config.width, Math.random() * config.height );
+ }
+ while ( Point.distance( position, spaceship.position.position ) <= 80 );
+ creator.createAsteroid( 30, position.x, position.y );
}
- while ( Point.distance( position, spaceship.position.position ) <= 80 );
- creator.createAsteroid( 30, position.x, position.y );
}
}
}
override public function removeFromGame( game : Game ) : void
{
+ gameNodes = null;
spaceships = null;
asteroids = null;
bullets = null;
@@ -1,19 +1,18 @@
package net.richardlord.asteroids.systems
{
import net.richardlord.ash.tools.ListIteratingSystem;
- import net.richardlord.asteroids.components.GameState;
+ import net.richardlord.asteroids.GameConfig;
import net.richardlord.asteroids.components.Motion;
import net.richardlord.asteroids.components.Position;
import net.richardlord.asteroids.nodes.MovementNode;
public class MovementSystem extends ListIteratingSystem
{
+ private var config : GameConfig;
- private var gameState : GameState;
-
- public function MovementSystem( gameState : GameState)
+ public function MovementSystem( config : GameConfig )
{
- this.gameState = gameState;
+ this.config = config;
super( MovementNode, updateNode );
}
@@ -29,19 +28,19 @@ package net.richardlord.asteroids.systems
position.position.y += motion.velocity.y * time;
if ( position.position.x < 0 )
{
- position.position.x += gameState.width;
+ position.position.x += config.width;
}
- if ( position.position.x > gameState.width )
+ if ( position.position.x > config.width )
{
- position.position.x -= gameState.width;
+ position.position.x -= config.width;
}
if ( position.position.y < 0 )
{
- position.position.y += gameState.height;
+ position.position.y += config.height;
}
- if ( position.position.y > gameState.height )
+ if ( position.position.y > config.height )
{
- position.position.y -= gameState.height;
+ position.position.y -= config.height;
}
position.rotation += motion.angularVelocity * time;
if ( motion.damping > 0 )
@@ -8,7 +8,6 @@ package net.richardlord.asteroids.systems
import net.richardlord.asteroids.components.Display;
import net.richardlord.asteroids.components.Position;
import net.richardlord.asteroids.nodes.RenderNode;
-
public class RenderSystem extends System
{
@@ -1,5 +1,6 @@
package net.richardlord.asteroids
{
+ import net.richardlord.asteroids.components.GameState;
import flash.ui.Keyboard;
import net.richardlord.ash.core.Entity;
import net.richardlord.ash.core.Game;
@@ -16,7 +17,6 @@ package net.richardlord.asteroids
import net.richardlord.asteroids.graphics.BulletView;
import net.richardlord.asteroids.graphics.SpaceshipView;
-
public class EntityCreator
{
private var game : Game;
@@ -30,6 +30,14 @@ package net.richardlord.asteroids
{
game.removeEntity( entity );
}
+
+ public function createGame() : Entity
+ {
+ var gameEntity : Entity = new Entity()
+ .add( new GameState() );
+ game.addEntity( gameEntity );
+ return gameEntity;
+ }
public function createAsteroid( radius : Number, x : Number, y : Number ) : Entity
{
@@ -0,0 +1,8 @@
+package net.richardlord.asteroids
+{
+ public class GameConfig
+ {
+ public var width : Number;
+ public var height : Number;
+ }
+}
Oops, something went wrong.

0 comments on commit 01c87f4

Please sign in to comment.