Game Development Reference
In-Depth Information
the
PlayerCharacter
class needs access to the manager. In the
Level
constructor, pass
the
BulletManager
into the
PlayerCharacter
constructor.
_playerCharacter
¼
new PlayerCharacter(_textureManager,
_bulletManager);
The
PlayerCharacter
class code then needs to be altered to accept and store
a reference to the
BulletManager
.
BulletManager _bulletManager;
Texture _bulletTexture;
public PlayerCharacter(TextureManager textureManager, BulletManager
bulletManager)
{
_bulletManager
¼
bulletManager;
_bulletTexture
¼
textureManager.Get("bullet");
The
PlayerCharacter
constructor also stores the
bulletTexture
that will
be used when firing bullets. To fire a bullet, a
bullet
object needs to be created
and positioned so that it starts near the player and then passes into the
BulletManager
. A new
Fire
method in the
PlayerCharacter
class will
be responsible for this.
Vector _gunOffset
¼
new Vector(55, 0, 0);
public void Fire()
{
Bullet bullet
¼
new Bullet(_bulletTexture);
bullet.SetColor(new Color(0, 1, 0, 1));
bullet.SetPosition(_sprite.GetPosition()
þ
_gunOffset);
_bulletManager.Shoot(bullet);
}
The bullet is created using the
bulletTexture
that was set up in the con-
structor. It's then colored green, but you can choose any color you want. The
position of the bullet is set so that it is the same position as the player's ship, but
with an offset so that the bullet appears to come from the front of the ship. If
there was no offset, the bullet would appear right in the middle of the
ship
sprite and this would look a little weird. The bullet direction isn't altered because
forward on the X axis is the default value. The default speed is also fine. Finally,
the bullet is given to the
BulletManager
and is officially fired using the
Shoot
method.
Search WWH ::
Custom Search