Advertisement
Guest User

Untitled

a guest
Apr 30th, 2015
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.83 KB | None | 0 0
  1. package game.ai;
  2.  
  3. import colliders.BoxCollider;
  4. import flash.media.Sound;
  5. import game.Damagable;
  6. import game.Explosion;
  7. import game.projectiles.CannonShell;
  8. import haxe.Timer;
  9. import starling.display.Image;
  10. import starling.display.MovieClip;
  11. import starling.display.Sprite;
  12. import starling.events.EnterFrameEvent;
  13. import utility.Point;
  14.  
  15. class Cannon extends Damagable
  16. {
  17.    
  18.     private var gunAnimation:MovieClip;
  19.     private var cannonBase:Image;
  20.    
  21.     private var sc:Sprite;
  22.    
  23.     private var canFire:Bool = false;
  24.     //private var gunTimer:Timer;
  25.     private var gunTimeRemaining:Float = 1.0;
  26.  
  27.     public function new(board:Board)
  28.     {
  29.         super(board, 20);
  30.        
  31.         sc = new Sprite();
  32.         sc.pivotX = 8;
  33.         sc.pivotY = 8;
  34.        
  35.         cannonBase = new Image(Root.assets.getTexture("cannon/cannon_base"));
  36.         cannonBase.smoothing = 'none';
  37.         sc.addChild(cannonBase);
  38.        
  39.         gunAnimation = new MovieClip(Root.assets.getTextures("cannon/cannon_gun_"));
  40.         gunAnimation.smoothing = 'none';
  41.         gunAnimation.pivotX = 8;
  42.         gunAnimation.pivotY = 8;
  43.         gunAnimation.x = 8;
  44.         gunAnimation.y = 8;
  45.         gunAnimation.loop = false;
  46.         gunAnimation.stop();
  47.         sc.addChild(gunAnimation);
  48.        
  49.         collider = new BoxCollider(this, ["ai"], 12, 12, new Point(8, 8));
  50.         //collider.toggleDebug();
  51.         sc.addChild(collider);
  52.        
  53.         //gunTimer = new Timer(Math.round(Math.random() * 1000));
  54.         gunTimeRemaining = Math.random() * 1000;
  55.         //gunTimer.run = function() {
  56.             //canFire = true;
  57.             //gunTimer.stop();
  58.         //}
  59.        
  60.         this.addChild(sc);
  61.     }
  62.    
  63.     public override function death() {
  64.         super.death();
  65.        
  66.         var exp = new Explosion("explosion_32x32_1_");
  67.         exp.x = this.x;
  68.         exp.y = this.y;
  69.         board.addChild(exp);
  70.     }
  71.    
  72.     public override function update(event:EnterFrameEvent) {
  73.        
  74.         var player = board.getPlayer();
  75.         var player_pos = Point.fromDispObj(player);
  76.         var this_pos = Point.fromDispObj(this).add(new Point(0.5, 0.5));
  77.        
  78.         var angle = this_pos.angleBetween(player_pos);
  79.         gunAnimation.rotation = angle;
  80.        
  81.         if (gunAnimation.isPlaying) {
  82.             gunAnimation.advanceTime(event.passedTime);
  83.         } else if (gunAnimation.isComplete) {
  84.             gunAnimation.stop();
  85.         }
  86.        
  87.         if (!canFire) {
  88.             gunTimeRemaining -= event.passedTime * 1000;
  89.             if (gunTimeRemaining <= 0) {
  90.                 gunTimeRemaining = 0;
  91.                 canFire = true;
  92.             }
  93.         }
  94.        
  95.         if (canFire) {
  96.             canFire = false;
  97.             gunAnimation.play();
  98.            
  99.             var projectile = new CannonShell(this.board, gunAnimation.rotation);
  100.             projectile.x = 20;
  101.             projectile.y = 8;
  102.             board.addProjectile(projectile, gunAnimation);
  103.            
  104.             Root.assets.playSound("Cannon2", 0, 0, Root.sfxTransform(0.5));
  105.            
  106.             gunTimeRemaining = Math.random() * 200 + 800;
  107.             //gunTimer = new Timer(Math.round(Math.random() * 200) + 800);
  108.             //gunTimer.run = function() {
  109.                 //canFire = true;
  110.                 //gunAnimation.stop();
  111.                 //gunTimer.stop();
  112.             //}
  113.         }
  114.        
  115.     }
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement