Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import actors.Bullet;
- import actors.EnemyShip;
- import actors.RotAnimSprite;
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.TimerEvent;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.text.TextField;
- import flash.utils.getTimer;
- import flash.utils.Timer;
- import utils.IPoolable;
- import utils.ObjectPool;
- import utils.Profiler;
- /*
- * GameEngine is a class that simulates stages that the player can play through. It handles
- * object creation and retention, as well as checking logic per frame. GameEngine does not
- * store the original assets for the game, although it will store prepared sprite sheets based
- * off of the original assets.
- */
- public class GameEngine extends Sprite
- {
- private var stageBMP:Bitmap;
- private var stageBMPData:BitmapData;
- public static var rotatableBulletRed:RotAnimSprite;
- public static var rotatableBulletBlue:RotAnimSprite;
- public static var rotatableBulletRainbow:RotAnimSprite;
- private var enemyTestShip:RotAnimSprite;
- //hud
- private var bulletPool:ObjectPool = new ObjectPool(Bullet, 10000);
- //bonuses
- private var enemyPool:ObjectPool = new ObjectPool(EnemyShip, 100);
- //player
- //player bullets
- //background
- private static var score:uint = 0;
- private static var lives:int = 0;
- private var rage:Number = 0;
- private var hitCount:uint = 0;
- private var text:TextField;
- private var maxCount:int = 0;
- private var timer:int = 0; //for profiling purposes, milliseconds
- private var gameTime:int = 0;
- public function GameEngine()
- {
- mouseEnabled = false; //might add mouse controls later
- stageBMPData = new BitmapData(Main.STAGE_WIDTH, Main.STAGE_HEIGHT);
- stageBMP = new Bitmap(stageBMPData, "auto", true);
- addChild(stageBMP);
- text = new TextField();
- text.text = "FPS: 0\nObjects: 0";
- text.textColor = 0xFFFFFF;
- text.selectable = false;
- text.y = 45
- addChild(text);
- addChild(Profiler.instance);
- var vector:Vector.<int> = new Vector.<int>();
- vector.push(3, 3, 3, 3, 3, 3, 3, 3);
- rotatableBulletRed = new RotAnimSprite(prepareAssets(Main.bulletRedGFX, 14, 10), 14, 72, true);
- rotatableBulletBlue = new RotAnimSprite(prepareAssets(Main.bulletBlueGFX, 14, 10), 14, 72, true);
- rotatableBulletRainbow = new RotAnimSprite(prepareAssets(Main.bulletRainbowGFX, 14, 10), 14, 72, true, vector);
- enemyTestShip = new RotAnimSprite(prepareAssets(Main.enemyBasicTestShipGFX, 162, 64, 1), 162, 1);
- spawnTestEnemy();
- addEventListener(Event.ENTER_FRAME, update);
- //addEventListener(Event.EXIT_FRAME, exit);
- }
- private function exit(e:Event):void {
- trace("lol");
- }
- private function update(e:Event = null):void
- {
- Profiler.instance.updateIdleTime(getTimer() - timer);
- timer = getTimer();
- gameTime++;
- //detect player input
- //create objects?
- updateObjects();
- //check collisions
- //invoke reactions
- updateText();
- Profiler.instance.updateCodeTime(getTimer() - timer);
- timer = getTimer();
- stageBMPData.lock();
- clearCanvas();
- //drawBackground();
- //drawPlayerBullets();
- //drawPlayerShip();
- drawEnemyShips();
- //drawBonuses();
- drawEnemyBullets();
- //drawHUD();
- stageBMPData.unlock();
- Profiler.instance.updateBlittingRenderTime(getTimer() - timer);
- //Profiler.instance.getIdleTime();
- timer = getTimer();
- }
- /***********************************************************************************
- * Game state update methods
- ***********************************************************************************/
- private function updateObjects():void
- {
- var poolable:IPoolable = null;
- var p:Vector.<IPoolable> = bulletPool.pool;
- for (var i:int = 0; i < p.length; i++) {
- poolable = p[i];
- Bullet(poolable).update();
- }
- p = enemyPool.pool;
- for (i = 0; i < p.length; i++) {
- poolable = p[i];
- EnemyShip(poolable).update();
- }
- }
- private function updateText():void
- {
- if (maxCount < bulletPool.activeCount) maxCount = bulletPool.activeCount;
- text.text = "Objects: " + bulletPool.activeCount + "\nMax Obj: " + maxCount;
- }
- /***********************************************************************************
- * Draw methods
- ***********************************************************************************/
- private function clearCanvas():void
- {
- stageBMPData.fillRect(new Rectangle(0, 0, Main.STAGE_WIDTH, Main.STAGE_HEIGHT), 0xFF000000);
- }
- private function drawBackground():void
- {
- //stageBMPData.fillRect(new Rectangle(0, 0, Main.STAGE_WIDTH, Main.STAGE_HEIGHT), 0xFF000000);
- }
- //private function drawPlayerBullets():void
- //private function drawPlayerShip():void
- private function drawEnemyShips():void
- {
- var es:EnemyShip = null;
- var pool:Vector.<IPoolable> = enemyPool.pool;
- for (var i:int = 0; i < pool.length; i++) {
- es = EnemyShip(pool[i]);
- if (es.alive) {
- stageBMPData.copyPixels(es.gfx.spriteSheet, es.scrollRect, es.point, null, null, true);
- }
- }
- }
- //private function drawBonuses():void
- private function drawEnemyBullets():void
- {
- var b:Bullet = null;
- var pool:Vector.<IPoolable> = bulletPool.pool;
- for (var i:int = 0; i < pool.length; i++) {
- b = Bullet(pool[i]);
- if (b.alive) {
- stageBMPData.copyPixels(b.gfx.spriteSheet, b.scrollRect, b.point, null, null, true);
- }
- }
- }
- //private function drawHUD():void
- /***********************************************************************************
- * Initialization methods
- ***********************************************************************************/
- private function prepareAssets(image:Bitmap, width:int, height:int, rotCount:int = 72):BitmapData
- {
- var n:int = width;
- if (n < height) n = height;
- var tileLength:int = image.width / width;
- var bmd:BitmapData = new BitmapData(tileLength * n, n * rotCount, true, 0x0);
- var tempbmd:BitmapData = new BitmapData(image.width, image.height, true, 0x0);
- var m:Matrix = new Matrix();
- var r:Rectangle = new Rectangle(0, 0, width, height);
- var p:Point = new Point(0, 0);
- for (var i:int = 0; i < tileLength; i++) {
- r.x = 0;
- tempbmd.fillRect(r, 0x0);
- r.x = i * width;
- tempbmd.copyPixels(image.bitmapData, r, p, null, null, true);
- for (var j:int = 0; j < rotCount; j++) {
- m.identity(); //resets the matrix
- m.translate( -0.5 * width, -0.5 * height);
- m.rotate(j * 5 * Main.CONVERT_TO_RADIANS);
- m.translate(0.5 * width + i * n, n * (j + 0.5));
- bmd.draw(tempbmd, m, null, null, null, true);
- }
- }
- return bmd;
- }
- /***********************************************************************************
- * Enemy spawn methods
- ***********************************************************************************/
- private function spawnTestEnemy():void {
- var es:EnemyShip = EnemyShip(enemyPool.getNext());
- es.spawn(enemyTestShip, es.behaviorTestInit, bulletPool, 400, 100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement