share/hedgewars/Data/Missions/Training/Basic_Training_-_Bazooka.lua
changeset 10655 1c85a442bd56
parent 10289 c3a77ff02a23
child 11015 7a905f0070ce
equal deleted inserted replaced
10654:cdce07f5a011 10655:1c85a442bd56
    28 local game_lost = false
    28 local game_lost = false
    29 -- This variable will point to the hog's gear
    29 -- This variable will point to the hog's gear
    30 local player = nil
    30 local player = nil
    31 -- This variable will grab the time left at the end of the round
    31 -- This variable will grab the time left at the end of the round
    32 local time_goal = 0
    32 local time_goal = 0
       
    33 -- This variable stores the number of bazooka shots
       
    34 local shots = 0
    33 
    35 
    34 -- This is a custom function to make it easier to
    36 -- This is a custom function to make it easier to
    35 -- spawn more targets with just one line of code
    37 -- spawn more targets with just one line of code
    36 -- You may define as many custom functions as you
    38 -- You may define as many custom functions as you
    37 -- like.
    39 -- like.
    97 
    99 
    98 -- This function is called when the round starts
   100 -- This function is called when the round starts
    99 -- it spawns the first target that has to be destroyed.
   101 -- it spawns the first target that has to be destroyed.
   100 -- In addition it shows the scenario goal(s).
   102 -- In addition it shows the scenario goal(s).
   101 function onGameStart()
   103 function onGameStart()
       
   104 	-- Disable the graph in the stats screen, we don't need it
       
   105 	SendHealthStatsOff()
   102 	-- Spawn the first target.
   106 	-- Spawn the first target.
   103 	spawnTarget()
   107 	spawnTarget()
   104 	
   108 	
   105 	-- Show some nice mission goals.
   109 	-- Show some nice mission goals.
   106 	-- Parameters are: caption, sub caption, description,
   110 	-- Parameters are: caption, sub caption, description,
   121 -- code here as this might slow down your game.
   125 -- code here as this might slow down your game.
   122 function onGameTick20()
   126 function onGameTick20()
   123 	-- If time's up, set the game to be lost.
   127 	-- If time's up, set the game to be lost.
   124 	-- We actually check the time to be "1 ms" as it
   128 	-- We actually check the time to be "1 ms" as it
   125 	-- will be at "0 ms" right at the start of the game.
   129 	-- will be at "0 ms" right at the start of the game.
   126 	if TurnTimeLeft < 40 and TurnTimeLeft > 0 and score < score_goal then
   130 	if TurnTimeLeft < 40 and TurnTimeLeft > 0 and score < score_goal and not game_lost then
   127 		game_lost = true
   131 		game_lost = true
   128 		-- ... and show a short message.
   132 		-- ... and show a short message.
   129 		ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0)
   133 		ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0)
   130 		-- How about killing our poor hog due to his poor performance?
   134 		-- How about killing our poor hog due to his poor performance?
   131 		SetHealth(player, 0)
   135 		SetHealth(player, 0)
   132 		-- Just to be sure set the goal time to 1 ms
   136 		-- Just to be sure set the goal time to 1 ms
   133 		time_goal = 1
   137 		time_goal = 1
   134 	end
   138 	end
       
   139 
       
   140 	if band(GetState(player), gstDrowning) == gstDrowning and game_lost == false and score < score_goal then
       
   141 		game_lost = true
       
   142 		time_goal = 1
       
   143 		AddCaption(loc("You lose!"), 0xFFFFFFFF, capgrpGameState)
       
   144 		ShowMission(loc("Bazooka Training"), loc("Aiming practice"), loc("Oh no! You failed! Just try again."), -amSkip, 0)
       
   145 	end
       
   146 
   135 	-- If the goal is reached or we've lost ...
   147 	-- If the goal is reached or we've lost ...
   136 	if score == score_goal or game_lost then
   148 	if score == score_goal or game_lost then
   137 		-- ... check to see if the time we'd like to
   149 		-- ... check to see if the time we'd like to
   138 		-- wait has passed and then ...
   150 		-- wait has passed and then ...
   139 		if end_timer == 0 then
   151 		if end_timer == 0 then
   140 			-- ... end the game ...
   152 			-- Let’s create some stats for the stats screen!
       
   153 			-- We will expose the number of hit targets hit, launched bazooka and the accuracy
       
   154 
       
   155 			SendStat(siPointType, loc("hits"))
       
   156 			SendStat(siPlayerKills, tostring(score), loc("'Zooka Team"))
       
   157 			SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets."), score, score_goal))
       
   158 			SendStat(siCustomAchievement, string.format(loc("You have launched %d bazookas."), shots))
       
   159 
       
   160 			-- We must avoid a division by zero
       
   161 			if(shots > 0) then
       
   162 				SendStat(siCustomAchievement, string.format(loc("Your accuracy was %.1f%%."), (score/shots)*100))
       
   163 			end
       
   164 			if score == score_goal then
       
   165 				SendStat(siGameResult, "You have finished the bazooka training!")
       
   166 				SendStat(siCustomAchievement, string.format(loc("%.1f seconds were remaining."), (time_goal/1000), math.ceil(time_goal/12)))
       
   167 			end
       
   168 			if game_lost then
       
   169 				SendStat(siGameResult, "You lose!")
       
   170 			end
       
   171 
       
   172 			-- Finally we end the game ...
   141 			EndGame()
   173 			EndGame()
   142 		else
   174 		else
   143 			-- ... or just lower the timer by 20ms.
   175 			-- ... or just lower the timer by 20ms.
   144 			-- Reset the time left to stop the timer
   176 			-- Reset the time left to stop the timer
   145 			TurnTimeLeft = time_goal
   177 			TurnTimeLeft = time_goal
   182 			time_goal = TurnTimeLeft
   214 			time_goal = TurnTimeLeft
   183 			end
   215 			end
   184 		end
   216 		end
   185 	end
   217 	end
   186 end
   218 end
       
   219 
       
   220 -- This function is called when a gear has been damaged.
       
   221 -- We only use it to determine wheather our hog took damage in order to abort the mission.
       
   222 function onGearDamage(gear, damage)
       
   223 	if GetGearType(gear) == gtHedgehog then
       
   224 		if not game_lost then
       
   225 			game_lost = true
       
   226 			AddCaption(loc("You lose!", 0xFFFFFFFF, capgrpGameState))
       
   227 			ShowMission(loc("Bazooka Training") , loc("Aiming practice"), loc("Oh no! You failed! Just try again."), -amSkip, 0)
       
   228 
       
   229 			time_goal = 1
       
   230 		end
       
   231 	end
       
   232 end
       
   233 
       
   234 
       
   235 -- This function is called after a gear is added.
       
   236 -- We use it to count the number of bazooka shots.
       
   237 function onGearAdd(gear)
       
   238 	-- Count the number of bazooka shots for our stats
       
   239 	if GetGearType(gear) == gtShell then
       
   240 		shots = shots + 1
       
   241 	end
       
   242 end