Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.     import actors.Bullet;
  4.     import actors.EnemyShip;
  5.     import actors.RotAnimSprite;
  6.     import flash.display.Bitmap;
  7.     import flash.display.BitmapData;
  8.     import flash.display.Sprite;
  9.     import flash.events.Event;
  10.     import flash.events.TimerEvent;
  11.     import flash.geom.Matrix;
  12.     import flash.geom.Point;
  13.     import flash.geom.Rectangle;
  14.     import flash.text.TextField;
  15.     import flash.utils.getTimer;
  16.     import flash.utils.Timer;
  17.     import utils.IPoolable;
  18.     import utils.ObjectPool;
  19.     import utils.Profiler;
  20.    
  21.     /*
  22.      * GameEngine is a class that simulates stages that the player can play through.  It handles
  23.      * object creation and retention, as well as checking logic per frame.  GameEngine does not
  24.      * store the original assets for the game, although it will store prepared sprite sheets based
  25.      * off of the original assets.
  26.      */
  27.     public class GameEngine extends Sprite
  28.     {
  29.         private var stageBMP:Bitmap;
  30.         private var stageBMPData:BitmapData;
  31.        
  32.         public static var rotatableBulletRed:RotAnimSprite;
  33.         public static var rotatableBulletBlue:RotAnimSprite;
  34.         public static var rotatableBulletRainbow:RotAnimSprite;
  35.        
  36.         private var enemyTestShip:RotAnimSprite;
  37.        
  38.         //hud
  39.         private var bulletPool:ObjectPool = new ObjectPool(Bullet, 10000);
  40.         //bonuses
  41.         private var enemyPool:ObjectPool = new ObjectPool(EnemyShip, 100);
  42.         //player
  43.         //player bullets
  44.         //background
  45.        
  46.         private static var score:uint = 0;
  47.         private static var lives:int = 0;
  48.         private var rage:Number = 0;
  49.         private var hitCount:uint = 0;
  50.        
  51.         private var text:TextField;
  52.         private var maxCount:int = 0;
  53.        
  54.         private var timer:int = 0; //for profiling purposes, milliseconds
  55.         private var gameTime:int = 0;
  56.        
  57.         public function GameEngine()
  58.         {
  59.             mouseEnabled = false; //might add mouse controls later
  60.            
  61.             stageBMPData = new BitmapData(Main.STAGE_WIDTH, Main.STAGE_HEIGHT);
  62.             stageBMP = new Bitmap(stageBMPData, "auto", true);
  63.             addChild(stageBMP);
  64.            
  65.             text = new TextField();
  66.             text.text = "FPS: 0\nObjects: 0";
  67.             text.textColor = 0xFFFFFF;
  68.             text.selectable = false;
  69.             text.y = 45
  70.             addChild(text);
  71.            
  72.             addChild(Profiler.instance);
  73.            
  74.             var vector:Vector.<int> = new Vector.<int>();
  75.             vector.push(3, 3, 3, 3, 3, 3, 3, 3);
  76.             rotatableBulletRed = new RotAnimSprite(prepareAssets(Main.bulletRedGFX, 14, 10), 14, 72, true);
  77.             rotatableBulletBlue = new RotAnimSprite(prepareAssets(Main.bulletBlueGFX, 14, 10), 14, 72, true);
  78.             rotatableBulletRainbow = new RotAnimSprite(prepareAssets(Main.bulletRainbowGFX, 14, 10), 14, 72, true, vector);
  79.             enemyTestShip = new RotAnimSprite(prepareAssets(Main.enemyBasicTestShipGFX, 162, 64, 1), 162, 1);
  80.            
  81.             spawnTestEnemy();
  82.            
  83.             addEventListener(Event.ENTER_FRAME, update);
  84.             //addEventListener(Event.EXIT_FRAME, exit);
  85.         }
  86.        
  87.         private function exit(e:Event):void {
  88.             trace("lol");
  89.         }
  90.        
  91.         private function update(e:Event = null):void
  92.         {
  93.             Profiler.instance.updateIdleTime(getTimer() - timer);
  94.             timer = getTimer();
  95.            
  96.             gameTime++;
  97.             //detect player input
  98.             //create objects?
  99.             updateObjects();
  100.             //check collisions
  101.             //invoke reactions
  102.             updateText();
  103.            
  104.             Profiler.instance.updateCodeTime(getTimer() - timer);
  105.             timer = getTimer();
  106.            
  107.             stageBMPData.lock();
  108.             clearCanvas();
  109.             //drawBackground();
  110.             //drawPlayerBullets();
  111.             //drawPlayerShip();
  112.             drawEnemyShips();
  113.             //drawBonuses();
  114.             drawEnemyBullets();
  115.             //drawHUD();
  116.             stageBMPData.unlock();
  117.            
  118.             Profiler.instance.updateBlittingRenderTime(getTimer() - timer);
  119.             //Profiler.instance.getIdleTime();
  120.             timer = getTimer();
  121.         }
  122.        
  123.        
  124.         /***********************************************************************************
  125.          * Game state update methods
  126.          ***********************************************************************************/
  127.        
  128.         private function updateObjects():void
  129.         {
  130.             var poolable:IPoolable = null;
  131.             var p:Vector.<IPoolable> = bulletPool.pool;
  132.             for (var i:int = 0; i < p.length; i++) {
  133.                 poolable = p[i];
  134.                 Bullet(poolable).update();
  135.             }
  136.             p = enemyPool.pool;
  137.             for (i = 0; i < p.length; i++) {
  138.                 poolable = p[i];
  139.                 EnemyShip(poolable).update();
  140.             }
  141.         }
  142.        
  143.         private function updateText():void
  144.         {
  145.             if (maxCount < bulletPool.activeCount) maxCount = bulletPool.activeCount;
  146.             text.text = "Objects: " + bulletPool.activeCount + "\nMax Obj: " + maxCount;
  147.         }
  148.        
  149.        
  150.         /***********************************************************************************
  151.          * Draw methods
  152.          ***********************************************************************************/
  153.        
  154.         private function clearCanvas():void
  155.         {
  156.             stageBMPData.fillRect(new Rectangle(0, 0, Main.STAGE_WIDTH, Main.STAGE_HEIGHT), 0xFF000000);
  157.         }
  158.        
  159.         private function drawBackground():void
  160.         {
  161.             //stageBMPData.fillRect(new Rectangle(0, 0, Main.STAGE_WIDTH, Main.STAGE_HEIGHT), 0xFF000000);
  162.         }
  163.        
  164.         //private function drawPlayerBullets():void
  165.         //private function drawPlayerShip():void
  166.        
  167.         private function drawEnemyShips():void
  168.         {
  169.             var es:EnemyShip = null;
  170.             var pool:Vector.<IPoolable> = enemyPool.pool;
  171.             for (var i:int = 0; i < pool.length; i++) {
  172.                 es = EnemyShip(pool[i]);
  173.                 if (es.alive) {
  174.                     stageBMPData.copyPixels(es.gfx.spriteSheet, es.scrollRect, es.point, null, null, true);
  175.                 }
  176.             }
  177.         }
  178.            
  179.         //private function drawBonuses():void
  180.            
  181.         private function drawEnemyBullets():void
  182.         {
  183.             var b:Bullet = null;
  184.             var pool:Vector.<IPoolable> = bulletPool.pool;
  185.             for (var i:int = 0; i < pool.length; i++) {
  186.                 b = Bullet(pool[i]);
  187.                 if (b.alive) {
  188.                     stageBMPData.copyPixels(b.gfx.spriteSheet, b.scrollRect, b.point, null, null, true);
  189.                 }
  190.             }
  191.         }
  192.        
  193.         //private function drawHUD():void
  194.        
  195.        
  196.         /***********************************************************************************
  197.          * Initialization methods
  198.          ***********************************************************************************/
  199.        
  200.         private function prepareAssets(image:Bitmap, width:int, height:int, rotCount:int = 72):BitmapData
  201.         {
  202.             var n:int = width;
  203.             if (n < height) n = height;
  204.             var tileLength:int = image.width / width;
  205.             var bmd:BitmapData = new BitmapData(tileLength * n, n * rotCount, true, 0x0);
  206.             var tempbmd:BitmapData = new BitmapData(image.width, image.height, true, 0x0);
  207.             var m:Matrix = new Matrix();
  208.             var r:Rectangle = new Rectangle(0, 0, width, height);
  209.             var p:Point = new Point(0, 0);
  210.            
  211.             for (var i:int = 0; i < tileLength; i++) {
  212.                 r.x = 0;
  213.                 tempbmd.fillRect(r, 0x0);
  214.                 r.x = i * width;
  215.                 tempbmd.copyPixels(image.bitmapData, r, p, null, null, true);
  216.                 for (var j:int = 0; j < rotCount; j++) {
  217.                     m.identity(); //resets the matrix
  218.                     m.translate( -0.5 * width, -0.5 * height);
  219.                     m.rotate(j * 5 * Main.CONVERT_TO_RADIANS);
  220.                     m.translate(0.5 * width + i * n, n * (j + 0.5));
  221.                     bmd.draw(tempbmd, m, null, null, null, true);
  222.                 }
  223.             }
  224.            
  225.             return bmd;
  226.         }
  227.        
  228.        
  229.         /***********************************************************************************
  230.          * Enemy spawn methods
  231.          ***********************************************************************************/
  232.        
  233.         private function spawnTestEnemy():void {
  234.             var es:EnemyShip = EnemyShip(enemyPool.getNext());
  235.             es.spawn(enemyTestShip, es.behaviorTestInit, bulletPool, 400, 100);
  236.         }
  237.        
  238.     }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement