Spawning Enemies in SFML / C#

In this post, we are going to create Enemy class and EnemyManager class. These enemies will spawn an invisible area at the top of the game. 

I wrote Enemy class like Player class but they are not exactly the same. Firstly I created a random object to generate random numbers within a certain range. 

namespace shmup {
    class Enemy 
    {
        private readonly Random random = new Random();
        private Sprite sprite;
        public const float ENEMY_SPEED = 5f;
        Vector2f position;
        public Vector2f Position { get { return position; } }

In the constructor method, we determine the position of the enemy sprite via a random generator. Also, we loaded the texture of the enemy from TextureManager class.

        public Enemy () 
        {
            this.sprite = new Sprite();
            this.sprite.Texture = TextureManager.EnemyTexture;
            this.position.X = (float)this.random.Next(0, 640);
            this.position.Y = -(float)this.random.Next(0, 480);
            this.sprite.Position = this.position;
        }

After that, we created an update method and increase the axis-y of position with ENEMY_SPEED. These methods are that we have already known what are they.

        public void update() {
            this.position.Y += ENEMY_SPEED;
            this.sprite.Position = this.position;
        }

        public void draw(RenderTarget window) 
        {
            window.Draw(this.sprite);           
        }
But we won't use them directly in Game class. Because we need to more enemies that will be strike on the player side. Therefore, we have to update and render them together. So, I decided to create EnemyManager class to manage these enemies together. After this operation, this feature provides much readable code. I defined generic list called enemies.
namespace shmup
{
    class EnemyManager
    {
        public List<Enemy> enemies = new List<Enemy>();
Let's examine update method. Firstly, The game will spawn 20 enemies on the screen. If these enemies destroyed or crossed the maximum axis-y value then they will be removed from the list. According to this, also the update method will produce as many objects.
        public void update() 
        {
            if (enemies.Count < 20){
                for (int i = 0; i < 19 - enemies.Count; i++)
                {
                    enemies.Add(new Enemy());
                }
            }
            for (int i = 0; i < enemies.Count; i++)
            {
                enemies[i].update();
                if (enemies[i].Position.Y > 480) 
                {
                    enemies.Remove(enemies[i]);
                }
            }
        }
Finally, we will draw them on the screen:
        public void draw(RenderTarget window)
        {
            for (int i = 0; i < enemies.Count; i++)
            {
                enemies[i].draw(window);
            }
        }
Now, we can use this manager in Game class.
		...
        EnemyManager enemies;

        public Game()
        {
            ...

            player = new Player();
            enemies = new EnemyManager();
            ...
And we use its methods appropriately:
        private void update() 
        {
            this.player.update();
            this.enemies.update();
        }

        private void draw() 
        {
            this.window.Clear(Color.Blue);
            
            this.window.Draw(this.background); 
            this.player.draw(this.window);
            this.enemies.draw(this.window);

            this.window.Display();
        }
Now, let's look at the result:
Spawning Invaders in SFML C#


 

No comments:

Post a Comment