Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- /**
- * ...
- * @author Kristian
- */
- import flash.geom.Point;
- import org.flixel.FlxArray;
- import org.flixel.FlxSpriteEx;
- import org.flixel.FlxSprite;
- import org.flixel.FlxG;
- import flash.ui.Mouse;
- public class Ship extends FlxSpriteEx
- {
- [Embed(source = 'data/ship.png')] private var ImgPlayer:Class;
- [Embed(source = 'data/rotor.png')] private var ImgRotor:Class;
- [Embed(source = 'data/sndPlayerDie.mp3')] private var sndPlayerDie:Class;
- [Embed(source = 'data/sndBullet.mp3')] private var SndBullet:Class;
- [Embed(source = 'data/sndLaser.mp3')] private var SndLaser:Class;
- [Embed(source = 'data/sndMissile.mp3')] private var SndMissile:Class;
- [Embed(source = 'data/sndLaserMissile.mp3')] private var SndLaserMissile:Class;
- private const MOVE_SPEED:int = 400;
- private const SHOOT_SPEED:Number = 1.05;
- public static var destroyed:Boolean;
- private var rotor:FlxSprite;
- public static var ammo:FlxArray;
- public static var ammoType:String;
- private var shootTimer:Number, sideShoot:int, explosion:Explosion;
- public static var exX:FlxArray, exY:FlxArray , count:Number;
- private var bullet:Object;
- public function Ship()
- {
- super(FlxG.width / 2, FlxG.height / 2, ImgPlayer,false);
- angle = -90;
- PlayState.fire = false;
- maxVelocity = new Point(MOVE_SPEED, MOVE_SPEED);
- ammo = new FlxArray();
- ammo.push(0); ammo.push(0); ammo.push(0);
- shootTimer = SHOOT_SPEED;
- sideShoot = 0;
- exX = new FlxArray();
- exY = new FlxArray();
- exX.push(x); exX.push(x); exX.push(x);
- exY.push(y); exY.push(y); exY.push(y);
- count = 0;
- destroyed = false;
- rotor = new FlxSprite(300, 300, ImgRotor);
- rotor.offset = new Point( rotor.width / 2, rotor.height / 2);
- rotor.angularVelocity = 1000;
- PlayState.spriteLayer.add(rotor);
- PlayState.spriteLayer.add(this);
- }
- private function updatePositions():void
- {
- if (FlxG.keys.LEFT || FlxG.keys.A) velocity.x -= 50;
- if (FlxG.keys.RIGHT || FlxG.keys.D) velocity.x += 50;
- if (FlxG.keys.UP || FlxG.keys.W) velocity.y -= 50;
- if (FlxG.keys.DOWN || FlxG.keys.S) velocity.y += 50;
- if (velocity.x > 0) velocity.x -= 25;
- else if (velocity.x < 0) velocity.x += 25;
- if (velocity.y > 0) velocity.y -= 25;
- else if (velocity.y < 0) velocity.y += 25;
- if (x < 22) velocity.x += 75;
- else if ( (x +22) > FlxG.width) velocity.x -= 75;
- if (y < 22) velocity.y += 75;
- else if ((y + 22) > FlxG.height) velocity.y -= 75;
- angle = Math.atan2((FlxG.mouse.y - (y)), (FlxG.mouse.x - (x))) * 180 / Math.PI;
- if (velocity.x == 0) rotor.x = x +2 * Math.cos(2 * Math.PI * angle / 360);
- else if ( velocity.x < 0) rotor.x = x -3 + 2 * Math.cos(2 * Math.PI * angle / 360);
- else rotor.x = x + 2 + 3 * Math.cos(2 * Math.PI * angle / 360);
- if (velocity.y == 0) rotor.y = y + 2 * Math.sin(2 * Math.PI * angle / 360);
- else if ( velocity.y < 0)rotor.y = y - 3 + 2 * Math.sin(2 * Math.PI * angle / 360);
- else rotor.y = y + 3 + 2 * Math.sin(2 * Math.PI * angle / 360);
- }
- private function handleShooting():void
- {
- if (FlxG.mouse.pressed() && shootTimer == SHOOT_SPEED )
- {
- FlxG.mouse.update(FlxG.mouse.x, FlxG.mouse.y);
- if (PlayState.fire == false) PlayState.fire = true;
- if (ammo[2] > 0 && ammo[1] > 0 ) ammoType = "missile_laser";
- else if (ammo[2] > 0) ammoType = "missile";
- else if (ammo[1] > 0) ammoType = "laser";
- else ammoType = "bullet";
- switch (ammoType)
- {
- case "bullet" :
- if (ammo[0] <= 0)
- {
- if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(24 , -5, angle);}
- if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(24, 5 , angle); }
- }
- else
- {
- if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(22 , 0 , angle);}
- if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , 1 , angle + 5);}
- if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , -1 , angle-5);}
- if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , 2 , angle + 10);}
- if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(22, -2 , angle-10);}
- if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , 3 , angle + 15);}
- if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22, -3, angle-15);}
- }
- FlxG.play(SndBullet, FlxG.volume + 0.2);
- break;
- case "laser" :
- if (ammo[0] <= 0)
- {
- //if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init( 5 , -12, angle);}
- //if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init(5, 0 , angle);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init( 475 , -6, angle);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init( 475, 6 , angle);}
- }
- else
- {
- if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 0 , angle);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 1 , angle - 5);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, -1, angle+5);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 2 , angle - 10);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init(0, -2, angle+10);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 3 , angle - 15);}
- if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, -3, angle+15);}
- }
- FlxG.play(SndLaser, FlxG.volume + 0.2);
- break;
- case "missile":
- if (ammo[0] <= 0)
- {
- if (sideShoot == 0)
- {
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(5 , -12, angle); sideShoot = 1;}
- }
- else
- {
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(5, 14 , angle); sideShoot = 0;}
- }
- }
- else
- {
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 0 , angle);}
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 1 , angle + 5);}
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , -1 , angle-5);}
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 2 , angle + 10);}
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32, -2 , angle-10);}
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 3 , angle + 15);}
- if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32, -3, angle-15);}
- }
- FlxG.play(SndMissile, FlxG.volume + 0.2);
- break;
- case "missile_laser":
- if (ammo[0] <= 0)
- {
- if (sideShoot == 0)
- {
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(5 , -12, angle); sideShoot = 1;}
- }
- else
- {
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(5, 14 , angle); sideShoot = 0;}
- }
- }
- else
- {
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 0 , angle); }
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 1 , angle + 5);}
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , -1 , angle-5);}
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 2 , angle + 10);}
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32, -2 , angle-10);}
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 3 , angle + 15);}
- if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32, -3, angle-15);}
- }
- FlxG.play(SndLaserMissile, FlxG.volume + 0.2);
- break;
- }
- if (ammo[0] > 0) ammo[0]--;
- if (ammo[1] > 0) ammo[1]--;
- if (ammo[2] > 0) ammo[2]--;
- shootTimer --;
- }
- if (shootTimer < 0) shootTimer = SHOOT_SPEED;
- else if (shootTimer < SHOOT_SPEED) shootTimer -= FlxG.elapsed ;
- }
- override public function update():void
- {
- updatePositions();
- handleShooting();
- count++;
- if (count > 2) count = 0;
- exX[count] = x;
- exY[count] = y;
- if (destroyed)
- {
- PlayState.deadX = x;
- PlayState.deadY = y;
- FlxG.play(sndPlayerDie, FlxG.volume + 0.2);
- if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship");}
- if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship");}
- if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship"); }
- if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship"); }
- if (Global.dExplosions.length) { explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship"); }
- new PPart(x, y, angle,"ship"); new PPart(x, y, angle,"ship"); new PPart(x, y, angle,"ship"); new PPart(x, y, angle,"ship"); new PPart(x, y, angle,"ship");
- PlayState.spriteLayer._children.remove(rotor, true);
- PlayState.spriteLayer._children.remove(this, true);
- exists = false;
- }
- super.update();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement