package Game{
	import flash.display.*;
	import flash.events.*;
	
	/**
	 * Класс самого пакмана
	 */
	public class PacMan extends MovieClip{
		public var speed:Number = 1.5;
		public var Direction:Number = 0; // 0 - stop
		// 1 - left
		// 2 - right
		// 3 - up
		// 4 - down
		
		public var Mode:Number = 0; // 0 - Still
		// 1 - Chase
		// 2 - Death
		
		/** callback функции **/
		public var onFoodTake:Function;
		public var onEnergyFood:Function;		
		
		/**
		 * Смена режима активности пакмана
		 * @param	mod
		 */
		public function SwitchMode(mod:Number):void{
			switch(mod){
				case 0: // Стоять на месте
					rotation = 0;
					gotoAndStop('Still');
				break;
				
				case 1: // Убегание. Основной режим
					gotoAndStop('Runaway');
				break;
				
				case 2: // Смерть
					rotation = 0;
					gotoAndStop('Death');
				break;
				
				case 3: // Погоня за крабами
					gotoAndStop('Agressive');
				break;
			}
			
			Mode = mod;
		}
		
		/**
		 * Смена направления движения пакмана
		 * @param	newDir 1 - лево, 2 - право, 3 - вверх, 4 - вниз
		 */
		public function ChangeDir(newDir:Number):void{
			var cell:Number;
			var X:Number;
			var Y:Number;
			
			if (newDir == Direction) {
				return;
			}
			
			if(Mode == 0){
				SwitchMode(1);
			}
			
			switch (newDir){
				case 1:
					X = Math.floor(((x - 4) - speed) / 8);
					Y = Math.floor(y / 8);
					
					if (X < 0) {
						X = ((parent as Level).MapWidth - 1) + X;						
					}
					
					cell = (parent as Level).Map.Fields[X][Y];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
					}else{
						Direction = newDir;
					}
				break;
				
				case 2:
					X = Math.floor(((x + 4) + speed) / 8);
					Y = Math.floor(y / 8);
					
					if (X > (parent as Level).MapWidth - 1) {						
						X = X - ((parent as Level).MapWidth - 1);						
					}
					
					cell = (parent as Level).Map.Fields[X][Y];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
					}else{
						Direction = newDir;
					}
				break;
				
				case 3:
					X = Math.floor((x) / 8);
					Y = Math.floor(((y - 4) - speed) / 8);
					
					if (Y < 0) {
						Y = ((parent as Level).MapHeight - 1) + Y;						
					}
					
					cell = (parent as Level).Map.Fields[X][Y];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
					}else{
						Direction = newDir;
					}
				break;
				
				case 4:
					X = Math.floor((x) / 8);
					Y = Math.floor(((y + 4) + speed) / 8);
					
					if (Y > ((parent as Level).MapHeight - 1)) {
						Y = Y - ((parent as Level).MapHeight - 1);						
					}
					
					cell = (parent as Level).Map.Fields[X][Y];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
					}else{
						Direction = newDir;
					}
				break;
			}			
		}
		
		/**
		 * Движение
		 */
		public function Move():void{
			var cell:Number;
			
			switch (Direction){
				case 1:
					if(Math.floor(((x-4) - speed) / 8) < 0){
						x = (parent as Level).MapWidth*8;
						return;
					}
					
					cell = (parent as Level).Map.Fields[Math.floor(((x-4) - speed) / 8)][Math.floor(y / 8)];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
						x = Math.floor(x / 8) * 8 + 4;
					}else{
						rotation = 0;
						scaleX = -1;
						x = x - speed;
						y = Math.floor(y / 8) * 8 + 4;
					}
				break;
				
				case 2:
					if(Math.floor(((x+4) + speed) / 8) >= (parent as Level).MapWidth){
						x = 0;
						return;
					}
					
					cell = (parent as Level).Map.Fields[Math.floor(((x+4) + speed) / 8)][Math.floor(y / 8)];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
						x = Math.floor(x / 8) * 8 + 4;
					}else {
						scaleX = 1;
						rotation = 0;
						x = x + speed;
						y = Math.floor(y / 8) * 8 + 4;
					}
				break;
				
				case 3:
					cell = (parent as Level).Map.Fields[Math.floor((x) / 8)][Math.floor(((y-4) - speed) / 8)];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
						y = Math.floor(y / 8) * 8 + 4;
					}else {
						scaleX = 1;
						rotation = 270;
						y = y - speed;
						x = Math.floor(x / 8) * 8 + 4;
					}
				break;
				
				case 4:
					cell = (parent as Level).Map.Fields[Math.floor((x) / 8)][Math.floor(((y+4) + speed) / 8)];
					
					if((cell != 0) && (cell != 1) && (cell != 2))
					{
						y = Math.floor(y / 8) * 8 + 4;
					}else {
						scaleX = 1;
						rotation = 90;
						y = y + speed;
						x = Math.floor(x / 8) * 8 + 4;
					}
				break;
			}
			
			cell = (parent as Level).Map.Fields[Math.floor(x / 8)][Math.floor(y / 8)];
			if((cell == 1) || (cell == 2))
			{
				var X:Number = Math.floor(x / 8);
				var Y:Number = Math.floor(y / 8);
				
				(parent as Level).replaceTile(X, Y, new tile_0());
				(parent as Level).Map.Fields[X][Y] = 0;
				
				if(cell == 1){
					onFoodTake();
				}else if(cell == 2){
					onEnergyFood();
				}
			}
		}
		
		/** Конструктор **/
		public function PacMan(){			
		}
	}	
}