Rock Entity
Last updated
Last updated
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