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

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

--print("BridgeLevel_Init.lua")

cameraInfo01 = LvlLoader:GetInfo("cameraPosition01")
cameraInfo02 = LvlLoader:GetInfo("cameraPosition02")
cameraInfo03 = LvlLoader:GetInfo("cameraPosition03")
cameraInfo04 = LvlLoader:GetInfo("cameraPosition04")
cameraInfo05 = LvlLoader:GetInfo("cameraPosition05")
cameraInfo06 = LvlLoader:GetInfo("cameraPosition06")
cameraInfo07 = LvlLoader:GetInfo("cameraPosition07")
cameraInfo08 = LvlLoader:GetInfo("cameraPosition08")

cameraStartPos = cameraInfo01.Pos_

--EventManager:AddEvent(false, 0.0, "PlayerData:LockPlayer(Vector3D(0.0, 0.0, 0.0))");
EventManager:AddEvent(false, 0.0, "PlayerData:LockPlayer(Vector3D(0.0, 0.0, 0.0))");


if GameEngine.FirstRun == true then
	startCameraPosition01 = LvlLoader:GetInfo("startCameraPosition01");
	startCameraTarget01 = LvlLoader:GetInfo("startCameraTarget01");
	cameraStartPos01 = LvlLoader:GetInfo("cameraStartPos01").Pos_;
	cameraStartDir01 = LvlLoader:GetInfo("cameraStartDir01").Pos_;
	
	StartPos = LvlLoader:GetInfo("playerStart01").Pos_;
	
	
	EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPositionLookat(cameraStartPos01, cameraStartDir01, 1.0)");
	
	startTime = 4.0;
	EventManager:AddEvent(false, startTime, "PlayerData:ShakeCamera(3.0, 30, 30)")
	EventManager:AddEvent(false, startTime + 1.0, "PlayerData:LockPlayer(Vector3D(0.0, 1.0, 0.0))");
	EventManager:AddEvent(false, startTime, "PlayerData:SetCameraPosition(startCameraPosition01.Pos_, 0.01, false, true)");
	
	blobSpawn01 = LvlLoader:GetInfo("blobSpawn01");
	blob = nil;
	
	EventManager:AddEvent(false, startTime + 2.0, "blob = GlobalSpawnController:SpawnBlob(blobSpawn01.Pos_, Vector3D(0.0, 150.0, 50.0), blobSpawn01.Scale_.x, 0.1, true, 0)");
	EventManager:AddEvent(false, startTime + 2.0, "blob:Delete(10)");
	
	particleName = "levelLoader/laserEffect.igobj";
	particlePos = LvlLoader:GetInfo("playerStart02").Pos_;
	--EventManager:AddEvent(false, startTime + 1.0, "GameEngine:PlayParticleEvent(particleName, blobSpawn01.Pos_, Vector3D(1000.0, 1000.0, 1000.0))");
	--EventManager:AddEvent(false, startTime + 1.5, "GameEngine:PlayParticleEvent(particleName, blobSpawn01.Pos_, Vector3D(1000.0, 1000.0, 1000.0))");
	--EventManager:AddEvent(false, startTime + 2.0, "GameEngine:PlayParticleEvent(particleName, blobSpawn01.Pos_, Vector3D(1000.0, 1000.0, 1000.0))");
	--EventManager:AddEvent(false, startTime + 2.5, "GameEngine:PlayParticleEvent(particleName, blobSpawn01.Pos_, Vector3D(1000.0, 1000.0, 1000.0))");
	--EventManager:AddEvent(false, startTime + 3.0, "GameEngine:PlayParticleEvent(particleName, blobSpawn01.Pos_, Vector3D(1000.0, 1000.0, 1000.0))");
	
	EventManager:AddEvent(false, startTime + 2.0, "Sound:PlayEvent(\"BridgeExplosion\", blobSpawn01.Pos_, Vector3D(0.0, 0.0, 0.0))");
	EventManager:AddEvent(false, startTime + 2.5, "Sound:PlayEvent(\"BridgeExplosion\", blobSpawn01.Pos_, Vector3D(0.0, 0.0, 0.0))");
	
	explosionPos01 = LvlLoader:GetInfo("explosionPos01")
	explosionDir01 = LvlLoader:GetInfo("explosionDIr01")
	explosionObj01 = GlobalSpawnController:SpawnSimple(explosionPos01.Pos_, explosionPos01.Scale_.x)
	explosionObj02 = GlobalSpawnController:SpawnSimple(explosionDir01.Pos_, explosionDir01.Scale_.x)
	
	EventManager:AddEvent(false, startTime + 1.0, "GameEngine:PlayParticleEvent(\"levelLoader/compositeObjects/lvl3Explosion.igobj\", explosionPos01.Pos_, explosionPos01.Scale_)")
	EventManager:AddEvent(false, startTime + 1.3, "GameEngine:PlayParticleEvent(\"levelLoader/compositeObjects/lvl3Explosion.igobj\", explosionPos01.Pos_, explosionPos01.Scale_)")
	
	
