Game Development Reference
In-Depth Information
bullet.SetColor(new Color(0, 1, 0, 1));
bullet.SetPosition(_sprite.GetPosition()
รพ
_gunOffset);
_bulletManager.Shoot(bullet);
}
To count down the recovery time, the
PlayerCharacter
class needs an
Up-
date
method to be added. The
Update
method will count down the recovery
time, but it never goes below 0. This is done by using the
Math.Max
function.
With the recovery time set, the
Fire
command returns immediately if the
spaceship is still recovering from the last shot. If the recovery time is 0, then the
ship can fire and the recovery time is reset so it can start counting down again.
The
Level
also needs a minor change; it needs to call the
PlayerChar-
acter
's
Update
method.
public void Update(double elapsedTime)
{
_playerCharacter.Update(elapsedTime);
_background.Update((float)elapsedTime);
_backgroundLayer.Update((float)elapsedTime);
UpdateCollisions();
_enemyList.ForEach(x =
>
x.Update(elapsedTime));
_bulletManager.Update(elapsedTime);
UpdateInput(elapsedTime);
}
Run the program again and you won't be able to fire as fast. This is
your
game, so
tweak the recovery rate to whatever feels right to you!
Damage and Explosions
The bullets have been added, but they sail right through the enemy inflicting no
damage; it's time to change that. The enemy should know when it's been hit and
respond appropriately. We'll start by handling the collisions and then create an
animated explosion.
The collision code is handled in the
Level
class, in the
UpdateCollisions
function. This function needs to be extended to also handle collisions between
bullets and enemies.
Search WWH ::
Custom Search