Rock States
Last updated
Last updated
When we added a Sprite to the Rock Entity, we added four different images. At this point our game only uses one image (the smallest one). This tutorial will create states for each of the sizes, and will implement Rocks breaking up into smaller rocks.
A state can be thought of as a collection of variables. States are usually created whenever multiple variables must be changed together. For example, we have four images for our rocks. If we change which rock we show (SpriteInstance Texture), we also want to change the collision size (CircleInstance Radius). We will be creating states for the four rock sizes in this tutorial, then add logic to break larger rocks into smaller rocks.
Before we create states, we will decide which variables we want to modify. In this case, the variables we want to modify are not on the Rock entity itself, but instead on objects inside the Rock. Therefore we will need to tunnel variables to get access to these variables in our states. First we'll tunnel the Texture variable on our SpriteInstance:
Expand the Rock entity in Glue
Drag+drop the SpriteInstance onto the Variables folder
Select Texture in the Variable dropdown
Click OK
Repeat the process above to tunnel the CircleInstance Radius variable. Now the Rock entity has a variable for the SpriteInstance Texture and CircleInstance radius.
Now we can create our four states. We recommend always placing states in a category - this keeps states organized and is very important for larger games. To add a category:
Expand the Rock object
Right-click on the States folder
Select Add State Category
Enter the name RockSize
Click OK
The easiest way to edit states is to use the State Data tab:
Expand the States folder
Click the RockSize state category
Click the State Data tab. The location of your State Data tab may be different depending on FlatRedBall settings.
Since states usually require setting many variables, FlatRedBall provides a spreadsheet-like view of state data. By default categories do not modify any variables, so we must explicitly include which variables to our RockSize category. The easiest way is to drag+drop the variables that we would like modified onto the RockSize category.
Alternatively, the ... button at the top-right of the State Data tab provides more control over included and excluded variables.
Now we can create four states - one for each size of rock. We will call the states Size1, Size2, Size3 and Size4. Size1 will be the smallest and Size4 will be the largest. Enter the values in the spreadsheet, with the state name being the first column.
New Rocks in Rock Blaster will default to State4 - the largest. Once they are shot they will break down into smaller rocks - much like the original Asteroids game. We can change the default size of Rocks with one line of code. To do this:
Switch to Visual Studio
Open RockSpawner.cs
Find the PerformSpawn function
Add the following line of code after Rock rock = RockFactory.CreateNew();
Currently when a Rock collides with a Bullet the game calls Destroy on the Rock instance. Instead, we will want the Rock to decide whether it should break up into smaller rocks before it is destroyed. First, let's replace the Destroy call with a TakeHit call:
Open GameScreen.Event.cs in Visual Studio
Find the OnBulletVsRockCollided function. Make sure to modify the bullet vs rock method.
Change Destroy to TakeHit. Your code should look like:
Next we'll create a TakeHit function in the Rock entity. This is a custom function which will create new rocks:
Open Rock.cs
Add the following code:
Finally, we'll create a BreakIntoPieces function in Rock.cs:
Notice that we are using two variables which haven't been defined:
NumberOfRocksToBreakInto
RandomSpeedOnBreak
Although we could have added these variables directly into code, it's much better to escalate these variables to Glue variables so that they can be easily modified at a later time. To do this:
Click the Rock entity
Click the Variables tab
Click Add New Variable
Change the type to int - the default (which we've used on all variables so far) is float
Enter the name NumberOfRocksToBreakInto
Click OK
Repeat the process above, but create a float named RandomSpeedOnBreak Set the values:
NumberOfRocksToBreakInto = 2
RandomSpeedOnBreak = 50
If you now run your game and shoot the rocks, you will see that they will break up into smaller pieces. Our game is becoming far more playable (and difficult). Next we'll add a HUD and logic for scoring.
If you now play the game, the rocks will be very large: