Game Development Reference
In-Depth Information
public double MinTimeToShoot { get; set; }
Random _random ¼ new Random();
double _shootCountDown;
public void RestartShootCountDown()
{
_shootCountDown ¼ MinTimeToShoot þ (_random.NextDouble() *
MaxTimeToShoot);
}
BulletManager _bulletManager;
Texture _bulletTexture;
public Enemy(TextureManager textureManager, EffectsManager
effectsManager, BulletManager bulletManager)
{
_bulletManager ¼ bulletManager;
_bulletTexture ¼ textureManager.Get("bullet");
MaxTimeToShoot ¼ 12;
MinTimeToShoot ¼ 1;
RestartShootCountDown();
// Code omitted
public void Update(double elapsedTime)
{
_shootCountDown ¼ _shootCountDown - elapsedTime;
if (_shootCountDown < =0)
{
Bullet bullet ¼ new Bullet(_bulletTexture);
bullet.Speed ¼ 350;
bullet.Direction ¼ new Vector(-1, 0, 0);
bullet.SetPosition(_sprite.GetPosition());
bullet.SetColor(new Engine.Color(1, 0, 0, 1));
_bulletManager.EnemyShoot(bullet);
RestartShootCountDown();
}
When the enemy is created, it sets a timer for the next time it will shoot. The
timer is set using C#'s Random class and a minimum and maximum time.
The timer will be set somewhere in between these minimum and maximum
values. All ships will shoot at different times. The RestartShootCountDown
method sets the random time when the enemy will shoot. Math.NextDouble
 
Search WWH ::




Custom Search