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

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

--print("CaveLevel3_Init.lua")


doomLava = nil;


if Sound:MusicPlaying("CaveMusic") == false then
	Sound:StopMusic();
	Sound:PlayMusic("CaveMusic");
	Sound:PlayMusic("AmbientFire");
end

EventManager:AddEvent(false, 0.0, "PlayerData.MoveSpeed_ = 550");
	
DefaultCameraSpeed_2 = DefaultCameraSpeed + 0.03
if GameEngine.FirstRun == true then
	StartPos = LvlLoader:GetInfo("playerStart01").Pos_
	startCameraPosition01 = LvlLoader:GetInfo("startCameraPosition01").Pos_
	startCameraTarget01 = LvlLoader:GetInfo("startCameraTarget01").Pos_
	cameraStartPos = startCameraPosition01;
	startLockPos01 = LvlLoader:GetInfo("startLockPos01").Pos_
	doomLavaInfo = LvlLoader:GetInfo("doomLava01")
	
	EventManager:AddEvent(false, 0.0, "PlayerData:LockPlayerToPos(startLockPos01)")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetEmissiveColor(Vector3D(1.0, 0.0, 0.0), true)")
	EventManager:AddEvent(false, 0.0, "PlayerData:ToggleLight(true)")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetRimColor(Vector3D(0.0, 0.3, 1.0), 1.0)")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPositionLookat(startCameraPosition01, startCameraTarget01, 1.0)");
	EventManager:AddEvent(false, 0.0, "PlayerData:ShakeCamera(8.0, 3.0, 3.0)");
	
	SpawnTriggerInfo(LvlLoader:GetInfo("startTrigger01"), false, false,
	[[
		doomLava = GlobalSpawnController:SpawnFollowingLava(doomLavaInfo.Pos_, doomLavaInfo.Scale_, Vector3D(0.0, 0.0, 0.0), 0.015, true);
	]])
	SpawnTriggerInfo(LvlLoader:GetInfo("startTrigger02"), false, false,
	[[
		PlayerData:SetCameraPosition(Vector3D(0.0, 400.0, 0.0), 0.08, true, true);
	]])
	SpawnTriggerInfo(LvlLoader:GetInfo("startTrigger03"), false, false,
	[[
		PlayerData:SetCameraPosition(Vector3D(0.0, -100.0, 10.0), 0.08, true, true);
		EventManager:AddEvent(false, 0.1, "PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed_2, DefaultCameraRelative, DefaultCameraFixed)")
		EventManager:AddEvent(false, 1.5, "PlayerData:UnlockPlayer()")
	]])
	--print("Checking music")
	
else
	StartPos = LvlLoader:GetInfo("playerStart02").Pos_
	--StartPos = LvlLoader:GetInfo("playerStartEnd01").Pos_
	startCameraPosition02 = LvlLoader:GetInfo("startCameraPosition02").Pos_
	startCameraTarget02 = LvlLoader:GetInfo("startCameraTarget02").Pos_
	cameraStartPos = startCameraPosition01;
	startLockPos02 = LvlLoader:GetInfo("startLockPos02").Pos_
	doomLavaInfo = LvlLoader:GetInfo("doomLava01")
	
	EventManager:AddEvent(false, 0.0, "PlayerData:LockPlayerToPos(startLockPos02)")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetEmissiveColor(Vector3D(1.0, 0.0, 0.0), true)")
	EventManager:AddEvent(false, 0.0, "PlayerData:ToggleLight(true)")
	EventManager:AddEvent(false, 0.0, "PlayerData:SetRimColor(Vector3D(0.0, 0.3, 1.0), 1.0)")
	EventManager:AddEvent(false, 0.0, "PlayerData.MoveSpeed_ = 550");
	EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPositionLookat(startCameraPosition02, startCameraTarget02, 1.0)");
	EventManager:AddEvent(false, 2.0, "PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed_2, DefaultCameraRelative, DefaultCameraFixed)")
	EventManager:AddEvent(false, 3.5, "PlayerData:UnlockPlayer()");
	EventManager:AddEvent(false, 0.0, "PlayerData:ShakeCamera(3.0, 3.0, 3.0)");
	
	doomLava = GlobalSpawnController:SpawnFollowingLava(doomLavaInfo.Pos_, doomLavaInfo.Scale_, Vector3D(0.0, 0.0, 0.0), 0.015, true);
	
	
