/lessons/Lesson5Base/src/Lesson5Base.as

https://github.com/joelhooks/PushButtonEngine
ActionScript | 107 lines | 56 code | 21 blank | 30 comment | 2 complexity | ad5cbf9e86bc2e4b9d53deb92bcd8bf9 MD5 | raw file
  1. /*******************************************************************************
  2. * PushButton Engine
  3. * Copyright (C) 2009 PushButton Labs, LLC
  4. * For more information see http://www.pushbuttonengine.com
  5. *
  6. * This file is licensed under the terms of the MIT license, which is included
  7. * in the License.html file at the root directory of this SDK.
  8. ******************************************************************************/
  9. package
  10. {
  11. import com.pblabs.engine.PBE;
  12. import com.pblabs.engine.core.*;
  13. import com.pblabs.engine.entity.IEntity;
  14. import com.pblabs.engine.entity.PropertyReference;
  15. import com.pblabs.rendering2D.*;
  16. import com.pblabs.rendering2D.ui.*;
  17. import flash.display.Sprite;
  18. import flash.geom.Point;
  19. [SWF(width="800", height="600", frameRate="60")]
  20. public class Lesson5Base extends Sprite
  21. {
  22. public function Lesson5Base()
  23. {
  24. // Start up PBE
  25. PBE.startup(this);
  26. // Set up a simple scene entity
  27. createScene();
  28. // Create an avatar entity
  29. createHero();
  30. }
  31. private function createScene():void
  32. {
  33. var sceneView:SceneView = new SceneView(); // Make the SceneView
  34. sceneView.width = 800;
  35. sceneView.height = 600;
  36. PBE.initializeScene(sceneView); // This is just a helper function that will set up a basic scene for us
  37. }
  38. private function createHero():void
  39. {
  40. // Allocate an entity for our hero avatar
  41. var hero:IEntity = PBE.allocateEntity();
  42. // Add our spatial component to the Hero entity ...
  43. createSpatial( hero,
  44. // with location of 0,150...
  45. new Point(0, 150),
  46. // and with size of 60,53...
  47. new Point(60, 53)
  48. );
  49. // Create a simple render component to display our object
  50. var render:SimpleShapeRenderer = new SimpleShapeRenderer();
  51. // Specify to draw the object as a circle
  52. render.isCircle = true;
  53. // Mark the radius of the circle as 25
  54. render.radius = 25;
  55. // Add the renderer to the scene.
  56. render.scene = PBE.scene;
  57. // Point the render component to this entity's Spatial component for position information
  58. render.positionProperty = new PropertyReference("@Spatial.position");
  59. // Point the render component to this entity's Spatial component for rotation information
  60. render.rotationProperty = new PropertyReference("@Spatial.rotation");
  61. // Add our render component to the Hero entity with the name "Render"
  62. hero.addComponent( render, "Render" );
  63. // Create an instance of our hero controller component
  64. var controller:HeroControllerComponent = new HeroControllerComponent();
  65. // Point the controller component to this entity's Spatial component for position information
  66. controller.positionReference = new PropertyReference("@Spatial.position");
  67. // Add the demo controller component to the Hero entity with the name "Controller"
  68. hero.addComponent( controller, "Controller" );
  69. // Register the entity with PBE under the name "Hero"
  70. hero.initialize("Hero");
  71. }
  72. // This is a shortcut function to help simplify the creation of spatial components
  73. private function createSpatial( ent:IEntity, pos:Point, size:Point = null ):void
  74. {
  75. // Create our spatial component
  76. var spatial:SimpleSpatialComponent = new SimpleSpatialComponent();
  77. // Do a named lookup to register our background with the scene spatial database
  78. spatial.spatialManager = PBE.spatialManager;
  79. // Set our background position in space
  80. spatial.position = pos;
  81. if (size != null)
  82. spatial.size = size;
  83. ent.addComponent(spatial, "Spatial");
  84. }
  85. }
  86. }