var gravity=9.8;
var meter=1;
var Floor=Math.floor;
var speed=1;
var sky=LoadImage("sky.jpg");

var debug=new _Debugger();
function _Debugger()
{
	this.showFallDetectionBoxes=true;
	this.render=function()
	{
		if (this.showFallDetectionBoxes)
		{
			var list=GetPersonList();
			for (p in list)
			{
				if (list[p]!="Player 1")
				{
					var detect=16
					if (GetPersonValue(list[p], "WalkDirection")=="WEST")
					{
						detect*=-1;
					};
					var base=GetPersonBase(list[p]);
					OutlinedRectangle(MapToScreenX(1, GetPersonX(list[p])+detect), MapToScreenY(1, GetPersonValue(list[p], "MaxY")+1), base.x2-base.x1, base.y2-base.y1, CreateColor(0,0,0))
				}
			}
		}
	}
}

function EnforceLawsOfGravity()
{
	var list=GetPersonList();
	for (p in list)
	{
		var x=GetPersonX(list[p]);
		var y=GetPersonY(list[p]);
		var yaccel=GetPersonValue(list[p], "Y_Acceleration");
		var restitution=GetPersonValue(list[p], "Restitution");
		if (!IsPersonObstructed(list[p], x, y+1) || IsPersonObstructed(list[p], x, y))
		{
			yaccel+=gravity*meter/60;
		}
		for (var i=0; i!=Floor(yaccel) && yaccel>0; i++)
		{
			if (!IsPersonObstructed(list[p], x, y+1) || IsPersonObstructed(list[p], x, y))
			{
				y+=speed;
			}
			else
			{
				yaccel*=-restitution;
			}
		}
		for (var i=0; i!=Floor(yaccel) && yaccel<0; i--)
		{
			if (!IsPersonObstructed(list[p], x, y-1) || IsPersonObstructed(list[p], x, y) || GetObstructingTile(list[p], x, y-1)!=-1)
			{
				y-=speed;
			}
		}
		SetPersonX(list[p], x);
		SetPersonY(list[p], y);
		SetPersonValue(list[p], "Y_Acceleration", yaccel);
		SetPersonValue(list[p], "Restitution", restitution);		
	}
}

function Update()
{
	SetLayerRenderer(0, "sky.blit(0,0)")
	EnforceLawsOfGravity();
	var x=GetPersonX("Player 1");
	var y=GetPersonY("Player 1");
	if (IsKeyPressed(KEY_LEFT))
	{
		QueuePersonCommand("Player 1", COMMAND_FACE_WEST, true);
		if (!IsPersonObstructed("Player 1", x-1, y) || IsPersonObstructed("Player 1", x, y))
		{
			QueuePersonCommand("Player 1", COMMAND_ANIMATE, true);
			SetPersonX("Player 1", x-1);
		}
	}
	if (IsKeyPressed(KEY_RIGHT) && !IsKeyPressed(KEY_LEFT))
	{
		QueuePersonCommand("Player 1", COMMAND_FACE_EAST, true);
		if (!IsPersonObstructed("Player 1", x+1, y) || IsPersonObstructed("Player 1", x, y))
		{
			QueuePersonCommand("Player 1", COMMAND_ANIMATE, true);
			SetPersonX("Player 1", x+1);
		}
	}
	if (IsKeyPressed(KEY_DOWN))
	{
		QueuePersonCommand("Player 1", COMMAND_FACE_SOUTH, true);
	}
	if (IsKeyPressed(KEY_SPACE) && IsPersonObstructed("Player 1", x, y+1) && GetPersonValue("Player 1", "Y_Acceleration")==0)
	{
		SetPersonValue("Player 1", "Y_Acceleration", -3);
	}
}

function Render()
{
	debug.render();
}

var BadGuySystem=new _BadGuySystem();
function _BadGuySystem()
{
	this.badguy_command_generator=function()
	{
		var person=GetCurrentPerson();
		var PersonX=GetPersonX(person);
		var PersonY=GetPersonY(person);
		var direction=GetPersonValue(person, "WalkDirection");
		if (direction=="WEST")
		{
			if (GetObstructingTile(person, PersonX-16, PersonY+1)!=-1)
			{
				QueuePersonCommand(person, COMMAND_FACE_WEST, false);
				QueuePersonCommand(person, COMMAND_MOVE_WEST, true);
			}
			else
			{
				direction="EAST";
			}
		}
		if (direction=="EAST")
		{
			if (GetObstructingTile(person, PersonX+16, PersonY+1)!=-1)
			{
				QueuePersonCommand(person, COMMAND_FACE_EAST, false);
				QueuePersonCommand(person, COMMAND_MOVE_EAST, true);
			}
			else
			{
				direction="WEST";
			}
		}
		SetPersonValue(person, "WalkDirection", direction);
	}
	
	this.jumper_command_generator=function()
	{
		var person=GetCurrentPerson();
		var PersonX=GetPersonX(person);
		var PersonY=GetPersonY(person);
		var yaccel=GetPersonValue(person, "Y_Acceleration");
		var MaxY=GetPersonValue(person, "MaxY");
		var direction=GetPersonValue(person, "WalkDirection");
		if (PersonY>MaxY)
		{
			MaxY=PersonY;
		}
		if (direction=="WEST")
		{
			if (GetObstructingTile(person, PersonX-16, MaxY+1)!=-1)
			{
				QueuePersonCommand(person, COMMAND_FACE_WEST, false);
				QueuePersonCommand(person, COMMAND_MOVE_WEST, false);
			}
			else
			{
				direction="EAST";
			}
		}
		if (direction=="EAST")
		{
			if (GetObstructingTile(person, PersonX+16, MaxY+1)!=-1)
			{
				QueuePersonCommand(person, COMMAND_FACE_EAST, false);
				QueuePersonCommand(person, COMMAND_MOVE_EAST, false);
			}
			else
			{
				direction="WEST";
			}
		}
		if (yaccel==0)
		{
			yaccel=-3;
		}
		SetPersonValue(person, "Y_Acceleration", yaccel);
		SetPersonValue(person, "WalkDirection", direction);
		SetPersonValue(person, "MaxY", MaxY);
	}
	
	this.InitBadGuy=function()
	{
		var person=GetCurrentPerson();
		QueuePersonCommand(person, COMMAND_FACE_WEST, false);
		QueuePersonCommand(person, COMMAND_MOVE_WEST, false);
		SetPersonValue(person, "Y_Acceleration", 0);
		SetPersonValue(person, "MaxY", GetPersonY(person));
		SetPersonValue(person, "Restitution", 0);
		SetPersonValue(person, "WalkDirection", "WEST");
		SetPersonScript(person, SCRIPT_COMMAND_GENERATOR, "BadGuySystem.badguy_command_generator()");
	}
	
	this.InitFastGuy=function()
	{
		var person=GetCurrentPerson();
		QueuePersonCommand(person, COMMAND_FACE_WEST, false);
		QueuePersonCommand(person, COMMAND_MOVE_WEST, false);
		SetPersonSpeed(person, 2);
		SetPersonValue(person, "Y_Acceleration", 0);
		SetPersonValue(person, "MaxY", GetPersonY(person));
		SetPersonValue(person, "Restitution", 0);
		SetPersonValue(person, "WalkDirection", "WEST");
		SetPersonScript(person, SCRIPT_COMMAND_GENERATOR, "BadGuySystem.badguy_command_generator()");
	}
	
	this.InitJumper=function()
	{
		var person=GetCurrentPerson();
		QueuePersonCommand(person, COMMAND_FACE_WEST, false);
		QueuePersonCommand(person, COMMAND_MOVE_WEST, false);
		SetPersonValue(person, "Y_Acceleration", 0);
		SetPersonValue(person, "MaxY", GetPersonY(person));
		SetPersonValue(person, "Restitution", 0);
		SetPersonValue(person, "WalkDirection", "WEST");
		SetPersonScript(person, SCRIPT_COMMAND_GENERATOR, "BadGuySystem.jumper_command_generator()");
	}
}

function game()
{

//S = LoadSound("chip-something-serious.it",true);
//S = LoadSound("international_karate.mp3",true);
//S.play(true);
//while(!IsAnyKeyPressed()){}
//Abort("");

	CreatePerson("Player 1", "player.rss", true);
	SetPersonValue("Player 1", "Y_Acceleration", 0);
	SetPersonValue("Player 1", "Restitution", 0);
	AttachCamera("Player 1");
	SetRenderScript("Render()");
	SetUpdateScript("Update()");
	MapEngine("Map 2.rmp", 60);
}