Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package actors
  2. {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.geom.Point;
  6.     import flash.geom.Rectangle;
  7.    
  8.     import utils.IPoolable;
  9.     import utils.ObjectPool;
  10.    
  11.     public class Bullet extends Actor implements IPoolable
  12.     {
  13.         private var _pool:ObjectPool; //reference
  14.         private var _nextObject:IPoolable; //reference, singly linked list
  15.         private var _alive:Boolean = false;
  16.        
  17.         //private var _gfx:RotAnimSprite = null; //reference
  18.         //private var _scrollRect:Rectangle = null;
  19.         //private var _time:int = 0;
  20.         //private var _currFrame:int = 0;
  21.        
  22.         //private var _behaviorFunction:Function;
  23.        
  24.         private var _vel:Number = 0;  //pixels per frame, magnitude
  25.         private var _velx:Number = 0; //pixels per frame
  26.         private var _vely:Number = 0; //pixels per frame
  27.         private var _acc:Number = 0;  //magnitude
  28.         //private var _accx:Number = 0;
  29.         //private var _accy:Number = 0;
  30.         //private var _point:Point = null;
  31.         //private var _dir:Number = 0; //degrees
  32.         private var _veldir:Number = 0;
  33.         private var _accdir:Number = 0;
  34.        
  35.         private var _a:Number = 0; //misc factors
  36.         private var _b:Number = 0;
  37.         private var _c:Number = 0;
  38.        
  39.         public function Bullet(pool:ObjectPool)
  40.         {
  41.             super();
  42.             this._pool = pool;
  43.         }
  44.        
  45.         public override function update():void
  46.         {
  47.             if (_alive) {
  48.                 super.update();
  49.             }
  50.         }
  51.        
  52.         public function revive():void
  53.         {
  54.             _alive = true;
  55.         }
  56.        
  57.         //Defines new attributes and behavior for the bullet object.  Some attributes passed may
  58.         //not be used by the defined behavior.
  59.         public function fire(image:RotAnimSprite, funct:Function, x:Number, y:Number, dir:Number, vel:Number, acc:Number = 0, veldir:Number = 0, accdir:Number = 0):void
  60.         {
  61.             if (funct != null) {
  62.                 _behaviorFunction = funct;
  63.             }
  64.            
  65.             gfx = image;
  66.             resetAnimation();
  67.            
  68.             _point.x = x - _gfx.width * 0.5;
  69.             _point.y = y - _gfx.height * 0.5;
  70.             _dir = dir;
  71.             _vel = vel;
  72.             _velx = _vel * Math.cos(_dir * Main.CONVERT_TO_RADIANS);
  73.             _vely = _vel * Math.sin(_dir * Main.CONVERT_TO_RADIANS);
  74.             _acc = acc;
  75.             //_accx = _acc * Math.cos(_dir * Main.CONVERT_TO_RADIANS);
  76.             //_accy = _acc * Math.sin(_dir * Main.CONVERT_TO_RADIANS);
  77.             _veldir = veldir;
  78.             _accdir = accdir;
  79.            
  80.             revive();
  81.         }
  82.        
  83.         public function recycle():void
  84.         {
  85.             _pool.recycle(this);
  86.             _alive = false;
  87.         }
  88.        
  89.        
  90.         public function behaviorBasic():void {
  91.             _point.x += _velx;
  92.             _point.y += _vely;
  93.             if (checkOutOfScreen()) recycle();
  94.         }
  95.         public function behaviorAdvanced():void {
  96.             var n:int = (_vel + _acc) / _vel;
  97.             _velx *= n;
  98.             _vely *= n;
  99.             _point.x += _velx;
  100.             _point.y += _vely;
  101.             if (checkOutOfScreen()) recycle();
  102.         }
  103.         public function behaviorSpiral():void {
  104.             _dir += _veldir;
  105.             _velx = _vel * Math.cos(_dir * Main.CONVERT_TO_RADIANS);
  106.             _vely = _vel * Math.sin(_dir * Main.CONVERT_TO_RADIANS);
  107.             _point.x += _velx;
  108.             _point.y += _vely;
  109.             if (checkOutOfScreen()) recycle();
  110.         }
  111.        
  112.         public function get alive():Boolean {
  113.             return _alive;
  114.         }
  115.         public function get nextObject():IPoolable {
  116.             return _nextObject;
  117.         }
  118.         public function set nextObject(obj:IPoolable):void {
  119.             _nextObject = obj;
  120.         }
  121.         public function get pool():ObjectPool {
  122.             return _pool;
  123.         }
  124.         public override function trash():void
  125.         {
  126.             super.trash();
  127.             _pool = null;
  128.             _nextObject = null;
  129.             _gfx = null;
  130.             _vel = 0;
  131.             _velx = 0;
  132.             _vely = 0;
  133.             _acc = 0;
  134.             //_accx = 0;
  135.             //_accy = 0;
  136.             _point = null;
  137.             _veldir = 0;
  138.             _accdir = 0;
  139.             _alive = false;
  140.             _behaviorFunction = null;
  141.             //this = null;
  142.         }
  143.        
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement