
package
{
	import flash.utils.ByteArray;
	import net.flashpunk.graphics.Tilemap;
	import net.flashpunk.masks.Grid;
	import flash.geom.Rectangle;
	import net.flashpunk.FP;
	import net.flashpunk.Sfx;
	
	public class BazaarMap extends Tilemap
	{
		public var w:uint = 128;
		public var h:uint = 128;
		
		public var s:uint = 32;
		
		public var sw:uint = w*s;
		public var sh:uint = h*s;
		
		
		[Embed(source='gfx/tileset.png')]
		protected var ImgTileSet:Class;
		
		[Embed(source = 'gfx/bazaar.bin', mimeType = "application/octet-stream")]
		protected var level:Class;
		
		[Embed(source = 'gfx/boom.mp3')]
		private const SND13:Class;
		public var boom:Sfx = new Sfx(SND13);
		
		public var grid:Grid;
		
		public function BazaarMap()
		{
			super(ImgTileSet, w * s, h *s,s, s);
			grid = new Grid(w*s, h*s, s, s, 0, 0);
			var b:ByteArray = new level();
			trace("Crating map!");
			loadFromByteArray(b);
			
			
			
		}
		public function loadFromByteArray(data:ByteArray):void
		{
			
			var levelmap:String = "";
			var lineend:Boolean ;
			var moneys:uint = 0;
			for (var yy:uint = 0; yy < h; yy ++)
			{
				for (var xx:uint = 0; xx < w; xx ++)
				{
					var tile:uint = data.readUnsignedByte()
					grid.setTile(xx, yy, !(tile < 21));
					//if (tile > 0 && tile < 5) {
						// nmes
						if (tile == 1) FP.world.add(new Kitten(xx * s, yy * s));
						if (tile == 2) FP.world.add(new Horse(xx * s, yy * s));
						if (tile == 3) FP.world.add(new Turtle(xx * s, yy * s));
						if (tile == 4) FP.world.add(new Bat(xx * s, yy * s));
						if (tile == 5) 
						{
							FP.world.add(new Heart(xx * s, yy * s));
							moneys++;
							
						}
						
						if (tile == 6) 
						{
							FP.world.add(new Coin(xx * s, yy * s));
							moneys++;
							
						}
						//trace("Adding enemy at " + xx + " x " + yy);
						if (tile > 7 && tile < 21) {
							FP.world.add(new Grandpa(xx * s, yy * s, tile-8));
							trace("Adding enemy at " + xx + " x " + yy);
						}
						if (tile == 7) 
						{
							Bazaar.player.x =  xx * s;
							Bazaar.player.y = yy * s;
							
							Bazaar.player.sx =  xx * s;
							Bazaar.player.sy = yy * s;
						}
						
					//}
					if (tile < 21) 
					{
						if (yy == 85) tile = 26;
						if (yy > 85) tile = 25;
						if (yy<85) tile = 27;
					}
					levelmap = levelmap.concat(tile);
					if (xx<w-1) levelmap = levelmap.concat(",");
				}
				if (yy<h-1) levelmap = levelmap.concat("|");
			}
			loadFromString(levelmap, ",", "|");
			trace("Moneys " + moneys);
		}
		public function setTile2(col:uint, row:uint, collide:Boolean):void
		{
			Bazaar.level.tilemap.fill(new Rectangle(col * s, row * s, s, s), 0, 0);
			boom.play();
			grid.setTile(col, row, collide);
		}
		
		public function inscreen(x:Number, y:Number):Boolean
		{
			var xx:Number = x - FP.camera.x;
			var yy:Number = y - FP.camera.y;
			//trace (x + " x " + y);
			if (xx > 0 && yy > 0 && xx < FP.screen.width && yy < FP.screen.height) 
			{
				//trace("out");
				return true;
				
			}
			return false;
			
		}
	}
}