Guest User

Untitled

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