At FlatRedBall, we view Glue as a development platform. Glue is a program which helps you create and organize games much more quickly than by programming alone.
How to share information between Screens and Entities is a very common problem. There are a lot of ways to solve this problem, but each has its downsides. While this problem may seem trivial, this article will talk about the preferred way to share object references between Screens (the owner) and Entities (contained objects that need the information).
Let's imagine you are creating a multi-player game where the players work together to defeat groups of enemies. Based off of this simple description of a game we can identify a few things:
The Screen will have a PositionedObjectList<Player>
The Screen will have a PositionedObjectList<Enemy>
Each enemy will need to know about the PositionedObjectList<Player> so that it can make decisions about where to move and where/when to attack.
Naturally you will want to make the two PositionedObjectLists (for Player and Entity types) in Glue in your Screen. This means that the Screen has access to the two PositionedObjectLists. For proper encapsulation, the logic for the Enemy AI should exist in the Enemy code (as opposed to the Screen code). However, the Enemy's AI logic will need access to the PositionedObjectList<Player> that exists in the Screen.
At a high level the steps required to share the information are:
Create a public PositionedObjectList<Player> property in the Enemy custom code (not through Glue)
Assign this property in the Screen class whenever a new Enemy is created
Use this property in your AI logic in Enemy.cs
Open your Enemy.cs file and add the following code at class scope:
That was easy!
Locate where you create your Enemy instances and add the following code:
You may have a method like AiActivity in your Enemy.cs file. If so, it would look like this:
This tutorial shows how to make an Entity which controls the Camera in first person mode. The general steps we'll follow are:
Create a FlatRedBall project
Create a large repeating Sprite which we'll use for the ground
Create an Entity which will control the Camera (called CameraController)
Set the initial position of our CameraController instance in our Screen
Implement code to move and look around
If you already have a project created, you can skip this step. If not, to Create a project:
Launch the FRB Editor
Select File->New Project
Name your project FirstPersonProject
Uncheck the Open New Project Wizard option - we will be creating our own game fully from scratch
Click Create Project!
Now we'll set the Camera to be in 3D mode:
In the FRB Editor, click the camera icon
Change the Perspective to 3D
Finally create a Screen which will contain the rest of our project. This is not necessary if you already have a GameScreen:
Select the Quick Actions tab
Click Add Screen/Level
Uncheck the Add LayeredTileMap option unless you intend to use Tiled for your levels. To keep this tutorial short, we will skip this step.
Click OK to create a new screen called GameScreen
Now we'll create a large Sprite which will serve as our ground. To create this:
Drag+drop the newly-downloaded .png file onto GameScreen's Files in the FRB Editor
Select the Quick Actions tab
Click Add Object to Game Screen
Search for Sprite
Click OK
Now that the Sprite has been created, let's make it really large and repeating:
Select the new SpriteInstance
Click the Variables tab
Set the Sprite's Texture to redball
Set the Sprite's TextureAddressMode to Wrap
Set the Sprite's Right Texture Pixel to 32000 - this makes the Sprite repeat 1000 times on the X axis
Set the Sprite's Bottom Texture Pixel to 32000 - this makes the Sprite repeat 1000 times on the Y axis
Now we'll create an Entity which will control the Camera. To do this:
In the FRB Editor, right-click on Entities
Select Add Entity
Name the new entity "CameraController" and click OK
Next let's have the CameraController control our Camera. To do this:
Right-click on CameraController's Objects
Select "Add Object"
Verify "FlatRedBall or Custom Type" is selected.
Select "Camera" and click OK
Drag+drop the "CameraController" into the GameScreen's Objects folder to create an instance of CameraController
Next we'll set up our initial position of the CameraController1. We'll be setting position in the FRB Editor, but we'll set the rotation and orientation in code since the FRB Editor does not allow us to set some of these values. To set the CameraController1's initial values:
Select CameraController1 in the FRB Editor
Set Z to 10. This is the height that the Camera will be positioned above the ground
Open Visual Studio
Open CameraController.cs which is located in the Entities folder
Add the following code to CustomInitialize:
At this point we can run the game and we should see the Camera looking into the distance:
Finally we'll add code for movement and looking. We'll add looking first, then add moving. To do this, add the following code to CustomActivity in the CameraController entity:
Now implement the LookingActivity function:
Next, we'll implement the MovementActivity function:
This shows the basics of how to set up a First Person camera, but it is by no means a final implementation. First person games (and 3D games in general) are complicated and require a good understanding of 3D math, and quite often of rendering technologies.
FlatRedBall supports playing sound effects. Sound effects can be loaded from .wav files.
The following will show you how to add a SoundEffect to a Screen. The same method can be used to add a SoundEffect to an Entity as well.
Right-click on a Screen's Files tree node
Add a file to the Screen
If you have an existing .wav file, select Add File->Existing File
If you would like to add a placeholder .wav file, or if you would like to test playing sound effects, select Add File -> New File and select Sound Effect (.wav)
Note: WAV files are used for sound effects. MP3 and WMA files are used for songs. For information on MP3 files, see this file.
Your WAV file should now be part of of the Screen.
To play the sound in code, add the following code to your GameScreen. This assumes that your file is called SoundEffectFile.wav.wav and that you are adding this code to the same Screen/Entity that contains the file:
The SoundEffectFile object in this example is a SoundEffect instance created by Glue. For information on working with the SoundEffect class, see the SoundEffect code reference page. The SoundEffect (which is added to a Screen/Entity when adding a WAV file) can also be played using the AudioManager.
By default the SoundEffect is fire-and-forget, and the only limitation on simultaneous sound effects is from the hardware. However, using the Duration property, a SoundEffect can be limited. In concept, to accomplish this the code needs to keep track of a variable for how many sounds are playing. When a sound plays, increment the number. When a sound stops playing, decrement the number.
To control volume, looping, and panning, you will need to use a SoundEffectInstance object. For information on this, see the SoundEffectInstance Glue reference page.
You may need to convert your file to 8 or 16-bit as XNA PC may have problems with 24 bit audio. For more information, see this page.
Download this file to your computer. Note that you should rename it to not begin with a letter, so renaming it as redball.png is a good idea:
Note that we set the CameraCullMode to CameraCullMode.None. Although this isn't strictly necessary for this demo, you most likely want to do this if you are making a game that supports camera rotation. For more information, see the .