package com.Tutorial 
{
    import com.adamatomic.flixel.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Stephen Lavelle
	 */
	public class WalkState extends FlxState
	{
	    [Embed(source = '../../data/levels/map.txt', mimeType = "application/octet-stream")] private var TileMapDat:Class;
		
	    [Embed(source = '../../data/gfx/tiles.png')] private var GfxTiles:Class;
					
        public static var lyrBG:FlxLayer;
		private var _descr:FlxText;
		public static var lyrPlayer:FlxLayer;
		
		public static var playerPos:Point=new Point(6*31+4,12*31+4);
		public static var pdir:Number;
		private var _map:FlxTilemap;
				
		private var _player:Player;
		
		private static var _positions:Array = [new Point(3*31,2*31),new Point(10*31,2*31),new Point(3*31,8*31),new Point(10*31,8*31)	];
		public function WalkState() 
		{			
			lyrBG = new FlxLayer;
			lyrPlayer = new FlxLayer;		
			
			_player = new Player(playerPos.x, playerPos.y, "up");
			lyrPlayer.add(_player);
			_player.update();
			lyrPlayer.update();
							
			_map = new FlxTilemap(new TileMapDat , GfxTiles, 1 );
			_descr = new FlxText(0, 400, FlxG.width, 30, "press x to calculate", 0xffffffff, null, 16, "center");
			_descr.visible = false;
			lyrBG.add(_map);
			lyrBG.add(_descr);
						
			this.add(lyrBG);
			this.add(lyrPlayer);
		}
		
		override public function update():void
        {
			_map.collide(_player);
			super.update();
			
			playerPos.x = _player.x;
			playerPos.y = _player.y;
			
			var bVisible:Boolean = false;
			for (var i:int = 0; i < _positions.length; i++)
			{
				if (playerPos.subtract(_positions[i]).length < 31)
				{
					if (FlxG.justPressed(FlxG.A) || FlxG.justPressed(FlxG.B))
					{
						CalculateState.mode = i;
						FlxG.switchState(CalculateState);
					}
					bVisible = true;					
					break;
				}
			}
			_descr.visible = bVisible;
		}
		
	}

}