home *** CD-ROM | disk | FTP | other *** search
- package core.objects
- {
- import Box2D.Collision.Shapes.b2ShapeDef;
- import Box2D.Common.Math.b2Vec2;
- import Box2D.Dynamics.b2Body;
- import Box2D.Dynamics.b2BodyDef;
- import core.CoreEngine;
- import core.events.CoreEvent;
- import flash.display.Sprite;
- import flash.events.EventDispatcher;
-
- public class BaseObject extends EventDispatcher
- {
-
-
- protected var body:b2Body;
-
- protected var def:BaseObjectDef;
-
- protected var engine:CoreEngine;
-
- public var isDead:Boolean;
-
- protected var skin:Sprite;
-
- public function BaseObject(def:BaseObjectDef)
- {
- super();
- this.def = def;
- init();
- }
-
- protected function createShapeDef() : b2ShapeDef
- {
- return null;
- }
-
- protected function postUpdateHandler(e:CoreEvent) : void
- {
- if(isDead)
- {
- destroy();
- }
- }
-
- public function updateSkin() : void
- {
- var pos:b2Vec2 = body.GetWorldCenter();
- skin.x = pos.x * engine.m_physScale;
- skin.y = pos.y * engine.m_physScale;
- skin.rotation = body.GetAngle() * 180 / Math.PI;
- }
-
- protected function createBodyDef() : b2BodyDef
- {
- return null;
- }
-
- public function moveTo(x:Number, y:Number) : void
- {
- var pos:b2Vec2 = new b2Vec2(x / engine.m_physScale,y / engine.m_physScale);
- body.SetXForm(pos,body.GetAngle());
- updateSkin();
- }
-
- protected function init() : void
- {
- engine = CoreEngine.getInstance();
- var shapeDef:b2ShapeDef = createShapeDef();
- var bodyDef:b2BodyDef = createBodyDef();
- body = createBody(bodyDef,shapeDef);
- skin = createSkin();
- isDead = false;
- engine.dispatcher.addEventListener(CoreEvent.POST_UPDATE,postUpdateHandler);
- engine.dispatcher.addEventListener(CoreEvent.DESTROY,destroy);
- }
-
- public function destroy(e:CoreEvent = null) : void
- {
- if(skin)
- {
- skin.parent.removeChild(skin);
- }
- if(body)
- {
- engine.m_world.DestroyBody(body);
- }
- engine.dispatcher.removeEventListener(CoreEvent.POST_UPDATE,postUpdateHandler);
- engine.dispatcher.removeEventListener(CoreEvent.DESTROY,destroy);
- }
-
- public function getSkin() : Sprite
- {
- return skin;
- }
-
- public function getBody() : b2Body
- {
- return body;
- }
-
- protected function createSkin() : Sprite
- {
- return null;
- }
-
- protected function createBody(bodyDef:b2BodyDef, shapeDef:b2ShapeDef) : b2Body
- {
- return null;
- }
- }
- }
-