Guest User

Untitled

a guest
Jan 5th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. package {
  2.  
  3. import Box2D.Common.Math.b2Vec2;
  4. import com.pblabs.box2D.Box2DDebugComponent;
  5. import com.pblabs.box2D.Box2DManagerComponent;
  6. import com.pblabs.box2D.Box2DSpatialComponent;
  7. import com.pblabs.box2D.CircleCollisionShape;
  8. import com.pblabs.box2D.PolygonCollisionShape;
  9. import com.pblabs.engine.PBE;
  10. import com.pblabs.engine.core.InputMap;
  11. import com.pblabs.engine.core.ObjectType;
  12. import com.pblabs.engine.entity.IEntity;
  13. import com.pblabs.engine.entity.PropertyReference;
  14. import com.pblabs.rendering2D.SimpleShapeRenderer;
  15. import com.pblabs.rendering2D.SpriteRenderer;
  16. import com.pblabs.rendering2D.SpriteSheetRenderer;
  17. import com.pblabs.rendering2D.spritesheet.SpriteSheetComponent;
  18. import com.pblabs.rendering2D.ui.SceneView;
  19.  
  20. import flash.display.Sprite;
  21. import flash.geom.Point;
  22. import flash.geom.Rectangle;
  23.  
  24. [SWF(width="800", height="600", frameRate="60")]
  25. public class gameBase extends Sprite
  26. {
  27. private var DEBUG:Boolean = false;
  28. public function gameBase()
  29. {
  30. PBE.startup(this);
  31.  
  32. createScene();
  33. createBox2DManager();
  34. if (DEBUG==true){createBox2DDebugger();}
  35. createHero();
  36. createFloor();
  37. }
  38.  
  39. private function createScene():void
  40. {
  41. var sceneView:SceneView = new SceneView();
  42. sceneView.width = 800;
  43. sceneView.height = 600;
  44. PBE.initializeScene(sceneView);
  45. }
  46.  
  47. private function createBox2DManager():void
  48. {
  49. // Create manager component.
  50. var comp:Box2DManagerComponent = new Box2DManagerComponent();
  51. comp.scale = 128;
  52. comp.gravity = new Point(0, 3.5);
  53.  
  54. // Allocate new entity and add components.
  55. var myEntity:IEntity = PBE.allocateEntity();
  56. myEntity.addComponent(comp, "Manager");
  57. myEntity.initialize("SpatialManager");
  58. }
  59.  
  60. private function createBox2DDebugger():void
  61. {
  62. // Create debugger component.
  63. var comp:Box2DDebugComponent = new Box2DDebugComponent();
  64. comp.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
  65. comp.scene = PBE.scene;
  66. comp.drawAABB = true;
  67.  
  68. // Allocate new entity and add components.
  69. var myEntity:IEntity = PBE.allocateEntity();
  70. myEntity.addComponent(comp, "Debug");
  71. myEntity.initialize("SpatialDebugger");
  72. }
  73.  
  74. private function createHero():void
  75. {
  76. // Create spatial component.
  77. var spatialComp:Box2DSpatialComponent = new Box2DSpatialComponent();
  78. spatialComp.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
  79. spatialComp.canMove = true;
  80. spatialComp.canRotate = false;
  81. spatialComp.canSleep = true;
  82. spatialComp.position = new Point(0, -300);
  83. spatialComp.size = new Point(60, 60);
  84. spatialComp.collisionType = new ObjectType("Hero");
  85. spatialComp.collidesWithTypes = new ObjectType("Floor");
  86.  
  87. // Collision shape.
  88. var shape:CircleCollisionShape = new CircleCollisionShape();
  89. shape.radius = 1.0;
  90. shape.density = 1;
  91. shape.restitution = 0.2;
  92. shape.friction = 0.4;
  93.  
  94. // Add the shape to spatial component.
  95. spatialComp.collisionShapes = new Array();
  96. spatialComp.collisionShapes.push(shape);
  97.  
  98.  
  99. // Create input component.
  100. //var gameInput:GameInput = new GameInput();
  101. //gameInput.input = new InputMap();
  102. //gameInput.velocityReference = new PropertyReference("@Spatial.linearVelocity");
  103.  
  104. // Create renderer component.
  105. var renderer:SimpleShapeRenderer = new SimpleShapeRenderer();
  106. renderer.fillColor = 0xdddddd;
  107. renderer.isCircle = true;
  108. renderer.lineColor = 0x000000;
  109. renderer.scene = PBE.scene;
  110. renderer.positionProperty = new PropertyReference("@Spatial.position");
  111. renderer.rotationProperty = new PropertyReference("@Spatial.rotation");
  112. renderer.sizeProperty = new PropertyReference("@Spatial.size");
  113.  
  114. // Allocate new entity and add components.
  115. var hero:IEntity = PBE.allocateEntity();
  116. hero.addComponent(spatialComp, "Spatial");
  117. //myEntity.addComponent(gameInput, "Input");
  118. hero.addComponent(renderer, "Render");
  119.  
  120. // Create an instance of our hero controller component
  121. var controller:HeroControllerComponent = new HeroControllerComponent();
  122.  
  123. // Point the controller component to this entity's Spatial component for position information
  124. controller.positionReference = new PropertyReference("@Spatial.position");
  125.  
  126. // Add the demo controller component to the Hero entity with the name "Controller"
  127. hero.addComponent( controller, "Controller" );
  128.  
  129. hero.initialize("Hero");
  130.  
  131. //Adjust box2d body
  132. spatialComp.body.m_linearDamping = 0;
  133.  
  134. }
  135.  
  136. private function createFloor():void
  137. {
  138. // Create spatial component.
  139. var spatialComp:Box2DSpatialComponent = new Box2DSpatialComponent();
  140. spatialComp.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
  141. spatialComp.position = new Point(0, 200);
  142. spatialComp.size = new Point(550, 60);
  143. spatialComp.canMove = false;
  144. spatialComp.canRotate = false;
  145. spatialComp.collisionType = new ObjectType("Floor");
  146. spatialComp.collidesWithTypes = new ObjectType("Hero");
  147.  
  148. // Collision shape.
  149. var shape:PolygonCollisionShape = new PolygonCollisionShape();
  150. shape.vertices = [new Point(-1, -1), new Point(1, -1), new Point(1, 1), new Point(-1, 1)];
  151. shape.density = 1;
  152. shape.restitution = 0.4;
  153. shape.friction = 0.3;
  154.  
  155.  
  156.  
  157. // Add the shape to spatial component.
  158. spatialComp.collisionShapes = new Array();
  159. spatialComp.collisionShapes.push(shape);
  160.  
  161. // Create renderer component.
  162. var renderer:SimpleShapeRenderer = new SimpleShapeRenderer();
  163. renderer.fillColor = 0xdddd00;
  164. renderer.isCircle = false;
  165. renderer.isSquare = true;
  166. renderer.lineColor = 0x000000;
  167. renderer.scene = PBE.scene;
  168. renderer.positionProperty = new PropertyReference("@Spatial.position");
  169. renderer.rotationProperty = new PropertyReference("@Spatial.rotation");
  170. renderer.sizeProperty = new PropertyReference("@Spatial.size");
  171.  
  172. // Allocate new entity and add components.
  173. var myEntity:IEntity = PBE.allocateEntity();
  174. myEntity.addComponent(spatialComp, "Spatial");
  175. myEntity.addComponent(renderer, "Renderer");
  176. myEntity.initialize("Floor");
  177. }
  178. }
  179. }
Add Comment
Please, Sign In to add comment