package objs {
	import flash.display.*;
	import flash.geom.*;
  import flash.events.*;
  import flash.net.*;
	import com.terry.engine.*;
	import com.terry.engine.util.*;
	import gamecontrol.control;
	
	//Note: "player" is special as a rule, with default implementations in several places
	public class ent_player extends ent_generic {
		public function ent_player() {
			name = "player";
		}
		
		override public function create(i:int, xp:Number, yp:Number, para1:String = "0", para2:String = "0", para3:String = "0"):void {
			obj.entities[i].rule = "player";
			obj.entities[i].tile = 540;
			obj.entities[i].w = 16;
			obj.entities[i].h = 8;
		}
		
		override public function update(i:int):void {
			//Player entity's actual position is usually controlled
			//in the input class: this is for state changes
			if (obj.entities[i].state == 0) {
				
			}
		}
		
		override public function animate(i:int):void {
			if (obj.entities[i].vx < 0) {
				obj.entities[i].dir = 3;
			}else if (obj.entities[i].vx > 0) {
				obj.entities[i].dir = 2;
			}else if (obj.entities[i].vy < 0) {
				obj.entities[i].dir = 1;
			}else if (obj.entities[i].vy > 0) {
				obj.entities[i].dir = 0;
			}
			
			obj.entities[i].drawframe = obj.entities[i].tile + (obj.entities[i].dir * 3);
			
			if (obj.entities[i].vx == 0 && obj.entities[i].vy == 0) {
				//Standing still
			}else {
				//Walking
				obj.entities[i].framedelay--;
				if (obj.entities[i].framedelay <= 0) {
					obj.entities[i].framedelay = 12;
					obj.entities[i].walkingframe = (obj.entities[i].walkingframe+1) % 2;
				}
				obj.entities[i].drawframe = obj.entities[i].drawframe+obj.entities[i].walkingframe+1;
			}
		}
		
		override public function draw(i:int):void {
			var xp:int = 0, yp:int = 0, jpos:int;
			xp = obj.entities[i].xp;
			yp = obj.entities[i].yp - 32;
			
			if (control.showplayer) {
				if (control.naked) {
					for (jpos = 0; jpos < 3; jpos++) {
						gfx.drawtile(xp, yp + (jpos * def.TILESIZE), 880 + obj.entities[i].drawframe - obj.entities[i].tile + (jpos * 20));
					}
				}else if (control.aim) {
					for (jpos = 0; jpos < 3; jpos++) {
						gfx.drawtile(xp, yp + (jpos * def.TILESIZE), 137 + obj.entities[i].tile + (jpos * 20));
						gfx.drawtile(xp - 16, yp + (jpos * def.TILESIZE), 136 + obj.entities[i].tile + (jpos * 20));
					}
				}else if (control.carryingsuitcase) {
					//always facing up here, just draw it behind the player
					for (jpos = 0; jpos < 3; jpos++) {
						gfx.drawtile(xp, yp + (jpos * def.TILESIZE), 66+obj.entities[i].drawframe + (jpos * 20));
					}
					gfx.drawtile(xp-8+1, yp+24, 396);
					gfx.drawtile(xp+16-8+1, yp +24, 397);
					gfx.drawtile(xp-8+1, yp+16+24, 416);
					gfx.drawtile(xp+16-8+1, yp + 16+24, 417);
				}else if (control.carryingtrash) {
					if (obj.entities[i].dir == 1) {
						//Draw rubbish first
						gfx.drawtile(xp, yp+8, 519);
						gfx.drawtile(xp, yp + 16+8, 539);
						
						for (jpos = 0; jpos < 3; jpos++) {
							gfx.drawtile(xp, yp + (jpos * def.TILESIZE), 66+obj.entities[i].drawframe + (jpos * 20));
						}
					}else {
						//Draw player first
						for (jpos = 0; jpos < 3; jpos++) {
							gfx.drawtile(xp, yp + (jpos * def.TILESIZE), 66+obj.entities[i].drawframe + (jpos * 20));
						}
						if (obj.entities[i].dir == 0) {
							gfx.drawtile(xp, yp+8, 519);
							gfx.drawtile(xp, yp + 16 + 8, 539);
						}else if (obj.entities[i].dir == 2) {
							gfx.drawtile(xp+8, yp+8, 519);
							gfx.drawtile(xp+8, yp + 16 + 8, 539);
						}else if (obj.entities[i].dir == 3) {
							gfx.drawtile(xp-8, yp+8, 519);
							gfx.drawtile(xp-8, yp + 16 + 8, 539);
						}
						
					}
				}else{
					for (jpos = 0; jpos < 3; jpos++) {
						gfx.drawtile(xp, yp + (jpos * def.TILESIZE), obj.entities[i].drawframe + (jpos * 20));
					}
				}
			}
			//gfx.drawbox(xp, yp+32, obj.entities[i].w, obj.entities[i].h, 255, 255, 0);			
		}
		
		override public function drawinit(i:int, xoff:int, yoff:int, frame:int):void {
			gfx.draw_defaultinit(i, xoff, yoff, frame);
		}
		
		override public function collision(i:int, j:int):void {
			//i is this entity, j is the other
			if (obj.entities[j].rule == "enemy") {
				if (obj.entitycollide(i, j)) {
					//trace("collision");
				}
			}
		}
	}
}