/classes/GameScreen.as
http://poucmoun.googlecode.com/ · ActionScript · 778 lines · 632 code · 53 blank · 93 comment · 119 complexity · 600866100a39ecc14c2e040a86489039 MD5 · raw file
- package
- {
- import flash.display.MovieClip;
- import flash.net.URLRequest;
- import flash.net.URLLoader;
- import flash.display.Loader;
- import flash.display.Bitmap;
- import flash.geom.*;
- import flash.display.BitmapData;
- import flash.events.Event;
- import flash.utils.Timer;
- import flash.events.TimerEvent;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
- import flash.utils.Dictionary;
- import Utils.Collision.*
- import GameObjects.GameObjectBase;
- import Characters.*;
- import Objects.WayPoint;
- import Objects.Tile;
- import Events.*;
- import Events.ParticleDyingEffectEvent;
- import Events.RedrawPointsEvent;
- import Powers.PowerBase;
- import Powers.WeakenEnemy;
- import Utils.LoaderLevelDatas;
- import Utils.Particles.ParticleLevelFadingEffect;
- import Utils.Particles.ParticleDyingEffect;
- import flash.text.TextField;
- import Managers.GameObjectsManager;
- import Managers.CharactersManager;
- import Objects.LevelDatas;
- import Utils.Particles.ParticleEffectBase;
- import GameObjects.Fruit;
- import Managers.ControllerManager;
- import flash.display.Sprite;
-
- public class GameScreen extends MovieClip
- {
- // Constantes
- private static var BEGINNING = 0;
- private static var WAITING_TO_START = 1;
- private static var PLAY = 2;
- private static var LOADING_LEVEL = 3;
- private static var NEXT_LEVEL = 4;
- private static var EXIT = 5;
-
- private static var LEVEL_FILE:String = "datas/level_datas/level_list.xml";
-
- private static var BLINK_TIMER:int = 1;
-
- private static var HALF_WAY_TILE_CHARAC_SIZE = 22;
- private static var TILE_CHARAC_SIZE = 44;
- private static var TILE_OBJS_SIZE = 35;
- // DEBUG
- private var m_debugImgLoader:Loader;
- private var m_debugBD:BitmapData;
- private var m_debugPosition:Point;
- // Screen
- private var m_tileMap:Vector.<Vector.<Tile>> = new Vector.<Vector.<Tile>>();
- private var m_backgroundTileMap:Vector.<Vector.<Tile>> = new Vector.<Vector.<Tile>>();
- private var m_screenB:Bitmap;
- private var m_screenBD:BitmapData;
- private var m_screenBDTilesCharacObj:BitmapData;
- private var m_screenBDNextLevel:BitmapData;
- private var mapRows:int = 16;
- private var mapCols:int = 18;
- private var tileSize:int = 36;
- private var screenWidth:int = mapCols * tileSize;
- private var screenHeight:int = mapRows * tileSize;
- //Game Timer
- private var gameTimer:Timer;
- // Managers
- private var m_gameObjectsManager:GameObjectsManager;
- private var m_characsManager:CharactersManager;
- private var m_controllerManager:ControllerManager;
- // Fruits
- private var m_fruitsTimer:int;
- private var m_fruitsWaiting:Boolean;
- // Particle Effects Array
- private var m_particleEffects:Vector.<ParticleEffectBase> = new Vector.<ParticleEffectBase>();
- // Screen state
- private var m_screenState:int;
- //Power
- private var m_powerAgainstEnemy:Vector.<PowerBase>;
- private var m_powerRef:Vector.<PowerBase>;
- // Level Data Loader
- private var m_loaderLevelDatas:LoaderLevelDatas;
- private var m_levelLoaded:Boolean;
- // alpha of the bitmap for fade in/out effect
- private var m_alphaBitmap:Number = 0.0;
- // Tiles sets
- private var m_wallsObjectsTiles:Tiles;
- private var m_characterTiles:CharactersTiles;
- // Loader
- private var m_xmlLevelLoader:URLLoader;
- // Current Level
- private var m_currentLevel:int;
- private var m_levelFiles:Array;
- // Exit variable
- private var m_exitScreenEvent:String;
- // Force action variable
- private var m_forceDrawLevel:Boolean;
- // Map that stores the points to redraw at each update
- private var m_dicPointsToRedraw:Dictionary;
- // Test
- private var m_gameMCTimerBlink:Number = BLINK_TIMER;
- private var m_firePressed:Boolean = false;
- private var m_screenBParticles:Bitmap;
- private var m_screenBDParticles:BitmapData;
- private var m_updateDebugTime:Number = 0.0;
- private var m_drawDebugTime:Number = 0.0;
- private var m_debugRectangle:BitmapData;
- private var m_debugRectangeB:Bitmap;
- private var m_debugRectangeBG:Bitmap;
- private var m_debugContainer:Sprite;
-
- public function GameScreen()
- {
- // Init Timer
- gameTimer = new Timer( 25 );
- gameTimer.addEventListener( TimerEvent.TIMER, updateGame, false, 1, true );
- gameTimer.addEventListener( TimerEvent.TIMER, drawGame, false, 0, true );
-
- // Stage is the main place where the game is drawn
- addEventListener( Event.ADDED_TO_STAGE, onAddToStage, false, 0, true );
- addEventListener( ScreenEvent.EXIT_SCREEN, onExitScreen, false, 0, true );
- // Init force variables
- m_forceDrawLevel = true;
-
- // Init power array
- m_powerAgainstEnemy = new Vector.<PowerBase>();
- m_powerRef = new Vector.<PowerBase>();
-
- // Tiles set loaded
- m_wallsObjectsTiles = new Tiles(576, 288);
- m_characterTiles = new CharactersTiles(351, 397);
-
- // Init the current level
- m_currentLevel = 0;
- m_levelFiles = new Array();
-
- // init the animation
- gotoAndStop(1);
-
- // Init the starting text variables to make sure it will be visible in front of the maze
- startingText.alpha = 0;
- startingText.z = 0;
-
- // Using the data in the array add to screenBitmapData
- m_screenBD = new BitmapData(screenWidth, screenHeight, true, 0);
- m_screenBDTilesCharacObj = new BitmapData(screenWidth, screenHeight, true);
- m_screenBDNextLevel = new BitmapData(screenWidth, screenHeight, false, 0);
- m_screenBDParticles = new BitmapData(screenWidth, screenHeight, true, 0);
-
- // Init InGame state
- m_screenState = BEGINNING;
-
- // Test
- m_screenBParticles = new Bitmap(m_screenBDParticles);
- m_screenBParticles.x = 36;
- m_screenBParticles.y = 36;
- addChild(m_screenBParticles);
-
- // Add the screenBitmap to display the level
- m_screenB = new Bitmap(m_screenBD);
- m_screenB.x = 36;
- m_screenB.y = 36;
- addChild(m_screenB);
- swapChildren(m_screenB, startingText);
- m_screenB.alpha = m_alphaBitmap;
-
- // Init exist screen event
- m_exitScreenEvent = "";
-
- // Stop displaying a yellow rectangle and losing the focus
- focusRect = false;
-
- // Init Level Loader
- m_loaderLevelDatas = new LoaderLevelDatas(36, m_screenBDNextLevel, m_wallsObjectsTiles, m_characterTiles);
- m_loaderLevelDatas.addEventListener( Event.COMPLETE, onRequestInitDatas, false, 0, true);
- addChild(m_loaderLevelDatas);
-
- // Create Managers
- m_gameObjectsManager = new GameObjectsManager(m_screenB);
- m_gameObjectsManager.addEventListener( ObjectEvent.NO_PACDOTS, onWinLevel, false, 0, true);
- m_gameObjectsManager.addEventListener( RedrawPointsEvent.GET_REDRAW_POINTS, onAddPointsToRedrawMap, false, 0, true);
- m_characsManager = new CharactersManager(m_screenB, m_screenBDTilesCharacObj);
- m_characsManager.addEventListener(ParticleDyingEffectEvent.PARTICLE_DYING_EFFECT, onGenerateParticleDyingEffect, false, 0, true);
- m_characsManager.addEventListener(ScreenEvent.GAME_OVER, onLoseLevel, false, 0, true);
-
- // Load levels
- m_xmlLevelLoader = new URLLoader();
- m_xmlLevelLoader.addEventListener(Event.COMPLETE, LoadLevelsName);
- m_xmlLevelLoader.load(new URLRequest(LEVEL_FILE));
- m_levelLoaded = false;
- }
-
- public function LoadLevelsName(e:Event):void
- {
- var xmlData:XML = new XML(m_xmlLevelLoader.data);
- var levelList:XMLList = xmlData.levelType;
- var levelXML:XML = levelList[i];
- var nameList:XMLList = levelXML.data.nameLevel;
- var nameCount:int = nameList.length();
- for ( var i:int = 0; i < nameCount; i++)
- {
- var nameLev:XML = nameList[i];
- var levelName:String = nameLev.@name;
- m_levelFiles.push(levelName);
- }
- // Load first Level
- m_loaderLevelDatas.loadMapXML(String(m_levelFiles[m_currentLevel]));
- }
-
- public function updateGame( timerEvent:TimerEvent ):void
- {
- if (m_screenState == BEGINNING)
- {
- if (currentFrame == 1)
- play();
- if (currentFrame == totalFrames)
- {
- m_screenState = WAITING_TO_START;
- startingText.alpha = 1;
- stop();
- }
- m_screenB.alpha += 0.02;
- }
- else if (m_screenState == WAITING_TO_START)
- {
- // Update the controls
- updateControls()
- // Update the Blink Timer for the Text
- if (m_gameMCTimerBlink <= 0)
- {
- m_gameMCTimerBlink = BLINK_TIMER;
- if (startingText.alpha == 1)
- startingText.alpha = 0;
- else
- startingText.alpha = 1
- }
- else
- m_gameMCTimerBlink -= 0.1;
-
- // Check if we press fire and want to start playing then
- if (ControllerManager.getKeyState(ControllerManager.FIRE))
- {
- m_gameMCTimerBlink = BLINK_TIMER;
- startingText.alpha = 0;
- m_screenState = PLAY;
- m_forceDrawLevel = false;
- m_debugRectangle = new BitmapData( 20, 20, false, 0xFF0000 );
- m_debugRectangeB = new Bitmap(m_debugRectangle);
- m_debugRectangle = new BitmapData( 20, 20, false, 0x00FF00 );
- m_debugRectangeBG = new Bitmap(m_debugRectangle);
- m_debugRectangeBG.x = 15.0;
- m_debugRectangeBG.y = 15.0;
- m_debugRectangeB.x = 10.0;
- m_debugRectangeB.y = 10.0;
- m_debugContainer = new Sprite();
- m_debugContainer.addChild(m_debugRectangeBG);
- m_debugContainer.addChild(m_debugRectangeB);
- addChild(m_debugContainer);
- //addChild(m_debugRectangeBG);
- //addChild(m_debugRectangeB);
- }
- }
- else if (m_screenState == PLAY)
- {
- var timerBeforeUpdate:Number = (new Date()).time;
- // Update the controls
- updateControls();
- // Update Characters
- m_characsManager.update();
- // Update the Collision between Objects and Characters
- var characterVect:Vector.<CharacterBase> = new Vector.<CharacterBase>()
- var hero:Hero = m_characsManager.getHero();
- for each (var character:CharacterBase in characterVect)
- m_gameObjectsManager.collisionWithCharac(character);
- if (hero != null)
- m_gameObjectsManager.collisionWithCharac(hero);
- // Update GameObjects
- m_gameObjectsManager.updateTimers();
- m_gameObjectsManager.update();
- // Update Powers
- updatePowers();
- // Update Particles
- updateParticles();
- // Update the score and life
- if (hero != null)
- {
- InputScoreText.text = String(hero.getScore());
- InputLifeText.text = String(hero.getLife());
- m_debugRectangeB.x += 0.5;
- m_debugRectangeB.y += 0.5;
- m_debugRectangeBG.x += 0.5;
- m_debugRectangeBG.y += 0.5;
- }
- var timerAfterUpdate:Number = (new Date()).time;
- if (m_updateDebugTime == 0.0)
- {
- m_updateDebugTime = timerAfterUpdate - timerBeforeUpdate;
- }
- else
- {
- m_updateDebugTime = (m_updateDebugTime + timerAfterUpdate - timerBeforeUpdate) / 2
- }
- }
- else if (m_screenState == LOADING_LEVEL)
- {
- addChild(m_loaderLevelDatas);
- // Create the new effect during the transition to the next level
- //var particleLevelLoadingEffect:ParticleLevelFadingEffect = new ParticleLevelFadingEffect(m_screenBDTilesCharacObj.width, m_screenBDTilesCharacObj.height, m_screenBDTilesCharacObj);
- //particleLevelLoadingEffect.init(m_screenBDTilesCharacObj, new Point());
- // Test stqrt
- //var particleLevelLoadingEffect:ParticleLevelFadingEffect = new ParticleLevelFadingEffect(m_screenBDParticles.width, m_screenBDParticles.height, m_screenBDParticles);
- //var positionToStartInitEffect:Point = new Point(m_screenBDParticles.width, m_screenBDParticles.height);
- //particleLevelLoadingEffect.init(m_screenBDParticles, positionToStartInitEffect);
- // Test end
- //m_particleEffects.push(particleLevelLoadingEffect);
- // Init the loader for the new level to load
- m_loaderLevelDatas.getLevelDatas().init();
- m_loaderLevelDatas.loadMapXML(String(m_levelFiles[m_currentLevel]));
- stopTimer();
- }
- else if (m_screenState == NEXT_LEVEL)
- {
- updateParticles();
- if (m_particleEffects.length == 0)
- {
- m_screenState = WAITING_TO_START;
- m_forceDrawLevel = true;
- startingText.alpha = 1;
- }
- }
- else if (m_screenState == EXIT)
- {
- if (currentFrame <= 1)
- {
- dispatchEvent( new ScreenEvent( m_exitScreenEvent ) );
- gameTimer.stop()
- }
- else
- {
- if (m_particleEffects.length > 0)
- {
- updateParticles();
- }
- prevFrame();
- m_screenB.alpha -= 0.02;
- }
- }
- }
-
- private function updatePowers():void
- {
- var indexPower:int = m_powerAgainstEnemy.length - 1;
- var powerObject:PowerBase;
- while (indexPower >= 0)
- {
- powerObject = m_powerAgainstEnemy[indexPower];
- if (powerObject.getPowerTimer() <= 0)
- m_powerAgainstEnemy.splice(indexPower, 1);
- else
- powerObject.update();
- indexPower--;
- }
- }
-
- private function updateParticles():void
- {
- var indexParticles:int = m_particleEffects.length - 1;
- var particleEffectObject:ParticleEffectBase;
- while (indexParticles >= 0)
- {
- particleEffectObject = m_particleEffects[indexParticles];
- particleEffectObject.update();
- if (particleEffectObject.isEffectFinished())
- m_particleEffects.splice(indexParticles,1);
- indexParticles--;
- }
- }
-
- public function drawGame(timerEvent:TimerEvent):void
- {
- var timerBeforeDraw:Number = (new Date()).time;
- // Fill in the map with the points to redraw
- fillRedrawPointsDic();
- // Draw Level
- drawLevel();
- // Draw Objectss
- drawObjects();
- // Draw Characters
- drawCharacters();
- // Draw Particles
- drawParticles();
- // Draw on bitmap object
- m_screenBD.draw(m_screenBDTilesCharacObj);
- // Test start
- //m_screenBDParticles.draw(m_screenBDParticles);
- // Test end
- m_dicPointsToRedraw = null;
- var timerAfterDraw:Number = (new Date()).time;
- if (m_drawDebugTime == 0.0)
- {
- m_drawDebugTime = timerAfterDraw - timerBeforeDraw;
- }
- else
- {
- m_drawDebugTime = (m_drawDebugTime + timerAfterDraw - timerBeforeDraw) / 2
- }
- }
-
- private function fillRedrawPointsDic()
- {
- if (!m_forceDrawLevel)
- {
- if (m_dicPointsToRedraw == null)
- m_dicPointsToRedraw = new Dictionary();
- var objectsVector:Vector.<GameObjectBase>;
- var fruitsVector:Vector.<Fruit>;
- var hero:Hero = m_characsManager.getHero();
- // Check the position of Poucmoun
- if (hero != null)
- addRedrawPointsMapFromCharac(hero.getPosition());
- // Check the previous position of Poucmoun
- if (hero != null)
- addRedrawPointsMapFromCharac(hero.getPreviousPosition());
-
- // Add the position of the other objects and characters
- var characterVect:Vector.<CharacterBase> = m_characsManager.getCharactersVector();
- for each ( var character:CharacterBase in characterVect)
- {
- addRedrawPointsMapFromCharac(character.getPosition());
- addRedrawPointsMapFromCharac(character.getPreviousPosition());
- }
- var gameObjec:GameObjectBase;
- objectsVector = m_gameObjectsManager.getGOAnimFix();
- if (objectsVector != null)
- {
- for each (gameObjec in objectsVector)
- addRedrawPointsMapFromObj(gameObjec.getPosition());
- }
-
- objectsVector = m_gameObjectsManager.getGOAnimMoving();
- if (objectsVector != null)
- {
- for each (gameObjec in objectsVector)
- addRedrawPointsMapFromObj(gameObjec.getPosition());
- }
-
- fruitsVector = m_gameObjectsManager.getFruits();
- if (objectsVector != null)
- {
- for each (var fruitObject:Fruit in fruitsVector)
- addRedrawPointsMapFromObj(fruitObject.getPosition());
- }
- }
- }
-
- private function drawLevel():void
- {
- var rect:Rectangle = new Rectangle(0, 0, tileSize, tileSize);
- var tileToUse:Tile;
- var destPoint:Point;
- if (m_screenBD != null && m_wallsObjectsTiles != null && m_tileMap.length > 0)
- {
- if (m_forceDrawLevel)
- {
- for (var rowCtr:int = 0 ; rowCtr < mapRows ; rowCtr++)
- {
- for (var colCtr:int = 0 ; colCtr < mapCols ; colCtr++)
- {
- destPoint = new Point(colCtr * tileSize, rowCtr * tileSize);
- tileToUse = Tile(m_backgroundTileMap[rowCtr][colCtr]);
- m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
- tileToUse = Tile(m_tileMap[rowCtr][colCtr]);
- if (tileToUse.getTileNumber() != -1)
- m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
- }
- }
- }
- else
- {
- var dicValues:Vector.<int>;
- var indexYCoord:int = 17;
- var row:int;
- var col:int;
- while (indexYCoord-- >= 0)
- {
- row = indexYCoord;
- dicValues = m_dicPointsToRedraw[indexYCoord];
- if (dicValues != null)
- {
- for each(var values:int in dicValues)
- {
- col = values;
- destPoint = new Point(col * tileSize, row * tileSize);
- tileToUse = Tile(m_backgroundTileMap[row][col]);
- m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
- tileToUse = Tile(m_tileMap[row][col]);
- m_screenBDTilesCharacObj.copyPixels(tileToUse.getPixelMap(), rect, destPoint);
- }
- }
- }
- }
- }
- }
-
- private function addRedrawPointsMapFromCharac(point:Point)
- {
- var positionTopLeft:Point = new Point(int(point.x / 36), int(point.y / 36));
- var positionTopLeftHWRight:Point = new Point(int((point.x + HALF_WAY_TILE_CHARAC_SIZE) / 36), int(point.y / 36));
- var positionTopRight:Point = new Point(int((point.x + TILE_CHARAC_SIZE) / 36), int(point.y / 36));
- var positionTopHWDownRight:Point = new Point(int((point.x + TILE_CHARAC_SIZE) / 36), int((point.y + HALF_WAY_TILE_CHARAC_SIZE) / 36));
- var positionDownRight:Point = new Point(int((point.x + TILE_CHARAC_SIZE) / 36), int((point.y + TILE_CHARAC_SIZE) / 36));
- var positionDownRightHWLeft:Point = new Point(int((point.x + HALF_WAY_TILE_CHARAC_SIZE) / 36), int((point.y + TILE_CHARAC_SIZE) / 36));
- var positionDownLeft:Point = new Point(int(point.x / 36), int((point.y + TILE_CHARAC_SIZE) / 36));
- var positionDownHWTopLeft:Point = new Point(int(point.x / 36), int((point.y + HALF_WAY_TILE_CHARAC_SIZE) / 36));
- var positionCentral:Point = new Point (int((point.x + HALF_WAY_TILE_CHARAC_SIZE) / 36), int((point.y + HALF_WAY_TILE_CHARAC_SIZE) / 36))
- // Start checking if we need to add each point to the map
- // Check the Top Left point
- checkPoint(positionTopLeft);
- // Check the point half way between Top Left and Top Right points
- checkPoint(positionTopLeftHWRight);
- // Check the Top Right point
- checkPoint(positionTopRight);
- // Check the point half way between Top Right and Down Right points
- checkPoint(positionTopHWDownRight);
- // Check the Down Right point
- checkPoint(positionDownRight);
- // Check the point half way between Down Right and Down Left points
- checkPoint(positionDownRightHWLeft);
- // Check the Down Left point
- checkPoint(positionDownLeft);
- // Check the point half way between Down Left and Top Left points
- checkPoint(positionDownHWTopLeft);
- // Check the Central poin
- checkPoint(positionCentral);
- }
-
- private function addRedrawPointsMapFromObj(point:Point)
- {
- var positionTopLeft:Point = new Point(int(point.x / 36), int(point.y / 36));
- var positionTopRight:Point = new Point(int((point.x + TILE_OBJS_SIZE) / 36), int(point.y / 36));
- var positionDownRight:Point = new Point(int((point.x + TILE_OBJS_SIZE) / 36), int((point.y + TILE_OBJS_SIZE) / 36));
- var positionDownLeft:Point = new Point(int(point.x / 36), int((point.y + TILE_OBJS_SIZE) / 36));
- // Start checking if we need to add each point to the map
- // Check the Top Left point
- checkPoint(positionTopLeft);
- // Check the Top Right point
- checkPoint(positionTopRight);
- // Check the Down Right point
- checkPoint(positionDownRight);
- // Check the Down Left point
- checkPoint(positionDownLeft);
- }
-
- private function checkPoint(pointToCheck:Point):void
- {
- if ((pointToCheck.x < 18) && (pointToCheck.y < 16))
- {
- var isValueInVector:Boolean = false;
- var valueInVector:int = 0;
- var xValuesVector:Vector.<int>;
-
- if (m_dicPointsToRedraw[pointToCheck.y] == null)
- m_dicPointsToRedraw[pointToCheck.y] = new Vector.<int>();
- xValuesVector = m_dicPointsToRedraw[pointToCheck.y];
-
- for each (var x_value:int in xValuesVector)
- {
- if (pointToCheck.x == x_value)
- {
- isValueInVector = true;
- break;
- }
- }
- if (!isValueInVector)
- xValuesVector.push(pointToCheck.x);
- }
- }
-
- private function drawObjects():void
- {
- if (m_screenState == WAITING_TO_START || m_screenState == BEGINNING)
- m_gameObjectsManager.forceDrawAll();
- else
- m_gameObjectsManager.draw(m_dicPointsToRedraw);
- }
-
- private function drawCharacters():void
- {
- m_characsManager.draw();
- }
-
- private function drawParticles():void
- {
- m_screenBDParticles.fillRect(new Rectangle(0, 0, m_screenBDParticles.width, m_screenBDParticles.height), 0);
- for each(var particleEffect:ParticleEffectBase in m_particleEffects)
- particleEffect.draw();
- }
-
- public function onExitScreen( screennEvent:ScreenEvent ):void
- {
- m_screenState = EXIT;
- }
-
- public function startTimer():void
- {
- gameTimer.start();
- }
-
- public function stopTimer():void
- {
- gameTimer.stop();
- }
-
- private function addListenerEventToPowers():void
- {
- var gameObjects:Vector.<GameObjectBase> = m_gameObjectsManager.getGOAnimFix();
- for (var indexObjects:int = 0; indexObjects < gameObjects.length; indexObjects++)
- if(gameObjects[indexObjects].getObjType() == GameObjectBase.BALL_WEAKEN_ENEMY)
- gameObjects[indexObjects].addEventListener(PowerEvent.WEAKEN_ENEMY, onWeakenEnemyPower, false, 0, true);
- }
-
- public function onRequestInitDatas(e:Event):void
- {
- var levelDatas:LevelDatas = m_loaderLevelDatas.getLevelDatas();
- m_characsManager.init(levelDatas.getCharacterVector(), levelDatas.getHero(), levelDatas.getTileMap());
- m_tileMap = levelDatas.getTileMap();
- m_backgroundTileMap = levelDatas.getBackgroundMap();
- m_powerRef = levelDatas.getPowerRef();
- m_screenBDTilesCharacObj = m_loaderLevelDatas.getScreenBD();
- // Set the Game Objects from the level datas
- m_gameObjectsManager.init(levelDatas.getGOStatic(),
- levelDatas.getGOAnimStatic(),
- levelDatas.getGOAnimMoving(),
- levelDatas.getGameObjRef(),
- levelDatas.getGameObjRandom(),
- levelDatas.getFruitPoints(),
- levelDatas.getObjectRandomPoints());
- // Re-init the loader of levels
- levelDatas.reset();
- removeChild(m_loaderLevelDatas);
- addListenerEventToPowers();
-
- if (m_currentLevel == 0)
- m_screenState = BEGINNING;
- else
- m_screenState = NEXT_LEVEL;
- startTimer();
- }
-
- public function onWinLevel(objectEvent:ObjectEvent):void
- {
- trace ("Change Level");
- m_currentLevel++;
- if (m_currentLevel < m_levelFiles.length)
- m_screenState = LOADING_LEVEL;
- else
- {
- m_screenState = EXIT;
- m_exitScreenEvent = ScreenEvent.WINNING_SCREEN;
- }
- }
-
- public function onLoseLevel(screenEvent:ScreenEvent):void
- {
- m_screenState = EXIT;
- m_exitScreenEvent = ScreenEvent.GAME_OVER;
- }
-
- public function onAddPointsToRedrawMap(redrawPoints:RedrawPointsEvent):void
- {
- var vectorObjPoints:Vector.<Point> = redrawPoints.getFruitsRedrawPoints();
- var vectorXCoordPoints:Vector.<int>;
- m_dicPointsToRedraw = new Dictionary();
- for each (var fruitRedrawPoint:Point in vectorObjPoints)
- {
- vectorXCoordPoints = m_dicPointsToRedraw[fruitRedrawPoint.y];
- if (vectorXCoordPoints == null)
- {
- vectorXCoordPoints = new Vector.<int>();
- m_dicPointsToRedraw[fruitRedrawPoint.y] = vectorXCoordPoints;
- }
- if (vectorXCoordPoints.indexOf(fruitRedrawPoint.x) == -1)
- vectorXCoordPoints.push(fruitRedrawPoint.x);
- }
- }
-
- public function onGenerateParticleDyingEffect (particleDyingEffectEvent:ParticleDyingEffectEvent):void
- {
- trace ("Generate Particle Effect");
- //Create the new effect during the transition to the next level
- //var particleDyingEffect:ParticleDyingEffect = new ParticleDyingEffect(particleDyingEffectEvent.getParentBD());
- //particleDyingEffect.init(particleDyingEffectEvent.getImageBD(), particleDyingEffectEvent.getInitPosition());
- //m_particleEffects.push(particleDyingEffect)
- }
-
- public function onWeakenEnemyPower( powerEvent:PowerEvent ):void
- {
- var found:Boolean = false;
- var isAllEnemyJailed:Boolean = true;
- var newPower:PowerBase;
- var enemyCharac:Enemy;
- var characterVect:Vector.<CharacterBase> = m_characsManager.getCharactersVector();
- // Check if at least one enemy isn't in Jail
- for each (var charac:CharacterBase in characterVect)
- {
- if (charac.getCharacterType() == CharacterBase.ENEMY)
- {
- enemyCharac = charac as Enemy;
- if (enemyCharac.getSubState() == Enemy.FREE)
- isAllEnemyJailed = false;
- }
- }
- // This way, we don't need to go through loops for nothing
- if (!isAllEnemyJailed)
- {
- var indexPower:int = 0;
- if (m_powerAgainstEnemy.length > 0)
- {
- for (; indexPower < m_powerAgainstEnemy.length && !found; indexPower++)
- if (m_powerAgainstEnemy[indexPower].getPowerType() == PowerBase.POWER_WEAKEN_ENEMY)
- {
- newPower = m_powerAgainstEnemy[indexPower];
- found = true;
- }
- }
-
- for (indexPower = 0; indexPower < m_powerRef.length; indexPower++)
- {
- if (m_powerRef[indexPower].getPowerType() == PowerBase.POWER_WEAKEN_ENEMY)
- {
- if (found)
- {
- newPower.setPowerTimer(newPower.getPowerTimer() + m_powerRef[indexPower].getPowerTimer());
- newPower.initPower();
- }
- else
- {
- newPower = new WeakenEnemy();
- newPower.copyObject(m_powerRef[indexPower]);
- newPower.initPower();
- m_powerAgainstEnemy.push(newPower);
- }
- }
- }
- }
- }
-
- private function onAddToStage( event:Event ):void
- {
- m_controllerManager = new ControllerManager();
- m_controllerManager.Initialise(stage);
- }
-
- private function updateControls():void
- {
- m_controllerManager.update();
- if (ControllerManager.getKeyState(ControllerManager.PAUSE))
- {
- if (m_screenState == PLAY)
- dispatchEvent( new SwitchScreenEvent( SwitchScreenEvent.SWITCH_TO_PAUSE ) );
- }
- if (ControllerManager.getKeyState(ControllerManager.DEBUG))
- {
- if (m_screenState == PLAY)
- {
- trace ("m_updateDebugTime : ", m_updateDebugTime);
- trace ("m_drawDebugTime : ", m_drawDebugTime);
- }
- }
- }
- }
- }