@@ -114,9 +114,7 @@ void Engine::Run() {
114114 running = true ;
115115
116116 // Main loop
117- int loopCount = 0 ;
118117 while (running) {
119- loopCount++;
120118 // Process platform events
121119 if (!platform->ProcessEvents ()) {
122120 running = false ;
@@ -180,17 +178,16 @@ void Engine::Cleanup() {
180178}
181179
182180Entity* Engine::CreateEntity (const std::string& name) {
183- // Check if an entity with this name already exists
184- if (entityMap.contains (name)) {
185- return nullptr ;
186- }
187-
181+ // Always allow duplicate names; map stores a representative entity
188182 // Create the entity
189183 auto entity = std::make_unique<Entity>(name);
190- // Add to the map and vector
184+ // Add to the vector and map
191185 entities.push_back (std::move (entity));
186+ Entity* rawPtr = entities.back ().get ();
187+ // Update the map to point to the most recently created entity with this name
188+ entityMap[name] = rawPtr;
192189
193- return entities. back (). get () ;
190+ return rawPtr ;
194191}
195192
196193Entity* Engine::GetEntity (const std::string& name) const {
@@ -206,19 +203,31 @@ bool Engine::RemoveEntity(Entity* entity) {
206203 return false ;
207204 }
208205
206+ // Remember the name before erasing ownership
207+ std::string name = entity->GetName ();
208+
209209 // Find the entity in the vector
210210 auto it = std::find_if (entities.begin (), entities.end (),
211211 [entity](const std::unique_ptr<Entity>& e) {
212212 return e.get () == entity;
213213 });
214214
215215 if (it != entities.end ()) {
216- // Remove from the map
217- entityMap.erase (entity->GetName ());
218-
219- // Remove from the vector
216+ // Remove from the vector (ownership)
220217 entities.erase (it);
221218
219+ // Update the map: point to another entity with the same name if one exists
220+ auto remainingIt = std::find_if (entities.begin (), entities.end (),
221+ [&name](const std::unique_ptr<Entity>& e) {
222+ return e->GetName () == name;
223+ });
224+
225+ if (remainingIt != entities.end ()) {
226+ entityMap[name] = remainingIt->get ();
227+ } else {
228+ entityMap.erase (name);
229+ }
230+
222231 return true ;
223232 }
224233
@@ -233,50 +242,39 @@ bool Engine::RemoveEntity(const std::string& name) {
233242 return false ;
234243}
235244
236- std::vector<Entity*> Engine::GetAllEntities () const {
237- std::vector<Entity*> result;
238- result.reserve (entities.size ());
239-
240- for (const auto & entity : entities) {
241- result.push_back (entity.get ());
242- }
243-
244- return result;
245- }
246-
247245void Engine::SetActiveCamera (CameraComponent* cameraComponent) {
248246 activeCamera = cameraComponent;
249247}
250248
251- CameraComponent* Engine::GetActiveCamera () const {
249+ const CameraComponent* Engine::GetActiveCamera () const {
252250 return activeCamera;
253251}
254252
255- ResourceManager* Engine::GetResourceManager () const {
253+ const ResourceManager* Engine::GetResourceManager () const {
256254 return resourceManager.get ();
257255}
258256
259- Platform* Engine::GetPlatform () const {
257+ const Platform* Engine::GetPlatform () const {
260258 return platform.get ();
261259}
262260
263- Renderer* Engine::GetRenderer () const {
261+ Renderer* Engine::GetRenderer () {
264262 return renderer.get ();
265263}
266264
267- ModelLoader* Engine::GetModelLoader () const {
265+ ModelLoader* Engine::GetModelLoader () {
268266 return modelLoader.get ();
269267}
270268
271- AudioSystem* Engine::GetAudioSystem () const {
269+ const AudioSystem* Engine::GetAudioSystem () const {
272270 return audioSystem.get ();
273271}
274272
275- PhysicsSystem* Engine::GetPhysicsSystem () const {
273+ PhysicsSystem* Engine::GetPhysicsSystem () {
276274 return physicsSystem.get ();
277275}
278276
279- ImGuiSystem* Engine::GetImGuiSystem () const {
277+ const ImGuiSystem* Engine::GetImGuiSystem () const {
280278 return imguiSystem.get ();
281279}
282280
0 commit comments