Advertisement
ulfben

Nape-entity

Mar 4th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ulfben.slingshot.entities
  2. {
  3.     import nape.geom.Vec2;
  4.     import nape.phys.Body; 
  5.     import nape.space.Space;
  6.    
  7.     import starling.display.DisplayObject;
  8.     import starling.display.Sprite;
  9.    
  10.     public class Entity extends Sprite
  11.     {
  12.         public var _isDead:Boolean = false;    
  13.         protected var _body:Body = null;       
  14.         protected var _space:Space = null;
  15.         protected var _visual:DisplayObject; //MovieClip, Sprite or what have you
  16.         protected var _graphicOffset:Vec2 = new Vec2(0,0); //from PhysicEditor
  17.        
  18.         public function set body(b:Body):void {_body = b;}
  19.         public function get body():Body {return _body;}
  20.         public function set space(s:Space):void { _space = s; if(_body){_body.space = _space;}}
  21.         public function set allowMovement(b:Boolean):void {_body.allowMovement = b;}
  22.         public function get allowMovement():Boolean {return _body.allowMovement;}      
  23.         public function get velocity():Vec2 {return _body.velocity;}
  24.         public function set velocity(v:Vec2):void {_body.velocity = v;}
  25.         public function get angularVel():Number  {return _body.angularVel;}
  26.         public function set angularVel(n:Number):void  {_body.angularVel = n;}     
  27.         public function get position():Vec2  {return _body.position;}
  28.         public function set position(v:Vec2):void  {_body.position = v; x = _body.position.x; y = _body.position.y; }
  29.        
  30.         public function Entity(space:Space)
  31.         {          
  32.             super();           
  33.             _space = space;
  34.             touchable = false;
  35.         }
  36.        
  37.         public function setxy(x:Number, y:Number):void
  38.         {
  39.             _body.position.x = x;
  40.             _body.position.y = y;
  41.         }
  42.            
  43.         private var pos:Vec2; //temp, for re-use.
  44.         public function tick(dt:Number):void
  45.         {          
  46.             pos = _body.localPointToWorld(_graphicOffset);                 
  47.             x = pos.x;
  48.             y = pos.y;         
  49.             rotation = _body.rotation;         
  50.         }
  51.                
  52.         override public function dispose():void
  53.         {              
  54.             if(_body){
  55.                 _space.bodies.remove(_body);
  56.                 _body.shapes.clear();              
  57.                 _body.userData.entity = null;
  58.                 _body.userData.graphicOffset = null;           
  59.                 _body = null;
  60.             }
  61.             if(_visual){
  62.                 _visual.removeFromParent(true);
  63.                 _visual = null;
  64.             }
  65.             _space = null;
  66.             super.dispose();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement