Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Grenade extends Entity
  2. {
  3.     public static function create(_data:Dynamic):Grenade
  4.     {
  5.         var s:Grenade = new Grenade(null, "Grenade");
  6.         if (_data)
  7.         {
  8.             s.m_mov.m_body.x = _data.position.x;
  9.             s.m_mov.m_body.y = _data.position.y;
  10.            
  11.             s.m_mov.setTargetPosition( _data.target.x, _data.target.y );
  12.         }
  13.         return s;
  14.     }
  15.    
  16.     public function new(?_name:String, ?_type:String)
  17.     {
  18.         super(_name, _type);
  19.        
  20.         // components
  21.         m_mov = Movable.create(0, 0);
  22.         m_mov.addColShape( new Circle(2, new Vector(0, 0)) );
  23.         m_mov.m_body.physical = false;
  24.         m_mov.m_moveSpeed = 5;
  25.        
  26.         m_mov.addListener(Movable.TARGET_REACHED, _onCollision);
  27.         m_mov.addListener(Movable.COLLISION, _onCollision);
  28.        
  29.         m_vis = Sprite.create("data.fx.grenade", 0, 0);
  30.         m_vis.m_sprite.rotation = MathUtils.randRange(0, 359); // random rotation
  31.        
  32.         addCmp(m_mov);
  33.         addCmp(m_vis);
  34.     }
  35.    
  36.     public override function update(_delta:Int):Void
  37.     {
  38.         // position the sprite
  39.         m_vis.setPosition(m_mov.m_position.x, m_mov.m_position.y);
  40.        
  41.         super.update(_delta);
  42.     }
  43.    
  44.     function _onCollision(_data:Dynamic):Void
  45.     {
  46.         _spawnExplosion();
  47.        
  48.         cast(Core.get(EntWorld), EntWorld).removeEntity(this, true);
  49.     }
  50.    
  51.     function _spawnExplosion():Void
  52.     {
  53.         var e:Entity = cast(Core.get(EntWorld), EntWorld).createEntity("Explosion", {
  54.                 position: m_mov.m_position
  55.             } );
  56.         cast(Core.get(EntWorld), EntWorld).addEntity(e);
  57.     }
  58.  
  59.     public var m_vis:Sprite;
  60.     public var m_mov:Movable;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement