Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import Box2D.Common.Math.b2Vec2;
- import com.pblabs.box2D.Box2DDebugComponent;
- import com.pblabs.box2D.Box2DManagerComponent;
- import com.pblabs.box2D.Box2DSpatialComponent;
- import com.pblabs.box2D.CircleCollisionShape;
- import com.pblabs.box2D.PolygonCollisionShape;
- import com.pblabs.engine.PBE;
- import com.pblabs.engine.core.InputMap;
- import com.pblabs.engine.core.ObjectType;
- import com.pblabs.engine.entity.IEntity;
- import com.pblabs.engine.entity.PropertyReference;
- import com.pblabs.rendering2D.SimpleShapeRenderer;
- import com.pblabs.rendering2D.SpriteRenderer;
- import com.pblabs.rendering2D.SpriteSheetRenderer;
- import com.pblabs.rendering2D.spritesheet.SpriteSheetComponent;
- import com.pblabs.rendering2D.ui.SceneView;
- import flash.display.Sprite;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- [SWF(width="800", height="600", frameRate="60")]
- public class gameBase extends Sprite
- {
- private var DEBUG:Boolean = false;
- public function gameBase()
- {
- PBE.startup(this);
- createScene();
- createBox2DManager();
- if (DEBUG==true){createBox2DDebugger();}
- createHero();
- createFloor();
- }
- private function createScene():void
- {
- var sceneView:SceneView = new SceneView();
- sceneView.width = 800;
- sceneView.height = 600;
- PBE.initializeScene(sceneView);
- }
- private function createBox2DManager():void
- {
- // Create manager component.
- var comp:Box2DManagerComponent = new Box2DManagerComponent();
- comp.scale = 128;
- comp.gravity = new Point(0, 3.5);
- // Allocate new entity and add components.
- var myEntity:IEntity = PBE.allocateEntity();
- myEntity.addComponent(comp, "Manager");
- myEntity.initialize("SpatialManager");
- }
- private function createBox2DDebugger():void
- {
- // Create debugger component.
- var comp:Box2DDebugComponent = new Box2DDebugComponent();
- comp.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
- comp.scene = PBE.scene;
- comp.drawAABB = true;
- // Allocate new entity and add components.
- var myEntity:IEntity = PBE.allocateEntity();
- myEntity.addComponent(comp, "Debug");
- myEntity.initialize("SpatialDebugger");
- }
- private function createHero():void
- {
- // Create spatial component.
- var spatialComp:Box2DSpatialComponent = new Box2DSpatialComponent();
- spatialComp.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
- spatialComp.canMove = true;
- spatialComp.canRotate = false;
- spatialComp.canSleep = true;
- spatialComp.position = new Point(0, -300);
- spatialComp.size = new Point(60, 60);
- spatialComp.collisionType = new ObjectType("Hero");
- spatialComp.collidesWithTypes = new ObjectType("Floor");
- // Collision shape.
- var shape:CircleCollisionShape = new CircleCollisionShape();
- shape.radius = 1.0;
- shape.density = 1;
- shape.restitution = 0.2;
- shape.friction = 0.4;
- // Add the shape to spatial component.
- spatialComp.collisionShapes = new Array();
- spatialComp.collisionShapes.push(shape);
- // Create input component.
- //var gameInput:GameInput = new GameInput();
- //gameInput.input = new InputMap();
- //gameInput.velocityReference = new PropertyReference("@Spatial.linearVelocity");
- // Create renderer component.
- var renderer:SimpleShapeRenderer = new SimpleShapeRenderer();
- renderer.fillColor = 0xdddddd;
- renderer.isCircle = true;
- renderer.lineColor = 0x000000;
- renderer.scene = PBE.scene;
- renderer.positionProperty = new PropertyReference("@Spatial.position");
- renderer.rotationProperty = new PropertyReference("@Spatial.rotation");
- renderer.sizeProperty = new PropertyReference("@Spatial.size");
- // Allocate new entity and add components.
- var hero:IEntity = PBE.allocateEntity();
- hero.addComponent(spatialComp, "Spatial");
- //myEntity.addComponent(gameInput, "Input");
- hero.addComponent(renderer, "Render");
- // Create an instance of our hero controller component
- var controller:HeroControllerComponent = new HeroControllerComponent();
- // Point the controller component to this entity's Spatial component for position information
- controller.positionReference = new PropertyReference("@Spatial.position");
- // Add the demo controller component to the Hero entity with the name "Controller"
- hero.addComponent( controller, "Controller" );
- hero.initialize("Hero");
- //Adjust box2d body
- spatialComp.body.m_linearDamping = 0;
- }
- private function createFloor():void
- {
- // Create spatial component.
- var spatialComp:Box2DSpatialComponent = new Box2DSpatialComponent();
- spatialComp.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
- spatialComp.position = new Point(0, 200);
- spatialComp.size = new Point(550, 60);
- spatialComp.canMove = false;
- spatialComp.canRotate = false;
- spatialComp.collisionType = new ObjectType("Floor");
- spatialComp.collidesWithTypes = new ObjectType("Hero");
- // Collision shape.
- var shape:PolygonCollisionShape = new PolygonCollisionShape();
- shape.vertices = [new Point(-1, -1), new Point(1, -1), new Point(1, 1), new Point(-1, 1)];
- shape.density = 1;
- shape.restitution = 0.4;
- shape.friction = 0.3;
- // Add the shape to spatial component.
- spatialComp.collisionShapes = new Array();
- spatialComp.collisionShapes.push(shape);
- // Create renderer component.
- var renderer:SimpleShapeRenderer = new SimpleShapeRenderer();
- renderer.fillColor = 0xdddd00;
- renderer.isCircle = false;
- renderer.isSquare = true;
- renderer.lineColor = 0x000000;
- renderer.scene = PBE.scene;
- renderer.positionProperty = new PropertyReference("@Spatial.position");
- renderer.rotationProperty = new PropertyReference("@Spatial.rotation");
- renderer.sizeProperty = new PropertyReference("@Spatial.size");
- // Allocate new entity and add components.
- var myEntity:IEntity = PBE.allocateEntity();
- myEntity.addComponent(spatialComp, "Spatial");
- myEntity.addComponent(renderer, "Renderer");
- myEntity.initialize("Floor");
- }
- }
- }
Add Comment
Please, Sign In to add comment