end


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

largeLavaPool01 = LvlLoader:GetInfo("largeLavaPool01")
GlobalSpawnController:SpawnLavaPool(largeLavaPool01.Pos_, largeLavaPool01.Scale_, largeLavaPool01.Rot_, 0.0, 0.65, 1)
largeLavaPool02 = LvlLoader:GetInfo("lavaPool05")
GlobalSpawnController:SpawnLavaPool(largeLavaPool02.Pos_, largeLavaPool02.Scale_, largeLavaPool02.Rot_, 0.0, 0.65, 1)

FinalTowerInfo = LvlLoader:GetInfo("level2EndTower01")
FinalTower = SpawnTowerInfo(FinalTowerInfo, false, false, false)
	
for i = 1, 0 do
	local PieceName = "level2Raft"
	if i < 10 then
		PieceName = PieceName .. 0 .. i
	else
		PieceName = PieceName .. i
	end
	local PieceInfo = LvlLoader:GetInfo(PieceName)
	SpawnTowerInfo(PieceInfo, true, false, false)
end

blobSpawns = {}
blobDirections = {}
towers = {}
towerNum = 26
for i = 1, towerNum do
	local PieceName = "level2Tower"
	if i < 10 then
		PieceName = PieceName .. 0 .. i
	else
		PieceName = PieceName .. i
	end
	local PieceInfo = LvlLoader:GetInfo(PieceName)
	towers[i] = SpawnTowerInfo(PieceInfo, false, false, true)
end
for i = 1, towerNum do
	local PieceName = "blobSpawn"
	if i < 10 then
		PieceName = PieceName .. 0 .. i
	else
		PieceName = PieceName .. i
	end
	local PieceInfo = LvlLoader:GetInfo(PieceName)
	blobSpawns[i] = PieceInfo.Pos_
end
for i = 1, towerNum do
	local PieceName = "blobDirection"
	if i < 10 then
		PieceName = PieceName .. 0 .. i
	else
		PieceName = PieceName .. i
	end
	local PieceInfo = LvlLoader:GetInfo(PieceName)
	blobDirections[i] = PieceInfo.Pos_
end

BlobRadius = 35.0
BlobMass = 0.1
BlobSpeed = 200.0
BlobNum = 10
ExplosionRadius = 300.0
ExplosionMagnitude = 400.0
function SpawnBlobExplosion(index)
	if index > 2 then
		towers[index]:InsertIntoWorld();
		GlobalSpawnController:SpawnBlobCluster(blobSpawns[index], blobDirections[index], BlobSpeed, BlobRadius, BlobMass, BlobNum, ExplosionRadius, ExplosionMagnitude);
	end
end

for i = 1, towerNum do
	local PieceName = "trigger"
	if i < 10 then
		PieceName = PieceName .. 0 .. i
	else
		PieceName = PieceName .. i
	end
	local PieceInfo = LvlLoader:GetInfo(PieceName)
	
	local Script = "SpawnBlobExplosion(" .. i .. ")"
	SpawnTriggerInfo(PieceInfo, false, false, Script)
end

for i = 1, 0 do
	local Name = "stalagmite"
	if i < 10 then
		Name = Name .. 0 .. i
	else
		Name = Name .. i
	end
	local Info = LvlLoader:GetInfo(Name)
	GlobalSpawnController:SpawnShard(Info.Pos_, Info.Scale_, Info.Rot_, 0.0);
end

GlobalSpawnController:AddJetSpawner(10)
for i = 1, 775 do
	local Name = "jetSpawnPoint"
	if i < 10 then
		Name = Name .. 0 .. i
	else
		Name = Name .. i
	end
	local Info = LvlLoader:GetInfo(Name)
	GlobalSpawnController.JetSpawner_:AddSpawn(JetSpawnPoint(Info.Pos_, Info.Scale_, Info.Rot_, 180))
