Home > Enterprise >  Make a spawner for enemies in OpenGL 2D
Make a spawner for enemies in OpenGL 2D

Time:11-16

I have a project called Survival Shooter where I have to make a spawner for the enemies. I dont know how to make multiple enemies spawn from random positions or like from one position but after 5 seconds for example.

void Tema1::SpawnEnemy(glm::mat3 visMatrix, float deltaTime, float* posX, float* posY, float speed)
{

enemyAngle = atan2(translateX - *posX, translateY - *posY);
*posX  = speed * sin(enemyAngle) * deltaTime;
*posY  = speed * cos(enemyAngle) * deltaTime;

//Body
modelMatrix = visMatrix;
modelMatrix *= transform2D::Translate(*posX, *posY);
modelMatrix *= transform2D::Scale(1, 1);
modelMatrix *= transform2D::Rotate(enemyAngle);
modelMatrix *= transform2D::Translate(-50, -50);
RenderMesh2D(meshes["border"], shaders["VertexColor"], modelMatrix);

//Eye1
modelMatrix = visMatrix;
modelMatrix *= transform2D::Translate(*posX, *posY);
modelMatrix *= transform2D::Scale(0.25f, 0.25f);
modelMatrix *= transform2D::Rotate(enemyAngle);
modelMatrix *= transform2D::Translate(150, 200);
modelMatrix *= transform2D::Rotate(0);
modelMatrix *= transform2D::Translate(-50, -50);
RenderMesh2D(meshes["square2"], shaders["VertexColor"], modelMatrix);

//Eye2
modelMatrix = visMatrix;
modelMatrix *= transform2D::Translate(*posX, *posY);
modelMatrix *= transform2D::Scale(0.25f, 0.25f);
modelMatrix *= transform2D::Rotate(enemyAngle);
modelMatrix *= transform2D::Translate(-150, 200);
modelMatrix *= transform2D::Rotate(0);
modelMatrix *= transform2D::Translate(-50, -50);
RenderMesh2D(meshes["square2"], shaders["VertexColor"], modelMatrix);

}

This is the method that I call when I want to draw a new enemy and I call it in the Update method.

The question is, how do I draw a new enemy after an amount of time without destroying the current enemies that I have in the scene?

CodePudding user response:

You need to explicitly represent enemies in your game state:

struct Enemy {
  glm::vec2 pos;
  glm::vec2 speed;
  ...
  void update(float deltaTime); // Update position and/or speed
  void draw(); // Actually draw
};

std::vector<Enemy> enemies;

Spawning an enemy is then simply a matter of creating a new Enemy object and adding it to the vector. Destroying an enemy is a matter of removing an Enemy object from the vector. Beware of the usual issues with removing items from a vector you are iterating over though.

Your game loop should first call update(deltaTime) on all objects and then draw() them.

CodePudding user response:

First, you need to pick a way to store your enemies. Since you are working in C , I'd recommend you to define a base class entity for the players, items, entities and anything else that will be rendered. It will define a single function: update(float delta), where delta is the time it took us to render the previous frame. After that, you can make a class movableEntity, inheriting entity, that can move (is controllable, either by a player or by an AI). The class will define a few functions: jump, moveBack, moveForward, attack, etc... It will define two fields, as well: float x and float y It really depends on what your enemies will be able to do. Now, we define a class for our enemy, which should be called enemyEntity, and it will inherit movableEntity.

Now, you need to have a list of all entities present in the world. You will iterate trough it and will call render(delta) on each one of them. For the enemies, you should implement an AI, that uses the controller to control the enemy (it is recommended that the controllableEntity class defines the moving functions so they have some sort of a cooldown). It should be included in the update funcion of the enemy. Preferably, you could write the enemy AI so that you just specify a target.

Here's an example enemy AI for a 2D game that is viewed from the side (like most platformers):

void update(float delta) {
    float distanceX = x - target->x;

    if (distanceX > 1) {
        moveLeft();
    }
    else if (distanceX < -1) {
        moveRight();
    }
    else {
        attack();
    }
}

Of course, this is a very basic AI, that doesn't take into account any obstacles on its way, but the movableEntity class ensures that the player can do as much as the entity can. If you need any help, write in the comments

  • Related