Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Grenade extends Entity
- {
- public static function create(_data:Dynamic):Grenade
- {
- var s:Grenade = new Grenade(null, "Grenade");
- if (_data)
- {
- s.m_mov.m_body.x = _data.position.x;
- s.m_mov.m_body.y = _data.position.y;
- s.m_mov.setTargetPosition( _data.target.x, _data.target.y );
- }
- return s;
- }
- public function new(?_name:String, ?_type:String)
- {
- super(_name, _type);
- // components
- m_mov = Movable.create(0, 0);
- m_mov.addColShape( new Circle(2, new Vector(0, 0)) );
- m_mov.m_body.physical = false;
- m_mov.m_moveSpeed = 5;
- m_mov.addListener(Movable.TARGET_REACHED, _onCollision);
- m_mov.addListener(Movable.COLLISION, _onCollision);
- m_vis = Sprite.create("data.fx.grenade", 0, 0);
- m_vis.m_sprite.rotation = MathUtils.randRange(0, 359); // random rotation
- addCmp(m_mov);
- addCmp(m_vis);
- }
- public override function update(_delta:Int):Void
- {
- // position the sprite
- m_vis.setPosition(m_mov.m_position.x, m_mov.m_position.y);
- super.update(_delta);
- }
- function _onCollision(_data:Dynamic):Void
- {
- _spawnExplosion();
- cast(Core.get(EntWorld), EntWorld).removeEntity(this, true);
- }
- function _spawnExplosion():Void
- {
- var e:Entity = cast(Core.get(EntWorld), EntWorld).createEntity("Explosion", {
- position: m_mov.m_position
- } );
- cast(Core.get(EntWorld), EntWorld).addEntity(e);
- }
- public var m_vis:Sprite;
- public var m_mov:Movable;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement