Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.     /**
  4.      * ...
  5.      * @author Kristian
  6.      */
  7.     import flash.geom.Point;
  8.     import org.flixel.FlxArray;
  9.     import org.flixel.FlxSpriteEx;
  10.     import org.flixel.FlxSprite;
  11.     import org.flixel.FlxG;
  12.     import flash.ui.Mouse;
  13.    
  14.    
  15.    
  16.     public class Ship extends FlxSpriteEx
  17.     {
  18.         [Embed(source = 'data/ship.png')] private var ImgPlayer:Class;
  19.         [Embed(source = 'data/rotor.png')] private var ImgRotor:Class;
  20.         [Embed(source = 'data/sndPlayerDie.mp3')] private var sndPlayerDie:Class;
  21.         [Embed(source = 'data/sndBullet.mp3')] private var SndBullet:Class;
  22.         [Embed(source = 'data/sndLaser.mp3')] private var SndLaser:Class;
  23.         [Embed(source = 'data/sndMissile.mp3')] private var SndMissile:Class;
  24.         [Embed(source = 'data/sndLaserMissile.mp3')] private var SndLaserMissile:Class;
  25.        
  26.        
  27.        
  28.         private const MOVE_SPEED:int = 400;
  29.         private const SHOOT_SPEED:Number = 1.05;
  30.        
  31.         public static var destroyed:Boolean;
  32.            
  33.         private var rotor:FlxSprite;
  34.  
  35.        
  36.         public static var ammo:FlxArray;
  37.         public static var ammoType:String;
  38.         private var shootTimer:Number, sideShoot:int, explosion:Explosion;
  39.        
  40.    
  41.        
  42.         public static var exX:FlxArray, exY:FlxArray , count:Number;
  43.        
  44.         private var bullet:Object;
  45.        
  46.        
  47.        
  48.    
  49.        
  50.         public function Ship()
  51.         {
  52.            
  53.            
  54.             super(FlxG.width / 2, FlxG.height / 2, ImgPlayer,false);
  55.             angle = -90;
  56.             PlayState.fire = false;
  57.        
  58.             maxVelocity = new Point(MOVE_SPEED, MOVE_SPEED);
  59.            
  60.             ammo = new FlxArray();
  61.             ammo.push(0); ammo.push(0); ammo.push(0);
  62.             shootTimer = SHOOT_SPEED;
  63.             sideShoot = 0;
  64.            
  65.             exX = new FlxArray();
  66.             exY = new FlxArray();
  67.             exX.push(x); exX.push(x); exX.push(x);
  68.             exY.push(y); exY.push(y); exY.push(y);
  69.             count = 0;
  70.             destroyed = false;
  71.  
  72.             rotor = new FlxSprite(300, 300, ImgRotor);
  73.             rotor.offset = new Point( rotor.width / 2, rotor.height / 2);
  74.             rotor.angularVelocity = 1000;
  75.             PlayState.spriteLayer.add(rotor);
  76.  
  77.            
  78.             PlayState.spriteLayer.add(this);
  79.  
  80.  
  81.         }
  82.        
  83.  
  84.        
  85.        
  86.         private function updatePositions():void
  87.         {
  88.             if (FlxG.keys.LEFT || FlxG.keys.A) velocity.x -= 50;
  89.             if (FlxG.keys.RIGHT || FlxG.keys.D) velocity.x += 50;
  90.  
  91.             if (FlxG.keys.UP || FlxG.keys.W) velocity.y -= 50;
  92.             if (FlxG.keys.DOWN || FlxG.keys.S) velocity.y += 50;
  93.            
  94.             if (velocity.x > 0) velocity.x -= 25;
  95.             else if (velocity.x < 0) velocity.x += 25;
  96.             if (velocity.y > 0) velocity.y -= 25;
  97.             else if (velocity.y < 0) velocity.y += 25;
  98.  
  99.  
  100.            
  101.             if (x < 22) velocity.x += 75;
  102.             else if ( (x +22) > FlxG.width) velocity.x -= 75;
  103.             if (y < 22) velocity.y += 75;
  104.             else if ((y + 22) > FlxG.height) velocity.y -= 75;
  105.  
  106.            
  107.             angle = Math.atan2((FlxG.mouse.y - (y)), (FlxG.mouse.x - (x))) * 180 / Math.PI;
  108.            
  109.             if (velocity.x == 0) rotor.x = x +2 * Math.cos(2 * Math.PI * angle / 360);
  110.             else if ( velocity.x < 0) rotor.x = x -3 + 2 * Math.cos(2 * Math.PI * angle / 360);
  111.             else  rotor.x = x + 2 + 3 * Math.cos(2 * Math.PI * angle / 360);
  112.            
  113.             if (velocity.y == 0) rotor.y = y + 2 * Math.sin(2 * Math.PI * angle / 360);
  114.             else if ( velocity.y < 0)rotor.y = y - 3 + 2 * Math.sin(2 * Math.PI * angle / 360);
  115.             else  rotor.y = y + 3 + 2 * Math.sin(2 * Math.PI * angle / 360);
  116.  
  117.  
  118.         }
  119.        
  120.         private function handleShooting():void
  121.         {
  122.        
  123.            
  124.    
  125.             if (FlxG.mouse.pressed() && shootTimer == SHOOT_SPEED )  
  126.             {
  127.                 FlxG.mouse.update(FlxG.mouse.x, FlxG.mouse.y);
  128.                 if (PlayState.fire == false) PlayState.fire = true;
  129.                
  130.                 if (ammo[2] > 0 && ammo[1] > 0 ) ammoType = "missile_laser";
  131.                 else if (ammo[2] > 0) ammoType = "missile";
  132.                 else if (ammo[1] > 0) ammoType = "laser";
  133.                 else ammoType = "bullet";
  134.  
  135.                
  136.                 switch (ammoType)
  137.                 {
  138.                     case "bullet" :
  139.                         if (ammo[0] <= 0)
  140.                         {
  141.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(24 , -5, angle);}
  142.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(24, 5 , angle); }
  143.                         }
  144.                         else
  145.                         {
  146.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(22 , 0 , angle);}
  147.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , 1 , angle + 5);}
  148.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , -1 , angle-5);}
  149.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , 2 , angle + 10);}
  150.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop(); bullet.init(22, -2 , angle-10);}
  151.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22 , 3 ,  angle + 15);}
  152.                             if(Global.dBullets.length) {bullet = Global.dBullets.pop();bullet.init(22, -3,  angle-15);}
  153.                         }
  154.                         FlxG.play(SndBullet, FlxG.volume + 0.2);
  155.                         break;
  156.                    
  157.                     case "laser" :
  158.                         if (ammo[0] <= 0)
  159.                         {
  160.                             //if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init( 5 , -12, angle);}
  161.                             //if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init(5, 0 , angle);}
  162.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init( 475 , -6, angle);}
  163.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init( 475, 6 , angle);}
  164.                        
  165.                         }
  166.                         else
  167.                         {
  168.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 0 , angle);}
  169.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 1 , angle - 5);}
  170.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, -1, angle+5);}
  171.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 2 , angle - 10);}
  172.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop(); bullet.init(0, -2, angle+10);}
  173.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, 3 ,  angle - 15);}
  174.                             if(Global.dLasers.length) {bullet = Global.dLasers.pop();bullet.init(0, -3,  angle+15);}
  175.                         }
  176.                        
  177.                         FlxG.play(SndLaser, FlxG.volume + 0.2);
  178.                         break;
  179.                    
  180.                     case "missile":
  181.                         if (ammo[0] <= 0)
  182.                         {
  183.                             if (sideShoot == 0)
  184.                             {
  185.                                 if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(5 , -12, angle); sideShoot = 1;}
  186.                             }
  187.                             else
  188.                             {
  189.                                 if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(5, 14 , angle); sideShoot = 0;}
  190.                             }  
  191.                         }
  192.                         else
  193.                         {
  194.                             if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 0 , angle);}
  195.                             if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 1 , angle + 5);}
  196.                             if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , -1 , angle-5);}
  197.                             if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 2 , angle + 10);}
  198.                             if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32, -2 , angle-10);}
  199.                             if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32 , 3 ,  angle + 15);}
  200.                             if(Global.dMissiles.length) {bullet = Global.dMissiles.pop(); bullet.init(32, -3,  angle-15);}
  201.                         }
  202.                         FlxG.play(SndMissile, FlxG.volume + 0.2);
  203.                         break;
  204.                    
  205.                     case "missile_laser":
  206.                         if (ammo[0] <= 0)
  207.                         {
  208.                             if (sideShoot == 0)
  209.                             {
  210.                                 if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(5 , -12, angle); sideShoot = 1;}
  211.                             }
  212.                             else
  213.                             {
  214.                                 if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(5, 14 , angle); sideShoot = 0;}
  215.                             }  
  216.                         }
  217.                         else
  218.                         {
  219.                             if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 0 , angle); }
  220.                             if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 1 , angle + 5);}
  221.                             if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , -1 , angle-5);}
  222.                             if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 2 , angle + 10);}
  223.                             if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32, -2 , angle-10);}
  224.                             if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32 , 3 ,  angle + 15);}
  225.                             if(Global.dLMissiles.length) {bullet = Global.dLMissiles.pop(); bullet.init(32, -3,  angle-15);}
  226.                         }
  227.                         FlxG.play(SndLaserMissile, FlxG.volume + 0.2);
  228.                         break;
  229.                    
  230.                    
  231.                 }
  232.                
  233.  
  234.                
  235.                 if (ammo[0] > 0) ammo[0]--;
  236.                 if (ammo[1] > 0) ammo[1]--;
  237.                 if (ammo[2] > 0) ammo[2]--;
  238.                 shootTimer --;
  239.                
  240.                
  241.             }
  242.            
  243.             if (shootTimer < 0) shootTimer = SHOOT_SPEED;
  244.             else if (shootTimer < SHOOT_SPEED) shootTimer -= FlxG.elapsed ;
  245.            
  246.            
  247.  
  248.            
  249.            
  250.         }
  251.        
  252.  
  253.         override public function update():void
  254.         {
  255.        
  256.        
  257.             updatePositions();
  258.             handleShooting();
  259.            
  260.              count++;
  261.             if (count > 2) count = 0;
  262.             exX[count] = x;
  263.             exY[count] = y;
  264.             if (destroyed)
  265.             {
  266.                 PlayState.deadX = x;
  267.                 PlayState.deadY = y;
  268.                 FlxG.play(sndPlayerDie, FlxG.volume + 0.2);
  269.                  if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship");}
  270.                  if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship");}
  271.                  if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship"); }
  272.                  if(Global.dExplosions.length) {explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship"); }
  273.                  if (Global.dExplosions.length) { explosion = Global.dExplosions.pop(); explosion.init(x, y, "ship"); }
  274.                 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");
  275.                 PlayState.spriteLayer._children.remove(rotor, true);
  276.                 PlayState.spriteLayer._children.remove(this, true);
  277.                 exists = false;
  278.             }
  279.  
  280.            
  281.    
  282.  
  283.             super.update();
  284.  
  285.  
  286.         }
  287.        
  288.     }
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement