Worm | Handling no worms left alive.

The main controller handles the state of the game. Remember each worm only knows about itself, not the condition of the other worms. Having a controlling script to manage the game so that each object can act independently is extremely useful as we have seen in the other tutorials.

We need to add an additional condition to the controller script to handle the "worm_died" message being received.

  1. In the assets panel, in the 'main' folder, double click, 'main.script'.
  2. In the on_message function, after the line, 'self.worms_alive = 2' insert the new condition.

	elseif message_id == hash("worm_died") then
		-- Game over
		self.worms_alive = self.worms_alive - 1
		if self.worms_alive == 0 then
			-- Set delay timer for 2 seconds
			self.delay = 2
		end
What does this code do?

If a message is received that a worm has died the number of worms alive is reduced by 1.

If there are no worms alive the delay is initialised to 2 seconds.

  1. Insert a new update function, replacing the one that exists if it is still in the script.

function update(self, dt)
	-- Update delay timer if the game is over
	if self.worms_alive == 0 then
		self.delay = self.delay - dt
		-- Reset when timer reaches end
		if self.delay < 0 then
			self.worms_alive = -1
			msg.post(self.current_collection, "unload")
			msg.post("#title_proxy", "load")
			self.current_collection = "#title_proxy"
		end
	end
end
What does this code do?

If there are no worms alive the game must be over. The delay is reduced by delta time. After two seconds the game is reset by setting the number of worms alive to -1.

The delay just allows the player enough time to see how they died before the screen closes.

The game world is unloaded and the title screen is shown again.

Using collection proxies loading and unloading the collections when they are needed has a useful effect of resetting the level, including the tile map.

  1. Save the changes by pressing CTRL-S or 'File', 'Save All'.
  2. Run the program by pressing F5 or choosing, 'Debug', 'Start / Attach' from the menu bar. You should be able to play the game in 1up mode and it return to the title screen when you die.
    Run

The final stage is to make the game two player. Stage 7 >