Game Development Reference
In-Depth Information
}
public void Render(Renderer renderer)
{
if (Dead)
{
return;
}
renderer.DrawSprite(_sprite);
}
public void Update(double elapsedTime)
{
if (Dead)
{
return;
}
Vector position
ΒΌ
_sprite.GetPosition();
position += Direction * Speed * elapsedTime;
_sprite.SetPosition(position);
}
}
}
The
BulletManager
only has two member variables: a list of bullets it's
managing and a rectangle representing the screen bounds. Remember to include
the using System.Drawing
statement at the top of the file so that the
RectangleF
class can be used. The screen bounds are used to determine if a
bullet has left the screen and can be destroyed.
The constructor takes in a rectangle,
playArea
, describing the playing area and
assigns it to the
_bounds
member. The
Shoot
method is used to add a bullet to
the
BulletManager
. Once a bullet is added, the
BulletManager
tracks it
until it leaves the play area or hits a ship. The
Update
method updates all the
bullets being tracked and then checks if any are out of bounds; finally, it deletes
any bullets that have the
Dead
flag set to true.
The
CheckOutOfBounds
function uses the rectangle intersection test
between the bullet and playing area to determine if it's off-screen. The
RemoveDeadBullets
performs an interesting trick; it iterates through the list
of bullets backwards and removes any bullets that are dead.
Foreach
can't be
Search WWH ::
Custom Search