Target Practice: Re-center camera to hog after projectiles get destroyed or drown
authorWuzzy <Wuzzy2@mail.ru>
Wed, 07 Mar 2018 23:52:45 +0100
changeset 13098 d3e9f3423ef3
parent 13097 03d065fc60dc
child 13099 071dcdf33f86
Target Practice: Re-center camera to hog after projectiles get destroyed or drown
ChangeLog.txt
share/hedgewars/Data/Missions/Challenge/Target_Practice_-_Cluster_Bomb.lua
share/hedgewars/Data/Scripts/TargetPractice.lua
--- a/ChangeLog.txt	Wed Mar 07 23:06:19 2018 +0100
+++ b/ChangeLog.txt	Wed Mar 07 23:52:45 2018 +0100
@@ -65,6 +65,9 @@
  * Mission 1: Fix error message spam when Leaks a Lot drowns
  * Mission 6: Fix cyborgs sometimes doing nothing for a full turn
 
+Missions:
+ + Target Practice: Re-center camera to hog when projectile is destroyed or drowns
+
 Theme customization:
  + Add fallback music with fallback-music and fallback-sd-music
  * Fix green color channel on themes with sd-tint
--- a/share/hedgewars/Data/Missions/Challenge/Target_Practice_-_Cluster_Bomb.lua	Wed Mar 07 23:06:19 2018 +0100
+++ b/share/hedgewars/Data/Missions/Challenge/Target_Practice_-_Cluster_Bomb.lua	Wed Mar 07 23:52:45 2018 +0100
@@ -3,6 +3,7 @@
 local params = {
 	ammoType = amClusterBomb,
 	gearType = gtClusterBomb,
+	secGearType = gtCluster,
 	missionTitle = loc("Cluster Bomb Training"),
 	solidLand = false,
 	map = "Trash",
--- a/share/hedgewars/Data/Scripts/TargetPractice.lua	Wed Mar 07 23:06:19 2018 +0100
+++ b/share/hedgewars/Data/Scripts/TargetPractice.lua	Wed Mar 07 23:52:45 2018 +0100
@@ -33,6 +33,7 @@
 	time = ,
 	ammoType = ,
 	gearType = ,
+	secondaryGearType = ,
 	targets = {
 		{ x = , y = },
 		{ x = , y = },
@@ -64,6 +65,8 @@
 local time_goal = 0
 local total_targets
 local targets
+local gearsInGameCount = 0
+local gearsInGame = {}
 
 --[[
 TrainingMission(params)
@@ -76,7 +79,7 @@
 	- theme:	the name of the theme (does not need to be a standalone theme)
 	- time:		the time limit in milliseconds
 	- ammoType:	the ammo type of the weapon to be used
-	- gearType:	the gear type of the gear which is fired (used to count shots)
+	- gearType:	the gear type of the gear which is fired (used to count shots and re-center camera)
 	- targets:	The coordinates of where the targets will be spawned.
 			It is a table containing tables containing coordinates of format
 			{ x=value, y=value }. The targets will be spawned in the same
@@ -97,6 +100,7 @@
 	- teamName:	name of the hedgehog’s team (default: "Training Team")
 	- teamGrave:	name of the hedgehog’s grave
 	- teamFlag:	name of the team’s flag (default: "cm_crosshair")
+	- secGearType:	cluster of projectile gear (if present) (used to re-center camera)
 	- clanColor:	color of the (only) clan (default: 0xFF0204, which is a red tone)
 	- goalText:	A short string explaining the goal of the mission
 			(default: "Destroy all targets within the time!")
@@ -199,13 +203,26 @@
 			else
 				TurnTimeLeft = time_goal
 			end
-	        end_timer = end_timer - 20
+	   	     end_timer = end_timer - 20
+		end
+
+		for gear, _ in pairs(gearsInGame) do
+			if band(GetState(gear), gstDrowning) ~= 0 then
+				-- Re-center camera on hog if projectile gears drown
+				gearsInGame[gear] = nil
+				gearsInGameCount = gearsInGameCount - 1
+				if gearsInGameCount == 0 and GetHealth(CurrentHedgehog) then
+					FollowGear(CurrentHedgehog)
+				end
+			end
 		end
 	end
 
 	_G.onGearAdd = function(gear)
-		if GetGearType(gear) == params.gearType then
+		if GetGearType(gear) == params.gearType or (params.secGearType and GetGearType(gear) == params.secGearType) then
 			shots = shots + 1
+			gearsInGameCount = gearsInGameCount + 1
+			gearsInGame[gear] = true
 		end
 	end
 
@@ -252,6 +269,13 @@
 			if not success then
 				WriteLnToConsole("ERROR: Failed to spawn girder under respawned target!")
 			end
+		elseif gearsInGame[gear] then
+			gearsInGame[gear] = nil
+			gearsInGameCount = gearsInGameCount - 1
+			if gearsInGameCount == 0 and GetHealth(CurrentHedgehog) then
+				-- Re-center camera to hog after all projectile gears were destroyed
+				FollowGear(CurrentHedgehog)
+			end
 		end
 	end