--[[
Author: Will Graham (wgraham@digipen.edu)

Copyright 2008-2009 DigiPen (USA) Corp.
]]--

--print("CaveLevel_Init.lua")

EventManager:AddEvent(false, 0.0, "PlayerData:SetRimColor(Vector3D(0.0, 0.0, 0.0), 0.0)")
MinZ = -2000.0
DoomLava = nil;

EventManager:AddEvent(false, 0.0, "PlayerData.MoveSpeed_ = 450.0")

if GameEngine.FirstRun == true then
	StartupScene = true
	FirstCutScene = false
	SecondCutScene = 0
	dummyLavaCutscene = false
	if Sound:MusicPlaying("CaveMusic") == false then
		Sound:StopMusic();
		Sound:PlayMusic("CaveMusic");
	end
else
	StartupScene = false;
	FirstCutScene = false;
	SecondCutScene = 2;
	dummyLavaCutscene = false
end

if StartupScene == true then
	StartPos = LvlLoader:GetInfo("playerStart01").Pos_
	cameraStartPos = StartPos
	cameraPosition01 = LvlLoader:GetInfo("cameraPosition01")
	cameraTarget01 = LvlLoader:GetInfo("cameraTarget01")
	cameraPosition02 = LvlLoader:GetInfo("cameraPosition02")
	cameraTarget02 = LvlLoader:GetInfo("cameraTarget02")
	lockPos01 = LvlLoader:GetInfo("lockPos01")
	lockPos02 = LvlLoader:GetInfo("lockPos02")
	followingLava01 = LvlLoader:GetInfo("followingLava01")
	dummyLava01 = LvlLoader:GetInfo("dummyLava01")

	EventManager:AddEvent(false, 0.0, "PlayerData:SetRimColor(Vector3D(0.0, 0.3, 1.0), 1.0)")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetEmissiveColor(Vector3D(10.0, 0.2, 0.2), true)")
	EventManager:AddEvent(false, 0.0, "PlayerData:ToggleLight(true)")	
	EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPositionLookat(cameraPosition02.Pos_, cameraTarget02.Pos_, 1.0)")
	EventManager:AddEvent(false, 0.0, "PlayerData:LockPlayerToPos(lockPos01.Pos_)")
	
	SpawnTriggerInfo(LvlLoader:GetInfo("startTrigger01"), false, false,
	[[
		PlayerData:LockPlayerToPos(lockPos02.Pos_);
		DummyLava = GlobalSpawnController:SpawnFollowingLava(dummyLava01.Pos_, dummyLava01.Scale_, Vector3D(100.0, 300.0, -450.0), 400.0, false);
	]])
	
	SpawnTriggerInfo(LvlLoader:GetInfo("startTrigger02"), false, false,
	[[
		PlayerData:SetCameraPosition(Vector3D(100.0, 300.0, 0.0), 0.07, true, true);
		DoomLava = GlobalSpawnController:SpawnFollowingLava(followingLava01.Pos_, followingLava01.Scale_, Vector3D(0.0, 0.0, 0.0), 0.015, true);
	]])
	
	SpawnTriggerInfo(LvlLoader:GetInfo("startTrigger03"), false, false,
	[[
		PlayerData:SetCameraPosition(Vector3D(-50.0, -150.0, 25.0), 0.07, true, true);
		EventManager:AddEvent(false, 0.5, "PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed)");
	]])
	
	SpawnTriggerInfo(LvlLoader:GetInfo("startTrigger04"), false, false,
	[[
		if DummyLava ~= nil then
			DummyLava:Delete(0);
		end
		PlayerData:UnlockPlayer();
	]])
	
else
	StartPos = LvlLoader:GetInfo("playerStart02").Pos_
	cameraStartPos = StartPos
	lockPos03 = LvlLoader:GetInfo("lockPos03")
	followingLava01 = LvlLoader:GetInfo("followingLava01")
	
	EventManager:AddEvent(false, 0.0, "PlayerData:SetRimColor(Vector3D(0.0, 0.3, 1.0), 1.0)")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetEmissiveColor(Vector3D(10.0, 0.2, 0.2), true)")
	EventManager:AddEvent(false, 0.0, "PlayerData:ToggleLight(true)")
	EventManager:AddEvent(false, 0.0, "PlayerData:LockPlayerToPos(lockPos02.Pos_)")
	EventManager:AddEvent(false, 0.0, "PlayerData:Launch(Vector3D(100.0, 350.0, 0.0))")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPosition(Vector3D(-50.0, -150.0, 25.0), 1.0, true, true)")
	EventManager:AddEvent(false, 0.1, "PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed)");
	EventManager:AddEvent(false, 4.5, "PlayerData:UnlockPlayer()");
		
	DoomLava = GlobalSpawnController:SpawnFollowingLava(followingLava01.Pos_, followingLava01.Scale_, Vector3D(0.0, 0.0, 0.0), 0.015, true);
	
	
end
				
