-- /*Start File Header -------------------------------------------------------
-- Copyright (C) 2010 DigiPen Institute of Technology.
-- Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited.
-- File Name: TimeLogic.lua
-- Purpose: Has some Lua functions to manage Tile Collision.  Created to manage time logic stuff.
-- Authors: Trevor Bennett - 40%
--  		    Dan Rosas      - 20%
--          Bryan Bishop   - 20%
--          Robert Francis - 20%
-- End File Header ---------------------------------------------------------*/

dofile "Resources\\lua\\defines.lua";

TimeStopx = 10000;
TimeStopy = 10000;
z = 10000;
timestop = false;
canstop = false;

RegisterForMessage("Tile", "TileCollision");

function TileCollision(obj1, obj2, inst1, inst2, enter)
	if obj1 == "Player" and obj2 == "MusicFlower" and GOGetActive(obj2, inst2) and enter then -- Collision with Music Flower
		PlaySound("Breadcrumb");	
		x1, y2, z3 = GOGetPos("MusicFlower", inst2);
		ParticleSystem("blueExplode", x1,y2,z3);	
		text = string.format([[
		GOActive("MusicFlower", %d, false);
		IncScore();]], inst2);
		SendDelayedLuaMessage(text, 0.0);
				
	elseif obj1 == "Player" and obj2 == "TimeFlower" and enter then -- Collision with Time Flower
	    TimeStopx, TimeStopy, z = GOGetPos(obj2, inst2);
	    --if canstop then
			--PlaySound("TimeStopWalkBy");
			--x, y, z = GOGetPos(obj2, inst2);
			--StopTime();
			--stop = 0;
		--end
    
	elseif obj1 == "Player" and obj2 == "BoomFlower" and enter then
		vinetext = string.format([[SetCompBool("BoomFlower", %i, "Vine", "Grow", true);]], inst2);
		SendDelayedLuaMessage(vinetext, 0.0);
		text = string.format([[
		GOActive("Thorns", %i * 5 + 0, false);
		GOActive("Thorns", %i * 5 + 1, false);
		GOActive("Thorns", %i * 5 + 2, false);
		GOActive("Thorns", %i * 5 + 3, false);
		GOActive("Thorns", %i * 5 + 4, false);]], 
		inst2, inst2, inst2, inst2, inst2);
		SendDelayedLuaMessage(text, 1.0); -- add another inst2 if you uncomment ParticleSystem
		PlaySound("PianoString");
		SetBGM("StringLine", true, false);
		
	elseif obj1 == "Player" and obj2 == "LevelEnder" and enter then
		x, y, z = GOGetPos(obj2, inst2);
		PlaySound("LevelEnder");
		ParticleSystem("Particle_LevelEnder", x,(y - 0.5),z);
		SendDelayedLuaMessage("EndLevel();", 0.6);
		
	elseif obj1 == "Player" and obj2 == "Thorns" and GOGetActive(obj2, inst2) and enter then
		PlaySound("Piano_Lo")
		RestartToCheckPoint();
		
	elseif obj1 == "Player" and obj2 == "CheckPoint" and enter then
		if (inst2 == 0) then
			if (GetLevelName() == "gold01.xml") then
				SendDelayedLuaMessage("StopLayerRange(1, 1)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(2, 2, false)", 0.0);
			elseif (GetLevelName() == "gold02.xml") then
				SendDelayedLuaMessage("StopLayerRange(2, 2)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(3, 3, false)", 0.0);
			elseif (GetLevelName() == "gold03.xml") then
				SendDelayedLuaMessage("StopLayerRange(4, 4)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(5, 5, false)", 0.0);
			elseif (GetLevelName() == "gold04.xml") then
				SendDelayedLuaMessage("StopLayerRange(6, 6)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(7, 7, false)", 0.0);
			elseif (GetLevelName() == "gold05.xml") then
				SendDelayedLuaMessage("StopLayerRange(8, 8)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(9, 9, false)", 0.0);
			elseif (GetLevelName() == "Outside.xml") then
				PlaySound("Outro");
        PlayCredits = true;
			end
		elseif (inst2 == 1) then
			if (GetLevelName() == "gold02.xml") then
				SendDelayedLuaMessage("StopLayerRange(3, 3)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(4, 4, false)", 0.0);
			elseif (GetLevelName() == "gold03.xml") then
				SendDelayedLuaMessage("StopLayerRange(5, 5)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(6, 6, false)", 0.0);
			elseif (GetLevelName() == "gold04.xml") then
				SendDelayedLuaMessage("StopLayerRange(7, 7)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(8, 8, false)", 0.0);
			elseif (GetLevelName() == "gold05.xml") then
				SendDelayedLuaMessage("StopLayerRange(9, 9)", 0.0);
				SendDelayedLuaMessage("MuteLayerRange(10, 10, false)", 0.0);
			end
		elseif (inst2 == 2) then
			SendDelayedLuaMessage("StopLayerRange(10, 10)", 0.0);
			SendDelayedLuaMessage("MuteLayerRange(11, 11, false)", 0.0);
		end
		x, y, z = GOGetPos(obj2, inst2);
		ParticleSystem("RedParticles", x,(y - .25),z);
		SaveCheckPoint();
		--vinetext = string.format([[SetCompBool("CheckPoint", %i, "Vine", "Grow", true);]], inst2);
		--SendDelayedLuaMessage(vinetext, 0.0);
		PlaySound("Checkpoint");
		
	elseif obj1 == "Player" and obj2 == "Switch" and GOGetActive(obj2, inst2) and enter then
		PlaySound("MachineOn");	
		x1, y2, z3 = GOGetPos("Switch", inst2);
		ParticleSystem("blueExplode", x1,y2,z3);	
		text = string.format([[
		GOActive("Switch", %d, false);
		IncScore();]], inst2);
		SendDelayedLuaMessage(text, 0.0);
		
		go = GetGOPointer("Move_Tile3", 0);
		p = GetPhysicsComp(go);
		PhysicsComp_ClearDestinations(p);
		PhysicsComp_AddDestination(p, 8, -2, 4);
		PhysicsComp_AddDestination(p, 8, -2, 1);
		PhysicsComp_AddDestination(p, 8, 3, 4);
		PhysicsComp_AddDestination(p, 8, 3, 1);

    SetModel("PHY_Generator_Off", 0, "PHY_Generator_On");
	end
