Game Development Reference
In-Depth Information
// A little later in the code
public void Render(Renderer renderer)
{
_background.Render(renderer);
_backgroundLayer.Render(renderer);
_enemyList.ForEach(x = > x.Render(renderer));
This code is all pretty standard. A list of enemies is created, and an enemy is
added to it. The list is then updated and rendered using the lambda syntax. Run
the program now and you should see two spaceships: the player spaceship and an
enemy facing it. With the current code, the player can fly through the enemy ship
with no reaction. If the player crashes into the enemy ship then the player should
take some damage or the state should change to the game over state. Before any
of that can happen, the collision needs to be detected.
The collision detection will be the simple rectangle-rectangle collision explored
earlier in the topic. Before coding the collision it may be useful to visualize the
bounding box around the enemy. This is quite simple to code using OpenGL's
immediate mode and GL_LINE_LOOP . Add the following code to the Enemy
class.
public RectangleF GetBoundingBox()
{
float width ¼ (float)(_spaceship.Texture.Width * _scale);
float height ¼ (float)(_spaceship.Texture.Height * _scale);
return new RectangleF( (float)_spaceship.GetPosition().X - width / 2,
(float)_spaceship.GetPosition().Y - height / 2,
width, height);
}
// Render a bounding box
public void Render_Debug()
{
Gl.glDisable(Gl.GL_TEXTURE_2D);
RectangleF bounds ¼ GetBoundingBox();
Gl.glBegin(Gl.GL_LINE_LOOP);
{
Gl.glColor3f(1, 0, 0);
Gl.glVertex2f(bounds.Left, bounds.Top);
Gl.glVertex2f(bounds.Right, bounds.Top);
 
Search WWH ::




Custom Search