Game over

All good things must come to an end and if you aren't careful, that will include your ship. We already saw that we checked whether the ship health was 0 or less in the TakeDamage function. If it was, we made a call to DestroyShip, so let's create that function now, as given in the following code:

-- Get the scene and user objects
local hScene = application.getCurrentUserScene ( )
local hUser = application.getCurrentUser ( )

-- Create an explosion based on the smaller collision explosions
local hExplosion = scene.createRuntimeObject ( hScene, "ExplosionHelper" )
-- Move the explosion, scale it up and trigger the explosion
object.matchTranslation ( hExplosion, this.hShip ( ), object.kGlobalSpace )
sfx.setParticleEmitterUniformScaleAt ( hExplosion, 0, 15 )
sfx.stopAllParticleEmitters ( hExplosion )
sfx.startAllParticleEmitters ( hExplosion )

-- Send the game over event
user.sendEvent ( hUser, "MainAI", "onGameOver" )

This function creates an ExplosionHelper object at runtime, so don't forget to drag-and-drop ExplosionHelper from the Data Explorer window to the Models tab in the Game Editor window.

First, we grab the location of the ship and handles to the scene and current user. Next, we create a new ExplosionHelper helper object—the same object that causes the explosions when asteroids hit the ship—and then move it to the same position as the ship (see where we're going with this?). The entire ship is blowing up so the next line of code uniformly scales the explosion to 15 times its original size for a nice, big explosion. We then stop and start the explosion to make sure it fires properly. That's the end of our ship! The last thing we do is send an event to MainAI telling it the game is over.

The last piece of the puzzle is to create the handler, onGameOver, so create a new handler in MainAI, as shown in the following code:

-- Get the user and handle to the "Game Over" button	
local hUser = application.getCurrentUser ( )
local hComponent = hud.getComponent ( hUser, "Game.btnGameOver" )

-- Enable the button and show it
hud.setComponentVisible ( hComponent, true )
hud.setComponentActive ( hComponent, true )

-- Make it modal so it blocks other input
hud.enterModalMode ( hComponent )

After getting a handle to the user and the game over button, we make the button both visible and active and then put the button in modal mode. Modal mode means that the player can only interact with that component—nothing else in the HUD can be clicked. As we saw before, clicking the game over button will restart the game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.123.73