Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# way of telling how many enemies alive

Tags:

c#

I have my base game class and an enemy class.

When I instantiate an enemy using the base game I would like a integer to increase And when one dies I would need it to decrease the integer.

The end result being a new enemy spawns every few seconds so long as that integer is less than my MAX_ENEMIES

any way I'm currently clue less and was hoping someone could direct me with how I should arrange this ( do I have the enemies increase the number when they spawn? )

like image 913
evilsponge Avatar asked Nov 24 '25 02:11

evilsponge


1 Answers

Here's the basic idea: use a Factory Method. You may want to handle some of the specifics differently.

void Main()
{
    var game = new Game();
    game.CreateEnemy("Blinky");
    Console.WriteLine(game.EnemyCount);
    game.CreateEnemy("Clyde");
    Console.WriteLine(game.EnemyCount);
    game.DestroyEnemy(game.Enemies[0]);
    Console.WriteLine(game.EnemyCount);
}

public class Game
{
    public List<Enemy> Enemies = new List<Enemy>();

    public void CreateEnemy(string name)
    {
        if (EnemyCount >= MAX_ENEMIES) return;
        var enemy = new Enemy { Name = name};
        Enemies.Add(enemy);
    }

    public void DestroyEnemy(Enemy enemy)
    {
        Enemies.Remove(enemy);
    }

    public int EnemyCount
    {
        get { return Enemies.Count(); }
    }
}

public class Enemy
{
    public string Name { get; set; }
}
like image 159
Todd Sprang Avatar answered Nov 26 '25 16:11

Todd Sprang