PageRenderTime 281ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/gameBoard/GameBoard.as

https://bitbucket.org/astrism/extinc
ActionScript | 299 lines | 236 code | 36 blank | 27 comment | 26 complexity | 110283b50c7da141637958894865e652 MD5 | raw file
  1. package com.gameBoard
  2. {
  3. import com.UI.UILoader;
  4. import com.attacks.Attack;
  5. import com.attacks.AttackEvent;
  6. import com.creatures.Entity;
  7. import com.creatures.EntityEvent;
  8. import com.lookup.AskTony;
  9. import com.statusBar.OverlayEvent;
  10. import flash.display.Sprite;
  11. import flash.events.Event;
  12. import flash.events.MouseEvent;
  13. import flash.geom.Point;
  14. import flash.geom.Rectangle;
  15. import flash.utils.Timer;
  16. import flash.utils.getTimer;
  17. public class GameBoard extends Sprite
  18. {
  19. public static var attackType:String = null;
  20. public function setAttackType($type:String):void
  21. {
  22. _entityLayer.mouseChildren = _entityLayer.mouseChildren = ($type == null);
  23. attackType = $type;
  24. }
  25. private var _masterTimer:Timer;
  26. private var _grid:Vector.<Vector.<Tile>>;
  27. private var entities:Vector.<Entity>;
  28. public function explode(target:Point):void
  29. {
  30. createEntity(target, AskTony.EXPLOSION);
  31. }
  32. public function GameBoard(_bg:UILoader, $type:Object) //$theGrid:Vector.<Vector.<Tile>>)
  33. {
  34. setAttackType(attackType);
  35. entities = new Vector.<Entity>();
  36. // _grid = $theGrid;
  37. // drawGrid();
  38. _bg.onComplete = onBgLoadComplete;
  39. _tileLayer.addChild(_bg);
  40. addChild(_tileLayer);
  41. // addChild(_hitArea);
  42. function onBgLoadComplete(e:Event):void {
  43. var count:int;
  44. var testPoint:Point;
  45. if($type[AskTony.DISTRACTOR] != null && $type[AskTony.DISTRACTOR] > 0)
  46. {
  47. createEntity(new Point(0,0), AskTony.DISTRACTOR);
  48. createEntity(new Point(_bg.width,0), AskTony.DISTRACTOR);
  49. createEntity(new Point(_bg.width,_bg.height), AskTony.DISTRACTOR);
  50. createEntity(new Point(0,_bg.height), AskTony.DISTRACTOR);
  51. delete $type[$type];
  52. }
  53. for(var enemyType:String in $type)
  54. {
  55. for(count = int($type[enemyType]); count > 0; count--)
  56. {
  57. testPoint = new Point(Math.random() * _bg.width, Math.random() * _bg.height);
  58. createEntity(testPoint, enemyType);
  59. // gameBoard.addEntity(new Entity(null, 100, testPoint, enemyType));
  60. }
  61. }
  62. var bound:Rectangle = _tileLayer.getBounds(this);
  63. _entityLayer.mouseEnabled = _entityLayer.mouseChildren = false;
  64. Entity.bounds = bound;
  65. addChild(_entityLayer);
  66. }
  67. addEventListener(Event.ADDED_TO_STAGE, init);
  68. addEventListener(MouseEvent.ROLL_OVER, onGameRollOver);
  69. }
  70. // EVENT LISTENERS
  71. private function onGameRollOver(e:MouseEvent):void
  72. {
  73. if(!attackType)
  74. return;
  75. notifyTool();
  76. }
  77. private function notifyTool():void
  78. {
  79. parent.dispatchEvent(new OverlayEvent(OverlayEvent.SHOW_MESSAGE, OverlayEvent.WEAPON_TIP, attackType));
  80. }
  81. //_weapons vector to track timing centrally from gameboard
  82. private var _attacks:Vector.<Attack> = new Vector.<Attack>();
  83. private var _attack:Attack;
  84. private var _startPoint:Point;
  85. private function onAttackClick(e:Event):void
  86. {
  87. if(attackType != null)
  88. {
  89. setAttackType(attackType);
  90. if(_attack == null)
  91. {
  92. _attack = new Attack(1, attackType);
  93. _attacks.push(_attack);
  94. _attack.addEventListener(AttackEvent.IN_FLIGHT, clearAttack);
  95. _attack.addEventListener(AttackEvent.FIRE, fireAttack);
  96. _attack.addEventListener(AttackEvent.FINISHED, finishAttack);
  97. _attack.drop(new Point(mouseX, mouseY));
  98. }
  99. else
  100. {
  101. _attack.dropAgain(new Point(mouseX, mouseY));
  102. }
  103. }
  104. }
  105. private function onSplode(e:EntityEvent):void
  106. {
  107. explode(e.entity.centerPoint);
  108. }
  109. private function onSplit(e:EntityEvent):void
  110. {
  111. if(entities.length < 40)
  112. {
  113. var entity:Entity = e.entity;
  114. createEntity(entity.centerPoint.add(new Point(Entity.TEMP_ENTITY_SIZE, Entity.TEMP_ENTITY_SIZE)), entity.type, entity.getHealth());
  115. }
  116. // explode(e.entity.centerPoint);
  117. }
  118. // private function onAttackUp(e:Event):void
  119. // {
  120. // _tileLayer.addEventListener(MouseEvent.MOUSE_DOWN, onAttackClick);
  121. // stage.removeEventListener(MouseEvent.MOUSE_UP, onAttackUp);
  122. //
  123. // _weapon.drop(new Point(mouseX, mouseY));
  124. // }
  125. private function clearAttack(attack:AttackEvent):void
  126. {
  127. _attack = null;
  128. setAttackType(null);
  129. dispatchEvent(new AttackEvent(attack.type, attack.bombType, attack.bombPosition));
  130. }
  131. private function fireAttack($attack:AttackEvent):void
  132. {
  133. createEntity($attack.bombPosition, $attack.bombType);
  134. }
  135. private function finishAttack(attack:AttackEvent):void
  136. {
  137. var weapon:Attack = attack.weapon;
  138. if(weapon != null)
  139. {
  140. weapon.removeEventListener(AttackEvent.IN_FLIGHT, clearAttack);
  141. weapon.removeEventListener(AttackEvent.FIRE, fireAttack);
  142. weapon.removeEventListener(AttackEvent.FINISHED, finishAttack);
  143. var index:int = _attacks.lastIndexOf(weapon);
  144. if(index >= 0)
  145. _attacks.splice(index, 1);
  146. }
  147. attackType = null;
  148. }
  149. private function tangoDown(e:EntityEvent):void
  150. {
  151. removeEntity(e.entity);
  152. }
  153. private function init(e:Event):void
  154. {
  155. removeEventListener(Event.ADDED_TO_STAGE, init);
  156. _tileLayer.addEventListener(MouseEvent.CLICK, onAttackClick);
  157. addEventListener(Event.ENTER_FRAME, mainLoop);
  158. }
  159. //UTILITY AND DRAW
  160. private var _tileLayer:Sprite = new Sprite();
  161. private var _entityLayer:Sprite = new Sprite();
  162. // private var _hitArea:Sprite = new Sprite();
  163. private static const OVERLAP_BUFFER:Number = 0.25;
  164. private function drawGrid():void
  165. {
  166. var drawPoint:Point = new Point();
  167. var tile:Tile;
  168. for each(var column:Vector.<Tile> in _grid)
  169. {
  170. for each(tile in column)
  171. {
  172. _tileLayer.addChild(tile);
  173. tile.x = drawPoint.x;
  174. tile.y = drawPoint.y;
  175. drawPoint.y += tile.height - OVERLAP_BUFFER;
  176. }
  177. drawPoint.x += tile.width - OVERLAP_BUFFER;
  178. drawPoint.y = 0;
  179. }
  180. }
  181. private var _deadList:Vector.<Entity> = new Vector.<Entity>();
  182. public function tick():void
  183. {
  184. var count:int;
  185. var entity:Entity;
  186. Entity.setMasterTime(Number(getTimer()) / 1000.0);
  187. for(count = (entities.length - 1); count >= 0; count--)
  188. {
  189. if(entities.length > count)
  190. entities[count].attackTick();
  191. }
  192. for(count = (_attacks.length - 1); count >= 0; count--) {
  193. _attacks[count].tick();
  194. }
  195. var index:int;
  196. for(count = _deadList.length - 1; count >= 0; count--)
  197. {
  198. index = entities.lastIndexOf(_deadList[count]);
  199. if(index >= 0)
  200. {
  201. dispatchEvent(new AttackEvent(AttackEvent.DEATH, entities[index].type, entities[index].centerPoint));
  202. entities.splice(index, 1);
  203. }
  204. }
  205. _deadList.length = 0;
  206. for(count = 0; count < entities.length; count++)
  207. entities[count].moveTick();
  208. }
  209. public function createEntity(point:Point, type:String, health:int = 100):void
  210. {
  211. var contructor:* = AskTony.classLookup[type];
  212. if(contructor != null)
  213. {
  214. var entity:Entity = new contructor(health, point);
  215. addEntity(entity);
  216. }
  217. }
  218. private function addEntity(newEntity:Entity):void
  219. {
  220. addEntityListeners(newEntity);
  221. newEntity.setHitList(entities);
  222. entities.push(newEntity);
  223. _entityLayer.addChild(newEntity.getGraphic());
  224. }
  225. public function removeEntity(deadEntity:Entity):void
  226. {
  227. var graphic:Sprite = deadEntity.getGraphic();
  228. //// deadEntity.getGraphic().alpha = 0.1;
  229. // graphic.scaleX = graphic.scaleY = 0.5;
  230. var index:int = entities.lastIndexOf(deadEntity);
  231. deadEntity.removeEventListener(EntityEvent.KILLED, tangoDown);
  232. _deadList.push(deadEntity);
  233. }
  234. private var _updater:uint = 0;
  235. private function mainLoop(e:Event):void
  236. {
  237. for(var count:uint = 0; count < entities.length; count++)
  238. entities[count].updateFearVector();
  239. // var lastCheck:uint = _updater + uint(entities.length / 24);
  240. // if(lastCheck >= entities.length)
  241. // lastCheck = entities.length - 1;
  242. //
  243. // for(_updater; _updater < lastCheck; _updater++)
  244. // entities[_updater].updateFearVector();
  245. //
  246. // if(_updater >= lastCheck)
  247. // _updater = 0;
  248. tick();
  249. }
  250. private function addEntityListeners(entity:Entity):void
  251. {
  252. entity.addEventListener(EntityEvent.KILLED, tangoDown);
  253. entity.addEventListener(EntityEvent.SPLODED, onSplode);
  254. entity.addEventListener(EntityEvent.SPLIT, onSplit);
  255. }
  256. private function removeEntityListeners(entity:Entity):void
  257. {
  258. entity.removeEventListener(EntityEvent.KILLED, tangoDown);
  259. entity.removeEventListener(EntityEvent.SPLODED, onSplode);
  260. entity.removeEventListener(EntityEvent.SPLIT, onSplit);
  261. }
  262. }
  263. }