function init(bIsRestart)

	-- Initialise arena - backdrop_name, start_pos, is_restart
	init_level("level-future-city", CCPoint(-50, -30), bIsRestart)

end

function onLevelIntro()

	bot_manager.MaxBotsOnScreen = 3

	say("lumberjack", localize("FUTURE_CITY_INTRO_1"), talkToFist)
	say("fist", localize("FUTURE_CITY_INTRO_2"), fistTalk)
	say("fist", localize("FUTURE_CITY_INTRO_3"), fistTalk)
	say("lumberjack", localize("FUTURE_CITY_INTRO_4"), talkToFist, onLevelIntroEnd)

--	say("fist", "That's because it is a train.", fistTalk)
--	say("lumberjack", "Good. Glad we got that sorted.", talkToFist, onLevelIntroEnd)


end

function sayNooo(bIsRestart)

	say("lumberjack", localize("FUTURE_CITY_ENTER_BARE_BEARS"))
	return true

end

function onStreetAmbush(bIsRestart)

	-- Restart from here
	if (bIsRestart ~= true) then
    
   		-- Make this a bit harder than the last ambush
		bot_manager.MaxBotsOnScreen = bot_manager.MaxBotsOnScreen + 1

		game_manager:setCheckpoint("onStreetAmbush")
	end

	-- Spawn regular enemies
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("boba-gruntbear.plist"))

	-- Spawn lots of enemies
	startAmbush(bot_descs, bot_manager.MaxBotsOnScreen * 2)

	return true
	
end

function onDanceFloorAmbush(bIsRestart)

	-- Restart from here
	if (bIsRestart ~= true) then
    
   		-- Make this a bit harder than the last ambush
		bot_manager.MaxBotsOnScreen = bot_manager.MaxBotsOnScreen + 1

		game_manager:setCheckpoint("onDanceFloorAmbush")
	end

	-- Spawn regular enemies
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("stripper-gruntbear.plist"))

	-- Spawn lots of enemies
	startAmbush(bot_descs, bot_manager.MaxBotsOnScreen * 3)

	return true
	
end

function onPrivateBoothAmbush(bIsRestart)

	-- Restart from here
	if (bIsRestart ~= true) then

   		-- Make this a bit harder than the last ambush
		bot_manager.MaxBotsOnScreen = bot_manager.MaxBotsOnScreen + 1

		game_manager:setCheckpoint("onPrivateBoothAmbush")
	end

	-- Spawn regular enemies
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("bare-bear.plist"))

	-- Spawn a couple of giant naked bears
	startAmbush(bot_descs, 2)

	bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("stripper-gruntbear.plist"))

	-- Spawn lots of strippers
	startAmbush(bot_descs, bot_manager.MaxBotsOnScreen * 2)

	return true
	
end

function onBossAmbush(bIsRestart)

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

	-- Dont't spawn too many grunts yet
	bot_manager.MaxBotsOnScreen = 3

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

	bot_manager:triggerOnBotEvent(boss_id, kBotTrigger_Spawned, onBrianSpawned)
	bot_manager:triggerOnBotEvent(boss_id, kBotTrigger_Killed, onBrianKilledStart)


	return true

end

function onLevelBossSpeech()

	say("brian-speech", localize("FUTURE_CITY_BOSS_1"))
	say("lumberjack", localize("FUTURE_CITY_BOSS_2"))
	say("fist", localize("FUTURE_CITY_BOSS_3"), fistTalk)
	say("fist", localize("FUTURE_CITY_BOSS_4"), fistTalk)
	say("lumberjack", localize("FUTURE_CITY_BOSS_5"), talkToFist)
	say("brian-speech", localize("FUTURE_CITY_BOSS_6"), relaxedWithFist)
	say("brian-speech", localize("FUTURE_CITY_BOSS_7"))
	say("brian-speech", localize("FUTURE_CITY_BOSS_8"))
	say("brian-speech", localize("FUTURE_CITY_BOSS_9"))
	say("fist", localize("FUTURE_CITY_BOSS_10"), fistTalk)
	say("brian-speech", localize("FUTURE_CITY_BOSS_11"), relaxedWithFist)
	say("brian-speech", localize("FUTURE_CITY_BOSS_12"))
	say("brian-speech", localize("FUTURE_CITY_BOSS_13"))
	say("brian-speech", localize("FUTURE_CITY_BOSS_14"))
	say("brian-speech", localize("FUTURE_CITY_BOSS_15"))
	say("lumberjack", localize("FUTURE_CITY_BOSS_16"), talkToFist)
	say("brian-speech", localize("FUTURE_CITY_BOSS_17"), relaxedWithFist)
	say("brian-speech", localize("FUTURE_CITY_BOSS_18"), relaxedWithFist)
	say("brian-speech", localize("FUTURE_CITY_BOSS_19"), relaxedWithFist)
	say("fist", localize("FUTURE_CITY_BOSS_20"), fistTalk)
	say("lumberjack", localize("FUTURE_CITY_BOSS_21"), talkToFist, onLevelBossSpeechEnd)