end

--CreateMouseAction(MOUSE_RIGHT, "ToggleTime"); --Right click
CreateKeyboardAction(VK_DOWN, "ToggleTime"); --Down Arrow
CreateKeyboardAction(VK_S, "ToggleTime"); --S

function ToggleTime()
	--if canstop and timestop then
		--canstop = false;
		--PlaySound("TimeDeactivate");
	--end
	p = IsGameObject("Player", 0);
	if p == false then
		return;
	end
	curx, cury, z = GOGetPos("Player", 0);
	if math.abs(TimeStopx - curx) < .5 and math.abs(TimeStopy - cury) < .5 then
		if timestop then
			StartTime();
		else
			StopTime();
		end
	end
	if not canstop or not timestop then
		canstop = not canstop;
		--if(canstop)
			--PlaySound("TimeActivate");
		--else
			--PlaySound("TimeDeactivate");
		--end
	end
  SetSelfIllum("GAME_Region", canstop);
end

function StopTime()
  if timestop == false then
    --ParticleSystem("Particle_TimeStop", x,y,z);
	  VisibleCursor(true);
		timestop = true;
		PlaySound("StopTime");
		Game("GameStopTime");
		SetBGM("TimeStopMusic", true, false);
	  --SaveCheckPoint();
  end
end

function StartTime()
	if timestop == true then
		PlaySound("StartTime");
		Game("GameStartTime");
		SetBGM("", true, true);
		timestop = false;
		canstop = false;
		stop = 1;
    VisibleCursor(false);
	end
end

function TimeRegion(name, inst)
  --if canstop == true or timestop == true then
    go = GetGOPointer(name, inst);
    DrawModel("GAME_Active", go);
  --end
end
