Permalink
Browse files

Add asteroids example

  • Loading branch information...
richardlord committed Oct 14, 2011
1 parent 1854f31 commit 027c30926d956dc0d4fb82eb2b0d3a6b11eee6c0
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
@@ -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 );
+ }
+ }
+}
@@ -0,0 +1,6 @@
+package net.richardlord.asteroidsember.components
+{
+ public class Asteroid
+ {
+ }
+}
@@ -0,0 +1,7 @@
+package net.richardlord.asteroidsember.components
+{
+ public class Bullet
+ {
+ public var lifeRemaining : Number;
+ }
+}
@@ -0,0 +1,9 @@
+package net.richardlord.asteroidsember.components
+{
+ import flash.display.DisplayObject;
+
+ public class Display
+ {
+ public var displayObject : DisplayObject = null;
+ }
+}
@@ -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;
+ }
+}
@@ -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;
+ }
+}
@@ -0,0 +1,7 @@
+package net.richardlord.asteroidsember.components
+{
+ public class GunControls
+ {
+ public var trigger : uint = 0;
+ }
+}
@@ -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;
+ }
+}
@@ -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;
+ }
+}
@@ -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;
+ }
+}
@@ -0,0 +1,6 @@
+package net.richardlord.asteroidsember.components
+{
+ public class Spaceship
+ {
+ }
+}
@@ -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 );
+ }
+ }
+}
@@ -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;
+ }
+ }
+}
@@ -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();
+ }
+ }
+}
@@ -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();
+ }
+ }
+}
@@ -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();
+ }
+
+ }
+}
@@ -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 ];
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 027c309

Please sign in to comment.