package MapObjects
{
	import FlDosBox.FlDosBoxSpecialChars;
	import Mobs.Pigeon;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class Building extends MapObject
	{
		public static const SEGMENT_WIDTH:int = 14;
		public static const SEGMENT_HEIGHT:int = 7;
		public static const SEGMENT_BORDER_H:int = 1;
		public static const SEGMENT_BORDER_W:int = 4;
		
		private var x:int;
		private var nsegments:int;
		private var nfloors:int;
		
		private var pigeons:Vector.<Pigeon> = new Vector.<Pigeon>();
		
		public function Building(sx:int, floors:int, segments:int, pigions:Boolean = false, solidwalls:Boolean = true, ground:int = -1)
		{
			super();
			x = sx;
			nfloors = floors;
			nsegments = segments;
			
			if (ground == -1)
				ground = map.mapH;
			
			for (var i:int = 0; i < segments; i++)
			{
				for (var j:int = 0; j < floors; j++)
				{
					generateBuildingSegment(sx + i * SEGMENT_WIDTH, ground - (j + 1) * SEGMENT_HEIGHT, Math.random() < 0.5, solidwalls);
				}
				
				for (var k:int = 0; k < SEGMENT_WIDTH; k++)
				{
					var roofCell:MapCell = solidwalls ? new MapCellPassable(FlDosBoxSpecialChars.BLOCK_FULL, 7, 7) : new MapCell(FlDosBoxSpecialChars.BLOCK_FULL_V_HALF_B, 7, 7);
					
					mapData[sx + i * SEGMENT_WIDTH + k][ground - floors * SEGMENT_HEIGHT - 1] = roofCell;
					
					if (pigions)
					{
						if (k % 8 == 0)
						{
							var newP:Pigeon = new Pigeon();
							map.addMob(newP, sx + i * SEGMENT_WIDTH + k, ground - floors * SEGMENT_HEIGHT - 1);
							newP.building = this;
							pigeons.push(newP);
						}
					}
				}
				
			}
		}
		
		public function spookPigeons():void
		{
			
			for (var i:int = 0; i < pigeons.length; i++)
			{
				pigeons[i].spook();
			}
		}
		
		private function generateBuildingSegment(atx:int, aty:int, light:Boolean = true, solid:Boolean = true):void
		{
			var CellClass:Class = solid ? MapCell : MapCellPassable;
			
			for (var i:int = 0; i < SEGMENT_WIDTH; i++)
			{
				if (i + SEGMENT_WIDTH < map.mapW)
				{
					for (var j:int = 0; j < SEGMENT_HEIGHT; j++)
					{
						if ((i < SEGMENT_BORDER_W) || (i > SEGMENT_WIDTH - SEGMENT_BORDER_W - 1) || (j < SEGMENT_BORDER_H) || (j > SEGMENT_HEIGHT - SEGMENT_BORDER_H - 1))
						{
							mapData[atx + i][aty + j] = new CellClass(15, 0, 0) as MapCell;
						}
						else
						{
							mapData[atx + i][aty + j] = new CellClass(15, light ? 6 : 8, light ? 6 : 8) as MapCell;
						}
					}
				}
			}
		}
		
		override public function update(delta:Number):void
		{
			super.update(delta);
		}
		
		override public function render():void
		{
			super.render();
		}
	
	}

}