Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package iceteroids
  2. {
  3.     import flash.display.DisplayObject;
  4.     import flash.geom.Point;
  5.     import flash.media.Sound;
  6.     import flash.media.SoundChannel;
  7.    
  8.     import iceteroids.sound.SoundManager;
  9.  
  10.  
  11.     public class BulletManager
  12.     {
  13.         private var allBullets:Array = new Array();
  14.         private var activeBullets:Array = new Array();
  15.         private var pooledBullets:Array = new Array();
  16.         public function get ActiveBullets():Array { return activeBullets; }
  17.        
  18.         private var game:Game;
  19.         private var speed:Number;
  20.         private var lifespan:int;
  21.        
  22.         private var laserSound:Sound;
  23.         private var laserChannel:SoundChannel;
  24.        
  25.         public function BulletManager(aGame:Game, aSize:int = 20, aSpeed:Number = 15, aLifetime:int = 60)
  26.         {
  27.             game = aGame;
  28.             speed = aSpeed;
  29.             lifespan = aLifetime;
  30.            
  31.             for ( var i:int = 0; i < aSize; i++ )
  32.             {
  33.                 allBullets.push(new Bullet(game));
  34.             }
  35.            
  36.             pooledBullets = allBullets.concat();
  37.            
  38.             laserSound = new LaserSound();
  39.         }
  40.        
  41.         public function fireBullet(aPos:Point, aRot:Number, ivx:Number, ivy:Number):Boolean
  42.         {
  43.             // make sure we have bullets available in our pool
  44.             if(pooledBullets.length > 0)
  45.             {
  46.                 // if so, pop one off the end of the pool
  47.                 var b:Bullet = pooledBullets.pop();
  48.                
  49.                 // calculate its movement values
  50.                 // which are compounded onto the passed in movement values
  51.                 var rotAsRad:Number = aRot*Math.PI/180;
  52.                 ivx += Math.cos(rotAsRad)*speed;
  53.                 ivy += Math.sin(rotAsRad)*speed;
  54.                
  55.                 // tell the bullet to fire
  56.                 b.fire(aPos, aRot, ivx, ivy);
  57.                
  58.                 // add the bullet to activeBullets, and the display list
  59.                 activeBullets.push(b);
  60.                 game.bLayer.addChild(b);
  61.                
  62.                
  63.                 laserChannel = laserSound.play();
  64.                
  65.                 // we fired a bullet, so return true!
  66.                 return true;
  67.             }
  68.             // no bullets available, return false!
  69.             return false;
  70.         }
  71.        
  72.         public function update():void
  73.         {
  74.             // a temporary bullet holding var
  75.             var b:Bullet;
  76.            
  77.             // iterate over activeBullets
  78.             for (var i:int = 0; i < activeBullets.length; i++)
  79.             {
  80.                 // cast activeBullets[i] as a Bullet and save it in b
  81.                 b = (activeBullets[i] as Bullet);
  82.                
  83.                 // move the bullet
  84.                 b.move();
  85.                
  86.                 // make the bullet older
  87.                 b.age++;
  88.                
  89.                 // if the bullet is too old
  90.                 if(b.age > lifespan)
  91.                 {
  92.                     // remove it from activeBullets
  93.                     activeBullets.splice(i,1);
  94.                     // add it to pooledBullets
  95.                     pooledBullets.push(b);
  96.                     // remove it from the display list
  97.                     game.bLayer.removeChild(b);
  98.                     // decrement i (so we don't skip the bullet that was next)
  99.                     i--;
  100.                 }
  101.             }
  102.         }
  103.        
  104.         public function killBullet(b:Bullet, index:int):void
  105.         {
  106.             activeBullets.splice(index,1);
  107.             pooledBullets.push(b);
  108.             game.bLayer.removeChild(b);
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement