
function init(bIsRestart)

	CCLuaLog("level-intro - init")

	-- initialise level
	local no_cinemascope = true
	init_level("level-intro", CCPoint(-7.5, -29), bIsRestart, no_cinemascope)

	if (bIsRestart ~= true) then

		-- Start intro cut scene
		onIntroStart()		
	end

	-- preload descriptors
	game_manager:actorDescWithFile("lumberjack-concerned.plist")

	-- EASY MODE
--[[
	local bot_desc = game_manager:actorDescWithFile("deer.plist")
	bot_desc:setHealth(1);
	bot_desc = game_manager:actorDescWithFile("lumberbear.plist")
	bot_desc:setHealth(1);
	bot_desc = game_manager:actorDescWithFile("warriors-gruntbear.plist")
	bot_desc:setHealth(1);
	bot_desc = game_manager:actorDescWithFile("warriors-bear.plist")
	bot_desc:setHealth(1);
	player_desc = game_manager:actorDescWithFile("lumberjack.plist")
	player_desc:setSpeed(50);
]]

    -- preload sfx
   	game_manager:getSFXManager():preloadSFX("sfx/timeline-catchup.mp3");

	local backdrop = game_manager.Background
	
	if (backdrop ~= nil) then
		CCLuaLog("FOUND BACKDROP")
	end
	
	-- TESTING CAMERA SETUP STUFF
--[[
	local camera_manager = game_manager.CameraManager;
	camera_manager.GameScale.Target = 2;
	camera_manager.CameraCenterHeight.Target = -15;
]]--

	hud:doScreenFade(ccc4(255,255,255,255), 0);
	
	-- Enable health regen in story mode
	game_manager.Player.HealthRegenActive = true

	-- End of level bear not spawned yet
	lumberbear_spawned = false
	lumberbear_dead = false
end

-- After Intro has loaded
function onLevelIntro()

	-- We do our own intro sequence stuff for this level...

end


function onIntroStart(bIsRestart)

--	CCLuaLog("onIntroStart - " .. (bIsRestart and 1 or 0))

	if (bIsRestart ~= true) then
		-- Restart from here
	 	game_manager:setCheckpoint("onIntroStart");
	end

	local backdrop = game_manager.Background
	local camera_target = backdrop:getNodeByName("cameraTarget")
	camera_manager:setTarget(camera_target)

	-- Fade in from white
	hud:doScreenFade(ccc4(0,0,0,0), 0.5);

	-- Player no move
	disconnectControls(false)

	-- Wave 
	player:doActionByName("Wave")
	player:setFacingDir(kFacingDir_Right)

	-- Show the location (after we've waited for the camera to pan over)
	game_manager:startTimer(4, doDisplayLocation);

	return true

end

-- Show time & location bar
function doDisplayLocation()

	hud:showTimeAndLocation(localize("TIME_PRESENT_DAY"), localize("PLACE_TIM_BURRS_HOUSE"), 3, sayHelloTim);

end

function sayHelloTim()

	 game_manager:startTimer(4, showTapToSpeedup);

     hud:playSpeech("lumberjack", localize("INTRO_SPEECH_1"), speech_duration);
     hud:playSpeech("lumberjack", localize("INTRO_SPEECH_2"), speech_duration);
     hud:playSpeech("lumberjack", localize("INTRO_SPEECH_3"), speech_duration, nil, startShake);

end

function showTapToSpeedup()

	if (game_manager:isUsingTouchControls()) then
		hud:showRightThumb(localize("TUTORIAL_TAP_TO_SPEED_UP"))
	else
		local tutorial_text = localize("TUTORIAL_TAP_X_TO_SPEED_UP")
		local button_text = game_manager:getGamepadButtonString(kFistInput_Jump);
   		hud:showRightThumb(string.format(tutorial_text, button_text) );
	end

   	game_manager:startTimer(2, function() hud:hideRightThumb() end)

end

function startShake()

	game_manager:startTimer(5.5, doShake, true)
   	game_manager:startTimer(2.0, sayWhaaaa)
   	game_manager:startTimer(5.0, function() hud:doScreenFade(ccc4(255,255,255,255), 0.5) end)
	game_manager:startTimer(6.0, loadForestLevel)
  	game_manager:getSFXManager():playSFX("sfx/timeline-catchup.mp3")

end

function doShake(progress)
		
	game_manager.CameraManager.ShakeScale = progress

end		
	
function sayWhaaaa()
	
	local player = game_manager.Player

	player:doActionByName("Concerned");
	hud:playSpeech("lumberjack", localize("INTRO_SPEECH_4"), speech_duration - 1);

end		
	
function loadForestLevel()
	
	game_manager:setBackdropFile("level-forest")
	game_manager.CameraManager.ShakeScale = 0

	-- Start forest cutscene
	onForestStart(false)
end

-- After Forest has loaded
function onForestStart(bIsRestart)

	if (bIsRestart ~= true) then
		-- Restart from here
		game_manager:setCheckpoint("onForestStart");
	end
	
	player:doActionByName("Concerned");

	showLevelTimeAndLocation();
	game_manager:startTimer(1, whatHappenedSpeech);

    -- Fade in from white
	hud:doScreenFade(ccc4(255,255,255,255), 0)
	hud:doScreenFade(ccc4(0,0,0,0), 0.5)

	-- Stop small shift when camera focusses on Tim
	camera_manager:setJustRestarted(true)

	return true

end

function whatHappenedSpeech()

	say("lumberjack", localize("INTRO_SPEECH_5"));
    say("lumberjack", localize("INTRO_SPEECH_6"));
    say("lumberjack", localize("INTRO_SPEECH_7"), nil, function() game_manager:startTimer(1, fistIntro) end);
	
end
	
function fistIntro()
	
	-- Use regular descriptor because it has all the speech animations
--	local player_desc = game_manager:actorDescWithFile("lumberjack.plist")
--	game_manager:setPlayerDesc(player_desc)
	talkToFist();

	
    say("lumberjack", localize("INTRO_SPEECH_8"));
		
--	say("fist", "The question is not 'what', but 'when'.", fistTalk);
	say("fist", localize("INTRO_SPEECH_9"), fistTalk);

    say("lumberjack", localize("INTRO_SPEECH_10"), talkToFistScared);
        
    say("fist", localize("INTRO_SPEECH_11"), fistTalk);
        
    say("lumberjack", localize("INTRO_SPEECH_12"), talkToFistScared);
    
    say("fist", localize("INTRO_SPEECH_13"), fistTalk);
    say("fist", localize("INTRO_SPEECH_14"));
    say("fist", localize("INTRO_SPEECH_15"));
    say("fist", localize("INTRO_SPEECH_16"));
    
    say("lumberjack", localize("INTRO_SPEECH_17"), talkToFist);
    
    say("fist", localize("INTRO_SPEECH_18"), fistTalk);
    say("fist", localize("INTRO_SPEECH_19"));
    
    say("lumberjack", localize("INTRO_SPEECH_20"), talkToFist);
    
    say("fist", localize("INTRO_SPEECH_21"), startPlayerControl);
    say("lumberjack", localize("INTRO_SPEECH_22"));

end

function startPlayerControl()
	
		-- Use walk only controls...
		local player_desc = game_manager:actorDescWithFile("lumberjack-concerned.plist")
		game_manager:setPlayerDesc(player_desc)

		player:doActionByName("Idle");
		player:connectInputSignals();
		
		if (game_manager:isUsingTouchControls()) then
		
			hud:showLeftThumb(localize("TUTORIAL_MOVEMENT_TOUCH"));
		
		elseif (game_manager:isPlatformType(kTargetWindows) or 
				game_manager:isPlatformType(kTargetLinux) or
				game_manager:isPlatformType(kTargetMacOS)) then
		
			local left = game_manager:getGamepadButtonString(kFistInput_Left);
			local right = game_manager:getGamepadButtonString(kFistInput_Right);
			local up = game_manager:getGamepadButtonString(kFistInput_Up);
			local down = game_manager:getGamepadButtonString(kFistInput_Down);

			local tutorial_text = localize("TUTORIAL_MOVEMENT_BUTTON")
			tutorial_text = string.format(tutorial_text, up, left, down, right)
			hud:showLeftThumb(tutorial_text);
		
		else
		
			hud:showLeftThumb(localize("TUTORIAL_MOVEMENT_GAMEPAD"));
		
		end

		game_manager:startTimer(2, showGO);

end

function hideLeftThumb()
	
	hud:hideLeftThumb();

	return true

end

function startCombatTutorial(bIsRestart)
	
	if (bIsRestart ~= true) then
	    -- Restart from here
		game_manager:setCheckpoint("startCombatTutorial");
	end

	disconnectControls()

    say("fist", localize("INTRO_SPEECH_23"), fistTalk);
    say("fist", localize("INTRO_SPEECH_24"), fistTalk, spawnDeer1);

    return true
end

function spawnDeer1()

	camera_manager:setLeftEdgeCollisionToCameraBounds(true);

	bot_manager.MaxBotsOnScreen = 1
	bot_desc = game_manager:actorDescWithFile("deer.plist")
		
	local bot_id = bot_manager:addBotToSpawnQueue(bot_desc, kSpawnDir_Right)
	bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Spawned, onEnemySpawned)
	bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Killed, onDeer1Killed)
	bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Died, onDeer1Died)

	enableAttacks()
end

function enableAttacks()
			
	-- Use full controls...
	connectControls(false)

	local player_desc = game_manager:actorDescWithFile("lumberjack.plist")
	game_manager:setPlayerDesc(player_desc)

	if (game_manager:isUsingTouchControls()) then
		hud:showRightThumb(localize("TUTORIAL_PUNCH_TOUCH"));
	else
		local button = game_manager:getGamepadButtonString(kFistInput_Punch);
   		hud:showRightThumb(string.format(localize("TUTORIAL_PUNCH_BUTTON"), button));
	end

	hud:showPlayerHealth(player)
end

function onDeer1Killed()

	game_manager:updateAchievement("achievement.awesome.first_blood", 1.0)

end

function onDeer1Died(bIsRestart)

	if (bIsRestart ~= true) then
	    -- Restart from here
		game_manager:setCheckpoint("onDeer1Died");
	end

	disconnectControls()

	hud:hideRightThumb();

	
	say("fist", localize("INTRO_SPEECH_25"), fistTalk);
    say("lumberjack", localize("INTRO_SPEECH_26"), talkToFist);
	say("fist", localize("INTRO_SPEECH_27"), fistTalk, spawnDeer2)

end

function spawnDeer2()

	connectControls(false)

	bot_manager.MaxBotsOnScreen = 2
	bot_desc = game_manager:actorDescWithFile("deer.plist")
	
	local num_bots = 10

	for i = 1, num_bots, 1 do
	
		local spawn_dir = kSpawnDir_Random;
		
		if (i < 2) then
			spawn_dir = kSpawnDir_Right
		else
			if ((i % 2) == 0) then
				spawn_dir = kSpawnDir_Left
			else
				spawn_dir = kSpawnDir_Right
			end
		end
	
		local bot_id = bot_manager:addBotToSpawnQueue(bot_desc, spawn_dir, 1)
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Spawned, onEnemySpawned)
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Killed, checkStopCameraBlock)

		if (i == 4) then
			bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Spawned, fistTutorial);
		end
		
		if (i == 6) then
			bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Spawned, jumpTutorial);	
		end
		
		if (i == 8) then
			bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Spawned, stompTutorial);
		end
	end
	
	if (game_manager:isUsingTouchControls()) then
	   	hud:showRightThumb(localize("TUTORIAL_KICK_TOUCH"))
	else
		local button = game_manager:getGamepadButtonString(kFistInput_Kick)
		local text = string.format(localize("TUTORIAL_KICK_BUTTON"), button)
   		hud:showRightThumb(text)
	end

   	game_manager:startTimer(5, hideRightThumb);

end

function hideRightThumb()

	hud:hideRightThumb()

end

function fistTutorial()
	
	if (game_manager:isUsingTouchControls()) then
		hud:showRightThumb(localize("TUTORIAL_FIST_TOUCH"))
	else
		local button = game_manager:getGamepadButtonString(kFistInput_Punch)
		local text = string.format(localize("TUTORIAL_FIST_BUTTON"), button)
	end

   	game_manager:startTimer(5, hideRightThumb);

end

function jumpTutorial()

	if (game_manager:isUsingTouchControls()) then
		hud:showRightThumb(localize("TUTORIAL_JUMP_TOUCH"))
	else
		local button = game_manager:getGamepadButtonString(kFistInput_Jump)
		local text = string.format(localize("TUTORIAL_JUMP_BUTTON"), button)
   		hud:showRightThumb(text)
	end
	
   	game_manager:startTimer(5, hideRightThumb);
   	game_manager:startTimer(6, jumpTutorial2, false);

end

function jumpTutorial2()
	
	if (game_manager:isUsingTouchControls()) then
		hud:showRightThumb(localize("TUTORIAL_JUMP_KICK_TOUCH"))
	else
		local button = game_manager:getGamepadButtonString(kFistInput_Punch)
		local text = string.format(localize("TUTORIAL_JUMP_KICK_BUTTON"), button)
   		hud:showRightThumb(text)
	end

   	game_manager:startTimer(5, hideRightThumb);

end 

function stompTutorial()
	
	if (game_manager:isUsingTouchControls()) then
		hud:showRightThumb("Swipe Down\nto Stomp");
	else
		local button_stomp = game_manager:getGamepadButtonString(kFistInput_Stomp);
		local button_back_kick = game_manager:getGamepadButtonString(kFistInput_BackKick);
		local text = string.format(localize("TUTORIAL_STOMP_BUTTON"), button_stomp, button_back_kick)
   		hud:showRightThumb(text)
	end

   	game_manager:startTimer(5, hideRightThumb);

end

function onLumberbearAmbush(bIsRestart)

	bot_manager.MaxBotsOnScreen = 3

	-- Restart from here
	if (bIsRestart ~= true) then
		game_manager:setCheckpoint("onLumberbearAmbush")
	end

	-- Spawn boss dude & his mates
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("deer.plist"))
	boss_id = startLevelBossFight("lumberbear", bot_descs, bIsRestart)

	lumberbear_spawned = true
	
	return true

end

function onLevelBossSpeech()

	say("lumberbear", localize("LUMBERBEAR_FIGHT_1"))
	say("lumberbear", localize("LUMBERBEAR_FIGHT_2"))
	say("lumberjack", localize("LUMBERBEAR_FIGHT_3"), nil, onLevelBossSpeechEnd)

end
