Skip to content

Commit

Permalink
Enable using Starling juggler as game tick, and add Starling example …
Browse files Browse the repository at this point in the history
…project
  • Loading branch information
richardlord committed Oct 9, 2012
1 parent 2508d42 commit 81b4944
Show file tree
Hide file tree
Showing 35 changed files with 1,185 additions and 0 deletions.
70 changes: 70 additions & 0 deletions examples/starling/asteroids/net/richardlord/asteroids/Asteroids.as
@@ -0,0 +1,70 @@
package net.richardlord.asteroids
{
import flash.geom.Rectangle;
import net.richardlord.ash.core.Game;
import net.richardlord.ash.integration.starling.StarlingFrameTickProvider;
import net.richardlord.asteroids.components.GameState;
import net.richardlord.asteroids.systems.BulletAgeSystem;
import net.richardlord.asteroids.systems.CollisionSystem;
import net.richardlord.asteroids.systems.GameManager;
import net.richardlord.asteroids.systems.GunControlSystem;
import net.richardlord.asteroids.systems.MotionControlSystem;
import net.richardlord.asteroids.systems.MovementSystem;
import net.richardlord.asteroids.systems.RenderSystem;
import net.richardlord.asteroids.systems.SystemPriorities;
import net.richardlord.input.KeyPoll;
import starling.core.Starling;
import starling.display.Sprite;
import starling.events.Event;



public class Asteroids extends Sprite
{
private var game : Game;
private var tickProvider : StarlingFrameTickProvider;
private var gameState : GameState;
private var creator : EntityCreator;
private var keyPoll : KeyPoll;

public function Asteroids()
{
addEventListener( Event.ADDED_TO_STAGE, startGame );
}

private function startGame( event : Event ) : void
{
prepare();
start();
}

private function prepare() : void
{
var viewPort : Rectangle = Starling.current.viewPort;
game = new Game();
gameState = new GameState( viewPort.width, viewPort.height );
creator = new EntityCreator( game );
keyPoll = new KeyPoll( Starling.current.nativeStage );

game.addSystem( new GameManager( gameState, creator ), 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 CollisionSystem( creator ), SystemPriorities.resolveCollisions );
game.addSystem( new RenderSystem( this ), SystemPriorities.render );

tickProvider = new StarlingFrameTickProvider( Starling.current.juggler );
}

private function start() : void
{
gameState.level = 0;
gameState.lives = 3;
gameState.points = 0;

tickProvider.add( game.update );
tickProvider.start();
}
}
}
@@ -0,0 +1,74 @@
package net.richardlord.asteroids
{
import flash.ui.Keyboard;
import net.richardlord.ash.core.Entity;
import net.richardlord.ash.core.Game;
import net.richardlord.asteroids.components.Asteroid;
import net.richardlord.asteroids.components.Bullet;
import net.richardlord.asteroids.components.Display;
import net.richardlord.asteroids.components.Gun;
import net.richardlord.asteroids.components.GunControls;
import net.richardlord.asteroids.components.Motion;
import net.richardlord.asteroids.components.MotionControls;
import net.richardlord.asteroids.components.Position;
import net.richardlord.asteroids.components.Spaceship;
import net.richardlord.asteroids.graphics.AsteroidView;
import net.richardlord.asteroids.graphics.BulletView;
import net.richardlord.asteroids.graphics.SpaceshipView;


public class EntityCreator
{
private var game : Game;

public function EntityCreator( game : Game )
{
this.game = game;
}

public function destroyEntity( entity : Entity ) : void
{
game.removeEntity( entity );
}

public function createAsteroid( radius : Number, x : Number, y : Number ) : Entity
{
var asteroid : Entity = new Entity()
.add( new Asteroid() )
.add( new Position( x, y, 0, radius ) )
.add( new Motion( ( Math.random() - 0.5 ) * 4 * ( 50 - radius ), ( Math.random() - 0.5 ) * 4 * ( 50 - radius ), Math.random() * 2 - 1, 0 ) )
.add( new Display( new AsteroidView( radius ) ) );
game.addEntity( asteroid );
return asteroid;
}

public function createSpaceship() : Entity
{
var spaceship : Entity = new Entity()
.add( new Spaceship() )
.add( new Position( 300, 225, 0, 6 ) )
.add( new Motion( 0, 0, 0, 15 ) )
.add( new MotionControls( Keyboard.LEFT, Keyboard.RIGHT, Keyboard.UP, 100, 3 ) )
.add( new Gun( 8, 0, 0.3, 2 ) )
.add( new GunControls( Keyboard.Z ) )
.add( new Display( new SpaceshipView() ) );
game.addEntity( spaceship );
return spaceship;
}

public function createUserBullet( gun : Gun, parentPosition : Position ) : Entity
{
var cos : Number = Math.cos( parentPosition.rotation );
var sin : Number = Math.sin( parentPosition.rotation );
var bullet : Entity = new Entity()
.add( new Bullet( gun.bulletLifetime ) )
.add( new Position(
cos * gun.offsetFromParent.x - sin * gun.offsetFromParent.y + parentPosition.position.x,
sin * gun.offsetFromParent.x + cos * gun.offsetFromParent.y + parentPosition.position.y, 0, 0 ) )
.add( new Motion( cos * 150, sin * 150, 0, 0 ) )
.add( new Display( new BulletView() ) );
game.addEntity( bullet );
return bullet;
}
}
}
30 changes: 30 additions & 0 deletions examples/starling/asteroids/net/richardlord/asteroids/Main.as
@@ -0,0 +1,30 @@
package net.richardlord.asteroids
{
import starling.core.Starling;

import flash.display.Sprite;
import flash.events.Event;

[SWF(width='600', height='450', frameRate='60', backgroundColor='#000000')]

public class Main extends Sprite
{
private var starling : Starling;

public function Main()
{
addEventListener( Event.ENTER_FRAME, init );
}

private function init( event : Event ) : void
{
if( stage.stageWidth && stage.stageHeight )
{
removeEventListener( Event.ENTER_FRAME, init );
starling = new Starling( Asteroids, stage );
starling.antiAliasing = 0;
starling.start();
}
}
}
}
@@ -0,0 +1,6 @@
package net.richardlord.asteroids.components
{
public class Asteroid
{
}
}
@@ -0,0 +1,12 @@
package net.richardlord.asteroids.components
{
public class Bullet
{
public var lifeRemaining : Number;

public function Bullet( lifetime : Number )
{
lifeRemaining = lifetime;
}
}
}
@@ -0,0 +1,14 @@
package net.richardlord.asteroids.components
{
import starling.display.DisplayObject;

public class Display
{
public var displayObject : DisplayObject = null;

public function Display( displayObject : DisplayObject )
{
this.displayObject = displayObject;
}
}
}
@@ -0,0 +1,17 @@
package net.richardlord.asteroids.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;