else
	EventManager:AddEvent(false, 0.1, "PlayerData:LockPlayer(Vector3D(0.0, 1.0, 0.0))");
	startCameraPosition02 = LvlLoader:GetInfo("startCameraPosition02");
	startCameraTarget02 = LvlLoader:GetInfo("startCameraTarget02");
	EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPositionLookat(startCameraPosition02.Pos_, startCameraTarget02.Pos_, 1.0)");
	--EventManager:AddEvent(false, 0.1, "PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed)");
	StartPos = LvlLoader:GetInfo("playerStart02").Pos_;
	--EventManager:AddEvent(false, 1.0, "PlayerData:UnlockPlayer()");
end

--EventManager:AddEvent(false, 0.1, "PlayerData:SetCameraPositionLookat(startCameraPosition02.Pos_, startCameraTarget02.Pos_, 0.02)");
--EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPosition(Vector3D(0.0, -100.0, 50.0), 1.0, true, true)");

--EventManager:AddEvent(false, 0.5, "PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed)");
EventManager:AddEvent(false, 0.0, "PlayerData.JumpDuration_ = 0");
EventManager:AddEvent(false, 0.0, "PlayerData.MoveSpeed_ = 350.0");

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:SetCameraPosition(Vector3D(0.0, -200.0, 100.0), 0.04, true, true)");
--EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPosition(cameraInfo01.Pos_, 0.01, false, true)");
--EventManager:AddEvent(false, 6.0, "PlayerData:SetCameraPosition(cameraInfo08.Pos_, 0.005, false, true)");
--EventManager:AddEvent(false, 12.0, "PlayerData:SetCameraPosition(cameraInfo01.Pos_, 0.005, false, true)");

-- Get player start position

BridgePieces = {}
BlockTowers = {}
BlockWalls = {}
Triggers = {}

StartPlatform = SpawnPlatformInfo(LvlLoader:GetInfo("startPlatform01"))
EndPlatform = SpawnPlatformInfo(LvlLoader:GetInfo("startPlatform02"))
TorchPlatform = SpawnPlatformInfo(LvlLoader:GetInfo("startPlatform03"))
SpawnPlatformInfo(LvlLoader:GetInfo("startPlatform04"))

DoomWall = nil

BridgeFirstRun = true
function TriggerActivated(TriggerIndex)

	if TriggerIndex + 2 < 38 then
		BridgePieces[TriggerIndex + 2]:InsertIntoWorld()
	end
	
	if TriggerIndex == 2 then
		if BridgeFirstRun == true then
			PlayerData:SetCameraPosition(Vector3D(0.0, 450.0, -100.0), 0.06, true, true)
		else
			PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed)
			EventManager:AddEvent(false, 1.0, "PlayerData:UnlockPlayer()");
		end
	elseif TriggerIndex == 4 then
		if BridgeFirstRun == true then
			PlayerData:SetCameraPosition(Vector3D(750.0, 150.0, 150.0), 0.06, true, true)
			--PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed)
		end
	elseif TriggerIndex == 5 then
		if BridgeFirstRun == true then
			PlayerData:SetCameraPosition(Vector3D(0.0, -100.0, 50.0), 0.06, true, true)
		end
	elseif TriggerIndex == 6 then
		if BridgeFirstRun == true then
			PlayerData:SetCameraPosition(DefaultCameraVec, DefaultCameraSpeed, DefaultCameraRelative, DefaultCameraFixed)
			PlayerData:UnlockPlayer()
		end
	elseif TriggerIndex == 36 then
		GlobalSpawnController:ClearSpawns();
	elseif TriggerIndex == 27 then
		BridgePieces[38]:InsertIntoWorld()
		BridgePieces[39]:InsertIntoWorld()
	elseif TriggerIndex == 28 then
		BridgePieces[40]:InsertIntoWorld()
	elseif TriggerIndex == 16 then
		BridgePieces[41]:InsertIntoWorld()
	end
	
	local RocksOffset = nil
	
	if TriggerIndex < 29 then
		RocksOffset = (TriggerIndex - 1) * 12
	else
		RocksOffset = 27 * 12 + (TriggerIndex - 28) * 18
	end

	if GameEngine.Difficulty_ == 1 then
		if TriggerIndex < 6 then
			--GlobalSpawnController:SpawnFallingRocks(1, 3.0, 3.5, 100.0, 300.0, 4.0, 8.0)
		elseif TriggerIndex < 25 then
			GlobalSpawnController:SpawnFallingRocks(4, 2.5, 4.0, 100.0, 300.0, 3.0, 15.0)
		elseif TriggerIndex < 36 then
			GlobalSpawnController:SpawnFallingRocks(5, 2.5, 4.0, 100.0, 300.0, 7.0, 15.0)
		end
	else
		if TriggerIndex < 6 then
			--GlobalSpawnController:SpawnFallingRocks(1, 3.0, 3.5, 100.0, 300.0, 4.0, 8.0)
		elseif TriggerIndex < 25 then
			GlobalSpawnController:SpawnFallingRocks(1, 3.0, 3.5, 100.0, 300.0, 3.0, 11.0)
		elseif TriggerIndex < 36 then
			GlobalSpawnController:SpawnFallingRocks(2, 3.0, 3.5, 100.0, 300.0, 5.0, 13.0)
		end
	end
	
	if TriggerIndex == 1 then
		DoomWall = SpawnDoomWallInfo(LvlLoader:GetInfo("doomWall01"))
		if BridgeFirstRun == true then
			PlayerData:ShakeCamera(4.0, 7, 7);
		else
			PlayerData:ShakeCamera(4.0, 2, 2);
		end
	end
end

--function SpawnTowerInfo(Info, Insert, Sleep)
for i = 1, 41 do
	local PieceName = "bridgePiece"
	if i < 10 then
		PieceName = PieceName .. 0 .. i
	else
		PieceName = PieceName .. i
	end
	local PieceInfo = LvlLoader:GetInfo(PieceName)
	BridgePieces[i] = SpawnTowerInfo(PieceInfo, false, true)	
end

for i = 1, 66 do
	local PieceName = "compositeObjects/crocHead"
	
	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\\crocHead.igobj", PieceName, 0.0, true, 30, true);
end

for i = 1, 0 do
	
	local CrocName = "lua"
	
	if i < 10 then
		CrocName = CrocName .. 0 .. i
	else
		CrocName = CrocName .. i
	end
	
	local CrocInfo = LvlLoader:GetInfo(CrocName)
	GlobalSpawnController:SpawnGenericObject(CrocInfo.Pos_, CrocInfo.Scale_, CrocInfo.Rot_, nil, nil, 0)

end

BridgePieces[1]:InsertIntoWorld()
BridgePieces[2]:InsertIntoWorld()
BridgePieces[3]:InsertIntoWorld()

for i = 1, 37 do

	local TriggerName = "trigger"

	if i < 10 then
		TriggerName = TriggerName .. 0 .. i
	else
		TriggerName = TriggerName .. i
	end
	
	local TriggerInfo = LvlLoader:GetInfo(TriggerName)
	
	local ScriptToRun = "TriggerActivated(" .. i .. ")"
	
	Triggers[i] = SpawnTriggerInfo(TriggerInfo, false, false, ScriptToRun)

end

for i = 1, 5 do

	local DeathBoundaryName = "deathBoundary"

	if i < 10 then
		DeathBoundaryName = DeathBoundaryName .. 0 .. i
	else
		DeathBoundaryName = DeathBoundaryName .. i
	end
	
	SpawnDeathBoundaryInfo(LvlLoader:GetInfo(DeathBoundaryName));

end

	--PlayerData:SetCameraPosition(cameraInfo05.Pos_, 0.04, false, true);
	
cameraEndPos01 = LvlLoader:GetInfo("cameraEndPos01").Pos_
cameraEndDir01 = LvlLoader:GetInfo("cameraEndDir01").Pos_
cameraEndPos02 = LvlLoader:GetInfo("cameraEndPos02").Pos_
cameraEndDir02 = LvlLoader:GetInfo("cameraEndDir02").Pos_
cameraEndPos03 = LvlLoader:GetInfo("cameraEndPos03").Pos_
SpawnTriggerInfo(LvlLoader:GetInfo("trigger38"), false, false, 
[[
	PlayerData:LockPlayer(Vector3D(0.0, 1.0, 0.0));
	EventManager:AddEvent(false, 8.0, "GameEngine.CurrentLevelNum_ = 3; GameEngine.GameOver = true; GameEngine.FirstRun = true;");
	EventManager:AddEvent(false, 0.0, "PlayerData:SetCameraPositionLookat(cameraEndPos01, cameraEndDir01, 0.005)");
	EventManager:AddEvent(false, 3.0, "PlayerData:SetCameraPositionLookat(cameraEndPos01, cameraEndDir02, 0.0085)");
	--EventManager:AddEvent(false, 5.0, "PlayerData:SetCameraPositionLookat(cameraEndPos03, cameraEndDir02, 0.015)");
	DoomWall:Delete(0)
]])


if GameEngine.FirstRun == true then
	BridgeFirstRun = true;
	GameEngine.FirstRun = false
	Sound:StopMusic();
	Sound:PlayMusic("BridgeMusic");
else
	BridgeFirstRun = false
end

MinZ = 500.0
KillsNeeded = 0