Skip to content

Instantly share code, notes, and snippets.

@micahrj
Created October 1, 2010 01:47
Show Gist options
  • Save micahrj/605601 to your computer and use it in GitHub Desktop.
Save micahrj/605601 to your computer and use it in GitHub Desktop.
// Lesson2Final.as
package
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.entity.*;
import com.pblabs.rendering2D.*;
import com.pblabs.rendering2D.ui.*;
import flash.display.Sprite;
import flash.geom.Point;
[SWF(width="800", height="600", frameRate="60")]
public class Lesson2Final extends Sprite
{
public function Lesson2Final()
{
PBE.startup(this); // Start PBE.
createScene(); // Set up a simple scene entity
createHero(); // Create a simple avatar entity
}
private function createScene():void
{
var sceneView:SceneView = new SceneView(); // Make the SceneView
sceneView.width = 800;
sceneView.height = 600;
PBE.initializeScene(sceneView); // This is just a helper function that will set up a basic scene for us
}
private function createHero():void
{
var hero:IEntity = allocateEntity(); // Allocate an entity for our hero avatar
var spatial:SimpleSpatialComponent = new SimpleSpatialComponent(); // Create our spatial component
spatial.position = new Point(0,0); // Set our hero's spatial position as 0,0
spatial.size = new Point(50,50); // Set our hero's size as 50,50
spatial.spatialManager = PBE.spatialManager;
hero.addComponent( spatial, "Spatial" ); // Add our spatial component to the Hero entity with the name "Spatial"
var render:SimpleShapeRenderer = new SimpleShapeRenderer(); // Create a renderer to display our object
render.fillColor = 0x0000FF0;
render.isCircle = true;
render.lineSize = 2;
render.radius = 25;
render.lineColor = 0x000000;
render.scene = PBE.scene; // Set which scene this is a part of
var rendersprite : SpriteRenderer = new SpriteRenderer();
rendersprite.fileName = "../assets/fanship.png";
rendersprite.scene = PBE.scene;
rendersprite.layerIndex = 10;
rendersprite.positionProperty = new PropertyReference("@Spatial.position");
rendersprite.sizeProperty = new PropertyReference("@Spatial.size");
hero.addComponent(rendersprite, "RenderSprite");
// Point the render component to this entity's Spatial component for position information
render.positionProperty = new PropertyReference("@Spatial.position");
// Point the render component to this entity's Spatial component for rotation information
render.rotationProperty = new PropertyReference("@Spatial.rotation");
hero.addComponent( render, "Render" ); // Add our render component to the Hero entity with the name "Render"
var control : HeroControllerComponent = new HeroControllerComponent();
control.positionReference = new PropertyReference("@Spatial.position");
hero.addComponent(control, "Control");
hero.initialize("Hero"); // Register the entity with PBE under the name "Hero"
}
}
}
// HeroControllerComponent.as
package {
import com.pblabs.engine.PBE;
import com.pblabs.engine.core.*;
import com.pblabs.engine.entity.*;
import com.pblabs.rendering2D.*;
import com.pblabs.rendering2D.ui.*;
import com.pblabs.engine.components.*;
import flash.display.Sprite;
import flash.geom.Point;
public class HeroControllerComponent extends EntityComponent implements IAnimatedObject {
public var positionReference : PropertyReference;
public function onFrame(elapsed : Number) : void {
var position : Point = owner.getProperty(positionReference);
if (PBE.isKeyDown(InputKey.RIGHT)) {
position.x += 4;
}
if (PBE.isKeyDown(InputKey.LEFT)) {
position.x -= 4;
}
if (PBE.isKeyDown(InputKey.DOWN)) {
position.y += 4;
}
if (PBE.isKeyDown(InputKey.UP)) {
position.y -= 4;
}
owner.setProperty(positionReference, position);
}
protected override function onAdd():void
{
PBE.processManager.addAnimatedObject(this);
}
protected override function onRemove():void
{
PBE.processManager.removeAnimatedObject(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment