Adding Enemies
Welcome to the enemy-adding section! As before, if it's not currently open, start by opening your scene in the builder. Now that we have the track-riding working, we should add some enemies to the scene. We'll start by adding one enemy, and then we'll make more later.
- Click on the Global Library.
- Type "cowboy" into the search box and click "Search."
- Find the object made by "jasonn" called "cowboy."
- Drag and drop one cowboy into the scene.
- Use the move and rotate tools to place the cowboy. Like you did with the tracks, use "Drop Object" and the blue rotation axis to make your life easier. Be sure that the cowboy is facing the track, so that it can shoot at you!
- Right-click on the cowboy and select "Anchor"
With the cowboy in place, let's begin writing the script for it. In addition to the generic scene script we wrote already, a script can be attached to an individual object to determine how that object behaves. As we will here, we can create a simple script and attach it to several different objects to make them all behave the same way.
- Select the cowboy
- Open the Properties window.
- Select the Script Properties tab (now it's the third one).
- Click the button labeled "Open Script Directory."
- Create another new text file for the game script that ends with the extension .lua, such as MyFirstGameEnemyClass.lua (again, be sure that it does not end up as .lua.txt)
- Open this file in any text editor.
Writing the Enemy Script
We will now start writing the code that will determine the enemy's behavior. We will put this code in the file we just created. The first line of this file should read:
Enemy = Class.new()
This declares a new class (think of a class as a set of behavior we can assign to an object) that we can apply to our enemies within the scene. We call this class Enemy. Next, we write the initialize function:
function Enemy:_init()
self:every(function() self:onShoot() end, 1)
end
This function will be called when the enemy is created by the Wild Pockets engine. In it, we set up a timer that will do something every second. This particular timer will call the function onShoot(), in which we will make the enemy shoot a bullet (we are about to write this). The 1 at the end of this line of code represents 1 second, and we could make the enemy shoot more or less often by changing this. We use self:every as opposed to Timer.every because self:every will clean itself up - if the enemy is destroyed, this function will no longer be in our way. Timer.every, on the other hand, will keep running forever.
Next, we write the onShoot() function itself:
function Enemy:onShoot()
local ammoObject = SceneManager.createObject("Wild Pockets Team/ACME bullet",
self:getPosition() + vec(0, 0, 3))
ammoObject:setHeading(self:getHeading())
Timer.every(function()
ammoObject:setPosition(ammoObject:getPosition() + self:getForwardVector())
end, 0)
Timer.later(function() delete(ammoObject) end, 15)
end
The first line of this function creates a scene object, called ammoObject, which will be the bullet the enemy shoots. The model for this object is "Wild Pockets Team/ACME bullet" similarly to how the model for our enemy was "jasonn/cowboy." We also, when we create it, set the position of the object to (0,0,3), three units above the ground plane, so that our bullet will appear to fly from the enemy's gun instead of sliding along the ground.
The next line sets the heading of our ammoObject to the heading of the enemy. In other words, it makes the bullet model point in the same direction as our enemy is pointing, again to make it look like it is coming out of the enemy's gun.
Then, we start another timer. This timer will actually cause the bullet to fly forwards, by increasing the position of our ammoObject by a little bit, continuously over time. We set the position equal to its current position, plus the "forward vector" of our enemy (which means a small amount pointed in the direction the enemy is facing). Don't worry too much about this line - it all means that the bullet will fly in the direction that our enemy's gun is pointed.
Finally, we set up an additional timer, to delete the ammo in 15 seconds. This is just cleanup code - it makes sure that, long after the ammo has left our field of view, it doesn't keep hanging out in the game forever.
More Scene Editing
Save the file - that wraps it up for our enemy class. When an enemy is created, it will shoot a bullet forwards every second. Now we need to make the enemy in our scene obey this behavior. We'll go back to the Game Builder to add our new class to the cowboy, making it into an enemy (again, if we have been following these instructions to the letter, the cowboy's properties window is already open and on the Script Properties tab).
- Select the cowboy.
- Open the Properties window.
- Select the Script Properties tab.
- In the "Script File" section, click the button labeled "Set" (again, if you do not see the "Set" button, click on the red X to clear the current script).
- In the box that pops up, enter your Wild Pockets username, followed by a slash, followed by the name of the file created earlier, but do not include the .lua extension - exactly as you did with the main scene script. For example, "YourUserName/MyFirstGameEnemyClass" (without the quotes). Click "Okay."
- In the "Script Class" section, enter the name of the class written in that file, which in this case is "Enemy" (without the quotes), and press Enter.
Once you've got the cowboy set up, we can add more enemies by right-clicking on the cowboy and selecting "Clone." The clones will have the same script and class as the cowboy. Now we can take the time to position these enemies around the scene - again, always point toward the track so they can shoot us!
Now, there's one more change we'll need in order to make the bullets fly correctly: we should disable gravity so the bullets don't fall towards the ground as they fly.
- Open the Global Properties window again.
- Select Basic Properties tab (the first one, no icon).
- Change "Gravity" to 0 and press Enter.
And that's it! Save the pocket, then select "Begin Test" from the Test menu again. As we move along the tracks, the enemies will shoot at us, but we can't shoot back! We'll get our revenge in the final section.
- Printer-friendly version
- Login or register to post comments
-



