原文地址
上篇文章中我们深入理解了我在我的AS3 Starling塔防游戏Demo中使用的各种游戏状态(game States).我忽略了Play State因为它是老大一块且(代码)需要一些清理(cleaning up).现在我已经把它全部清理并做好了继续的准备!另外,我本打算在这篇文章末尾涉及(get into)Map Tiling,但这要花很长时间(ran long)且我想要展示下Config文件,因为用到的地方很多.所以Map Tiling将会以后涉及.这是PlayState和我的Config类.
快速说明...在这里我移除了函数所有的注释块以便节省空间...(你最好还是)注释你的代码!
在你开始之前,随便看看我完成的AS3 Starling塔防Demo,你也可以找到在srcView中使用的所有代码,或者你可以下载整个打包压缩的工程文件或者检出(check out)Bitbucket上面的repo.
Play State(com.zf.states.Play.as)是游戏的要点.就是在这个状态(State)里绘制地图,刷怪,摆放防御塔,且玩家希望能爽一把.在玩家在MapSelect State选择一个地图后,我们知道了其选择的地图ID然后实际是MapLoad State加载地图JSON文件并为Play(状态)处理它做好准备
Play也是游戏中使用的绝大部分"Manager"类的创建者和维护者.我们将从构造函数和addedToStage函数开始.因为这个类是如此重要,我在其中包含了几乎所有的东西.
package com.zf.states { import com.zf.core.Assets; import com.zf.core.Config; import com.zf.core.Game; import com.zf.managers.*; import com.zf.objects.map.Map; import flash.display.Stage; import flash.geom.Point; import flash.utils.getTimer; import starling.animation.Juggler; import starling.core.Starling; import starling.display.Sprite; import starling.events.Event; public class Play extends Sprite implements IZFState { public static var zfJuggler:Juggler; public static const GAME_OVER_HP : int = 0; public static const GAME_OVER_ENEMIES : int = 1; public static const GAME_OVER_QUIT : int = 2; public static const GAME_STATE_PAUSE : int = 0; public static const GAME_STATE_PLAY : int = 1; public static const GAME_STATE_END : int = 2; public static const GAME_STATE_OVER : int = 3; public static var gameState : int = GAME_STATE_PLAY; public static var gameOverState : int = -1; // Public Managers & Objects public var wpMgr : WaypointManager; public var towerMgr : TowerManager; public var keyMgr : KeyboardManager; public var bulletMgr : BulletManager; public var enemyMgr : EnemyManager; public var hitMgr : CollisionManager; public var hudMgr : HudManager; public var map : Map; public var ns : Stage; public var mapOffsetX:int = 36; public var mapOffsetY:int = 36; public var endGameOnNextFrame:Boolean = false; //Public state values public var isPaused:Boolean = false; public var currentGold:int = 100; public var mapLayer:Sprite; public var enemyLayer:Sprite; public var towerLayer:Sprite; public var hudLayer:Sprite; public var topLayer:Sprite; private var _game:Game; // Game Conditions private var _gameCondEndOfWaves:Boolean; private var _gameCondEndOfEnemies:Boolean; private var _zfMgrs:Vector.<IZFManager>; private var _isGameStartPause:Boolean; public function Play(g:Game) { _game = g; zfJuggler = new Juggler(); _zfMgrs = new Vector.<IZFManager>(); // reset Config variables for new game Config.resetForNewGame(); _gameCondEndOfWaves = false; _gameCondEndOfEnemies = false; addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); } private function onAddedToStage(evt:Event):void { removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); Starling.juggler.add(zfJuggler); ns = Starling.current.nativeStage; keyMgr = new KeyboardManager(this, ns); keyMgr.onPause.add(onPauseEvent); mapLayer = new Sprite(); addChild(mapLayer); enemyLayer = new Sprite(); addChild(enemyLayer); towerLayer = new Sprite(); addChild(towerLayer); hudLayer = new Sprite(); addChild(hudLayer); topLayer = new Sprite(); addChild(topLayer); enemyMgr = new EnemyManager(this); _zfMgrs.push(enemyMgr); hudMgr = new HudManager(this); _zfMgrs.push(hudMgr); wpMgr = new WaypointManager(this); towerMgr = new TowerManager(this , Assets.towerData); _zfMgrs.push(towerMgr); map = new Map(Config.currentMapData, wpMgr, mapOffsetX, mapOffsetY); map.x = mapOffsetX; map.y = mapOffsetY; mapLayer.addChild(map); bulletMgr = new BulletManager(this); _zfMgrs.push(bulletMgr); hitMgr = new CollisionManager(this); _zfMgrs.push(hitMgr); // Set up enemy data enemyMgr.handleNewMapData(Config.currentMapData.enemyData); /** * Set up signals listeners */ enemyMgr.onEnemyAdded.add(hitMgr.onEnemyAdded); enemyMgr.onEnemyRemoved.add(hitMgr.onEnemyRemoved); towerMgr.onTowerAdded.add(hitMgr.onTowerAdded); towerMgr.onTowerRemoved.add(hitMgr.onTowerRemoved); towerMgr.onTowerManagerRemovingEnemy.add(hitMgr.onEnemyRemoved); hudMgr.endOfHP.add(onEndOfHP); enemyMgr.endOfEnemies.add(onEndOfEnemies); // set the current start HP Config.changeCurrentHP(map.startHP, false); // set the current start Gold from map data Config.changeCurrentGold(map.startGold, false); hudMgr.showNextWaveButtons(); // update hud ui hudMgr.updateUI(); // this is the initial pause at the start of the game _isGameStartPause = true; // pause the game so nothing happens until we resume onGamePaused(); }