SpawnTriggerInfo(LvlLoader:GetInfo("trigger03"), false, false,
[[
	GlobalSpawnController:PushDynamicSpawn(DynamicSpawnPoint(Vector3D(0.0, 1.2, 140.0), Vector3D(10.0, 0.0, 0.0), Vector3D(0.0, 0.0, -200.0), 1.0, 50.0, GlobalSpawnController, 1));
	--GlobalSpawnController:PushDynamicSpawn(DynamicSpawnPoint(Vector3D(0.0, 1.7, 30.0), Vector3D(40.0, 40.0, 90.0), Vector3D(0.0, 0.0, 0.0), 1.0, 100.0, GlobalSpawnController, 2));
]])

mainRoomLavaInfo = LvlLoader:GetInfo("mainRoomLava01")
SpawnTriggerInfo(LvlLoader:GetInfo("mainRoomLavaTrigger01"), false, false, 
[[
	GlobalSpawnController:SpawnLavaPool(mainRoomLavaInfo.Pos_, mainRoomLavaInfo.Scale_, mainRoomLavaInfo.Rot_, 200.0, 500.0);
]])

endPos01 = LvlLoader:GetInfo("endPos01").Pos_
endCameraPos01 = LvlLoader:GetInfo("endCameraPos01").Pos_
endCameraPos02 = LvlLoader:GetInfo("endCameraPos02").Pos_
endTargetPos01 = LvlLoader:GetInfo("endTargetPos01").Pos_
endBlobSpawn01 = LvlLoader:GetInfo("endBlobSpawn01").Pos_
endBlobSpawn02 = LvlLoader:GetInfo("endBlobSpawn02").Pos_
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger01"), false, false, 
[[
	PlayerData:LockPlayerToPos(endPos02);
	
	EventManager:AddEvent(false, 0.0, "GlobalSpawnController:SpawnBlobCluster(endBlobSpawn01, Vector3D( -350.0, -40.0, 0.0), 5.0, 30.0, 5)");
	EventManager:AddEvent(false, 0.0, "GlobalSpawnController:SpawnBlobCluster(endBlobSpawn02, Vector3D( -350.0, -40.0, 0.0), 5.0, 30.0, 5)");
	EventManager:AddEvent(false, 0.5, "GlobalSpawnController:SpawnBlobCluster(endBlobSpawn01, Vector3D( -350.0, -40.0, 0.0), 5.0, 30.0, 5)");
	EventManager:AddEvent(false, 0.5, "GlobalSpawnController:SpawnBlobCluster(endBlobSpawn02, Vector3D( -350.0, -40.0, 0.0), 5.0, 30.0, 5)");
	
	if DoomLava ~= nil then
		DoomLava:Delete(0);
	end
	
	PlayerData:SetCameraPosition(endCameraPos02, 0.03, false, true);
	--PlayerData:SetCameraPositionLookat(endCameraPos01, endTargetPos01, 1.0);
	
	GlobalSpawnController:ClearSpawns();
]])

endPos02 = LvlLoader:GetInfo("endPos02").Pos_
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger02"), false, false, 
[[
	PlayerData:LockPlayerToPos(endPos02);
	PlayerData:Launch(Vector3D(-930.0, 0.0, 340.0));
]])

SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger03"), false, false, 
[[
	--PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed);
]])

SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger06"), false, false, 
[[
	PlayerData:Launch(Vector3D(-500.0, 0.0, 0.0));
	PlayerData:LockPlayer(Vector3D(-1.0, 0.0, 0.0));
]])

SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger05"), false, false, 
[[
	GameEngine.FirstRun = true;
	GameEngine.CurrentLevelNum_ = 2;
	GameEngine.GameOver = true;
	Sound:StopMusic();
]])

for i = 1, 0 do
	local lavaName = "lavaPool"
	
	if i < 10 then
		lavaName = lavaName .. 0 .. i	
	else
		lavaName = lavaName .. i
	end
	
	Info = LvlLoader:GetInfo(lavaName)
	
	GlobalSpawnController:SpawnLavaPool(Info.Pos_, Info.Scale_, Info.Rot_, 200.0, 500.0)
end

--41
for i = 1, 50 do
	local debrisName = "debris"
	
	if i < 10 then
		debrisName = debrisName .. 0 .. i	
	else
		debrisName = debrisName .. i
	end
	
	Info = LvlLoader:GetInfo(debrisName)

	--GlobalSpawnController:SpawnColumn(Info.Pos_, Vector3D(0.0, 0.0, 0.0), Info.Scale_, Info.Rot_, Vector3D(5.0, 5.0, 5.0), 0.0);
	
	Scale = Vector3D(Info.Scale_.x, Info.Scale_.z, Info.Scale_.y)
	GlobalSpawnController:SpawnShard(Info.Pos_, Scale, Info.Rot_, 0.0);
end


if GameEngine.FirstRun == true then
	GameEngine.FirstRun = false
end