end

function onBrianSpawned(bot)

	brian_bot = bot

end

function onBrianKilledStart(bot)

	brian_bot:setHealth(100)

	game_manager:startTimer(1.0, onBrianKilledComplete)

end

function onBrianKilledComplete(bIsRestart)

	-- Pause player movement
	disconnectControls()

	-- Restart from here
	if (bIsRestart ~= true) then

		game_manager:startTimer(2.0, onBrianWalkToSafeSpot)

		game_manager:setCheckpoint("onBrianKilledComplete")
	else

		-- Get descriptor for enemy
		local bot_desc = game_manager:actorDescWithFile("brian.plist")

		-- Tell Bot Manager to spawn enemy
		boss_id = bot_manager:addBotToSpawnQueue(bot_desc, kSpawnDir_Right, 1)
		bot_manager:triggerOnBotEvent(boss_id, kBotTrigger_Spawned, onBrianSpawned)
		bot_manager:triggerOnBotEvent(boss_id, kBotTrigger_Spawned, onBrianWalkToSafeSpot)

	end
end

function onBrianWalkToSafeSpot()

	CCLuaLog("onBrianWalkToSafeSpot")

	local backdrop = game_manager.Background
	local bot_target = backdrop:getNodeByName("bossPoleObject")
	local pole_pos = bot_target:getPositionLua()
	local player_pos = player:getBasePosition()

	local target_pos
	if (player_pos.x < pole_pos.x) then
		target_pos = CCPoint(player_pos.x + 35, player_pos.y)
		player:setFacingDir(kFacingDir_Right)
		player:setNextFacingDir(kFacingDir_Right)
	else
		target_pos = CCPoint(player_pos.x - 35, player_pos.y)
		player:setFacingDir(kFacingDir_Left)
		player:setNextFacingDir(kFacingDir_Left)
	end

	brian_bot:setTargetPosition(target_pos)
	brian_bot:setTargetPositionLocked(true)

	bot_manager:triggerOnBotEvent(boss_id, kBotTrigger_ReachedTarget, onBrianRessurection)

end

function onBrianRessurection()

	say("brian-speech", localize("FUTURE_CITY_BEAR_BOSS_1"))
	say("brian-speech", localize("FUTURE_CITY_BEAR_BOSS_2"))
	say("brian-speech", localize("FUTURE_CITY_BEAR_BOSS_3"), nil, onBrianMorphStart)

end

function onBrianMorphStart()

	brian_bot:doActionByName("MorphToBear")

	game_manager:startTimer(3.0, onBrianMorphInProgress, true)

	game_manager:startTimer(3.0, onBrianMorphComplete)
end

function onBrianMorphInProgress(progress)

	local newScale = 1 + progress * 0.5

	if (brian_bot:getScaleX() > 0) then
		brian_bot:setScaleX(newScale)
	else
		brian_bot:setScaleX(-newScale)
	end

	brian_bot:setScaleY(newScale)


end

function onBrianMorphComplete()

	bear_desc = game_manager:actorDescWithFile("brian-bear.plist")
	brian_bot:setDesc(bear_desc)

	local newScale = 1.5
	if (brian_bot:getScaleX() > 0) then
		brian_bot:setScaleX(newScale)
	else
		brian_bot:setScaleX(-newScale)
	end

	brian_bot:setScaleY(newScale)

	-- Pause again because chaing the desc can kick the bot into action
	bot_manager:pause()

	say("brian-bear", "MWAHAHAHAHAHA!", nil, onBrianBearAttack)
end

function onBrianBearAttack()

	connectControls(false)
	bot_manager:resume()

	showBearFight(brian_bot)

	hud:showBossHealth(brian_bot)

	-- Kill all other bots when brian-bear dies
	bot_manager:triggerOnBotEvent(boss_id, kBotTrigger_Killed, onBrianBearKilled)
	bot_manager:triggerOnBotEvent(boss_id, kBotTrigger_Died, onBrianBearDied)


		-- Spawn regular enemies
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("stripper-gruntbear.plist"))

	-- Spawn lots of enemies
	bot_manager.MaxBotsOnScreen = 5
	startAmbush(bot_descs, 200, nil, false)

end

function onBrianBearKilled(bot)

	-- Kill all remaining bots
	bot_manager:killAllBots()

end

function onBrianBearDied(bIsRestart)

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

	disconnectControls()

	say("lumberjack", localize("FUTURE_CITY_END_1"), talkToFist)
	say("fist", localize("FUTURE_CITY_END_2"), fistTalk, startShake)

end

function startShake()

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

end

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

end		
	
function onShakeSpeech()
	
	say("lumberjack", localize("FUTURE_CITY_END_3"), talkToFist)

end
