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