Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package actors
  2. {
  3.     import flash.display.BitmapData;
  4.     import flash.geom.Point;
  5.     import flash.geom.Rectangle;
  6.    
  7.     import actors.Bullet;
  8.     import utils.IPoolable;
  9.     import utils.ObjectPool;
  10.    
  11.     public class EnemyShip extends Actor implements IPoolable
  12.     {
  13.         private var _bulletPool:ObjectPool = null;
  14.        
  15.         private var _pool:ObjectPool;
  16.         private var _nextObject:IPoolable;
  17.         private var _alive:Boolean = false;
  18.        
  19.         private var _time:int = 0;
  20.         private var _hitPoints:Number = 7;
  21.        
  22.         private var _vel:Number = 0;  //pixels per frame, magnitude
  23.         private var _velx:Number = 0; //pixels per frame
  24.         private var _vely:Number = 0; //pixels per frame
  25.         private var _acc:Number = 0;  //magnitude
  26.        
  27.         private var _a:Number = 0;
  28.         private var _b:Number = 0;
  29.         private var _c:Number = 0;
  30.         private var _d:Number = 0;
  31.         private var _e:Number = 0;
  32.         private var _f:Number = 0;
  33.         private var _g:Number = 0;
  34.        
  35.         public function EnemyShip(pool:ObjectPool)
  36.         {
  37.             super();
  38.             _pool = pool;
  39.         }
  40.        
  41.         public override function update():void
  42.         {
  43.             if (_alive) {
  44.                 super.update();
  45.             }
  46.         }
  47.        
  48.         public function revive():void
  49.         {
  50.             _alive = true;
  51.         }
  52.        
  53.         public function spawn(image:RotAnimSprite, funct:Function, bulletPool:ObjectPool, x:Number, y:Number):void {
  54.             gfx = image;
  55.             _behaviorFunction = funct;
  56.             _bulletPool = bulletPool;
  57.             _point.x = x - _gfx.width * 0.5;
  58.             _point.y = y - _gfx.height * 0.5;
  59.            
  60.             _a = 0;
  61.             _b = 0;
  62.             _c = 0;
  63.             _d = 0;
  64.             _e = 0;
  65.             _f = 0;
  66.             _g = 0
  67.            
  68.             revive();
  69.         }
  70.        
  71.         public function recycle():void
  72.         {
  73.             _pool.recycle(this);
  74.             _alive = false;
  75.         }
  76.        
  77.        
  78.         public function behaviorDefault():void {
  79.             defaultBehavior();
  80.         }
  81.         public function behaviorTestInit():void {
  82.             _vel = 0.4;
  83.             dir = 90;
  84.             _behaviorFunction = behaviorTest;
  85.         }
  86.         private function behaviorTest():void {
  87.             //Movement
  88.             //Does not work correctly
  89.             _f++;
  90.             if(_f > 20){
  91.                 if(Math.random() > 1.08 - ((_point.x - 319) * 0.01 + (_point.y - 19) * 0.02)){ //change direction randomly
  92.                     if (Math.random() < 0.5 + _g * 0.25) {
  93.                         dir = _dir - 45;        //left
  94.                         _g--;
  95.                     }
  96.                     else {
  97.                         dir = _dir + 45;        //right
  98.                         _g++;
  99.                     }
  100.                     _f = 0;
  101.                 }
  102.             }
  103.             _point.x += _velx;
  104.             _point.y += _vely;
  105.            
  106.             //Firing bullets
  107.             _time++;
  108.             _e++;
  109.             if (_e > 3) {
  110.                 if (_d == 0) {
  111.                     _d = 1;
  112.                     _e = 0;
  113.                 }
  114.             }
  115.             if (_e > 11) {
  116.                 if (_d == 1) {
  117.                     _d = 0;
  118.                     _e = 0;
  119.                 }
  120.             }
  121.             if(_d == 1){
  122.                 if (_time % 4 == 0) {
  123.                     _b += 1;
  124.                     var bullet:Bullet = null;
  125.                     _a += Math.random() + 0.5;
  126.                     if (_a > 2) {
  127.                         _a = Math.random() - 0.5;
  128.                         if (_a < 0) _c = -5 + _a * 7.5;
  129.                         else _c = 5 + _a * 7.5;
  130.                         _a = 0;
  131.                     }
  132.                     if(_c != 0){
  133.                         for (var i:int = 0; i < 3; i++) {
  134.                             bullet = Bullet(_bulletPool.getNext());
  135.                             bullet.fire(GameEngine.rotatableBulletRed, bullet.behaviorBasic, _point.x + 81, _point.y + 101, 70 + _c + 20 * i, 4.5 + _b * 0.025);
  136.                         }
  137.                         _c = 0;
  138.                     }
  139.                     else {
  140.                         for (i = 0; i < 3; i++) {
  141.                             bullet = Bullet(_bulletPool.getNext());
  142.                             bullet.fire(GameEngine.rotatableBulletRed, bullet.behaviorBasic, _point.x + 81, _point.y + 101, 70 + 20 * i, 5.5 + _b * 0.025);
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.             if (_time % 450 == 0) {
  148.                 _b = 0;
  149.             }
  150.         }
  151.        
  152.        
  153.         public function get pool():ObjectPool {
  154.             return _pool;
  155.         }
  156.         public function get nextObject():IPoolable {
  157.             return _nextObject;
  158.         }
  159.         public function set nextObject(obj:IPoolable):void {
  160.             _nextObject = obj;
  161.         }
  162.         public function get alive():Boolean {
  163.             return _alive;
  164.         }
  165.         private function set dir(a:Number):void {
  166.             _dir = a;
  167.             _velx = _vel * Math.cos(_dir * Main.CONVERT_TO_RADIANS);
  168.             _vely = _vel * Math.sin(_dir * Main.CONVERT_TO_RADIANS);
  169.         }
  170.        
  171.     }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement