At this point we have a ship which is visible on Screen, but it doesn't do anything. This tutorial adds behavior to our Player Entity so that it can move, turn, and shoot.
For this game, the Player will continuously move forward at a constant speed. The Player object will be turned left and right with the keyboard or Xbox gamepad. Before we begin writing any code we'll add two variables to Player: MovementSpeed and TurningSpeed. To do this:
Select the Player entity
Select the Variables tab
Click the Create a new variable button\
Leave the defaults Create a new variable option and float type
Enter the name MovementSpeed
Click OK
Repeat the steps above to also add a TurningSpeed variable.
Next let's give the variables some default values:
Enter a value of 100 for MovementSpeed. This is the number of pixels the Player will travel in one second.
Enter a value of 3.14 for TurningSpeed. This is the maximum number of radians the Player will rotate in one second.
To apply movement we will need to write some C# code. To do this:
Open the project in Visual Studio (or switch to Visual Studio if you already have it open)
Open Player.cs. This is in the Entities folder in the Solution Explorer.
Scroll to the CustomActivity method in Player.cs
Modify CustomActivity as shown in the following snippet:
If you now run the game you will see the ship move upward, then eventually move off-screen.
If you are unfamiliar with the RotationMatrix property, or with matrices in general then you may be wondering about the RotationMatrix.Up variable, and why we're using it. The RotationMatrix property contains information about how an Entity, Sprite, or any other PositionedObject is rotated. The Up property indicates you which way is "up" for the object given its current rotation. This value is in "object space" meaning that if the object rotates, then this value will rotate along with the object. This is especially convenient for this tutorial because this game will have the ships always moving forward. The code above will work regardless of which way the Player is rotated - something which we'll see in the coming sections.
The next step is to assign input logic so the Player can turn. We will add an object to our Player representing the input device. This could be a gampad, keyboard, or any other object. By using the I1DInput interface, we can write the code the same regardless of the actual hardware used to control the Player. Modify Player.cs as shown in the following snippet:
Note that all of the code we have written uses coefficients (MovementSpeed and TurningSpeed) defined in the FRB Editor. This means that you can modify these values in the Player Entity at any time if you want to tune how the game feels. For example, if you want the ship to turn faster, increase TurningSpeed to a larger value.
Also, keep in mind that the variables MovementSpeed and TurningSpeed can be modified both in the FRB Editor and also in code - so where should you make the change? Typically, these variables can be thought of as variables which a game designer might edit. Larger teams may include designers who are not as comfortable making changes in code. The FlatRedBall Editor provides a less-technical environment so that designers can make changes and see them in-game without venturing into complex C# code.
But you might be wondering if this is important in your case - after all you are likely just learning to use FlatRedBall, so you are a team of one. You are both the designer and programmer, so where should the changes be made? Even in this situation, the answer is the same - if you can make the changes in the FlatRedBall Editor, do so! This makes it easier to tune your game in one place instead of needing to hop around different code files.
Next we will add a file and Sprite to the Bullet entity. This process is essentially the same as when we added PNG files and a Sprite to our Player Entity so you may find these steps familiar. To add the PNG:
Expand the Bullet entity in Glue
Drag+drop Bullet1.png onto the Files folder in the Bullet
To add a Sprite to the Bullet entity:
Select the Bullet entity in Glue
Click the Quick Actions tab
Click the Add Object to Bullet button
Select the Sprite type
Click OK
Now we can set the Sprite's Texture:
Expand the Bullet Entity's Object folder
Select the newly-created SpriteInstance
Select the Variables tab
Set the Texture drop-down to Bullet1
The next step is to add firing bullets. We'll be using the BulletFactory which we created in an earlier tutorial to create a new bullet and automatically add it to the GameScreen's BulletList. For more information on factories, see the page on this topic. Next we will need to define a bullet speed. To do this:
Click the Bullet entity in Glue
Click the Variables tab
Click the Add New Variable button
Leave the defaults
Set the variable name to MovementSpeed
Set MovementSpeed to 300
Now we can use BulletFactory to create bullets when the player shoots. To do this:
Go to Player.cs in Visual Studio
Modify the Player.cs code so that it contains the following code:
Notice that we are using the RotationMatrix of the MainShip to adjust the initial positions of the bullets, as well as their velocity
If you run the game you should be able to fly, turn, and shoot.
Although we have a long way to go, this is a big milestone for Rock Blaster. You can now see how the game feels for the very first time. Since relevant coefficients are set in Glue, you can change the values to make the game feel differently. Now that we can shoot bullets we'll need something to shoot at. The next tutorial will add Rock entity instances to the GameScreen.
Download the following file to your computer: