Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game.ai;
- import colliders.BoxCollider;
- import flash.media.Sound;
- import game.Damagable;
- import game.Explosion;
- import game.projectiles.CannonShell;
- import haxe.Timer;
- import starling.display.Image;
- import starling.display.MovieClip;
- import starling.display.Sprite;
- import starling.events.EnterFrameEvent;
- import utility.Point;
- class Cannon extends Damagable
- {
- private var gunAnimation:MovieClip;
- private var cannonBase:Image;
- private var sc:Sprite;
- private var canFire:Bool = false;
- //private var gunTimer:Timer;
- private var gunTimeRemaining:Float = 1.0;
- public function new(board:Board)
- {
- super(board, 20);
- sc = new Sprite();
- sc.pivotX = 8;
- sc.pivotY = 8;
- cannonBase = new Image(Root.assets.getTexture("cannon/cannon_base"));
- cannonBase.smoothing = 'none';
- sc.addChild(cannonBase);
- gunAnimation = new MovieClip(Root.assets.getTextures("cannon/cannon_gun_"));
- gunAnimation.smoothing = 'none';
- gunAnimation.pivotX = 8;
- gunAnimation.pivotY = 8;
- gunAnimation.x = 8;
- gunAnimation.y = 8;
- gunAnimation.loop = false;
- gunAnimation.stop();
- sc.addChild(gunAnimation);
- collider = new BoxCollider(this, ["ai"], 12, 12, new Point(8, 8));
- //collider.toggleDebug();
- sc.addChild(collider);
- //gunTimer = new Timer(Math.round(Math.random() * 1000));
- gunTimeRemaining = Math.random() * 1000;
- //gunTimer.run = function() {
- //canFire = true;
- //gunTimer.stop();
- //}
- this.addChild(sc);
- }
- public override function death() {
- super.death();
- var exp = new Explosion("explosion_32x32_1_");
- exp.x = this.x;
- exp.y = this.y;
- board.addChild(exp);
- }
- public override function update(event:EnterFrameEvent) {
- var player = board.getPlayer();
- var player_pos = Point.fromDispObj(player);
- var this_pos = Point.fromDispObj(this).add(new Point(0.5, 0.5));
- var angle = this_pos.angleBetween(player_pos);
- gunAnimation.rotation = angle;
- if (gunAnimation.isPlaying) {
- gunAnimation.advanceTime(event.passedTime);
- } else if (gunAnimation.isComplete) {
- gunAnimation.stop();
- }
- if (!canFire) {
- gunTimeRemaining -= event.passedTime * 1000;
- if (gunTimeRemaining <= 0) {
- gunTimeRemaining = 0;
- canFire = true;
- }
- }
- if (canFire) {
- canFire = false;
- gunAnimation.play();
- var projectile = new CannonShell(this.board, gunAnimation.rotation);
- projectile.x = 20;
- projectile.y = 8;
- board.addProjectile(projectile, gunAnimation);
- Root.assets.playSound("Cannon2", 0, 0, Root.sfxTransform(0.5));
- gunTimeRemaining = Math.random() * 200 + 800;
- //gunTimer = new Timer(Math.round(Math.random() * 200) + 800);
- //gunTimer.run = function() {
- //canFire = true;
- //gunAnimation.stop();
- //gunTimer.stop();
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement