#include scener
#include input
#include kdsqueststate
#include kds3
#include sound

export ON_CREATE;
export ON_LBUTTONDOWN;
export ON_COMMAND;
export ON_SETFOCUS;
export ON_KILLFOCUS;

vec3_t dist;
vec3_t lookForward;
vec3_t lookBack;

vec3_t spos;

void ON_CREATE()
{
	dist.Assign(50.0, -100.0, 0.0);
	lookBack.Assign(0.0, 1.0, 0.0);
	lookForward.Assign(0.0, -1.0, 0.0);
}

void ON_LBUTTONDOWN()
{
	ObjectClick(0, dist, self);
}

void ON_SETFOCUS()
{
	if (!GetProperty("OpenWindow"))
	{
		SetKdsCursor(CURSOR_TAKE);
		SetFocusObject(self, "WINDOW");
	}
}

void ON_KILLFOCUS()
{
	SetKdsCursor(CURSOR_NORMAL);
	SetFocusObject(NULL, "");
}

void ON_COMMAND(int lparam, int rparam)
{
	if (lparam == EMC_WOODY_QUERY) // запрос от Вуди
	{
		LINK* woody = scene.FindObjectByName("01Hero");

		if (ActiveItem() || GetProperty("OpenWindow")) // выбран ли предмет в инвентаре
		{
			PersJob* woodyJob = AddAnimationStackJob(woody);
			AddFreeAnimation(woodyJob, 7, lookForward); // What?
		}
		else
		{
			PersJob* woodyJob = AddAnimationStackJob(woody);
			AddFreeAnimation(woodyJob, 8, lookBack); // Aha!
			AddFreeAnimation(woodyJob, 2, lookBack); // Manipulate
			AddEvent(woodyJob, self, 1, 5);
			AddEvent(woodyJob, self, 1, 100);
			AddEvent(woodyJob, self, 1, 10);
			AddEvent(woodyJob, self, 1, 103);
		}
		if (ActiveItem())
			ResetActiveItem();
	}

	 if (lparam == EMC_EVENT)
	 {
		if (rparam == 2)
		{
			AddPrize();
			AddAngryLevel(50);
		}
		if (rparam == 5)
		{
			SetProperty("OpenWindow", true);
			WoodyTalk("WINDOW_SHOW", 5000);
		}
		if (rparam == 10)
		{
			LINK* neib = scene.FindObjectByName("01Hero");
			spos.Assign(0, 0, 0);
			sound.CreateSource( neib, "window_open", 4, 0, 0, spos, 1.0 );
		}
		if (rparam == 11)
		{
			LINK* neib = scene.FindObjectByName("01Hero");
			spos.Assign(0, 0, 0);
			sound.CreateSource( neib, "window_close", 4, 0, 0, spos, 1.0 );
		}
		if (rparam == 51)
		{
			EndGameTest();
		}
		if (rparam == 100)
		{
			LINK *pLink = scene.FindObjectByName("_Wind_open");
			pLink->GetChild()->SetVisible(1);
			LINK *pLink = scene.FindObjectByName("_Wind_close");
			pLink->GetChild()->SetVisible(0);
        }
		if (rparam == 101)
		{
			LINK *pLink = scene.FindObjectByName("_Wind_open");
			pLink->GetChild()->SetVisible(0);
			LINK *pLink = scene.FindObjectByName("_Wind_close");
			pLink->GetChild()->SetVisible(1);
        }
		if (rparam == 103)
		{
			WriteLog("___________StopkaBumagi");
			AddDeletedEffect("TopolPuh2", 100000);
		}
	}
}
