Skip to content
Browse files

Add asteroids example

  • Loading branch information...
1 parent 1854f31 commit 027c30926d956dc0d4fb82eb2b0d3a6b11eee6c0 @richardlord richardlord committed Oct 14, 2011
Showing with 1,334 additions and 0 deletions.
  1. +17 −0 examples/asteroids/src/net/richardlord/asteroidsember/Main.as
  2. +6 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/Asteroid.as
  3. +7 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/Bullet.as
  4. +9 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/Display.as
  5. +11 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/GameState.as
  6. +13 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/Gun.as
  7. +7 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/GunControls.as
  8. +11 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/Motion.as
  9. +12 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/MotionControls.as
  10. +11 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/Position.as
  11. +6 −0 examples/asteroids/src/net/richardlord/asteroidsember/components/Spaceship.as
  12. +63 −0 examples/asteroids/src/net/richardlord/asteroidsember/game/Asteroids.as
  13. +127 −0 examples/asteroids/src/net/richardlord/asteroidsember/game/EntityCreator.as
  14. +24 −0 examples/asteroids/src/net/richardlord/asteroidsember/graphics/AsteroidView.as
  15. +14 −0 examples/asteroids/src/net/richardlord/asteroidsember/graphics/BulletView.as
  16. +19 −0 examples/asteroids/src/net/richardlord/asteroidsember/graphics/SpaceshipView.as
  17. +24 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/AsteroidCollisionNode.as
  18. +21 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/BulletAgeNode.as
  19. +24 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/BulletCollisionNode.as
  20. +27 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/GunControlNode.as
  21. +27 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/MotionControlNode.as
  22. +24 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/MovementNode.as
  23. +24 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/RenderNode.as
  24. +24 −0 examples/asteroids/src/net/richardlord/asteroidsember/nodes/SpaceshipCollisionNode.as
  25. +12 −0 examples/asteroids/src/net/richardlord/asteroidsember/signals/Move.as
  26. +8 −0 examples/asteroids/src/net/richardlord/asteroidsember/signals/PreUpdate.as
  27. +8 −0 examples/asteroids/src/net/richardlord/asteroidsember/signals/Render.as
  28. +12 −0 examples/asteroids/src/net/richardlord/asteroidsember/signals/ResolveCollisions.as
  29. +12 −0 examples/asteroids/src/net/richardlord/asteroidsember/signals/Update.as
  30. +54 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/BulletAgeSystem.as
  31. +84 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/CollisionSystem.as
  32. +96 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/GameManager.as
  33. +66 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/GunControlSystem.as
  34. +75 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/MotionControlSystem.as
  35. +95 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/MovementSystem.as
  36. +67 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/ProcessManager.as
  37. +65 −0 examples/asteroids/src/net/richardlord/asteroidsember/systems/RenderSystem.as
  38. +128 −0 examples/asteroids/src/net/richardlord/utils/KeyPoll.as