public function GameState( width : Number = 0, height : Number = 0 )
{
this.width = width;
this.height = height;
}
}
}
@@ -0,0 +1,20 @@
package net.richardlord.asteroids.components
{
import flash.geom.Point;

public class Gun
{
public var shooting : Boolean = false;
public var offsetFromParent : Point;
public var timeSinceLastShot : Number = 0;
public var minimumShotInterval : Number = 0;
public var bulletLifetime : Number = 0;

public function Gun( offsetX : Number, offsetY : Number, minimumShotInterval : Number, bulletLifetime : Number )
{
offsetFromParent = new Point( offsetX, offsetY );
this.minimumShotInterval = minimumShotInterval;
this.bulletLifetime = bulletLifetime;
}
}
}
@@ -0,0 +1,12 @@
package net.richardlord.asteroids.components
{
public class GunControls
{
public var trigger : uint = 0;

public function GunControls( trigger : uint )
{
this.trigger = trigger;
}
}
}
@@ -0,0 +1,18 @@
package net.richardlord.asteroids.components
{
import flash.geom.Point;

public class Motion
{
public var velocity : Point = new Point();
public var angularVelocity : Number = 0;
public var damping : Number = 0;

public function Motion( velocityX : Number, velocityY : Number, angularVelocity : Number, damping : Number )
{
velocity = new Point( velocityX, velocityY );
this.angularVelocity = angularVelocity;
this.damping = damping;
}
}
}
@@ -0,0 +1,21 @@
package net.richardlord.asteroids.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;

public function MotionControls( left : uint, right : uint, accelerate : uint, accelerationRate : Number, rotationRate : Number )
{
this.left = left;
this.right = right;
this.accelerate = accelerate;
this.accelerationRate = accelerationRate;
this.rotationRate = rotationRate;
}
}
}
@@ -0,0 +1,18 @@
package net.richardlord.asteroids.components
{
import flash.geom.Point;

public class Position
{
public var position : Point;
public var rotation : Number = 0;
public var collisionRadius : Number = 0;

public function Position( x : Number, y : Number, rotation : Number, collisionRadius : Number )
{
position = new Point( x, y );
this.rotation = rotation;
this.collisionRadius = collisionRadius;
}
}
}
@@ -0,0 +1,6 @@
package net.richardlord.asteroids.components
{
public class Spaceship
{
}
}
@@ -0,0 +1,40 @@
package net.richardlord.asteroids.graphics
{
import starling.textures.Texture;
import starling.display.Image;

import flash.display.BitmapData;
import flash.display.Shape;
import flash.geom.Matrix;

public class AsteroidView extends Image
{
public function AsteroidView( radius : Number )
{
var angle : Number = 0;
var shape : Shape = new Shape();
shape.graphics.beginFill( 0xFFFFFF );
shape.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;
shape.graphics.lineTo( posX, posY );
angle += Math.random() * 0.5;
}
shape.graphics.lineTo( radius, 0 );
shape.graphics.endFill();

var bitmapData : BitmapData = new BitmapData( radius * 2, radius * 2, true, 0 );
var transform : Matrix = new Matrix();
transform.tx = transform.ty = radius;
bitmapData.draw( shape, transform, null, null, null, true );

var texture : Texture = Texture.fromBitmapData( bitmapData, false, false, 1 );
super( texture );
pivotX = radius;
pivotY = radius;
}
}
}

0 comments on commit 81b4944

Please sign in to comment.