A few things
Make child entites work. z-sort entity list before rendering. entites have a creation timestamp.
This commit is contained in:
@@ -59,8 +59,11 @@ Vector2 Engine::Entity::GetPosition() const {
|
||||
|
||||
bool Engine::Entity::AppendChild(Entity* entity) {
|
||||
bool success = false;
|
||||
if (!Globals::CurrentScene->EntityListContains(entity))
|
||||
children.push_back(entity), success = true;
|
||||
if (!Globals::CurrentScene->EntityListContains(entity)) {
|
||||
entity->parent = this;
|
||||
children.push_back(entity);
|
||||
success = true;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -88,3 +91,19 @@ void Engine::Entity::RemoveChild(Entity* entity) {
|
||||
if (it != children.end())
|
||||
children.erase(it);
|
||||
}
|
||||
|
||||
long Engine::Entity::GetCreationTime() const {
|
||||
return creation_time;
|
||||
}
|
||||
|
||||
long Engine::Entity::GetAge() const {
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - creation_time;
|
||||
}
|
||||
|
||||
Engine::Entity* Engine::Entity::GetParent() const {
|
||||
return parent;
|
||||
}
|
||||
|
||||
std::vector<Engine::Entity*> Engine::Entity::GetChildren() const {
|
||||
return children;
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#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)
|
||||
@@ -20,7 +21,7 @@ size_t Engine::Scene::FixedCount() const {
|
||||
}
|
||||
|
||||
size_t Engine::Scene::EntityCount() const {
|
||||
return entity_list.size();
|
||||
return GetFlatEntityList(entity_list).size();
|
||||
}
|
||||
|
||||
void Engine::Scene::Update() {
|
||||
@@ -33,24 +34,33 @@ void Engine::Scene::Update() {
|
||||
}
|
||||
|
||||
void Engine::Scene::Render(RenderTarget* render_target) {
|
||||
for (auto& f : fixed_list)
|
||||
if (f->Enabled())
|
||||
f->Render();
|
||||
// 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);
|
||||
|
||||
// TODO Render order. In this system it's not possible for child entities to be rendered before the parent.
|
||||
std::sort(display_list.begin(), display_list.end(), [](Renderable* a, Renderable* b) {
|
||||
return a->GetDepth() > b->GetDepth();
|
||||
});
|
||||
|
||||
J2D::Begin(render_target, true);
|
||||
J2D::Begin( render_target, true);
|
||||
|
||||
if (active_camera)
|
||||
active_camera->Render();
|
||||
|
||||
for (auto& e : entity_list)
|
||||
if (auto* r = dynamic_cast<Renderable*>(e))
|
||||
if (auto* c = dynamic_cast<Camera*>(r); c == nullptr)
|
||||
r->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();
|
||||
}
|
||||
|
||||
@@ -118,3 +128,15 @@ 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;
|
||||
}
|
||||
|
||||
|
@@ -4,7 +4,7 @@
|
||||
|
||||
void ControllableBox::Init() {
|
||||
auto* hud = new Game::DemoGameHud();
|
||||
auto* b = new Game::Box({0, 0});
|
||||
heads_up_display = hud;
|
||||
auto *b = new Game::Box({0, 0});
|
||||
AppendEntity(b);
|
||||
heads_up_display = hud;
|
||||
}
|
||||
|
Reference in New Issue
Block a user