View
17 examples/asteroids/src/net/richardlord/asteroidsember/Main.as
@@ -0,0 +1,17 @@
+package net.richardlord.asteroidsember
+{
+ import flash.display.Sprite;
+ import net.richardlord.asteroidsember.game.Asteroids;
+
+ [SWF(width='600', height='450', frameRate='60', backgroundColor='#000000')]
+
+ public class Main extends Sprite
+ {
+ private var asteroids : Asteroids;
+
+ public function Main()
+ {
+ asteroids = new Asteroids( this );
+ }
+ }
+}
View
6 examples/asteroids/src/net/richardlord/asteroidsember/components/Asteroid.as
@@ -0,0 +1,6 @@
+package net.richardlord.asteroidsember.components
+{
+ public class Asteroid
+ {
+ }
+}
View
7 examples/asteroids/src/net/richardlord/asteroidsember/components/Bullet.as
@@ -0,0 +1,7 @@
+package net.richardlord.asteroidsember.components
+{
+ public class Bullet
+ {
+ public var lifeRemaining : Number;
+ }
+}
View
9 examples/asteroids/src/net/richardlord/asteroidsember/components/Display.as
@@ -0,0 +1,9 @@
+package net.richardlord.asteroidsember.components
+{
+ import flash.display.DisplayObject;
+
+ public class Display
+ {
+ public var displayObject : DisplayObject = null;
+ }
+}
View
11 examples/asteroids/src/net/richardlord/asteroidsember/components/GameState.as
@@ -0,0 +1,11 @@
+package net.richardlord.asteroidsember.components
+{
+ public class GameState
+ {
+ public var lives : int = 0;
+ public var level : int = 0;
+ public var points : int = 0;
+ public var width : Number = 0;
+ public var height : Number = 0;
+ }
+}
View
13 examples/asteroids/src/net/richardlord/asteroidsember/components/Gun.as
@@ -0,0 +1,13 @@
+package net.richardlord.asteroidsember.components
+{
+ import flash.geom.Point;
+
+ public class Gun
+ {
+ public var shooting : Boolean = false;
+ public var offsetFromParent : Point = new Point();
+ public var timeSinceLastShot : Number = 0;
+ public var minimumShotInterval : Number = 0;
+ public var bulletLifetime : Number = 0;
+ }
+}
View
7 examples/asteroids/src/net/richardlord/asteroidsember/components/GunControls.as
@@ -0,0 +1,7 @@
+package net.richardlord.asteroidsember.components
+{
+ public class GunControls
+ {
+ public var trigger : uint = 0;
+ }
+}
View
11 examples/asteroids/src/net/richardlord/asteroidsember/components/Motion.as
@@ -0,0 +1,11 @@
+package net.richardlord.asteroidsember.components
+{
+ import flash.geom.Point;
+
+ public class Motion
+ {
+ public var velocity : Point = new Point();
+ public var angularVelocity : Number = 0;
+ public var damping : Number = 0;
+ }
+}
View
12 examples/asteroids/src/net/richardlord/asteroidsember/components/MotionControls.as
@@ -0,0 +1,12 @@
+package net.richardlord.asteroidsember.components
+{
+ public class MotionControls
+ {
+ public var left : uint = 0;
+ public var right : uint = 0;
+ public var accelerate : uint = 0;
+
+ public var accelerationRate : Number = 0;
+ public var rotationRate : Number = 0;
+ }
+}
View
11 examples/asteroids/src/net/richardlord/asteroidsember/components/Position.as
@@ -0,0 +1,11 @@
+package net.richardlord.asteroidsember.components
+{
+ import flash.geom.Point;
+
+ public class Position
+ {
+ public var position : Point = new Point();
+ public var rotation : Number = 0;
+ public var collisionRadius : Number = 0;
+ }
+}
View
6 examples/asteroids/src/net/richardlord/asteroidsember/components/Spaceship.as
@@ -0,0 +1,6 @@
+package net.richardlord.asteroidsember.components
+{
+ public class Spaceship
+ {
+ }
+}
View
63 examples/asteroids/src/net/richardlord/asteroidsember/game/Asteroids.as
@@ -0,0 +1,63 @@
+package net.richardlord.asteroidsember.game
+{
+ import net.richardlord.asteroidsember.components.GameState;
+ import net.richardlord.asteroidsember.signals.*;
+ import net.richardlord.asteroidsember.systems.*;
+ import net.richardlord.utils.KeyPoll;
+
+ import com.tomseysdavies.ember.base.Game;
+
+ import flash.display.DisplayObjectContainer;
+
+ public class Asteroids extends Game
+ {
+ [Inject]
+ public var entityCreator : EntityCreator;
+
+ [Inject]
+ public var gameState : GameState;
+
+ public function Asteroids( contextView : DisplayObjectContainer = null )
+ {
+ super( contextView );
+ }
+
+ override protected function startUp() : void
+ {
+ injector.injectInto( this );
+
+ gameState.level = 0;
+ gameState.lives = 3;
+ gameState.points = 0;
+ gameState.width = contextView.stage.stageWidth;
+ gameState.height = contextView.stage.stageHeight;
+
+ addSystems();
+ }
+
+ override protected function mapInjectors() : void
+ {
+ super.mapInjectors();
+ injector.mapSingleton( EntityCreator );
+ injector.mapSingleton( GameState );
+ injector.mapSingleton( PreUpdate );
+ injector.mapSingleton( Update );
+ injector.mapSingleton( Move );
+ injector.mapSingleton( ResolveCollisions );
+ injector.mapSingleton( Render );
+ injector.mapValue( KeyPoll, new KeyPoll( contextView.stage ) );
+ }
+
+ private function addSystems() : void
+ {
+ systemManager.addSystem( GameManager );
+ systemManager.addSystem( MotionControlSystem );
+ systemManager.addSystem( GunControlSystem );
+ systemManager.addSystem( BulletAgeSystem );
+ systemManager.addSystem( MovementSystem );
+ systemManager.addSystem( CollisionSystem );
+ systemManager.addSystem( RenderSystem );
+ systemManager.addSystem( ProcessManager );
+ }
+ }
+}
View
127 examples/asteroids/src/net/richardlord/asteroidsember/game/EntityCreator.as
@@ -0,0 +1,127 @@
+package net.richardlord.asteroidsember.game
+{
+ import net.richardlord.asteroidsember.components.*;
+ import net.richardlord.asteroidsember.graphics.AsteroidView;
+ import net.richardlord.asteroidsember.graphics.BulletView;
+ import net.richardlord.asteroidsember.graphics.SpaceshipView;
+
+ import com.tomseysdavies.ember.core.IEntity;
+ import com.tomseysdavies.ember.core.IEntityManager;
+
+ import flash.ui.Keyboard;
+
+ public class EntityCreator
+ {
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ public function destroyEntity( entityId : String ) : void
+ {
+ if ( !entityManager.hasEntity( entityId ) )
+ {
+ return;
+ }
+ if ( entityManager.hasComponent( entityId, Display ) )
+ {
+ var display : Display = entityManager.getComponent( entityId, Display );
+ display.displayObject.parent.removeChild( display.displayObject );
+ }
+ entityManager.removeEntity( entityId );
+ }
+
+ public function createAsteroid( radius : Number, x : Number, y : Number ) : IEntity
+ {
+ var asteroid : IEntity = entityManager.createEntity();
+
+ asteroid.addComponent( new Asteroid() );
+
+ var position : Position = new Position();
+ position.position.x = x;
+ position.position.y = y;
+ position.collisionRadius = radius;
+ asteroid.addComponent( position );
+
+ var motion : Motion = new Motion();
+ motion.angularVelocity = Math.random() * 2 - 1;
+ motion.velocity.x = ( Math.random() - 0.5 ) * 4 * ( 50 - radius );
+ motion.velocity.y = ( Math.random() - 0.5 ) * 4 * ( 50 - radius );
+ asteroid.addComponent( motion );
+
+ var display : Display = new Display();
+ display.displayObject = new AsteroidView( radius );
+ asteroid.addComponent( display );
+
+ return asteroid;
+ }
+
+ public function createSpaceship() : IEntity
+ {
+ var spaceship : IEntity = entityManager.createEntity();
+
+ spaceship.addComponent( new Spaceship() );
+
+ var position : Position = new Position();
+ position.position.x = 300;
+ position.position.y = 225;
+ position.collisionRadius = 6;
+ spaceship.addComponent( position );
+
+ var motion : Motion = new Motion();
+ motion.damping = 15;
+ spaceship.addComponent( motion );
+
+ var motionControls : MotionControls = new MotionControls();
+ motionControls.left = Keyboard.LEFT;
+ motionControls.right = Keyboard.RIGHT;
+ motionControls.accelerate = Keyboard.UP;
+ motionControls.accelerationRate = 100;
+ motionControls.rotationRate = 3;
+ spaceship.addComponent( motionControls );
+
+ var gun : Gun = new Gun();
+ gun.minimumShotInterval = 0.3;
+ gun.offsetFromParent.x = 8;
+ gun.offsetFromParent.y = 0;
+ gun.bulletLifetime = 2;
+ spaceship.addComponent( gun );
+
+ var gunControls : GunControls = new GunControls();
+ gunControls.trigger = Keyboard.Z;
+ spaceship.addComponent( gunControls );
+
+ var display : Display = new Display();
+ display.displayObject = new SpaceshipView();
+ spaceship.addComponent( display );
+
+ return spaceship;
+ }
+
+ public function createUserBullet( gun : Gun, parentPosition : Position ) : IEntity
+ {
+ var bullet : IEntity = entityManager.createEntity();
+
+ var userBullet : Bullet = new Bullet();
+ userBullet.lifeRemaining = gun.bulletLifetime;
+ bullet.addComponent( userBullet );
+
+ var cos : Number = Math.cos( parentPosition.rotation );
+ var sin : Number = Math.sin( parentPosition.rotation );
+
+ var bulletPosition : Position = new Position();
+ bulletPosition.position.x = cos * gun.offsetFromParent.x - sin * gun.offsetFromParent.y + parentPosition.position.x;
+ bulletPosition.position.y = sin * gun.offsetFromParent.x + cos * gun.offsetFromParent.y + parentPosition.position.y;
+ bullet.addComponent( bulletPosition );
+
+ var motion : Motion = new Motion();
+ motion.velocity.x = cos * 150;
+ motion.velocity.y = sin * 150;
+ bullet.addComponent( motion );
+
+ var display : Display = new Display();
+ display.displayObject = new BulletView();
+ bullet.addComponent( display );
+
+ return bullet;
+ }
+ }
+}
View
24 examples/asteroids/src/net/richardlord/asteroidsember/graphics/AsteroidView.as
@@ -0,0 +1,24 @@
+package net.richardlord.asteroidsember.graphics
+{
+ import flash.display.Shape;
+
+ public class AsteroidView extends Shape
+ {
+ public function AsteroidView( radius : Number )
+ {
+ var angle : Number = 0;
+ graphics.beginFill( 0xFFFFFF );
+ graphics.moveTo( radius, 0 );
+ while( angle < Math.PI * 2 )
+ {
+ var length : Number = ( 0.75 + Math.random() * 0.25 ) * radius;
+ var posX : Number = Math.cos( angle ) * length;
+ var posY : Number = Math.sin( angle ) * length;
+ graphics.lineTo( posX, posY );
+ angle += Math.random() * 0.5;
+ }
+ graphics.lineTo( radius, 0 );
+ graphics.endFill();
+ }
+ }
+}
View
14 examples/asteroids/src/net/richardlord/asteroidsember/graphics/BulletView.as
@@ -0,0 +1,14 @@
+package net.richardlord.asteroidsember.graphics
+{
+ import flash.display.Shape;
+
+ public class BulletView extends Shape
+ {
+ public function BulletView()
+ {
+ graphics.beginFill( 0xFFFFFF );
+ graphics.drawCircle( 0, 0, 2 );
+ graphics.endFill();
+ }
+ }
+}
View
19 examples/asteroids/src/net/richardlord/asteroidsember/graphics/SpaceshipView.as
@@ -0,0 +1,19 @@
+package net.richardlord.asteroidsember.graphics
+{
+ import flash.display.Shape;
+
+ public class SpaceshipView extends Shape
+ {
+ public function SpaceshipView()
+ {
+ graphics.beginFill( 0xFFFFFF );
+ graphics.moveTo( 8, 0 );
+ graphics.lineTo( -7, 7 );
+ graphics.lineTo( -4, 0 );
+ graphics.lineTo( -7, -7 );
+ graphics.lineTo( 10, 0 );
+ graphics.endFill();
+ }
+
+ }
+}
View
24 examples/asteroids/src/net/richardlord/asteroidsember/nodes/AsteroidCollisionNode.as
@@ -0,0 +1,24 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Asteroid;
+ import net.richardlord.asteroidsember.components.Position;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class AsteroidCollisionNode extends Node
+ {
+ public static const componentClasses : Array = [ Asteroid, Position ];
+
+ public var asteroid : Asteroid;
+ public var position : Position;
+
+ public function AsteroidCollisionNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ asteroid = components[ Asteroid ];
+ position = components[ Position ];
+ }
+ }
+}
View
21 examples/asteroids/src/net/richardlord/asteroidsember/nodes/BulletAgeNode.as
@@ -0,0 +1,21 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Bullet;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class BulletAgeNode extends Node
+ {
+ public static const componentClasses : Array = [ Bullet ];
+
+ public var bullet : Bullet;
+
+ public function BulletAgeNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ bullet = components[ Bullet ];
+ }
+ }
+}
View
24 examples/asteroids/src/net/richardlord/asteroidsember/nodes/BulletCollisionNode.as
@@ -0,0 +1,24 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Bullet;
+ import net.richardlord.asteroidsember.components.Position;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class BulletCollisionNode extends Node
+ {
+ public static const componentClasses : Array = [ Bullet, Position ];
+
+ public var bullet : Bullet;
+ public var position : Position;
+
+ public function BulletCollisionNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ bullet = components[ Bullet ];
+ position = components[ Position ];
+ }
+ }
+}
View
27 examples/asteroids/src/net/richardlord/asteroidsember/nodes/GunControlNode.as
@@ -0,0 +1,27 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Gun;
+ import net.richardlord.asteroidsember.components.GunControls;
+ import net.richardlord.asteroidsember.components.Position;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class GunControlNode extends Node
+ {
+ public static const componentClasses : Array = [ GunControls, Gun, Position ];
+
+ public var control : GunControls;
+ public var gun : Gun;
+ public var position : Position;
+
+ public function GunControlNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ control = components[ GunControls ];
+ gun = components[ Gun ];
+ position = components[ Position ];
+ }
+ }
+}
View
27 examples/asteroids/src/net/richardlord/asteroidsember/nodes/MotionControlNode.as
@@ -0,0 +1,27 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Motion;
+ import net.richardlord.asteroidsember.components.MotionControls;
+ import net.richardlord.asteroidsember.components.Position;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class MotionControlNode extends Node
+ {
+ public static const componentClasses : Array = [ MotionControls, Position, Motion ];
+
+ public var control : MotionControls;
+ public var position : Position;
+ public var motion : Motion;
+
+ public function MotionControlNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ control = components[ MotionControls ];
+ position = components[ Position ];
+ motion = components[ Motion ];
+ }
+ }
+}
View
24 examples/asteroids/src/net/richardlord/asteroidsember/nodes/MovementNode.as
@@ -0,0 +1,24 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Motion;
+ import net.richardlord.asteroidsember.components.Position;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class MovementNode extends Node
+ {
+ public static const componentClasses : Array = [ Position, Motion ];
+
+ public var position : Position;
+ public var motion : Motion;
+
+ public function MovementNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ position = components[ Position ];
+ motion = components[ Motion ];
+ }
+ }
+}
View
24 examples/asteroids/src/net/richardlord/asteroidsember/nodes/RenderNode.as
@@ -0,0 +1,24 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Display;
+ import net.richardlord.asteroidsember.components.Position;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class RenderNode extends Node
+ {
+ public static const componentClasses : Array = [ Position, Display ];
+
+ public var position : Position;
+ public var display : Display;
+
+ public function RenderNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ position = components[ Position ];
+ display = components[ Display ];
+ }
+ }
+}
View
24 examples/asteroids/src/net/richardlord/asteroidsember/nodes/SpaceshipCollisionNode.as
@@ -0,0 +1,24 @@
+package net.richardlord.asteroidsember.nodes
+{
+ import net.richardlord.asteroidsember.components.Position;
+ import net.richardlord.asteroidsember.components.Spaceship;
+
+ import com.tomseysdavies.ember.base.Node;
+
+ import flash.utils.Dictionary;
+
+ public class SpaceshipCollisionNode extends Node
+ {
+ public static const componentClasses : Array = [ Spaceship, Position ];
+
+ public var spaceship : Spaceship;
+ public var position : Position;
+
+ public function SpaceshipCollisionNode( entityID : String, components : Dictionary )
+ {
+ super( entityID, components );
+ spaceship = components[ Spaceship ];
+ position = components[ Position ];
+ }
+ }
+}
View
12 examples/asteroids/src/net/richardlord/asteroidsember/signals/Move.as
@@ -0,0 +1,12 @@
+package net.richardlord.asteroidsember.signals
+{
+ import org.osflash.signals.Signal;
+
+ public class Move extends Signal
+ {
+ public function Move()
+ {
+ super( Number );
+ }
+ }
+}
View
8 examples/asteroids/src/net/richardlord/asteroidsember/signals/PreUpdate.as
@@ -0,0 +1,8 @@
+package net.richardlord.asteroidsember.signals
+{
+ import org.osflash.signals.Signal;
+
+ public class PreUpdate extends Signal
+ {
+ }
+}
View
8 examples/asteroids/src/net/richardlord/asteroidsember/signals/Render.as
@@ -0,0 +1,8 @@
+package net.richardlord.asteroidsember.signals
+{
+ import org.osflash.signals.Signal;
+
+ public class Render extends Signal
+ {
+ }
+}
View
12 examples/asteroids/src/net/richardlord/asteroidsember/signals/ResolveCollisions.as
@@ -0,0 +1,12 @@
+package net.richardlord.asteroidsember.signals
+{
+ import org.osflash.signals.Signal;
+
+ public class ResolveCollisions extends Signal
+ {
+ public function ResolveCollisions()
+ {
+ super( Number );
+ }
+ }
+}
View
12 examples/asteroids/src/net/richardlord/asteroidsember/signals/Update.as
@@ -0,0 +1,12 @@
+package net.richardlord.asteroidsember.signals
+{
+ import org.osflash.signals.Signal;
+
+ public class Update extends Signal
+ {
+ public function Update()
+ {
+ super( Number );
+ }
+ }
+}
View
54 examples/asteroids/src/net/richardlord/asteroidsember/systems/BulletAgeSystem.as
@@ -0,0 +1,54 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.components.Bullet;
+ import net.richardlord.asteroidsember.game.EntityCreator;
+ import net.richardlord.asteroidsember.nodes.BulletAgeNode;
+ import net.richardlord.asteroidsember.signals.Update;
+
+ import com.tomseysdavies.ember.core.IEntityManager;
+ import com.tomseysdavies.ember.core.IFamily;
+ import com.tomseysdavies.ember.core.ISystem;
+
+ public class BulletAgeSystem implements ISystem
+ {
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ [Inject]
+ public var entityCreator : EntityCreator;
+
+ [Inject]
+ public var tick : Update;
+
+ private var family : IFamily;
+
+ public function onRegister() : void
+ {
+ family = entityManager.getEntityFamily( BulletAgeNode );
+ tick.add( update );
+ }
+
+ private function update( time : Number ) : void
+ {
+ var node : BulletAgeNode;
+ var bullet : Bullet;
+
+ for ( family.start(); family.hasNext; family.next() )
+ {
+ node = family.currentNode;
+ bullet = node.bullet;
+
+ bullet.lifeRemaining -= time;
+ if ( bullet.lifeRemaining <= 0 )
+ {
+ entityCreator.destroyEntity( node.entityID );
+ }
+ }
+ }
+
+ public function dispose() : void
+ {
+ tick.remove( update );
+ }
+ }
+}
View
84 examples/asteroids/src/net/richardlord/asteroidsember/systems/CollisionSystem.as
@@ -0,0 +1,84 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.game.EntityCreator;
+ import net.richardlord.asteroidsember.nodes.AsteroidCollisionNode;
+ import net.richardlord.asteroidsember.nodes.SpaceshipCollisionNode;
+ import net.richardlord.asteroidsember.nodes.BulletCollisionNode;
+ import net.richardlord.asteroidsember.signals.ResolveCollisions;
+
+ import com.tomseysdavies.ember.core.IEntityManager;
+ import com.tomseysdavies.ember.core.IFamily;
+ import com.tomseysdavies.ember.core.ISystem;
+
+ import flash.geom.Point;
+
+ public class CollisionSystem implements ISystem
+ {
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ [Inject]
+ public var entityCreator : EntityCreator;
+
+ [Inject]
+ public var tick : ResolveCollisions;
+
+ private var spaceships : IFamily;
+ private var asteroids : IFamily;
+ private var userBullets : IFamily;
+
+ public function onRegister() : void
+ {
+ spaceships = entityManager.getEntityFamily( SpaceshipCollisionNode );
+ asteroids = entityManager.getEntityFamily( AsteroidCollisionNode );
+ userBullets = entityManager.getEntityFamily( BulletCollisionNode );
+ tick.add( update );
+ }
+
+ private function update( time : Number ) : void
+ {
+ var bullet : BulletCollisionNode;
+ var asteroid : AsteroidCollisionNode;
+ var spaceship : SpaceshipCollisionNode;
+
+ for ( userBullets.start(); userBullets.hasNext; userBullets.next() )
+ {
+ bullet = userBullets.currentNode;
+ for ( asteroids.start(); asteroids.hasNext; asteroids.next() )
+ {
+ asteroid = asteroids.currentNode;
+ if ( Point.distance( asteroid.position.position, bullet.position.position ) <= asteroid.position.collisionRadius )
+ {
+ entityCreator.destroyEntity( bullet.entityID );
+ if ( asteroid.position.collisionRadius > 10 )
+ {
+ entityCreator.createAsteroid( asteroid.position.collisionRadius - 10, asteroid.position.position.x + Math.random() * 10 - 5, asteroid.position.position.y + Math.random() * 10 - 5 );
+ entityCreator.createAsteroid( asteroid.position.collisionRadius - 10, asteroid.position.position.x + Math.random() * 10 - 5, asteroid.position.position.y + Math.random() * 10 - 5 );
+ }
+ entityCreator.destroyEntity( asteroid.entityID );
+ break;
+ }
+ }
+ }
+
+ for ( spaceships.start(); spaceships.hasNext; spaceships.next() )
+ {
+ spaceship = spaceships.currentNode;
+ for ( asteroids.start(); asteroids.hasNext; asteroids.next() )
+ {
+ asteroid = asteroids.currentNode;
+ if ( Point.distance( asteroid.position.position, spaceship.position.position ) <= asteroid.position.collisionRadius + spaceship.position.collisionRadius )
+ {
+ entityCreator.destroyEntity( spaceship.entityID );
+ break;
+ }
+ }
+ }
+ }
+
+ public function dispose() : void
+ {
+ tick.remove( update );
+ }
+ }
+}
View
96 examples/asteroids/src/net/richardlord/asteroidsember/systems/GameManager.as
@@ -0,0 +1,96 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.components.GameState;
+ import net.richardlord.asteroidsember.game.EntityCreator;
+ import net.richardlord.asteroidsember.nodes.AsteroidCollisionNode;
+ import net.richardlord.asteroidsember.nodes.SpaceshipCollisionNode;
+ import net.richardlord.asteroidsember.nodes.BulletCollisionNode;
+ import net.richardlord.asteroidsember.signals.PreUpdate;
+
+ import com.tomseysdavies.ember.core.IEntityManager;
+ import com.tomseysdavies.ember.core.IFamily;
+ import com.tomseysdavies.ember.core.ISystem;
+
+ import flash.geom.Point;
+
+ public class GameManager implements ISystem
+ {
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ [Inject]
+ public var entityCreator : EntityCreator;
+
+ [Inject]
+ public var gameState : GameState;
+
+ [Inject]
+ public var tick : PreUpdate;
+
+ private var spaceships : IFamily;
+ private var asteroids : IFamily;
+ private var bullets : IFamily;
+
+ public function onRegister() : void
+ {
+ spaceships = entityManager.getEntityFamily( SpaceshipCollisionNode );
+ asteroids = entityManager.getEntityFamily( AsteroidCollisionNode );
+ bullets = entityManager.getEntityFamily( BulletCollisionNode );
+ tick.add( update );
+ }
+
+ private function update() : void
+ {
+ if( spaceships.empty )
+ {
+ if( gameState.lives > 0 )
+ {
+ var newSpaceshipPosition : Point = new Point( gameState.width * 0.5, gameState.height * 0.5 );
+ var clearToAddSpaceship : Boolean = true;
+ for( asteroids.start(); asteroids.hasNext; asteroids.next() )
+ {
+ var asteroid : AsteroidCollisionNode = asteroids.currentNode;
+ if( Point.distance( asteroid.position.position, newSpaceshipPosition ) <= asteroid.position.collisionRadius + 50 )
+ {
+ clearToAddSpaceship = false;
+ break;
+ }
+ }
+ if( clearToAddSpaceship )
+ {
+ entityCreator.createSpaceship();
+ gameState.lives--;
+ }
+ }
+ else
+ {
+ // game over
+ }
+ }
+
+ if( asteroids.empty && bullets.empty && !spaceships.empty )
+ {
+ // next level
+ spaceships.start();
+ var spaceship : SpaceshipCollisionNode = spaceships.currentNode;
+ gameState.level++;
+ var asteroidCount : int = 2 + gameState.level;
+ for( var i:int = 0; i < asteroidCount; ++i )
+ {
+ // check not on top of spaceship
+ do
+ {
+ var position : Point = new Point( Math.random() * gameState.width, Math.random() * gameState.height );
+ }
+ while ( Point.distance( position, spaceship.position.position ) <= 80 );
+ entityCreator.createAsteroid( 30, position.x, position.y );
+ }
+ }
+ }
+
+ public function dispose() : void
+ {
+ tick.remove( update );
+ }
+ }
+}
View
66 examples/asteroids/src/net/richardlord/asteroidsember/systems/GunControlSystem.as
@@ -0,0 +1,66 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.components.Gun;
+ import net.richardlord.asteroidsember.components.GunControls;
+ import net.richardlord.asteroidsember.components.Position;
+ import net.richardlord.asteroidsember.game.EntityCreator;
+ import net.richardlord.asteroidsember.nodes.GunControlNode;
+ import net.richardlord.asteroidsember.signals.Update;
+ import net.richardlord.utils.KeyPoll;
+
+ import com.tomseysdavies.ember.core.IEntityManager;
+ import com.tomseysdavies.ember.core.IFamily;
+ import com.tomseysdavies.ember.core.ISystem;
+
+ public class GunControlSystem implements ISystem
+ {
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ [Inject]
+ public var tick : Update;
+
+ [Inject]
+ public var entityCreator : EntityCreator;
+
+ [Inject]
+ public var keyPoll : KeyPoll;
+
+ private var family : IFamily;
+
+ public function onRegister() : void
+ {
+ family = entityManager.getEntityFamily( GunControlNode );
+ tick.add( update );
+ }
+
+ private function update( time : Number ) : void
+ {
+ var node : GunControlNode;
+ var control : GunControls;
+ var position : Position;
+ var gun : Gun;
+
+ for ( family.start(); family.hasNext; family.next() )
+ {
+ node = family.currentNode;
+ control = node.control;
+ gun = node.gun;
+ position = node.position;
+
+ gun.shooting = keyPoll.isDown( control.trigger );
+ gun.timeSinceLastShot += time;
+ if ( gun.shooting && gun.timeSinceLastShot >= gun.minimumShotInterval )
+ {
+ entityCreator.createUserBullet( gun, position );
+ gun.timeSinceLastShot = 0;
+ }
+ }
+ }
+
+ public function dispose() : void
+ {
+ tick.remove( update );
+ }
+ }
+}
View
75 examples/asteroids/src/net/richardlord/asteroidsember/systems/MotionControlSystem.as
@@ -0,0 +1,75 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.components.Motion;
+ import net.richardlord.asteroidsember.components.MotionControls;
+ import net.richardlord.asteroidsember.components.Position;
+ import net.richardlord.asteroidsember.nodes.MotionControlNode;
+ import net.richardlord.asteroidsember.signals.Update;
+ import net.richardlord.utils.KeyPoll;
+
+ import com.tomseysdavies.ember.core.IEntityManager;
+ import com.tomseysdavies.ember.core.IFamily;
+ import com.tomseysdavies.ember.core.ISystem;
+
+ import flash.display.DisplayObjectContainer;
+
+ public class MotionControlSystem implements ISystem
+ {
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ [Inject]
+ public var tick : Update;
+
+ [Inject]
+ public var contextView : DisplayObjectContainer;
+
+ [Inject]
+ public var keyPoll : KeyPoll;
+
+ private var family : IFamily;
+
+ public function onRegister() : void
+ {
+ family = entityManager.getEntityFamily( MotionControlNode );
+ tick.add( update );
+ }
+
+ private function update( time : Number ) : void
+ {
+ var node : MotionControlNode;
+ var control : MotionControls;
+ var position : Position;
+ var motion : Motion;
+
+ for ( family.start(); family.hasNext; family.next() )
+ {
+ node = family.currentNode;
+ control = node.control;
+ position = node.position;
+ motion = node.motion;
+
+ if ( keyPoll.isDown( control.left ) )
+ {
+ position.rotation -= control.rotationRate * time;
+ }
+
+ if ( keyPoll.isDown( control.right ) )
+ {
+ position.rotation += control.rotationRate * time;
+ }
+
+ if ( keyPoll.isDown( control.accelerate ) )
+ {
+ motion.velocity.x += Math.cos( position.rotation ) * control.accelerationRate * time;
+ motion.velocity.y += Math.sin( position.rotation ) * control.accelerationRate * time;
+ }
+ }
+ }
+
+ public function dispose() : void
+ {
+ tick.remove( update );
+ }
+ }
+}
View
95 examples/asteroids/src/net/richardlord/asteroidsember/systems/MovementSystem.as
@@ -0,0 +1,95 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.components.Motion;
+ import net.richardlord.asteroidsember.components.Position;
+ import net.richardlord.asteroidsember.nodes.MovementNode;
+ import net.richardlord.asteroidsember.signals.Move;
+
+ import com.tomseysdavies.ember.core.IEntityManager;
+ import com.tomseysdavies.ember.core.IFamily;
+ import com.tomseysdavies.ember.core.ISystem;
+
+ public class MovementSystem implements ISystem
+ {
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ [Inject]
+ public var tick : Move;
+
+ private var family : IFamily;
+
+ public function onRegister() : void
+ {
+ family = entityManager.getEntityFamily( MovementNode );
+ tick.add( update );
+ }
+
+ private function update( time : Number ) : void
+ {
+ var node : MovementNode;
+ var position : Position;
+ var motion : Motion;
+
+ for ( family.start(); family.hasNext; family.next() )
+ {
+ node = family.currentNode;
+ position = node.position;
+ motion = node.motion;
+ position.position.x += motion.velocity.x * time;
+ position.position.y += motion.velocity.y * time;
+ if ( position.position.x < 0 )
+ {
+ position.position.x += 600;
+ }
+ if ( position.position.x > 600 )
+ {
+ position.position.x -= 600;
+ }
+ if ( position.position.y < 0 )
+ {
+ position.position.y += 450;
+ }
+ if ( position.position.y > 450 )
+ {
+ position.position.y -= 450;
+ }
+ position.rotation += motion.angularVelocity * time;
+ if ( motion.damping > 0 )
+ {
+ var xDamp : Number = Math.abs( Math.cos( position.rotation ) * motion.damping * time );
+ var yDamp : Number = Math.abs( Math.sin( position.rotation ) * motion.damping * time );
+ if ( motion.velocity.x > xDamp )
+ {
+ motion.velocity.x -= xDamp;
+ }
+ else if ( motion.velocity.x < -xDamp )
+ {
+ motion.velocity.x += xDamp;
+ }
+ else
+ {
+ motion.velocity.x = 0;
+ }
+ if ( motion.velocity.y > yDamp )
+ {
+ motion.velocity.y -= yDamp;
+ }
+ else if ( motion.velocity.y < -yDamp )
+ {
+ motion.velocity.y += yDamp;
+ }
+ else
+ {
+ motion.velocity.y = 0;
+ }
+ }
+ }
+ }
+
+ public function dispose() : void
+ {
+ tick.remove( update );
+ }
+ }
+}
View
67 examples/asteroids/src/net/richardlord/asteroidsember/systems/ProcessManager.as
@@ -0,0 +1,67 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.signals.Move;
+ import net.richardlord.asteroidsember.signals.PreUpdate;
+ import net.richardlord.asteroidsember.signals.Render;
+ import net.richardlord.asteroidsember.signals.ResolveCollisions;
+ import net.richardlord.asteroidsember.signals.Update;
+
+ import com.tomseysdavies.ember.core.ISystem;
+
+ import flash.display.DisplayObjectContainer;
+ import flash.events.Event;
+ import flash.utils.getTimer;
+
+ public class ProcessManager implements ISystem
+ {
+ private static const MAX_FRAME_TIME : Number = 0.05;
+
+ [Inject]
+ public var preUpdate : PreUpdate;
+
+ [Inject]
+ public var update : Update;
+
+ [Inject]
+ public var move : Move;
+
+ [Inject]
+ public var resolveCollisions : ResolveCollisions;
+
+ [Inject]
+ public var render : Render;
+
+ [Inject]
+ public var viewContext : DisplayObjectContainer;
+
+ private var time : uint;
+
+ public function onRegister() : void
+ {
+ time = getTimer();
+ viewContext.addEventListener( Event.ENTER_FRAME, frameUpdate );
+ }
+
+ private function frameUpdate( event : Event ) : void
+ {
+ var oldTime : uint = time;
+ time = getTimer();
+ var frameTime : Number = ( time - oldTime ) / 1000;
+ if( frameTime > MAX_FRAME_TIME )
+ {
+ frameTime = MAX_FRAME_TIME;
+ }
+
+ preUpdate.dispatch();
+ update.dispatch( frameTime );
+ move.dispatch( frameTime );
+ resolveCollisions.dispatch( frameTime );
+ render.dispatch();
+ }
+
+ public function dispose() : void
+ {
+ viewContext.removeEventListener( Event.ENTER_FRAME, frameUpdate );
+ }
+ }
+}
View
65 examples/asteroids/src/net/richardlord/asteroidsember/systems/RenderSystem.as
@@ -0,0 +1,65 @@
+package net.richardlord.asteroidsember.systems
+{
+ import net.richardlord.asteroidsember.components.Display;
+ import net.richardlord.asteroidsember.components.Position;
+ import net.richardlord.asteroidsember.nodes.RenderNode;
+ import net.richardlord.asteroidsember.signals.Render;
+
+ import com.tomseysdavies.ember.core.IEntityManager;
+ import com.tomseysdavies.ember.core.IFamily;
+ import com.tomseysdavies.ember.core.ISystem;
+
+ import flash.display.DisplayObject;
+ import flash.display.DisplayObjectContainer;
+
+
+
+ public class RenderSystem implements ISystem
+ {
+ [Inject]
+ public var contextView : DisplayObjectContainer;
+
+ [Inject]
+ public var entityManager : IEntityManager;
+
+ [Inject]
+ public var tick : Render;
+
+ private var family : IFamily;
+
+ public function onRegister() : void
+ {
+ family = entityManager.getEntityFamily( RenderNode );
+ tick.add( render );
+ }
+
+ public function render() : void
+ {
+ var node : RenderNode;
+ var position : Position;
+ var display : Display;
+ var displayObject : DisplayObject;
+
+ for( family.start(); family.hasNext; family.next() )
+ {
+ node = family.currentNode;
+ display = node.display;
+ displayObject = display.displayObject;
+ position = node.position;
+
+ if( !displayObject.parent )
+ {
+ contextView.addChild( displayObject );
+ }
+ displayObject.x = position.position.x;
+ displayObject.y = position.position.y;
+ displayObject.rotation = position.rotation * 180 / Math.PI;
+ }
+ }
+
+ public function dispose() : void
+ {
+ tick.remove( render );
+ }
+ }
+}
View
128 examples/asteroids/src/net/richardlord/utils/KeyPoll.as
@@ -0,0 +1,128 @@
+/*
+ * Author: Richard Lord
+ * Copyright (c) Big Room Ventures Ltd. 2007
+ * Version: 1.0.2
+ *
+ * Licence Agreement
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package net.richardlord.utils
+{
+ import flash.events.KeyboardEvent;
+ import flash.events.Event;
+ import flash.display.DisplayObject;
+ import flash.utils.ByteArray;
+
+ /**
+ * <p>Games often need to get the current state of various keys in order to respond to user input.
+ * This is not the same as responding to key down and key up events, but is rather a case of discovering
+ * if a particular key is currently pressed.</p>
+ *
+ * <p>In Actionscript 2 this was a simple matter of calling Key.isDown() with the appropriate key code.
+ * But in Actionscript 3 Key.isDown no longer exists and the only intrinsic way to react to the keyboard
+ * is via the keyUp and keyDown events.</p>
+ *
+ * <p>The KeyPoll class rectifies this. It has isDown and isUp methods, each taking a key code as a
+ * parameter and returning a Boolean.</p>
+ */
+ public class KeyPoll
+ {
+ private var states:ByteArray;
+ private var dispObj:DisplayObject;
+
+ /**
+ * Constructor
+ *
+ * @param displayObj a display object on which to test listen for keyboard events. To catch all key events use the stage.
+ */
+ public function KeyPoll( displayObj:DisplayObject )
+ {
+ states = new ByteArray();
+ states.writeUnsignedInt( 0 );
+ states.writeUnsignedInt( 0 );
+ states.writeUnsignedInt( 0 );
+ states.writeUnsignedInt( 0 );
+ states.writeUnsignedInt( 0 );
+ states.writeUnsignedInt( 0 );
+ states.writeUnsignedInt( 0 );
+ states.writeUnsignedInt( 0 );
+ dispObj = displayObj;
+ dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
+ dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true );
+ dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true );
+ dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true );
+ }
+
+ private function keyDownListener( ev:KeyboardEvent ):void
+ {
+ states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7);
+ }
+
+ private function keyUpListener( ev:KeyboardEvent ):void
+ {
+ states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7));
+ }
+
+ private function activateListener( ev:Event ):void
+ {
+ for( var i:int = 0; i < 8; ++i )
+ {
+ states[ i ] = 0;
+ }
+ }
+
+ private function deactivateListener( ev:Event ):void
+ {
+ for( var i:int = 0; i < 8; ++i )
+ {
+ states[ i ] = 0;
+ }
+ }
+
+ /**
+ * To test whether a key is down.
+ *
+ * @param keyCode code for the key to test.
+ *
+ * @return true if the key is down, false otherwise.
+ *
+ * @see isUp
+ */
+ public function isDown( keyCode:uint ):Boolean
+ {
+ return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0;
+ }
+
+ /**
+ * To test whetrher a key is up.
+ *
+ * @param keyCode code for the key to test.
+ *
+ * @return true if the key is up, false otherwise.
+ *
+ * @see isDown
+ */
+ public function isUp( keyCode:uint ):Boolean
+ {
+ return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0;
+ }
+ }
+}

0 comments on commit 027c309

Please sign in to comment.
Something went wrong with that request. Please try again.