Skip to content

Commit 19b407d

Browse files
committed
refactor: update enemy AI system to accept player entity directly
1 parent 8a5b85f commit 19b407d

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/game/systems/enemy-ai.hpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "../components/enemy.hpp"
1414
#include "../components/health.hpp"
15-
#include "../components/player.hpp"
1615

1716
namespace gameplay {
1817

@@ -44,18 +43,8 @@ namespace gameplay {
4443
}
4544

4645
public:
47-
void update(our::World* world, float deltaTime) {
48-
if (!world || deltaTime <= 0.0f) return;
49-
50-
// get player position
51-
our::Entity* playerEntity = nullptr;
52-
for (our::Entity* entity : world->getEntities()) {
53-
if (entity->getComponent<PlayerComponent>()) {
54-
playerEntity = entity;
55-
break;
56-
}
57-
}
58-
if (!playerEntity) return;
46+
void update(our::World* world, our::Entity* playerEntity, float deltaTime) {
47+
if (!world || !playerEntity || deltaTime <= 0.0f) return;
5948

6049
glm::vec3 playerPos = glm::vec3(playerEntity->getLocalToWorldMatrix() * glm::vec4(0, 0, 0, 1));
6150
float t = static_cast<float>(glfwGetTime());

src/states/play-state.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <application.hpp>
4+
#include <components/player.hpp>
45
#include <ecs/world.hpp>
56
#include <systems/enemy-ai.hpp>
67
#include <systems/enemy-spawner.hpp>
@@ -14,6 +15,7 @@ class Playstate : public our::State {
1415
our::ForwardRenderer renderer;
1516
our::FreeCameraControllerSystem cameraController;
1617
our::MovementSystem movementSystem;
18+
our::Entity* playerEntity = nullptr;
1719
gameplay::EnemyAISystem enemyAI;
1820
gameplay::EnemySpawner enemySpawner;
1921

@@ -50,6 +52,15 @@ class Playstate : public our::State {
5052
if (config.contains("world")) {
5153
world.deserialize(config["world"]);
5254
}
55+
56+
// Store a pointer to the player entity
57+
for (our::Entity* entity : world.getEntities()) {
58+
if (entity->getComponent<gameplay::PlayerComponent>()) {
59+
playerEntity = entity;
60+
break;
61+
}
62+
}
63+
5364
// We initialize the camera controller system since it needs a pointer to the app
5465
cameraController.enter(getApp());
5566
// Then we initialize the renderer
@@ -64,7 +75,7 @@ class Playstate : public our::State {
6475
// Here, we just run a bunch of systems to control the world logic
6576
movementSystem.update(&world, (float)deltaTime);
6677
cameraController.update(&world, (float)deltaTime);
67-
enemyAI.update(&world, (float)deltaTime);
78+
enemyAI.update(&world, playerEntity, (float)deltaTime);
6879
enemySpawner.update(&world, (float)deltaTime);
6980
// And finally we use the renderer system to draw the scene
7081
renderer.render(&world);
@@ -83,6 +94,7 @@ class Playstate : public our::State {
8394
renderer.destroy();
8495
// On exit, we call exit for the camera controller system to make sure that the mouse is unlocked
8596
cameraController.exit();
97+
playerEntity = nullptr;
8698
// Clear the world
8799
world.clear();
88100
// and we delete all the loaded assets to free memory on the RAM and the VRAM

0 commit comments

Comments
 (0)