Advertisement
Guest User

Untitled

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