TGB/BasicTutorial3
From TDN
|
[edit] OverviewThe following links can take you to previous/further sections of the tutorial:
[edit] TGB Basic Tutorial - Part 3
[edit] Creating an Enemy Ship
So, now that we have our player in the level and moving around, bound by the constraints of the map, let's give him an opponent to worry about. In your Static Sprites library in your Create tab, click and drag an enemy ship into the level.
You will notice something peculiar when you drag your enemy into the scene: The enemy ship is twice as big as the player's ship! The reason the enemy ship is bigger is not because the image itself is twice as large, it is because TGB resizes artwork relative to your camera view. Since we doubled the size of the camera view at the beginning of the tutorial, the player ship was reduced in size relative to it, making the enemy ship twice as large. To remedy this, we need to resize the enemy to half of its current size, to match the size of the player. You can do this visually by simply resizing the ship in the level view, or by changing the Width and Height values in the Scene Object section of the Edit tab with the enemy ship selected to 25 and 12.5 respectively. Now that the enemy is positioned in the level, let's get it moving. Like the player ship, we will need to assign this enemy ship to a class. Go into the Edit tab with the enemy selected and type in the class of “EnemyShipâ€.
As we did with the player ship, we'll need to set the world limits for the enemy ship as well. This time, however, instead of limiting the ship to the camera view, it will instead be limited to be within the vertical space of the camera view, but horizontally can be allowed over the edges by quite a bit. Visually, the world limits will look like this:
By letting the enemy ship's world limits spill off the left and right edges of the camera view, the enemy ship can smoothly fly into and out of view, rather than being forced to immediately show up within the camera view and disappear before it leaves the camera view. Set the values for the world limits as shown in the image below:
The NULL limit mode will not automatically do anything whatsoever to affect the enemy ship when it goes out of bounds, but it allows us to set boundaries. With the Callback checkbox checked, when the enemy ship does go out of bounds, a function in the EnemyShip class will be called telling us, so we can handle the event in code.
[edit] Making the Enemy Ship Move
At this point, you should save all of the changes you have made in the level builder, and we'll move back out to writing the code which controls the enemy ship. Open your yourProjectName/game/gameScripts folder, the same folder we created our player.cs file, and create a new text file named enemy.cs. Like we did with player.cs, we need to load the code in enemy.cs by modifying yourProjectFolder/game/main.cs:
// Exec game scripts
exec("./gameScripts/game.cs");
exec("./gameScripts/player.cs");
exec("./gameScripts/enemy.cs");
Now, inside of enemy.cs, add the following code:
function EnemyShip::onLevelLoaded(%this, %scenegraph)
{
// Save the initial X value so we can use it when we respawn this ship
%this.startX = %this.getPositionX();
// Spawn this ship
%this.spawn();
}
When the level is loaded, the first thing we do is save the current X position of the ship so we can use it to reposition the ship whenever it respawns. The current X position when the level loads will be the X position of the ship from the ship's position as it is laid out in the level builder. Next we call the spawn function (which we define below) to set the position and speed of the ship.
function EnemyShip::spawn(%this)
{
// Set the minimum and maximum Y positions to be the
// world limits, accounting for the size of the ship
%this.minY = getword(%this.getWorldLimit(), 2) + (%this.getHeight() / 2);
%this.maxY = getword(%this.getWorldLimit(), 4) - (%this.getHeight() / 2);
// Set the speed limits
%this.minSpeed = -50;
%this.maxSpeed = -100;
// Set the speed and position of this ship
%this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed));
%this.setPositionY(getRandom(%this.minY, %this.maxY));
%this.setPositionX(%this.startX);
}
Whenever the enemy ship respawns it will start at the X position startX, have a random Y position that is between the minimum and maximum world limit values, and have a random horizontal speed between -50 and -100 units per second.
The code above will randomly position the ship off the right edge of the camera's view and give it a random speed to the left. But the ship will simply keep flying to the left, even after it goes out of the camera's view, and will never return. To know when the ship goes off of the left side of the world limits we set up earlier, we use the onWorldLimit() callback. When the callback is called, we check to see if the limit the ship overstepped is the "left" side of the world limits rectangle. If it is, we call the spawn() function to reposition the ship on the right side of the screen again.
function EnemyShip::onWorldLimit(%this, %mode, %limit)
{
// When the ship hits the left side of the world limits, then respawn the ship
if (%limit $= "left") {
%this.spawn();
}
}
Save the enemy.cs file and run the game from Torque Game Builder using the Play button. You should see the ship flying to the left and respawning when it gets to the edge of the screen!
[edit] Adding Multiple Enemy Instances
You may be saying to yourself “Yeah, this is cool, but I want to battle hordes of enemies. Well, thanks to the config datablock system in TGB and the way we set up our code, making that dream a reality is as simple as copy/paste literally!
To add multiple enemies, simply go into the Level Builder, click on your enemy, type CTRL+C for Copy (Command-C on a Mac) and CTRL+V (Command-V on a Mac) as many times as you want, to paste a copy of the enemy. Each time you paste, the new copy of the enemy ship is placed directly in the same exact location of the original enemy ship you copied. You can move them to a different position if you want to be able to see them all separately, but it will not matter that they're all in the same location.
Now that you have multiple enemies in the level, you can move them around so that they start with different X locations. Plus, with the way we set up our code, when we test our level, they will all start at different Y locations and have different speeds! Test it out! Now that we have our player and enemies moving, we need to get them firing weapons at each other! |
Categories: T2D | TGB | GameExample | Tutorial













