Shooting Back

Shooting Back

Welcome to the final step. We only have a little bit left to do: we need to be able to shoot those enemies before they shoot us! We'll add the code to do this in the scene script. One more time, if it's not currently open, reopen your scene in the builder. In your text editor, return to the scene script we created earlier (MyFirstGameScript.lua).

We will start by adding an "Event Map" to our script, which will allow users to interact with our game by using the mouse and keyboard. Add the following code to the script's onSceneStart function, above the line that says "end" at the bottom:

controlsKeymap = EventMap.new()
controlsKeymap:setEvent("keyPress-space", function() myPath:go() end)
controlsKeymap:setEvent("keyPress-leftMouse", function() shoot() end)
EventHandler.push(controlsKeymap)

This creates a new Event Map and sets it up to pay attention to two kinds of input: pressing the spacebar and clicking the left mouse button. We want the spacebar to restart our camera moving along the path from the beginning again, so we use the line myPath:go() again. Left-clicking the mouse will call a function called shoot(), which will make us shoot at the enemies.

Now, here is what our shoot() function looks like. Put this function all the way at the bottom of the script (after the "end" statement of the onSceneStart function):

function shoot()
    local bullet = SceneManager.createObject("jasonn/bullet",
                                             SceneManager.getCamera():getPosition())
    bullet:setCollisionHandler(handleCollisions)
    bullet:setVelocity(Mouse.getRay() / 2)
    Timer.later(function() delete(bullet) end, 15)
end

The lines "function shoot()" and "end" just define the function so that our EventMap code knows what code to run. Inside this function, the first line (SceneManager.createObject) should look a little familiar, from the Enemy class we wrote earlier. It creates a bullet object and places it at the camera's current position. The next line adds a "collision handler," which is a function that will be called when this bullet collides with something. Then, we set the velocity of the bullet, so that it will start flying forwards. We use Mouse.getRay() to shoot the bullet toward wherever the mouse is, so that it will go in the direction that the player clicked. Finally, like we did with the enemy's ammo, we set up a timer to delete the bullet 15 seconds after we shoot, so that we don't have bullets hanging out in the scene forever.

Almost done! For the last step, we will write the collision handler we added earlier (which we named handleCollisions). This function will be be called when the bullet hits something, and it will be responsible for determining what to do about it (in this case, killing the enemy). Here is what the function looks like (add this to the bottom of the script file):

function handleCollisions(self, other, mode)
    if other:hasKeyword("enemy") then
        delete(other)
        delete(self)
    end
end

When this function is called, it will be given three pieces of information: self, which represents the object that hit something, other, which represents the object that it hit, and mode, which we can ignore. self will always be our bullet, because the bullet is what we called setCollisionHandler() on back in the shoot() function.

The first line of the collision handler checks to see if the thing that the bullet hit is an enemy, by checking for the keyword "enemy," which we will soon go back and add to the enemies in our scene. If it is, we delete both the enemy and the bullet, removing them from the scene.

Know Your Enemies

Save the scene script and return to the builder. Now we just need to identify the things in the scene that are considered enemies.

    1. Select one of the enemies you created earlier.
    2. Open the Properties window.
    3. Select the first tab, "basic Properties."
    4. Click in the Keywords field, type in "enemy", and press enter.
    5. Repeat for each enemy in the scene.

Now your enemies are properly identified for the collision handler to find them.

Our First Game

Now it's time to test our game! Save the scene, then test the pocket one more time ("Begin Test" in the "Test" menu). Shoot the enemies before they shoot you! Press the spacebar to go back to the beginning and get them all!

Congratulations on your first game! When you're done testing it, close the test window that popped up. Feel free to make more changes in the Game Builder. Play around. This is a good time to add background scenery to enhance the experience. Try searching the Global Library for "cactus" or "shack" and dragging them in. We can add any model we find in the library to the scene. As you add them, be sure to right-click on them and select "Anchor" from the menu; if the objects aren't anchored, they might float away when hit by a bullet.

For reference, here is a link to the Wild Pockets Team's completed version of the pocket.

One more thing you can do, in the builder, is go up to the "Test" menu and select "Play Pocket in View Page." This brings up the page where you can play your game. Share it with your friends (be sure to save any changes you make)! You can even use the embed code at the bottom of the page to embed it in any other page. Congratulations on writing your first game, and welcome to Wild Pockets!