PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/GameScreen.as

http://poucmoun.googlecode.com/
ActionScript | 778 lines | 632 code | 53 blank | 93 comment | 119 complexity | 600866100a39ecc14c2e040a86489039 MD5 | raw file
  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.net.URLRequest;
  5. import flash.net.URLLoader;
  6. import flash.display.Loader;
  7. import flash.display.Bitmap;
  8. import flash.geom.*;
  9. import flash.display.BitmapData;
  10. import flash.events.Event;
  11. import flash.utils.Timer;
  12. import flash.events.TimerEvent;
  13. import flash.events.KeyboardEvent;
  14. import flash.ui.Keyboard;
  15. import flash.utils.Dictionary;
  16. import Utils.Collision.*
  17. import GameObjects.GameObjectBase;
  18. import Characters.*;
  19. import Objects.WayPoint;
  20. import Objects.Tile;
  21. import Events.*;
  22. import Events.ParticleDyingEffectEvent;
  23. import Events.RedrawPointsEvent;
  24. import Powers.PowerBase;
  25. import Powers.WeakenEnemy;
  26. import Utils.LoaderLevelDatas;
  27. import Utils.Particles.ParticleLevelFadingEffect;
  28. import Utils.Particles.ParticleDyingEffect;
  29. import flash.text.TextField;
  30. import Managers.GameObjectsManager;
  31. import Managers.CharactersManager;
  32. import Objects.LevelDatas;
  33. import Utils.Particles.ParticleEffectBase;
  34. import GameObjects.Fruit;
  35. import Managers.ControllerManager;
  36. import flash.display.Sprite;
  37. public class GameScreen extends MovieClip
  38. {
  39. // Constantes
  40. private static var BEGINNING = 0;
  41. private static var WAITING_TO_START = 1;
  42. private static var PLAY = 2;
  43. private static var LOADING_LEVEL = 3;
  44. private static var NEXT_LEVEL = 4;
  45. private static var EXIT = 5;
  46. private static var LEVEL_FILE:String = "datas/level_datas/level_list.xml";
  47. private static var BLINK_TIMER:int = 1;
  48. private static var HALF_WAY_TILE_CHARAC_SIZE = 22;
  49. private static var TILE_CHARAC_SIZE = 44;
  50. private static var TILE_OBJS_SIZE = 35;
  51. // DEBUG
  52. private var m_debugImgLoader:Loader;
  53. private var m_debugBD:BitmapData;
  54. private var m_debugPosition:Point;
  55. // Screen
  56. private var m_tileMap:Vector.<Vector.<Tile>> = new Vector.<Vector.<Tile>>();
  57. private var m_backgroundTileMap:Vector.<Vector.<Tile>> = new Vector.<Vector.<Tile>>();
  58. private var m_screenB:Bitmap;
  59. private var m_screenBD:BitmapData;
  60. private var m_screenBDTilesCharacObj:BitmapData;
  61. private var m_screenBDNextLevel:BitmapData;
  62. private var mapRows:int = 16;
  63. private var mapCols:int = 18;
  64. private var tileSize:int = 36;
  65. private var screenWidth:int = mapCols * tileSize;
  66. private var screenHeight:int = mapRows * tileSize;
  67. //Game Timer
  68. private var gameTimer:Timer;
  69. // Managers
  70. private var m_gameObjectsManager:GameObjectsManager;
  71. private var m_characsManager:CharactersManager;
  72. private var m_controllerManager:ControllerManager;
  73. // Fruits
  74. private var m_fruitsTimer:int;
  75. private var m_fruitsWaiting:Boolean;
  76. // Particle Effects Array
  77. private var m_particleEffects:Vector.<ParticleEffectBase> = new Vector.<ParticleEffectBase>();
  78. // Screen state
  79. private var m_screenState:int;
  80. //Power
  81. private var m_powerAgainstEnemy:Vector.<PowerBase>;
  82. private var m_powerRef:Vector.<PowerBase>;
  83. // Level Data Loader
  84. private var m_loaderLevelDatas:LoaderLevelDatas;
  85. private var m_levelLoaded:Boolean;
  86. // alpha of the bitmap for fade in/out effect
  87. private var m_alphaBitmap:Number = 0.0;
  88. // Tiles sets
  89. private var m_wallsObjectsTiles:Tiles;
  90. private var m_characterTiles:CharactersTiles;
  91. // Loader
  92. private var m_xmlLevelLoader:URLLoader;
  93. // Current Level
  94. private var m_currentLevel:int;
  95. private var m_levelFiles:Array;
  96. // Exit variable
  97. private var m_exitScreenEvent:String;
  98. // Force action variable
  99. private var m_forceDrawLevel:Boolean;
  100. // Map that stores the points to redraw at each update
  101. private var m_dicPointsToRedraw:Dictionary;
  102. // Test
  103. private var m_gameMCTimerBlink:Number = BLINK_TIMER;
  104. private var m_firePressed:Boolean = false;
  105. private var m_screenBParticles:Bitmap;
  106. private var m_screenBDParticles:BitmapData;
  107. private var m_updateDebugTime:Number = 0.0;
  108. private var m_drawDebugTime:Number = 0.0;
  109. private var m_debugRectangle:BitmapData;
  110. private var m_debugRectangeB:Bitmap;
  111. private var m_debugRectangeBG:Bitmap;
  112. private var m_debugContainer:Sprite;
  113. public function GameScreen()
  114. {
  115. // Init Timer
  116. gameTimer = new Timer( 25 );
  117. gameTimer.addEventListener( TimerEvent.TIMER, updateGame, false, 1, true );
  118. gameTimer.addEventListener( TimerEvent.TIMER, drawGame, false, 0, true );
  119. // Stage is the main place where the game is drawn
  120. addEventListener( Event.ADDED_TO_STAGE, onAddToStage, false, 0, true );
  121. addEventListener( ScreenEvent.EXIT_SCREEN, onExitScreen, false, 0, true );
  122. // Init force variables
  123. m_forceDrawLevel = true;
  124. // Init power array
  125. m_powerAgainstEnemy = new Vector.<PowerBase>();
  126. m_powerRef = new Vector.<PowerBase>();
  127. // Tiles set loaded
  128. m_wallsObjectsTiles = new Tiles(576, 288);
  129. m_characterTiles = new CharactersTiles(351, 397);
  130. // Init the current level
  131. m_currentLevel = 0;
  132. m_levelFiles = new Array();
  133. // init the animation
  134. gotoAndStop(1);
  135. // Init the starting text variables to make sure it will be visible in front of the maze
  136. startingText.alpha = 0;
  137. startingText.z = 0;
  138. // Using the data in the array add to screenBitmapData
  139. m_screenBD = new BitmapData(screenWidth, screenHeight, true, 0);
  140. m_screenBDTilesCharacObj = new BitmapData(screenWidth, screenHeight, true);
  141. m_screenBDNextLevel = new BitmapData(screenWidth, screenHeight, false, 0);
  142. m_screenBDParticles = new BitmapData(screenWidth, screenHeight, true, 0);
  143. // Init InGame state
  144. m_screenState = BEGINNING;
  145. // Test
  146. m_screenBParticles = new Bitmap(m_screenBDParticles);
  147. m_screenBParticles.x = 36;
  148. m_screenBParticles.y = 36;
  149. addChild(m_screenBParticles);
  150. // Add the screenBitmap to display the level
  151. m_screenB = new Bitmap(m_screenBD);
  152. m_screenB.x = 36;
  153. m_screenB.y = 36;
  154. addChild(m_screenB);
  155. swapChildren(m_screenB, startingText);
  156. m_screenB.alpha = m_alphaBitmap;
  157. // Init exist screen event
  158. m_exitScreenEvent = "";
  159. // Stop displaying a yellow rectangle and losing the focus
  160. focusRect = false;
  161. // Init Level Loader
  162. m_loaderLevelDatas = new LoaderLevelDatas(36, m_screenBDNextLevel, m_wallsObjectsTiles, m_characterTiles);
  163. m_loaderLevelDatas.addEventListener( Event.COMPLETE, onRequestInitDatas, false, 0, true);
  164. addChild(m_loaderLevelDatas);
  165. // Create Managers
  166. m_gameObjectsManager = new GameObjectsManager(m_screenB);
  167. m_gameObjectsManager.addEventListener( ObjectEvent.NO_PACDOTS, onWinLevel, false, 0, true);
  168. m_gameObjectsManager.addEventListener( RedrawPointsEvent.GET_REDRAW_POINTS, onAddPointsToRedrawMap, false, 0, true);
  169. m_characsManager = new CharactersManager(m_screenB, m_screenBDTilesCharacObj);
  170. m_characsManager.addEventListener(ParticleDyingEffectEvent.PARTICLE_DYING_EFFECT, onGenerateParticleDyingEffect, false, 0, true);
  171. m_characsManager.addEventListener(ScreenEvent.GAME_OVER, onLoseLevel, false, 0, true);
  172. // Load levels
  173. m_xmlLevelLoader = new URLLoader();
  174. m_xmlLevelLoader.addEventListener(Event.COMPLETE, LoadLevelsName);
  175. m_xmlLevelLoader.load(new URLRequest(LEVEL_FILE));
  176. m_levelLoaded = false;
  177. }
  178. public function LoadLevelsName(e:Event):void
  179. {
  180. var xmlData:XML = new XML(m_xmlLevelLoader.data);
  181. var levelList:XMLList = xmlData.levelType;
  182. var levelXML:XML = levelList[i];
  183. var nameList:XMLList = levelXML.data.nameLevel;
  184. var nameCount:int = nameList.length();
  185. for ( var i:int = 0; i < nameCount; i++)
  186. {
  187. var nameLev:XML = nameList[i];
  188. var levelName:String = nameLev.@name;
  189. m_levelFiles.push(levelName);
  190. }
  191. // Load first Level
  192. m_loaderLevelDatas.loadMapXML(String(m_levelFiles[m_currentLevel]));
  193. }
  194. public function updateGame( timerEvent:TimerEvent ):void
  195. {
  196. if (m_screenState == BEGINNING)
  197. {
  198. if (currentFrame == 1)
  199. play();
  200. if (currentFrame == totalFrames)
  201. {
  202. m_screenState = WAITING_TO_START;
  203. startingText.alpha = 1;
  204. stop();
  205. }
  206. m_screenB.alpha += 0.02;
  207. }
  208. else if (m_screenState == WAITING_TO_START)
  209. {
  210. // Update the controls
  211. updateControls()
  212. // Update the Blink Timer for the Text
  213. if (m_gameMCTimerBlink <= 0)
  214. {
  215. m_gameMCTimerBlink = BLINK_TIMER;
  216. if (startingText.alpha == 1)
  217. startingText.alpha = 0;
  218. else
  219. startingText.alpha = 1
  220. }
  221. else
  222. m_gameMCTimerBlink -= 0.1;
  223. // Check if we press fire and want to start playing then
  224. if (ControllerManager.getKeyState(ControllerManager.FIRE))
  225. {
  226. m_gameMCTimerBlink = BLINK_TIMER;
  227. startingText.alpha = 0;
  228. m_screenState = PLAY;
  229. m_forceDrawLevel = false;
  230. m_debugRectangle = new BitmapData( 20, 20, false, 0xFF0000 );
  231. m_debugRectangeB = new Bitmap(m_debugRectangle);
  232. m_debugRectangle = new BitmapData( 20, 20, false, 0x00FF00 );
  233. m_debugRectangeBG = new Bitmap(m_debugRectangle);
  234. m_debugRectangeBG.x = 15.0;
  235. m_debugRectangeBG.y = 15.0;
  236. m_debugRectangeB.x = 10.0;
  237. m_debugRectangeB.y = 10.0;
  238. m_debugContainer = new Sprite();
  239. m_debugContainer.addChild(m_debugRectangeBG);
  240. m_debugContainer.addChild(m_debugRectangeB);
  241. addChild(m_debugContainer);
  242. //addChild(m_debugRectangeBG);
  243. //addChild(m_debugRectangeB);
  244. }
  245. }
  246. else if (m_screenState == PLAY)
  247. {
  248. var timerBeforeUpdate:Number = (new Date()).time;
  249. // Update the controls
  250. updateControls();
  251. // Update Characters
  252. m_characsManager.update();
  253. // Update the Collision between Objects and Characters
  254. var characterVect:Vector.<CharacterBase> = new Vector.<CharacterBase>()
  255. var hero:Hero = m_characsManager.getHero();
  256. for each (var character:CharacterBase in characterVect)
  257. m_gameObjectsManager.collisionWithCharac(character);
  258. if (hero != null)
  259. m_gameObjectsManager.collisionWithCharac(hero);
  260. // Update GameObjects
  261. m_gameObjectsManager.updateTimers();
  262. m_gameObjectsManager.update();
  263. // Update Powers
  264. updatePowers();
  265. // Update Particles
  266. updateParticles();
  267. // Update the score and life
  268. if (hero != null)
  269. {
  270. InputScoreText.text = String(hero.getScore());
  271. InputLifeText.text = String(hero.getLife());
  272. m_debugRectangeB.x += 0.5;
  273. m_debugRectangeB.y += 0.5;
  274. m_debugRectangeBG.x += 0.5;
  275. m_debugRectangeBG.y += 0.5;
  276. }
  277. var timerAfterUpdate:Number = (new Date()).time;
  278. if (m_updateDebugTime == 0.0)
  279. {
  280. m_updateDebugTime = timerAfterUpdate - timerBeforeUpdate;
  281. }
  282. else
  283. {
  284. m_updateDebugTime = (m_updateDebugTime + timerAfterUpdate - timerBeforeUpdate) / 2
  285. }
  286. }
  287. else if (m_screenState == LOADING_LEVEL)
  288. {
  289. addChild(m_loaderLevelDatas);
  290. // Create the new effect during the transition to the next level
  291. //var particleLevelLoadingEffect:ParticleLevelFadingEffect = new ParticleLevelFadingEffect(m_screenBDTilesCharacObj.width, m_screenBDTilesCharacObj.height, m_screenBDTilesCharacObj);
  292. //particleLevelLoadingEffect.init(m_screenBDTilesCharacObj, new Point());
  293. // Test stqrt
  294. //var particleLevelLoadingEffect:ParticleLevelFadingEffect = new ParticleLevelFadingEffect(m_screenBDParticles.width, m_screenBDParticles.height, m_screenBDParticles);
  295. //var positionToStartInitEffect:Point = new Point(m_screenBDParticles.width, m_screenBDParticles.height);
  296. //particleLevelLoadingEffect.init(m_screenBDParticles, positionToStartInitEffect);
  297. // Test end
  298. //m_particleEffects.push(particleLevelLoadingEffect);
  299. // Init the loader for the new level to load
  300. m_loaderLevelDatas.getLevelDatas().init();
  301. m_loaderLevelDatas.loadMapXML(String(m_levelFiles[m_currentLevel]));
  302. stopTimer();
  303. }
  304. else if (m_screenState == NEXT_LEVEL)
  305. {
  306. updateParticles();
  307. if (m_particleEffects.length == 0)
  308. {
  309. m_screenState = WAITING_TO_START;
  310. m_forceDrawLevel = true;
  311. startingText.alpha = 1;
  312. }
  313. }
  314. else if (m_screenState == EXIT)
  315. {
  316. if (currentFrame <= 1)
  317. {
  318. dispatchEvent( new ScreenEvent( m_exitScreenEvent ) );
  319. gameTimer.stop()
  320. }
  321. else
  322. {
  323. if (m_particleEffects.length > 0)
  324. {
  325. updateParticles();
  326. }
  327. prevFrame();
  328. m_screenB.alpha -= 0.02;
  329. }
  330. }
  331. }
  332. private function updatePowers():void
  333. {
  334. var indexPower:int = m_powerAgainstEnemy.length - 1;
  335. var powerObject:PowerBase;
  336. while (indexPower >= 0)
  337. {
  338. powerObject = m_powerAgainstEnemy[indexPower];
  339. if (powerObject.getPowerTimer() <= 0)
  340. m_powerAgainstEnemy.splice(indexPower, 1);
  341. else
  342. powerObject.update();
  343. indexPower--;
  344. }
  345. }
  346. private function updateParticles():void
  347. {
  348. var indexParticles:int = m_particleEffects.length - 1;
  349. var particleEffectObject:ParticleEffectBase;
  350. while (indexParticles >= 0)
  351. {
  352. particleEffectObject = m_particleEffects[indexParticles];
  353. particleEffectObject.update();
  354. if (particleEffectObject.isEffectFinished())
  355. m_particleEffects.splice(indexParticles,1);
  356. indexParticles--;
  357. }
  358. }
  359. public function drawGame(timerEvent:TimerEvent):void
  360. {
  361. var timerBeforeDraw:Number = (new Date()).time;
  362. // Fill in the map with the points to redraw
  363. fillRedrawPointsDic();
  364. // Draw Level
  365. drawLevel();
  366. // Draw Objectss
  367. drawObjects();
  368. // Draw Characters
  369. drawCharacters();
  370. // Draw Particles
  371. drawParticles();
  372. // Draw on bitmap object
  373. m_screenBD.draw(m_screenBDTilesCharacObj);
  374. // Test start
  375. //m_screenBDParticles.draw(m_screenBDParticles);
  376. // Test end
  377. m_dicPointsToRedraw = null;
  378. var timerAfterDraw:Number = (new Date()).time;
  379. if (m_drawDebugTime == 0.0)
  380. {
  381. m_drawDebugTime = timerAfterDraw - timerBeforeDraw;
  382. }
  383. else
  384. {
  385. m_drawDebugTime = (m_drawDebugTime + timerAfterDraw - timerBeforeDraw) / 2
  386. }
  387. }
  388. private function fillRedrawPointsDic()
  389. {
  390. if (!m_forceDrawLevel)
  391. {
  392. if (m_dicPointsToRedraw == null)
  393. m_dicPointsToRedraw = new Dictionary();
  394. var objectsVector:Vector.<GameObjectBase>;
  395. var fruitsVector:Vector.<Fruit>;
  396. var hero:Hero = m_characsManager.getHero();
  397. // Check the position of Poucmoun
  398. if (hero != null)
  399. addRedrawPointsMapFromCharac(hero.getPosition());
  400. // Check the previous position of Poucmoun
  401. if (hero != null)
  402. addRedrawPointsMapFromCharac(hero.getPreviousPosition());
  403. // Add the position of the other objects and characters
  404. var characterVect:Vector.<CharacterBase> = m_characsManager.getCharactersVector();
  405. for each ( var character:CharacterBase in characterVect)
  406. {
  407. addRedrawPointsMapFromCharac(character.getPosition());
  408. addRedrawPointsMapFromCharac(character.getPreviousPosition());
  409. }
  410. var gameObjec:GameObjectBase;
  411. objectsVector = m_gameObjectsManager.getGOAnimFix();
  412. if (objectsVector != null)
  413. {
  414. for each (gameObjec in objectsVector)
  415. addRedrawPointsMapFromObj(gameObjec.getPosition());
  416. }
  417. objectsVector = m_gameObjectsManager.getGOAnimMoving();
  418. if (objectsVector != null)
  419. {
  420. for each (gameObjec in objectsVector)
  421. addRedrawPointsMapFromObj(gameObjec.getPosition());
  422. }
  423. fruitsVector = m_gameObjectsManager.getFruits();
  424. if (objectsVector != null)
  425. {
  426. for each (var fruitObject:Fruit in fruitsVector)
  427. addRedrawPointsMapFromObj(fruitObject.getPosition());
  428. }
  429. }
  430. }
  431. private function drawLevel():void
  432. {
  433. var rect:Rectangle = new Rectangle(0, 0, tileSize, tileSize);
  434. var tileToUse:Tile;
  435. var destPoint:Point;
  436. if (m_screenBD != null && m_wallsObjectsTiles != null && m_tileMap.length > 0)
  437. {
  438. if (m_forceDrawLevel)
  439. {
  440. for (var rowCtr:int = 0 ; rowCtr < mapRows ; rowCtr++)
  441. {
  442. for (var colCtr:int = 0 ; colCtr < mapCols ; colCtr++)
  443. {
  444. destPoint = new Point(colCtr * tileSize, rowCtr * tileSize);
  445. tileToUse = Tile(m_backgroundTileMap[rowCtr][colCtr]);
  446. m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
  447. tileToUse = Tile(m_tileMap[rowCtr][colCtr]);
  448. if (tileToUse.getTileNumber() != -1)
  449. m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
  450. }
  451. }
  452. }
  453. else
  454. {
  455. var dicValues:Vector.<int>;
  456. var indexYCoord:int = 17;
  457. var row:int;
  458. var col:int;
  459. while (indexYCoord-- >= 0)
  460. {
  461. row = indexYCoord;
  462. dicValues = m_dicPointsToRedraw[indexYCoord];
  463. if (dicValues != null)
  464. {
  465. for each(var values:int in dicValues)
  466. {
  467. col = values;
  468. destPoint = new Point(col * tileSize, row * tileSize);
  469. tileToUse = Tile(m_backgroundTileMap[row][col]);
  470. m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
  471. tileToUse = Tile(m_tileMap[row][col]);
  472. m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. private function addRedrawPointsMapFromCharac(point:Point)
  480. {
  481. var positionTopLeft:Point = new Point(int(point.x / 36), int(point.y / 36));
  482. var positionTopLeftHWRight:Point = new Point(int((point.x + HALF_WAY_TILE_CHARAC_SIZE) / 36), int(point.y / 36));
  483. var positionTopRight:Point = new Point(int((point.x + TILE_CHARAC_SIZE) / 36), int(point.y / 36));
  484. var positionTopHWDownRight:Point = new Point(int((point.x + TILE_CHARAC_SIZE) / 36), int((point.y + HALF_WAY_TILE_CHARAC_SIZE) / 36));
  485. var positionDownRight:Point = new Point(int((point.x + TILE_CHARAC_SIZE) / 36), int((point.y + TILE_CHARAC_SIZE) / 36));
  486. var positionDownRightHWLeft:Point = new Point(int((point.x + HALF_WAY_TILE_CHARAC_SIZE) / 36), int((point.y + TILE_CHARAC_SIZE) / 36));
  487. var positionDownLeft:Point = new Point(int(point.x / 36), int((point.y + TILE_CHARAC_SIZE) / 36));
  488. var positionDownHWTopLeft:Point = new Point(int(point.x / 36), int((point.y + HALF_WAY_TILE_CHARAC_SIZE) / 36));
  489. var positionCentral:Point = new Point (int((point.x + HALF_WAY_TILE_CHARAC_SIZE) / 36), int((point.y + HALF_WAY_TILE_CHARAC_SIZE) / 36))
  490. // Start checking if we need to add each point to the map
  491. // Check the Top Left point
  492. checkPoint(positionTopLeft);
  493. // Check the point half way between Top Left and Top Right points
  494. checkPoint(positionTopLeftHWRight);
  495. // Check the Top Right point
  496. checkPoint(positionTopRight);
  497. // Check the point half way between Top Right and Down Right points
  498. checkPoint(positionTopHWDownRight);
  499. // Check the Down Right point
  500. checkPoint(positionDownRight);
  501. // Check the point half way between Down Right and Down Left points
  502. checkPoint(positionDownRightHWLeft);
  503. // Check the Down Left point
  504. checkPoint(positionDownLeft);
  505. // Check the point half way between Down Left and Top Left points
  506. checkPoint(positionDownHWTopLeft);
  507. // Check the Central poin
  508. checkPoint(positionCentral);
  509. }
  510. private function addRedrawPointsMapFromObj(point:Point)
  511. {
  512. var positionTopLeft:Point = new Point(int(point.x / 36), int(point.y / 36));
  513. var positionTopRight:Point = new Point(int((point.x + TILE_OBJS_SIZE) / 36), int(point.y / 36));
  514. var positionDownRight:Point = new Point(int((point.x + TILE_OBJS_SIZE) / 36), int((point.y + TILE_OBJS_SIZE) / 36));
  515. var positionDownLeft:Point = new Point(int(point.x / 36), int((point.y + TILE_OBJS_SIZE) / 36));
  516. // Start checking if we need to add each point to the map
  517. // Check the Top Left point
  518. checkPoint(positionTopLeft);
  519. // Check the Top Right point
  520. checkPoint(positionTopRight);
  521. // Check the Down Right point
  522. checkPoint(positionDownRight);
  523. // Check the Down Left point
  524. checkPoint(positionDownLeft);
  525. }
  526. private function checkPoint(pointToCheck:Point):void
  527. {
  528. if ((pointToCheck.x < 18) && (pointToCheck.y < 16))
  529. {
  530. var isValueInVector:Boolean = false;
  531. var valueInVector:int = 0;
  532. var xValuesVector:Vector.<int>;
  533. if (m_dicPointsToRedraw[pointToCheck.y] == null)
  534. m_dicPointsToRedraw[pointToCheck.y] = new Vector.<int>();
  535. xValuesVector = m_dicPointsToRedraw[pointToCheck.y];
  536. for each (var x_value:int in xValuesVector)
  537. {
  538. if (pointToCheck.x == x_value)
  539. {
  540. isValueInVector = true;
  541. break;
  542. }
  543. }
  544. if (!isValueInVector)
  545. xValuesVector.push(pointToCheck.x);
  546. }
  547. }
  548. private function drawObjects():void
  549. {
  550. if (m_screenState == WAITING_TO_START || m_screenState == BEGINNING)
  551. m_gameObjectsManager.forceDrawAll();
  552. else
  553. m_gameObjectsManager.draw(m_dicPointsToRedraw);
  554. }
  555. private function drawCharacters():void
  556. {
  557. m_characsManager.draw();
  558. }
  559. private function drawParticles():void
  560. {
  561. m_screenBDParticles.fillRect(new Rectangle(0, 0, m_screenBDParticles.width, m_screenBDParticles.height), 0);
  562. for each(var particleEffect:ParticleEffectBase in m_particleEffects)
  563. particleEffect.draw();
  564. }
  565. public function onExitScreen( screennEvent:ScreenEvent ):void
  566. {
  567. m_screenState = EXIT;
  568. }
  569. public function startTimer():void
  570. {
  571. gameTimer.start();
  572. }
  573. public function stopTimer():void
  574. {
  575. gameTimer.stop();
  576. }
  577. private function addListenerEventToPowers():void
  578. {
  579. var gameObjects:Vector.<GameObjectBase> = m_gameObjectsManager.getGOAnimFix();
  580. for (var indexObjects:int = 0; indexObjects < gameObjects.length; indexObjects++)
  581. if(gameObjects[indexObjects].getObjType() == GameObjectBase.BALL_WEAKEN_ENEMY)
  582. gameObjects[indexObjects].addEventListener(PowerEvent.WEAKEN_ENEMY, onWeakenEnemyPower, false, 0, true);
  583. }
  584. public function onRequestInitDatas(e:Event):void
  585. {
  586. var levelDatas:LevelDatas = m_loaderLevelDatas.getLevelDatas();
  587. m_characsManager.init(levelDatas.getCharacterVector(), levelDatas.getHero(), levelDatas.getTileMap());
  588. m_tileMap = levelDatas.getTileMap();
  589. m_backgroundTileMap = levelDatas.getBackgroundMap();
  590. m_powerRef = levelDatas.getPowerRef();
  591. m_screenBDTilesCharacObj = m_loaderLevelDatas.getScreenBD();
  592. // Set the Game Objects from the level datas
  593. m_gameObjectsManager.init(levelDatas.getGOStatic(),
  594. levelDatas.getGOAnimStatic(),
  595. levelDatas.getGOAnimMoving(),
  596. levelDatas.getGameObjRef(),
  597. levelDatas.getGameObjRandom(),
  598. levelDatas.getFruitPoints(),
  599. levelDatas.getObjectRandomPoints());
  600. // Re-init the loader of levels
  601. levelDatas.reset();
  602. removeChild(m_loaderLevelDatas);
  603. addListenerEventToPowers();
  604. if (m_currentLevel == 0)
  605. m_screenState = BEGINNING;
  606. else
  607. m_screenState = NEXT_LEVEL;
  608. startTimer();
  609. }
  610. public function onWinLevel(objectEvent:ObjectEvent):void
  611. {
  612. trace ("Change Level");
  613. m_currentLevel++;
  614. if (m_currentLevel < m_levelFiles.length)
  615. m_screenState = LOADING_LEVEL;
  616. else
  617. {
  618. m_screenState = EXIT;
  619. m_exitScreenEvent = ScreenEvent.WINNING_SCREEN;
  620. }
  621. }
  622. public function onLoseLevel(screenEvent:ScreenEvent):void
  623. {
  624. m_screenState = EXIT;
  625. m_exitScreenEvent = ScreenEvent.GAME_OVER;
  626. }
  627. public function onAddPointsToRedrawMap(redrawPoints:RedrawPointsEvent):void
  628. {
  629. var vectorObjPoints:Vector.<Point> = redrawPoints.getFruitsRedrawPoints();
  630. var vectorXCoordPoints:Vector.<int>;
  631. m_dicPointsToRedraw = new Dictionary();
  632. for each (var fruitRedrawPoint:Point in vectorObjPoints)
  633. {
  634. vectorXCoordPoints = m_dicPointsToRedraw[fruitRedrawPoint.y];
  635. if (vectorXCoordPoints == null)
  636. {
  637. vectorXCoordPoints = new Vector.<int>();
  638. m_dicPointsToRedraw[fruitRedrawPoint.y] = vectorXCoordPoints;
  639. }
  640. if (vectorXCoordPoints.indexOf(fruitRedrawPoint.x) == -1)
  641. vectorXCoordPoints.push(fruitRedrawPoint.x);
  642. }
  643. }
  644. public function onGenerateParticleDyingEffect (particleDyingEffectEvent:ParticleDyingEffectEvent):void
  645. {
  646. trace ("Generate Particle Effect");
  647. //Create the new effect during the transition to the next level
  648. //var particleDyingEffect:ParticleDyingEffect = new ParticleDyingEffect(particleDyingEffectEvent.getParentBD());
  649. //particleDyingEffect.init(particleDyingEffectEvent.getImageBD(), particleDyingEffectEvent.getInitPosition());
  650. //m_particleEffects.push(particleDyingEffect)
  651. }
  652. public function onWeakenEnemyPower( powerEvent:PowerEvent ):void
  653. {
  654. var found:Boolean = false;
  655. var isAllEnemyJailed:Boolean = true;
  656. var newPower:PowerBase;
  657. var enemyCharac:Enemy;
  658. var characterVect:Vector.<CharacterBase> = m_characsManager.getCharactersVector();
  659. // Check if at least one enemy isn't in Jail
  660. for each (var charac:CharacterBase in characterVect)
  661. {
  662. if (charac.getCharacterType() == CharacterBase.ENEMY)
  663. {
  664. enemyCharac = charac as Enemy;
  665. if (enemyCharac.getSubState() == Enemy.FREE)
  666. isAllEnemyJailed = false;
  667. }
  668. }
  669. // This way, we don't need to go through loops for nothing
  670. if (!isAllEnemyJailed)
  671. {
  672. var indexPower:int = 0;
  673. if (m_powerAgainstEnemy.length > 0)
  674. {
  675. for (; indexPower < m_powerAgainstEnemy.length && !found; indexPower++)
  676. if (m_powerAgainstEnemy[indexPower].getPowerType() == PowerBase.POWER_WEAKEN_ENEMY)
  677. {
  678. newPower = m_powerAgainstEnemy[indexPower];
  679. found = true;
  680. }
  681. }
  682. for (indexPower = 0; indexPower < m_powerRef.length; indexPower++)
  683. {
  684. if (m_powerRef[indexPower].getPowerType() == PowerBase.POWER_WEAKEN_ENEMY)
  685. {
  686. if (found)
  687. {
  688. newPower.setPowerTimer(newPower.getPowerTimer() + m_powerRef[indexPower].getPowerTimer());
  689. newPower.initPower();
  690. }
  691. else
  692. {
  693. newPower = new WeakenEnemy();
  694. newPower.copyObject(m_powerRef[indexPower]);
  695. newPower.initPower();
  696. m_powerAgainstEnemy.push(newPower);
  697. }
  698. }
  699. }
  700. }
  701. }
  702. private function onAddToStage( event:Event ):void
  703. {
  704. m_controllerManager = new ControllerManager();
  705. m_controllerManager.Initialise(stage);
  706. }
  707. private function updateControls():void
  708. {
  709. m_controllerManager.update();
  710. if (ControllerManager.getKeyState(ControllerManager.PAUSE))
  711. {
  712. if (m_screenState == PLAY)
  713. dispatchEvent( new SwitchScreenEvent( SwitchScreenEvent.SWITCH_TO_PAUSE ) );
  714. }
  715. if (ControllerManager.getKeyState(ControllerManager.DEBUG))
  716. {
  717. if (m_screenState == PLAY)
  718. {
  719. trace ("m_updateDebugTime : ", m_updateDebugTime);
  720. trace ("m_drawDebugTime : ", m_drawDebugTime);
  721. }
  722. }
  723. }
  724. }
  725. }