home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / core / objects / BaseObject.as < prev    next >
Encoding:
Text File  |  2008-09-03  |  2.9 KB  |  113 lines

  1. package core.objects
  2. {
  3.    import Box2D.Collision.Shapes.b2ShapeDef;
  4.    import Box2D.Common.Math.b2Vec2;
  5.    import Box2D.Dynamics.b2Body;
  6.    import Box2D.Dynamics.b2BodyDef;
  7.    import core.CoreEngine;
  8.    import core.events.CoreEvent;
  9.    import flash.display.Sprite;
  10.    import flash.events.EventDispatcher;
  11.    
  12.    public class BaseObject extends EventDispatcher
  13.    {
  14.        
  15.       
  16.       protected var body:b2Body;
  17.       
  18.       protected var def:BaseObjectDef;
  19.       
  20.       protected var engine:CoreEngine;
  21.       
  22.       public var isDead:Boolean;
  23.       
  24.       protected var skin:Sprite;
  25.       
  26.       public function BaseObject(def:BaseObjectDef)
  27.       {
  28.          super();
  29.          this.def = def;
  30.          init();
  31.       }
  32.       
  33.       protected function createShapeDef() : b2ShapeDef
  34.       {
  35.          return null;
  36.       }
  37.       
  38.       protected function postUpdateHandler(e:CoreEvent) : void
  39.       {
  40.          if(isDead)
  41.          {
  42.             destroy();
  43.          }
  44.       }
  45.       
  46.       public function updateSkin() : void
  47.       {
  48.          var pos:b2Vec2 = body.GetWorldCenter();
  49.          skin.x = pos.x * engine.m_physScale;
  50.          skin.y = pos.y * engine.m_physScale;
  51.          skin.rotation = body.GetAngle() * 180 / Math.PI;
  52.       }
  53.       
  54.       protected function createBodyDef() : b2BodyDef
  55.       {
  56.          return null;
  57.       }
  58.       
  59.       public function moveTo(x:Number, y:Number) : void
  60.       {
  61.          var pos:b2Vec2 = new b2Vec2(x / engine.m_physScale,y / engine.m_physScale);
  62.          body.SetXForm(pos,body.GetAngle());
  63.          updateSkin();
  64.       }
  65.       
  66.       protected function init() : void
  67.       {
  68.          engine = CoreEngine.getInstance();
  69.          var shapeDef:b2ShapeDef = createShapeDef();
  70.          var bodyDef:b2BodyDef = createBodyDef();
  71.          body = createBody(bodyDef,shapeDef);
  72.          skin = createSkin();
  73.          isDead = false;
  74.          engine.dispatcher.addEventListener(CoreEvent.POST_UPDATE,postUpdateHandler);
  75.          engine.dispatcher.addEventListener(CoreEvent.DESTROY,destroy);
  76.       }
  77.       
  78.       public function destroy(e:CoreEvent = null) : void
  79.       {
  80.          if(skin)
  81.          {
  82.             skin.parent.removeChild(skin);
  83.          }
  84.          if(body)
  85.          {
  86.             engine.m_world.DestroyBody(body);
  87.          }
  88.          engine.dispatcher.removeEventListener(CoreEvent.POST_UPDATE,postUpdateHandler);
  89.          engine.dispatcher.removeEventListener(CoreEvent.DESTROY,destroy);
  90.       }
  91.       
  92.       public function getSkin() : Sprite
  93.       {
  94.          return skin;
  95.       }
  96.       
  97.       public function getBody() : b2Body
  98.       {
  99.          return body;
  100.       }
  101.       
  102.       protected function createSkin() : Sprite
  103.       {
  104.          return null;
  105.       }
  106.       
  107.       protected function createBody(bodyDef:b2BodyDef, shapeDef:b2ShapeDef) : b2Body
  108.       {
  109.          return null;
  110.       }
  111.    }
  112. }
  113.