Game Development Reference
In-Depth Information
Gl.glVertex2f(bounds.Right, bounds.Bottom);
Gl.glVertex2f(bounds.Left, bounds.Bottom);
}
Gl.glEnd();
Gl.glEnable(Gl.GL_TEXTURE_2D);
}
C#'s RectangleF class is used; therefore, the System.Drawing library needs
to be added to the using statements at the top of Enemy.cs . The function
GetBoundingBox uses the sprite to calculate a bounding box around it. The
width and height are scaled according to the sprite, so even if the sprite is scaled,
the bounding box will be correct. The RectangleF constructor takes in the
x and y position of the top-left corner, and then the width and height of the
rectangle. The position of the sprite is its center, so to get the top-left corner, half
the width and height must be subtracted from the position.
The Render_Debug method draws a red box around the sprite. The
Render_Debug method should be called from the Enemy.Render method.
This debug function can be removed at any time.
public void Render(Renderer renderer)
{
renderer.DrawSprite(_spaceship);
Render_Debug();
}
Run the code and a red box will be drawn around the enemy, as can be seen in
Figure 10.7. Visual debug routines are a great way to understand what your code
is really doing.
The GetBoundingBox function can be used to determine if the enemy is
colliding with anything else. At the moment, the player ship doesn't have a
GetBoundingBox function, and the principle of DRY (Don't Repeat Yourself)
means you shouldn't just copy this code! Instead, a new parent class should be
created that centralizes this functionality; then the Enemy and PlayerChar-
acter can both inherit from this.
Before the Enemy and the PlayerCharacter classes are generalized, this
Sprite class needs to be modified. To make the bounding box drawing func-
tions simpler, the sprite should have some methods to report the current scale.
 
Search WWH ::




Custom Search