root/Scripts/PhagocitosisGame.as

User picture

Author: Ying

Revision: 7 («Previous)


File Size: 15 KB

(May 02, 2009 11:39 UTC) About 3 years ago


  

 
Show/hide line numbers

package Scripts
{
	// Imports needed for variables and so on. Senocular is a keypress lib.
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.ui.Mouse;
	import flash.events.*;
	import com.senocular.utils.KeyObject;
	import com.tilemap.TileMap;
	import flash.ui.Keyboard;
	import flash.net.URLRequest;
	import flash.net.*;
    import flash.utils.Timer;
	
	
	
 
	// Game class, a Movieclip.
	public class PhagocitosisGame extends MovieClip
	{
		// Class variables
		private var pressedkey:KeyObject;
		
		// Entities
		public  var playerAmoeba:Amoeba;
		public  var playerGoal:Goal;
		public  var playerEnemy:Enemy;
		
		// Map Variables
		private var gameMap:TileMap;
		public  var tileSize:int = 25;
		
		// Map lists
		private var mapPath:Array = new Array();
		private var mapMetaPath:Array = new Array();
		private var tilePath:Array = new Array();
			
		// Scene related data
		private var CurrentScene:Number = -1;
		private var MaxScenes:int;
		private var firstLoop:Boolean   = true;		
		
		// Scene metadata
		private var xmlLoader:URLLoader;
		private var mapMetadata:XML;
		
		// Loading limiters
		private var playTimer:Timer;
		private var playReady:Boolean = false;
		
		// Pause Vars:
		private var pauAX:int;
		private var pauAY:int;
		private var pauAD:int;
		private var pauAM:Boolean;
		
		private var pauEX:int;
		private var pauEY:int;
		private var pauED:int;
		private var pauEM:Boolean;
		
		private var ResumeFromPause:Boolean;
		
		
 
		// Constructor, called once when the game is started.
		public function PhagocitosisGame()
		{
			trace("Phagocitosis: Constructor");
			
			// Checking if we enter a frame, or exit it
			addEventListener(Event.ENTER_FRAME, GameLoop, false, 0, true);
			pressedkey= new KeyObject(stage);
			
			ResumeFromPause = false;
			
			// Manually load maps, and set the number of scenes
			MaxScenes=6;
			mapPath.push("Maps/Test1Map.xml");
			mapPath.push("Maps/Trial1.xml");
			mapPath.push("Maps/Trial2.xml");
			mapPath.push("Maps/Trial3.xml");
			mapPath.push("Maps/Trial4.xml");
			mapPath.push("Maps/Trial5.xml");
			tilePath.push("Art/Tilemap.png");
			tilePath.push("Art/Tilemap.png");
			tilePath.push("Art/Tilemap.png");
			tilePath.push("Art/Tilemap.png");
			tilePath.push("Art/Tilemap.png");
			tilePath.push("Art/Tilemap.png");
			mapMetaPath.push("Maps/Test1MapData.xml");
			mapMetaPath.push("Maps/Trial1Data.xml");			
			mapMetaPath.push("Maps/Trial2Data.xml");			
			mapMetaPath.push("Maps/Trial3Data.xml");						
			mapMetaPath.push("Maps/Trial4Data.xml");						
			mapMetaPath.push("Maps/Trial5Data.xml");						
			
			
			stop();
		}		
		
		
		// Main game loop
		public function GameLoop(e:Event)
		{
			// Label to keep track of current Frame.
			// Label1.text = "Debug: " + currentLabel;
			
			// Code whilst in the menu
			if(currentLabel == "fMenu") RunfMenu();
			
			// Code whilst in the SelectScene frame
			if(currentLabel == "fSceneSelection") RunfSceneSelection();
						
			// Code whilst in the play game frame
			if(currentLabel == "fPlay") RunfPlay();
			
			// Code to run in Pause mode
			if(currentLabel == "fPause") RunfPause();
			
			// Code to show the Game Over screen
			if(currentLabel == "fGameOver") RunfGameOver();			
			
			// Code to show the palyer she's won
			if(currentLabel == "fGameWon") RunfGameWon();
		}
		
		/* -------------------------------------------------------
		----------------------------------------------------------
		
		                       Menu Frame
		
		----------------------------------------------------------
		--------------------------------------------------------*/
		
		private function RunfMenu()
		{
			if(firstLoop) InitfMenu();				
			stop();
		}
		
		private function InitfMenu()
		{
			trace("Phagocitosis: Init fMenu");
			ResumeFromPause= false;
			MenuPlayButton.addEventListener(MouseEvent.CLICK, FromfMenuGotofPlay);
			MenuSelectMapButton.addEventListener(MouseEvent.CLICK, FromfMenuGotofSceneSelection);
			firstLoop = false;
		}
		
		private function ExitfMenu()
		{			
			firstLoop = true;
			trace("Phagocitosis: Exit fMenu");			
		}		
		
		public function FromfMenuGotofPlay(event: MouseEvent) 
		{ 	
			ExitfMenu();			
			CurrentScene = 0;
			gotoAndStop("fPlay");  
		}
		
		public function FromfMenuGotofSceneSelection(event: MouseEvent)
		{
			ExitfMenu();
			gotoAndStop("fSceneSelection")
		}		
		
		
		/* -------------------------------------------------------
		----------------------------------------------------------
		
		                   Scene Select Frame
		
		----------------------------------------------------------
		--------------------------------------------------------*/
		
		private function RunfSceneSelection()
		{
			if(firstLoop) InitfSceneSelection();
			stop();
			
		}
		
		private function InitfSceneSelection()
		{
			trace("Phagocitosis: Init fSceneSelection");
			
			SelectMapMenuButton.addEventListener(MouseEvent.CLICK, FromfSceneSelectionGotofMenu);
			SMButton0.addEventListener(MouseEvent.CLICK, SceneSelectionGotoScene0);
			SMButton1.addEventListener(MouseEvent.CLICK, SceneSelectionGotoScene1);
			SMButton2.addEventListener(MouseEvent.CLICK, SceneSelectionGotoScene2);
			SMButton3.addEventListener(MouseEvent.CLICK, SceneSelectionGotoScene3);
			SMButton4.addEventListener(MouseEvent.CLICK, SceneSelectionGotoScene4);
			SMButton5.addEventListener(MouseEvent.CLICK, SceneSelectionGotoScene5);
			firstLoop = false;
		}
		
		public function SceneSelectionGotoScene0(event: MouseEvent)
		{ExitfSceneSelection(); CurrentScene=0; gotoAndStop("fPlay");}
		
		public function SceneSelectionGotoScene1(event: MouseEvent)
		{ExitfSceneSelection(); CurrentScene=1; gotoAndStop("fPlay");}
		
		public function SceneSelectionGotoScene2(event: MouseEvent)
		{ExitfSceneSelection(); CurrentScene=2; gotoAndStop("fPlay");}
		
		public function SceneSelectionGotoScene3(event: MouseEvent)
		{ExitfSceneSelection(); CurrentScene=3; gotoAndStop("fPlay");}
		
		public function SceneSelectionGotoScene4(event: MouseEvent)
		{ExitfSceneSelection(); CurrentScene=4; gotoAndStop("fPlay");}
		
		public function SceneSelectionGotoScene5(event: MouseEvent)
		{ExitfSceneSelection(); CurrentScene=5; gotoAndStop("fPlay");}
		
		public function FromfSceneSelectionGotofMenu(event: MouseEvent)
		{
			ExitfSceneSelection();
			gotoAndStop("fMenu");
		}
		
		private function ExitfSceneSelection()
		{			
			firstLoop = true;
			trace("Phagocitosis: Exit fSceneSelection");			
		}		
		
		/* -------------------------------------------------------
		----------------------------------------------------------
		
		                       Play Frame
		
		----------------------------------------------------------
		--------------------------------------------------------*/
		
		private function RunfPlay()
		{				

			if(firstLoop)  InitfPlay();				
			if(playReady)
			{						
				CheckVictoryConditions();
				
				playerEnemy.ScoutAmoebaAt(playerAmoeba.x, playerAmoeba.y);
				if (pressedkey.isDown(Keyboard.ESCAPE)) 
				{
					StorePreviousData();
					ExitfPlay();
					gotoAndStop("fPause");
				}
			}
		}
		
		public function InitfPlay()
		{
			trace("Phagocitosis: Init fPlay");
			firstLoop = false;
			playReady = false;
			
			// Load Metadata
			LoadMapMetadata();				
			

			stop();
		}
		
		public function ExitfPlay()
		{			
			firstLoop = true;
			
			// Remove all suspects
			playerAmoeba.removeSelf();
			playerEnemy.removeSelf();
			playerGoal.removeSelf();
			gameMap.removeSelf();
			
			trace("Phagocitosis: Exit fPlay");
			 
		}
		
		private function LoadPreviousData()
		{
			trace("LoadPreviousData");
			
			ResumeFromPause = false;
			
			playerAmoeba.x = pauAX;
			playerAmoeba.y = pauAY;
			playerAmoeba.iDir = pauAD;
			playerAmoeba.bMoving = pauAM;
			
			playerEnemy.x = pauEX;
			playerEnemy.y = pauEY;
			playerEnemy.iDir = pauED;
			playerEnemy.bMoving = pauEM;
		}
		
		private function StorePreviousData()
		{
			trace("StorePreviousData");
			
			ResumeFromPause = true;
			
			pauAX = playerAmoeba.x;
			pauAY = playerAmoeba.y;
			pauAD = playerAmoeba.iDir;
			pauAM = playerAmoeba.bMoving;
			
			pauEX = playerEnemy.x;
			pauEY = playerEnemy.y;
			pauED = playerEnemy.iDir;
			pauEM = playerEnemy.bMoving;
		}
		
		
		public function LoadMapMetadata()
		{
			trace("Metadata: " + mapMetaPath[CurrentScene]);
			
			 xmlLoader=new URLLoader();
		   	 xmlLoader.addEventListener(Event.COMPLETE, LoadMapData, false, 0 , true);
		     xmlLoader.load(new URLRequest( mapMetaPath[CurrentScene] )); // "Maps/Test1MapData.xml"			 
		}
		
		public function LoadMapData(e:Event)
		{
			// Create data document
			mapMetadata=new XML(xmlLoader.data);
			
			// Create Amoeba
			createAmoeba();										
			
			// Create Goal
			createGoal();
			
			// Create Enemy
			createEnemy();
			
			// Create Map
			gameMap = new TileMap;
			
			gameMap.CreateMap(stage,tilePath[CurrentScene], mapPath[CurrentScene], 16, 22, 3, tileSize );
			
			trace("Setting timer");
			//trace(gameMap.IsReady());			
			playTimer = new Timer(200);
			playTimer.addEventListener(TimerEvent.TIMER, CheckIfPlayReady);
			playTimer.start();
			//playerAmoeba.SetMap(gameMap.aTileMap);
		}
		
		public function CheckIfPlayReady(e:TimerEvent):void
		{
			if(playerAmoeba.IsReady() && gameMap.IsReady() && playerEnemy.IsReady() )
			{
				// Ambos han terminado de cargarse
				playerAmoeba.SetMap(gameMap.aTileMap);
				playerEnemy.SetMap(gameMap.aTileMap);
				playTimer.stop();
				playerAmoeba.Activate();
				playerEnemy.Activate();
				playReady = true;
				
				
				if(ResumeFromPause) LoadPreviousData();
				
			}
		}
		
		private function CheckVictoryConditions()
		{
			if( playerGoal.x < playerAmoeba.x+25 && 
			   	playerAmoeba.x < playerGoal.x+10 &&
				playerGoal.y < playerAmoeba.y+25 && 
			   	playerAmoeba.y < playerGoal.y+10    ) 
			{
				// Won the map
				CurrentScene++;				
				ExitfPlay();
				
				if(MaxScenes ==CurrentScene)
				{
					CurrentScene = 0;
					gotoAndStop("fGameWon");					
				}
				
			}
			
			if( playerEnemy.x < playerAmoeba.x+25 && 
			   	playerAmoeba.x < playerEnemy.x+25 &&
				playerEnemy.y < playerAmoeba.y+25 && 
			   	playerAmoeba.y < playerEnemy.y+25    ) 
			{
				// Lost the game
				ExitfPlay();
				gotoAndStop("fGameOver");
			}
		}
		
		private function createAmoeba()
		{
			//create an object of our ship from the Ship class
			playerAmoeba = new Amoeba;
			playerAmoeba.CreateAmoeba(stage);
			//add it to the display list
			stage.addChild(playerAmoeba); 
			playerAmoeba.x = (mapMetadata.Player.StartPosition.x) * tileSize; //stage.stageWidth / 2;
			playerAmoeba.y = (mapMetadata.Player.StartPosition.y) * tileSize; //stage.stageHeight / 2;
		}
		
		private function createGoal()
		{
			//create an object of our ship from the Ship class
			playerGoal = new Goal(stage);
			//add it to the display list
			stage.addChild(playerGoal); 
			
			playerGoal.x = (mapMetadata.Goal.x) * tileSize; //stage.stageWidth / 2;
			playerGoal.y = (mapMetadata.Goal.y) * tileSize; //stage.stageHeight / 2;
			
		}
		
		public function createEnemy()
		{
			//create an object of our ship from the Ship class
			playerEnemy = new Enemy();
			playerEnemy.CreateEnemy(stage);
			//add it to the display list
			stage.addChild(playerEnemy); 
			
			playerEnemy.x = (mapMetadata.Goal.x) * tileSize; //stage.stageWidth / 2;
			playerEnemy.y = (mapMetadata.Goal.y) * tileSize; //stage.stageHeight / 2;
		}
		
		
		
		/* -------------------------------------------------------
		----------------------------------------------------------
		
		                       Pause Frame
		
		----------------------------------------------------------
		--------------------------------------------------------*/
		
		private function RunfPause()
		{
			if(firstLoop)  InitfPause();
			if (pressedkey.isDown(Keyboard.ENTER)) 
			{
				ExitfPause();
				gotoAndStop("fPlay");
			}
		}
		
		private function InitfPause()
		{
			trace("Phagocitosis: Init fPause");
			PausedMenuButton.addEventListener(MouseEvent.CLICK, FromfPauseGotofMenu);
			firstLoop = false;
			stop();
		}
		
		private function ExitfPause()
		{
			firstLoop = true;
			trace("Phagocitosis: Exit fPause");
		}
		
		public function FromfPauseGotofMenu(event: MouseEvent)
		{
			ExitfPause();
			gotoAndStop("fMenu");
		}
		
		
		/* -------------------------------------------------------
		----------------------------------------------------------
		
		                     Game Over Frame
		
		----------------------------------------------------------
		--------------------------------------------------------*/
		
		private function RunfGameOver()
		{
			if(firstLoop)  InitfGameOver();
			if (pressedkey.isDown(Keyboard.ENTER)) 
			{
				trace("Clickiy");
				ExitfGameOver();
			    gotoAndStop("fPlay");
			}
		}
		
		private function InitfGameOver()
		{
			trace("Phagocitosis: Init fGameOver");
			GameOverMenuButton.addEventListener(MouseEvent.CLICK, FromfGameOverGotofMenu);
			firstLoop = false;
			stop();
		}
		
		private function ExitfGameOver()
		{
			firstLoop = true;
			trace("Phagocitosis: Exit fGameOver");
		}
		
		public function FromfGameOverGotofMenu(event: MouseEvent)
		{
			ExitfGameOver();
			gotoAndStop("fMenu");
		}
		
		/* -------------------------------------------------------
		----------------------------------------------------------
		
		                       GameWon Frame
		
		----------------------------------------------------------
		--------------------------------------------------------*/
		
		private function RunfGameWon()
		{
			if(firstLoop)  InitfGameWon();
			
		}
		
		private function InitfGameWon()
		{
			trace("Phagocitosis: Init fGameWon");
			GameWonMenuButton.addEventListener(MouseEvent.CLICK, FromfGameWonGotofMenu);
			firstLoop = false;
			stop();
		}
		
		private function ExitfGameWon()
		{
			firstLoop = true;
			trace("Phagocitosis: Exit fGameWon");
		}
		
		public function FromfGameWonGotofMenu(event: MouseEvent)
		{
			ExitfGameWon();
			gotoAndStop("fMenu");
		}
		
		
		
		
		/* -------------------------------------------------------
		----------------------------------------------------------
		----------------------------------------------------------
		----------------------------------------------------------
		
		              ------  Misc Functions  -----
		
		..............------..................------..............
		----------------------------------------------------------
		--------------------------------------------------------*/
		
		// Code called each time a frame changes.
		public function ExitFrame(e:Event)
		{
			trace("Phagocitosis: Changing frame");
			firstLoop = true;
		} 
		
		
		
		
		
 
	}
 
}