Defeating Enemies

When a enemy's life total reaches zero, they are defeated and removed from the level.

How removing enemies works

New functions were created so that default values can be passed to the Enemy class, which hides their location away from the player's reach and creates a collider of zero size.

private Vector2 RemovePosition(int x, int y) {
            return new Vector2(x, y);
        }
        
private Collider RemoveCollider()
        {
            Rectangle rect = new Rectangle(0,0,0,0);
            return new Collider(rect);
        }

This was later used in the Fight function, where when the player character collides with an enemy, the enemy would be checked by name to then reinitialized as a new enemy with no image or collider.

switch (enemy.Name) {
                case "bossKoolaid":
                    bossKoolaid = new Enemy(RemovePosition(0,0), RemoveCollider());
                    bossKoolaid.Img = null;
                    picBossKoolAid.BackgroundImage = null;
                    break;
                case "enemyPoisonPacket":
                    enemyPoisonPacket = new Enemy(RemovePosition(0,0), RemoveCollider());
                    enemyPoisonPacket.Img = null;
                    picEnemyPoisonPacket.BackgroundImage = null;

                    break;
                case "enemyCheeto":
                    enemyCheeto = new Enemy(RemovePosition(0, 0), RemoveCollider());
                    enemyCheeto.Img = null;
                    picEnemyCheeto.BackgroundImage = null;
                    break;
                case "enemyFlea":
                    enemyFlea = new Enemy(RemovePosition(0, 0), RemoveCollider());
                    picEnemyFlea.BackgroundImage = null;
                    fleaFlag = false;
                    break;
            }

How the Level looks

Here is a screen shot of the level after the Flea has been defeated.

With each enemy defeated, a default value of 20 points is added to the player's score.

Last updated