
m_minSpawnMS = 5000;
m_spawnRandomAddMS = 5000;

function OnInit() //before the entity is placed in the world, init visuals here, so it knows the size dimensions
	this:SetPersistent(false); //don't save us with the map
	LogMsg("Initting gamelogic object");
	this:SetImage("lua_script.png", nil); //the image we'll show so we can be seen in  the editor, dunno why we care though
	//this:SetAttach(C_ENTITY_CAMERA, Vector2(0,0); //so we are always on screen and processed	
	this:SetName("GameLogic"); //so we can find it by name later
end

function OnPostInit() //run during the entity's first logic update
	this:SetLayerByName("Hidden Data"); //we can only see the image when using the editor
	GetInputManager:AddBinding("mouse_left", "OnMouseLeft", this:GetID());

	//show one time message in a bit
	Schedule(500, this:GetID(), "ShowStartupMessage()");


	Schedule(5000+Random(5000), this:GetID(), "OnSpawnSomething()"); //run this again..
	g_SmartSound:SetMusic("audio/Arcadia.ogg", true, true);
	//Schedule(40000, this:GetID(), "m_minSpawnMS=50; m_spawnRandomAddMS = 50;"); 
	
end

function OnSpray()

	m_minSpawnMS=50;
  m_spawnRandomAddMS = 50;
end

function ShowStartupMessage()
	ShowMessage("Welcome to the adventure!", "After landing, you grab your gear and head-out.\n\nIf you see any strange and exciting creatures, click on them to capture for fame and fortune.\n\nGood luck!", false);

end

function OnCreatureClicked(ent)

	local name = ent:RunFunction("GetCreatureName");
	
	LogMsg("You clicked a", name);
	if (name == "bird") then
		ShowMessage("FAILURE", "You clicked a bird, dumbass.\n\nThat is not a mythological creature or legend.", false);
	end

	if (name == "rabbit") then
		ShowMessage("FAILURE", "No, that's just a large rabbit.  Can't sell that.", false);
	end

	if (name == "deer") then
		ShowMessage("FAILURE", "What is wrong with you?!\n\nThat's just a deer.", false);
	end

	if (name == "cat") then
		ShowMessage("FAILURE", "Yeah, right, a house-cat is special and rare.\n\nQuit goofing around and keep your eyes peeled!", false);
	end

	
end

function OnMouseLeft(bKeyDown)
	
	//we only care if they pressed down the button, not if they are releasing it
	if (bKeyDown == false) then return true; end;
	
		local pt = ScreenToWorld(GetInputManager:GetMousePos());
		local ent = GetEntityByWorldPos(pt, nil, false); //that last false can be true for pixel accurate checks
	
		if (ent == nil) then return true; end; //clicked nothing useful

		//well, they clicked an entity, but is it one of ours?
		
		if ent:FunctionExists("GetCreatureName") then
		  OnCreatureClicked(ent);		
			end
	
		
	return true; //Let other callbacks get called
end

function OnSpawnSomething()

	local creatureName = "creature" .. tostring(random(4))-1 .. "_template";
	LogMsg("Spawning a ", creatureName);
	local ent = GetEntityByName(creatureName):Clone(nil, Vector2(-64, 300+Random(200)));
	ent:SetPersistent(false);
	ent:GetBrainManager():Add("StandardBase", "");
	ent:GetBrainManager():SetStateByName("Walk");
	ent:SetFacing(C_FACING_RIGHT);
	ent:SetDesiredSpeed(6+Random(7)); //how fast we actually want to go
	

	Schedule(5000,ent:GetID(), "this:SetDeleteFlag(true)"); //kill ourselves in a bit
	
	Schedule(m_minSpawnMS+Random(m_spawnRandomAddMS), this:GetID(), "OnSpawnSomething()"); //run this again..

end

function OnKill()  //run when removed
end