

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	
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());
	
	g_SmartSound:SetMusic("audio/Exciting Trailer.ogg", false, false);
	
end

function OnMapClicked(pt)

	//put the arrow on the screen
	
	local ent = CreateEntity(nil, pt, "items/pointer/pointer.lua");
	ent:SetLayerByName("GUI 1"); //overlay  the rest of the action
	ent:SetScale(Vector2(0.5,0.5));
	ent:SetPersistent(false); //don't save with the map
	//ent:SetAttach(entToAttachTo:GetID(), Vector2(0,-(entToAttachTo:GetSizeY() + offsetY))); //so it stays on the screen

	GetSoundManager:Play("audio/jet.ogg");
	g_SmartSound:FadeOutCurrentMusic(); //just in case any is playing
	
	//show loading graphic
	local entLoad = GetEntityByName("loading_template"):Clone(nil, GetApp:GetScreenSize()/2);
	entLoad:SetPersistent(false); //don't save with map
	entLoad:GetBrainManager():Add("FadeIn", "fade_speed_ms=2000");		
	
	GetInputManager:RemoveBinding("mouse_left", "OnMouseLeft", this:GetID()); //so they can't click again
	Schedule(4000, C_ENTITY_NONE, "SetupGame()");

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, true); //that last false can be true for pixel accurate checks
	
		if (ent == nil) then 
				GetSoundManager:Play("audio/splash.ogg");
				return true; 
				end; //clicked nothing useful

		//well, they clicked an entity, but is it one of ours?
		  
		  OnMapClicked(pt);		
		
	return true; //Let other callbacks get called
end

function OnSpawnSomething()

	local ent = GetEntityByName("bird_template"):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(5000, this:GetID(), "OnSpawnSomething()"); //run this again..

end

function OnKill()  //run when removed
end