This tutorial will define the Rock entity and add it to our GameScreen so that we can shoot it. This tutorial will focus specifically on the creation of the Rock entity (adding a Sprite) as well as collision. We will revisit the creation of Rock entities in the next tutorial as well when we handle the creation of Rocks over time.
As you might have guessed, the first step is to add PNGs and a Sprite to the Rock entity. To add the PNGs:
Download the following four images:
Expand the Rock Entity in Glue
Drag+drop the four files into the Files folder
Now you should have four files in the Rock entity. Like before, the PNG files will be copied into the game project. To add the Sprite to the Rock entity:
Select the Rock entity
Click the Add Object to Rock button in the Quick Actions tab
Select the Sprite type
Click OK
To set the Sprite's Texture:
Select the newly-added SpriteInstance
Select the Variables tab
Use the Texture drop-down to select Rock1
The next question we are faced with is how to handle spawning of the rocks (spawning also means to create an Entity at runtime on a time interval). Our spawn code should have the following criteria:
We should be able to control the difficulty of the game by increasing or reducing the rate at which Rocks spawn
Rocks need to appear at random locations so the player cannot predict their movement
Rocks should not spawn on-screen. This both looks unrealistic, and it can also be frustrating to the player if a Rock suddenly appears on top of the player
Rocks should move in all directions. Our game will require the player to rotate the ship to navigate in every direction, so rocks shouldn't just appear on the top of the screen.
Furthermore, we must also consider where we should put the code for Rock spawning. One option is to put the code in the GameScreen. However, we can keep our code easier to maintain if we separate this logic into its own class. Also, since we will want to have variables to control the initial spawn rate as well as how fast the spawn rate grows, we will create a new Entity to store these variables.
To create the RockSpawner Entity:
Select the Quick Actions tab
Click the Add Entity button
Enter the name RockSpawner
Click OK - we won't need any collision or visuals in this entity
The RockSpawner is different from our other entities - we only need a single RockSpawner instance. To add an instance to the GameScreen:
Select the RockSpawner
Click the Quick Actions tab
Click the Add RockSpawner Instance to GameScreen button
Next we'll add four variables to the RockSpawner. These are:
RocksPerSecond
SpawnRateIncrease
MinVelocity
MaxVelocity
To add RocksPerSecond:
Select the RockSpawner
Click the Variables tab
Click the Add New Variable button
Enter the name RocksPerSecond
Click OK
Repeat the steps above for the other three variables. We'll set some defaults for our variables as well:
RocksPerSecond = 0.2
SpawnRateIncrease = 0.015
MinVelocity = 50
MaxVelocity = 100
Now that we have variables to use for spawning, we can create our spawning logic. The logic will be broken up into two parts:
Deciding if it's time to spawn a rock
Spawning a rock and giving it its initial velocity
First, we'll add the top-level logic to CustomActivity. To do this:
Go to Visual Studio
Open RockSpawner.cs
Modify this file so that it contains the following code
Vector3 not found? If Visual Studio draws the red squiggly line underneath Vector3 and complains that it can't find it, then add the following using statement at the top of the file:
Why do we create so many functions? You may be wondering why we have created so many functions when we implement the logic for our Entities. It would be possible to write all of the code in a single function. However, there are many reasons for breaking your code up into multiple functions. One of the most important is readability. Looking at a long function can be difficult to understand, and also difficult to maintain. Separating your code into multiple functions makes it far more readable and easy to maintain.
At this point we have two functions left to write - a GetRandomRockPosition function and a GetRandomRockVelocity. Each function will require some math and explanation so we will cover each in its own section.
GetRandomRockPosition will give us a position on the edge of the screen. Conceptually the process will be:
Picking a random side on the screen - options are top, right, bottom, and left
Picking a random point on the selected side
Moving the point off-screen so that the Rock will be fully off-screen when it is spawned
To perform these three steps, add the following code to the RockSpawner.cs file:
GetRandomRockVelocity will give us the velocity for the new Rock. This implementation will result in all rocks starting on the edge of the screen and moving towards the center. A more advanced velocity might randomize the angle at which they move so that they don't always pass through the middle; however, this approach will give us sufficiently random movement. The steps to this method are:
Find the center of the screen at Z = 0
Get the direction towards the center of the screen
Normalize the direction, then multiply it by the desired speed to get the final velocity
To perform these steps, add the following method to your RockSpawner.cs:
If you run the game at this point you will notice that the RockSpawner spawns rocks, but spawn rate doesn't increase so the game never gets harder over time. To change this, add the following code to the RockSpawner's CustomActivity:
Now the game is really making progress. If you run the game now you will be able to fly around and fire bullets at rocks. Of course, we haven't put collision in just yet. We'll do that next tutorial.