Creating the Puck Entity
Last updated
Last updated
Now that we have our PlayerBall movement working, we'll add a Puck Entity which the user can hit around. You'll find that the Puck is very similar to the PlayerBall.
To create a Puck Entity:
Click on the Quick Actions tab
Click the Add Entity button
Name the Entity Puck
Check the Circle check box under Collisions
Verify that ICollidable is checked (it should be checked automatically when Circle is checked)
Click OK
The Puck entity should appear in the FlatRedBall Editor.
For more information on how to perform the above steps, you may want to review the tutorial which created the PlayerBall Entity here.
Currently our Puck and PlayerBall both have Circle bodies, and by default the Circles have the same size and color. To differentiate the Puck from the PlayerBall:
Expand the Puck entity
Click on the CircleInstance object under the Puck Entity
Click the Variables tab
Find the Color variable
Change the value to Red using the drop-down
Change the Radius value to 6
Computer settings matter: If your computer is set up so the decimal separator is the comma ',' instead of the period '.' then you should enter values using the ',' character. Unlike C# code, Glue obeys your computer's language settings.
By default the FlatRedBall Editor adds lists of newly-created entities to the GameScreen. Therefore, you should already have a PuckList in your GameScreen.
If you unchecked the option, or if you would like to know how to manually add a PuckList to your GameScreen, the following section shows how to add a list. This is not necessary if you kept the defaults.
Click the Puck entity
Select the Quick Actions tab
Click the Add Puck List to GameScreen button
Select the Puck entity
Click the Add Puck Instance to GameScreen button
Now the GameScreen has a list and a single Puck.
If you run your game you'll notice that the PlayerBallInstance and PuckInstance are both at the center of the Screen. Let's reposition the PlayerBall1:
Select the PlayerBall1 object under your GameScreen
Change the X value to -180
Now that we have a Puck in our game, we need to create two collision relationships:
Puck vs Walls - this prevents the Puck from moving through the walls.
Puck vs PlayerBall - this allows the PlayerBall to "hit" the puck to try to score a goal. We haven't yet created the handling of goals, but this is the first step towards implementing that feature.
We create these two collision relationships just like the previous PlayerListVsWall collision relationship. To create a relationship between the PuckList and Wall:
Expand GameScreen's Objects folder in Glue
Drag+drop the PuckList onto the Walls object
Select the new PuckVsWalls collision relationship
Select the Collisions tab
Set the Collision Physics to Bounce
Change the Puck Mass to 0
Optionally adjust the Elasticity value
To create a relationship between the PuckList and PlayerBallList:
Expand GameScreen's Objects folder in Glue
Drag+drop the PlayerBallList onto the PuckList object
Select the new PlayerBallVsPuck collision relationship
Select the Collisions tab
Set the Collision Physics to Bounce
Change the Puck Mass to 0.3 - this makes it 30% the mass of the PlayerBall
Optionally adjust the Elasticity value
Notice that the mass variables for PlayerInstance vs. PuckInstance differ compared to wall collision. The PuckInstance is given a mass of .3 relative to a mass of 1 for the PlayerInstance, resulting in the PuckInstance behaving as if it has 30% of the mass of the PlayerInstance. If you run the game, you should be able to hit the Puck around the level.
Currently the Puck moves indefinitely after being hit. We'll assign the Drag value to the Puck just like we did to PlayerBall:
Select the Puck Entity in Glue
Select the Variables tab
Click the Add New Variable button
Select the Expose an existing variable option
Select the value Drag
Enter a value of 0.4 for Drag
Now the Puck slows down over time just like the PlayerBall.