Files
DemoGame/src/Engine/Level/Scene.cpp
Redacted 25d080ff2c A few things
Make child entites work.

z-sort entity list before rendering.

entites have a creation timestamp.
2025-01-02 21:44:14 -05:00

143 lines
3.5 KiB
C++

#include <Engine/Level/Scene.h>
#include <Engine/Entity/Camera.h>
#include <algorithm>
bool Engine::Scene::EntityListContains(const Entity* entity) const {
for (auto* e : entity_list)
if (e == entity)
return true;
return false;
}
bool Engine::Scene::FixedListContains(const Fixed* fixed) const {
for (auto* f : fixed_list)
if (f == fixed)
return true;
return false;
}
size_t Engine::Scene::FixedCount() const {
return fixed_list.size();
}
size_t Engine::Scene::EntityCount() const {
return GetFlatEntityList(entity_list).size();
}
void Engine::Scene::Update() {
if (active_camera)
active_camera->Update();
for (auto& e : entity_list)
if (auto* c = dynamic_cast<Camera*>(e); c == nullptr)
e->Update();
}
void Engine::Scene::Render(RenderTarget* render_target) {
// The Camera and the HUD are ignored here because they're special cases.
std::vector<Renderable*> display_list{};
for (auto* e : GetFlatEntityList(entity_list))
if (auto* r = dynamic_cast<Renderable*>(e))
if (auto* c = dynamic_cast<Camera*>(r); c == nullptr)
if (auto* h = dynamic_cast<Hud*>(c); h == nullptr)
display_list.push_back(r);
std::sort(display_list.begin(), display_list.end(), [](Renderable* a, Renderable* b) {
return a->GetDepth() > b->GetDepth();
});
J2D::Begin( render_target, true);
if (active_camera)
active_camera->Render();
for (auto& f : fixed_list)
if (f->Enabled())
f->Render();
for (auto& r : display_list)
r->Render();
if (heads_up_display)
heads_up_display->Render();
J2D::End();
}
Engine::Scene::~Scene() {
for (auto* f : fixed_list)
delete f;
for (auto* e : entity_list)
delete e;
delete heads_up_display;
delete active_camera;
}
void Engine::Scene::Unload() {
for (auto* f : fixed_list)
delete f;
for (auto* e : entity_list)
delete e;
delete heads_up_display;
}
void Engine::Scene::AppendEntity(Entity* entity) {
if (!EntityListContains(entity))
entity_list.push_back(entity);
}
void Engine::Scene::AppendFixed(Fixed* fixed) {
if (!FixedListContains(fixed))
fixed_list.push_back(fixed);
}
void Engine::Scene::DestroyEntity(Entity *entity) {
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
if (it != entity_list.end())
delete *it, entity_list.erase(it);
}
void Engine::Scene::DestroyFixed(Fixed* fixed) {
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != fixed_list.end())
delete *it, fixed_list.erase(it);
}
void Engine::Scene::RemoveEntity(Entity* entity) {
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
if (it != entity_list.end())
entity_list.erase(it);
}
void Engine::Scene::RemoveFixed(Fixed* fixed) {
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != fixed_list.end())
fixed_list.erase(it);
}
std::string Engine::Scene::GetName() const {
return name;
}
Engine::Camera* Engine::Scene::GetActiveCamera() const {
return active_camera;
}
std::vector<Engine::Entity*> Engine::Scene::GetFlatEntityList(const std::vector<Entity*>& ent_list) const {
std::vector<Entity*> result{};
for (auto* e : ent_list) {
auto children = GetFlatEntityList(e->GetChildren());
for (auto* c : children)
result.push_back(c);
result.push_back(e);
}
return result;
}