end

for i = 1, 3 do
	local debrisName = "breakableRock"
	if i < 10 then
		debrisName = debrisName .. 0 .. i	
	else
		debrisName = debrisName .. i
	end
	Info = LvlLoader:GetInfo(debrisName)
	Scale = Vector3D(Info.Scale_.x, Info.Scale_.z, Info.Scale_.y)
	GlobalSpawnController:SpawnShard(Info.Pos_, Scale, Info.Rot_, 0.0);
end
--102
for i = 1, 0 do
	local PieceName = "compositeObjects/tikiTorch"
	if i < 10 then
		PieceName = PieceName .. 0 .. i
	else
		PieceName = PieceName .. i
	end
	local PieceInfo = LvlLoader:GetInfo(PieceName)
	GlobalSpawnController:SpawnIgobj(PieceInfo.Pos_, PieceInfo.Scale_, PieceInfo.Rot_, "levelLoader\\tikiTorch.igobj", PieceName, 0.01, true, 39);
end

finalBlobSpawn01 = LvlLoader:GetInfo("finalBlobSpawn01")
finalBlobDirection01 = LvlLoader:GetInfo("finalBlobDirection01")
SpawnTriggerInfo(LvlLoader:GetInfo("finalTrigger01"), false, false,
[[
	FinalTower:InsertIntoWorld();
	GlobalSpawnController:SpawnBlobCluster(finalBlobSpawn01.Pos_, finalBlobDirection01.Pos_, 1200.0, 60.0, 10.0, 1, 500.0, 1000.0);
	Sound:PlayEvent("LargeExplosion", finalBlobSpawn01.Pos_, Vector3D(0.0, 0.0, 0.0), true, false);
	PlayerData:ShakeCamera(2.0, 10, 10);
]])

doomLava02Info = LvlLoader:GetInfo("doomLava02")
SpawnTriggerInfo(LvlLoader:GetInfo("doomLavaTrigger01"), false, false,
[[
	if doomLava ~= nil then
		doomLava:Delete(0)
	end
	doomLava = GlobalSpawnController:SpawnDoomLava(doomLava02Info.Pos_, doomLava02Info.Scale_, doomLava02Info.Rot_, 0.017);
]])

SpawnTriggerInfo(LvlLoader:GetInfo("explosionTrigger01"), false, false,
[[
	Sound:PlayEvent("LargeExplosion2D", Vector3D(0.0, 0.0, 0.0), Vector3D(0.0, 0.0, 0.0), true, false);
	PlayerData:ShakeCamera(2.0, 10, 10);
]])



doomLava03Info = LvlLoader:GetInfo("doomLava03")
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger01"), false, false,
[[
	if doomLava ~= nil then
		doomLava:Delete(3.0)
	end
	doomLava = GlobalSpawnController:SpawnFollowingLava(doomLava03Info.Pos_, doomLava03Info.Scale_, Vector3D(0.0, 0.0, 0.0), 0.015, true);
]])
endLockPos01 = LvlLoader:GetInfo("endLockPos01").Pos_
endCameraPosition01 = LvlLoader:GetInfo("endCameraPosition01").Pos_
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger02"), false, false,
[[
	if doomLava ~= nil then
		doomLava:Delete(1.0)
	end
	PlayerData:LockPlayerToPos(endLockPos01);
]])
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger03"), false, false,
[[
	PlayerData:Launch(Vector3D(0.0, 1400.0, 0.0));
]])
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger04"), false, false,
[[
	--PlayerData:Launch(Vector3D(0.0, 0.0, 200.0));
]])
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger05"), false, false,
[[
	PlayerData:Launch(Vector3D(0.0, 500.0, 0.0));
]])
SpawnTriggerInfo(LvlLoader:GetInfo("endTrigger06"), false, false,
[[
	GameEngine.FirstRun = true;
	GameEngine.CurrentLevelNum_ = 2;
	GameEngine.GameOver = true;
	Sound:StopMusic();
]])



MinZ = -100000.0
KillsNeeded = 0

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