Skip to content

Commit 02baf4d

Browse files
committed
addressing more comments.
1 parent 6db6b9d commit 02baf4d

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

attachments/simple_engine/audio_system.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ void AudioSystem::Update(float deltaTime) {
587587

588588
// Synchronize HRTF listener position and orientation with active camera
589589
if (engine) {
590-
CameraComponent* activeCamera = engine->GetActiveCamera();
590+
const CameraComponent* activeCamera = engine->GetActiveCamera();
591591
if (activeCamera) {
592592
// Get camera position
593593
glm::vec3 cameraPos = activeCamera->GetPosition();

attachments/simple_engine/engine.cpp

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

182180
Entity* 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

196193
Entity* 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-
247245
void 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

attachments/simple_engine/engine.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ class Engine {
8181
*/
8282
bool RemoveEntity(const std::string& name);
8383

84-
/**
85-
* @brief Get all entities.
86-
* @return A vector of pointers to all entities.
87-
*/
88-
std::vector<Entity*> GetAllEntities() const;
89-
9084
/**
9185
* @brief Set the active camera.
9286
* @param cameraComponent The camera component to set as active.
@@ -97,49 +91,49 @@ class Engine {
9791
* @brief Get the active camera.
9892
* @return A pointer to the active camera component, or nullptr if none is set.
9993
*/
100-
CameraComponent* GetActiveCamera() const;
94+
const CameraComponent* GetActiveCamera() const;
10195

10296
/**
10397
* @brief Get the resource manager.
10498
* @return A pointer to the resource manager.
10599
*/
106-
ResourceManager* GetResourceManager() const;
100+
const ResourceManager* GetResourceManager() const;
107101

108102
/**
109103
* @brief Get the platform.
110104
* @return A pointer to the platform.
111105
*/
112-
Platform* GetPlatform() const;
106+
const Platform* GetPlatform() const;
113107

114108
/**
115109
* @brief Get the renderer.
116110
* @return A pointer to the renderer.
117111
*/
118-
Renderer* GetRenderer() const;
112+
Renderer* GetRenderer();
119113

120114
/**
121115
* @brief Get the model loader.
122116
* @return A pointer to the model loader.
123117
*/
124-
ModelLoader* GetModelLoader() const;
118+
ModelLoader* GetModelLoader();
125119

126120
/**
127121
* @brief Get the audio system.
128122
* @return A pointer to the audio system.
129123
*/
130-
AudioSystem* GetAudioSystem() const;
124+
const AudioSystem* GetAudioSystem() const;
131125

132126
/**
133127
* @brief Get the physics system.
134128
* @return A pointer to the physics system.
135129
*/
136-
PhysicsSystem* GetPhysicsSystem() const;
130+
PhysicsSystem* GetPhysicsSystem();
137131

138132
/**
139133
* @brief Get the ImGui system.
140134
* @return A pointer to the ImGui system.
141135
*/
142-
ImGuiSystem* GetImGuiSystem() const;
136+
const ImGuiSystem* GetImGuiSystem() const;
143137

144138
/**
145139
* @brief Handles mouse input for interaction and camera control.

0 commit comments

Comments
 (0)