Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package iceteroids
- {
- import flash.display.DisplayObject;
- import flash.geom.Point;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- public class BulletManager
- {
- private var allBullets:Array = new Array();
- private var activeBullets:Array = new Array();
- private var pooledBullets:Array = new Array();
- public function get ActiveBullets():Array { return activeBullets; }
- private var game:Game;
- private var speed:Number;
- private var lifespan:int;
- private var laserSound:Sound;
- private var laserChannel:SoundChannel;
- public function BulletManager(aGame:Game, aSize:int = 20, aSpeed:Number = 15, aLifetime:int = 60)
- {
- game = aGame;
- speed = aSpeed;
- lifespan = aLifetime;
- for ( var i:int = 0; i < aSize; i++ )
- {
- allBullets.push(new Bullet(game));
- }
- pooledBullets = allBullets.concat();
- laserSound = new LaserSound();
- }
- public function fireBullet(aPos:Point, aRot:Number, ivx:Number, ivy:Number):Boolean
- {
- // make sure we have bullets available in our pool
- if(pooledBullets.length > 0)
- {
- // if so, pop one off the end of the pool
- var b:Bullet = pooledBullets.pop();
- // calculate its movement values
- // which are compounded onto the passed in movement values
- var rotAsRad:Number = aRot*Math.PI/180;
- ivx += Math.cos(rotAsRad)*speed;
- ivy += Math.sin(rotAsRad)*speed;
- // tell the bullet to fire
- b.fire(aPos, aRot, ivx, ivy);
- // add the bullet to activeBullets, and the display list
- activeBullets.push(b);
- game.bLayer.addChild(b);
- laserChannel = laserSound.play();
- // we fired a bullet, so return true!
- return true;
- }
- // no bullets available, return false!
- return false;
- }
- public function update():void
- {
- // a temporary bullet holding var
- var b:Bullet;
- // iterate over activeBullets
- for (var i:int = 0; i < activeBullets.length; i++)
- {
- // cast activeBullets[i] as a Bullet and save it in b
- b = (activeBullets[i] as Bullet);
- // move the bullet
- b.move();
- // make the bullet older
- b.age++;
- // if the bullet is too old
- if(b.age > lifespan)
- {
- // remove it from activeBullets
- activeBullets.splice(i,1);
- // add it to pooledBullets
- pooledBullets.push(b);
- // remove it from the display list
- game.bLayer.removeChild(b);
- // decrement i (so we don't skip the bullet that was next)
- i--;
- }
- }
- }
- public function killBullet(b:Bullet, index:int):void
- {
- activeBullets.splice(index,1);
- pooledBullets.push(b);
- game.bLayer.removeChild(b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement