

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
	
	m_bMoveLeft = false;
	m_bMoveRight = false;
	m_bMoveUp = false;
	m_bMoveDown = false;
		
	m_levelFinished = false;

	GetInputManager:AddBinding("up,always", "OnKeyUp", this:GetID());
	GetInputManager:AddBinding("left,always", "OnKeyLeft", this:GetID());
	GetInputManager:AddBinding("down,always", "OnKeyDown", this:GetID());
	GetInputManager:AddBinding("right,always", "OnKeyRight", this:GetID());
	GetInputManager:AddBinding("control", "OnFire", this:GetID());
	GetInputManager:AddBinding("return", "OnFire", this:GetID());
	
	GetInputManager:AddBinding("escape", "OnKeyEscape", this:GetID());	
	Schedule(8000, this:GetID(), "OnOutOfTime();"); 	
	
end


function OnKeyEscape(bDown)
	Schedule(1, C_ENTITY_NONE, "SetupMainMenu();"); 	
	return false; //don't process other handlers
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

	//Create a new entity so we can attach our custom C++ brain to it
		m_entWall = CreateEntity(GetMapManager:GetActiveMap(), Vector2(0,0), "entity/doomwall.lua"); 	

  m_cursorX = g_gridSize/2;
  m_cursorY = g_gridSize/2;
 
 	m_healthBar = GetEntityByName("bar_template"):Clone(nil, Vector2(0,0));
	//preload these so they can be accessed as map/tilepics from anywhere from only the file name
	m_healthBar:RunFunction("Setup", "Horizontal", "", g_currentWave, g_totalWaves, C_ORIGIN_TOP_LEFT);
	if (g_currentWave > 0) then
		m_healthBar:RunFunction("SetVisualNum", g_currentWave-1);
	end

 	UpdatePosition();
 //show one time message in a bit
	//	g_SmartSound:SetMusic("", false, false);
	
	//	GetSoundManager:Play("audio/wall.ogg");
end

function ModifyPosition(changeX, changeY)

local query = "x=" .. m_cursorX+changeX .. ";y=" .. m_cursorY+changeY;
local ret = m_entWall:GetBrainManager():AskBrainByName("DoomWall", query);

	if (toboolean(ret)) then
		//success
			m_cursorX = m_cursorX+changeX;
			m_cursorY = m_cursorY+changeY;
  		GetSoundManager:Play("audio/move.wav");

	else
	  		GetSoundManager:Play("audio/blocked.wav");
	end

end

function UpdatePosition()

local query = "x=" .. m_cursorX .. ";y=" .. m_cursorY;
	m_entWall:GetBrainManager():SendToBrainByName("DoomWall", query);
end
 
 function OnFire(bKeyDown)
    m_bMoveLeft = bKeyDown;
	
	if (bKeyDown) then
	
	if (m_levelFinished) then
	return true;
	end
	
		local ret = m_entWall:GetBrainManager():AskBrainByName("DoomWall", "destroy_highlighted");

	if (toboolean(ret)) then
		//success
			//LogMsg("Destroyed brick");
		  GetSoundManager:Play("audio/block_removed.wav");
		  
				//well, did they win or are they dead in the water?
			m_levelFinished = true;
			
			Schedule(100, this:GetID(), "OnExamineMove();"); 	
	end
	end
	
	return true; //continue to process key callbacks for this key stroke
end

function OnExamineMove()

	  //did we solve the puzzle, alex?
			local ret = m_entWall:GetBrainManager():AskBrainByName("DoomWall", "is_solved");

	if (toboolean(ret)) then
			//they won
				//Start win level sequence, it will start the camera thing on its size automatically
				Schedule(450, C_ENTITY_NONE, 'GetSoundManager:Play("audio/wall_break.ogg");'); 	
				_G.g_currentWave = _G.g_currentWave + 1;
		
				if (_G.g_currentWave == _G.g_totalWaves) then
						m_healthBar:RunFunction("SetNum", g_currentWave);

					//Schedule(1000, C_ENTITY_NONE, "SetupGame();");
			
				Schedule(1500, C_ENTITY_NONE, "SetupPassed();");
				
				else
					//schedule next wave
				Schedule(1000, C_ENTITY_NONE, "SetupGame();");
				
				end
		
	  	else
	  	//they lost
				//Start win level sequence, it will start the camera thing on its size automatically
				Schedule(600, C_ENTITY_NONE, 'GetSoundManager:Play("audio/explode_chimes2.ogg");'); 	
				Schedule(3000, C_ENTITY_NONE, "SetupMainMenu();"); 	
  	
	  	end
end

function OnOutOfTime()
		
		if (m_levelFinished) then
	return true;
	end
	
		local ret = m_entWall:GetBrainManager():AskBrainByName("DoomWall", "is_solved");
	m_levelFinished = true; //so they can't keep moving
	Schedule(600, C_ENTITY_NONE, 'GetSoundManager:Play("audio/explode_chimes2.ogg");'); 	
	Schedule(3000, C_ENTITY_NONE, "SetupMainMenu();"); 	
end


function OnKeyLeft(bKeyDown)
    m_bMoveLeft = bKeyDown;
	
	if (bKeyDown) then
	
		ModifyPosition(-1, 0);
	end
	
	return true; //continue to process key callbacks for this key stroke
end

function OnKeyRight(bKeyDown)
    m_bMoveRight = bKeyDown;
	
	if (bKeyDown) then
	
				ModifyPosition(1, 0);
	end
	
	return true; //continue to process key callbacks for this key stroke
end

function OnKeyUp(bKeyDown)
    m_bMoveUp = bKeyDown;
	
	if (bKeyDown) then
	
				ModifyPosition(0, -1);
	end
	
	return true; //continue to process key callbacks for this key stroke
end

function OnKeyDown(bKeyDown)
    m_bMoveDown = bKeyDown;
	
	if (bKeyDown) then
	
				ModifyPosition(0, 1);
	end
	return true; //continue to process key callbacks for this key stroke
end


function OnKill()  //run when removed
end