This commit is contained in:
2025-01-02 18:46:27 -05:00
parent bd72263944
commit 3349474e3a
6 changed files with 63 additions and 40 deletions

View File

@@ -1,7 +1,13 @@
#include "J3ML/LinearAlgebra/Vector2.hpp"
#include "Entity.h"
#pragma once #pragma once
#include <Engine/Entity/Renderable.h>
class Camera : public Engine::Entity { namespace Engine {
class Camera;
}
class Engine::Camera : public Renderable {
public:
void Render() override {};
public:
explicit Camera(const Vector2& position) : Renderable(position) {}
}; };

View File

@@ -7,21 +7,24 @@
#include <JGL/types/RenderTarget.h> #include <JGL/types/RenderTarget.h>
namespace Engine { namespace Engine {
class Camera;
class Scene; class Scene;
} }
class Engine::Scene { class Engine::Scene {
protected: protected:
std::string name; std::string name;
Hud* HeadsUpDisplay = nullptr; Hud* heads_up_display = nullptr;
std::vector<Fixed*> FixedList{}; Camera* active_camera = nullptr;
std::vector<Entity*> EntityList{}; std::vector<Fixed*> fixed_list{};
std::vector<Entity*> entity_list{};
public: public:
[[nodiscard]] bool EntityListContains(const Entity* entity) const; [[nodiscard]] bool EntityListContains(const Entity* entity) const;
[[nodiscard]] bool FixedListContains(const Fixed* fixed) const; [[nodiscard]] bool FixedListContains(const Fixed* fixed) const;
[[nodiscard]] size_t FixedCount() const; [[nodiscard]] size_t FixedCount() const;
[[nodiscard]] size_t EntityCount() const; [[nodiscard]] size_t EntityCount() const;
[[nodiscard]] std::string GetName() const; [[nodiscard]] std::string GetName() const;
[[nodiscard]] Camera* GetActiveCamera() const;
public: public:
void AppendEntity(Entity* entity); void AppendEntity(Entity* entity);
void AppendFixed(Fixed* fixed); void AppendFixed(Fixed* fixed);

View File

@@ -67,7 +67,6 @@ bool Engine::Entity::AppendChild(Entity* entity) {
Engine::Entity::~Entity() { Engine::Entity::~Entity() {
for (auto* e : children) for (auto* e : children)
delete e; delete e;
children = {};
} }
void Engine::Entity::UpdateChildren() { void Engine::Entity::UpdateChildren() {

View File

@@ -1,105 +1,120 @@
#include <Engine/Level/Scene.h> #include <Engine/Level/Scene.h>
#include <Engine/Entity/Camera.h>
bool Engine::Scene::EntityListContains(const Entity* entity) const { bool Engine::Scene::EntityListContains(const Entity* entity) const {
for (auto* e : EntityList) for (auto* e : entity_list)
if (e == entity) if (e == entity)
return true; return true;
return false; return false;
} }
bool Engine::Scene::FixedListContains(const Fixed* fixed) const { bool Engine::Scene::FixedListContains(const Fixed* fixed) const {
for (auto* f : FixedList) for (auto* f : fixed_list)
if (f == fixed) if (f == fixed)
return true; return true;
return false; return false;
} }
size_t Engine::Scene::FixedCount() const { size_t Engine::Scene::FixedCount() const {
return FixedList.size(); return fixed_list.size();
} }
size_t Engine::Scene::EntityCount() const { size_t Engine::Scene::EntityCount() const {
return EntityList.size(); return entity_list.size();
} }
void Engine::Scene::Update() { void Engine::Scene::Update() {
for (auto& e : EntityList) if (active_camera)
e->Update(); 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) { void Engine::Scene::Render(RenderTarget* render_target) {
for (auto& f : FixedList) for (auto& f : fixed_list)
if (f->Enabled()) if (f->Enabled())
f->Render(); f->Render();
// TODO Render order. In this system it's not possible for child entities to be rendered before the parent. // TODO Render order. In this system it's not possible for child entities to be rendered before the parent.
J2D::Begin(render_target, true); J2D::Begin(render_target, true);
for (auto& e : EntityList)
if (auto* r = dynamic_cast<Renderable*>(e))
r->Render();
if (HeadsUpDisplay) if (active_camera)
HeadsUpDisplay->Render(); 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();
if (heads_up_display)
heads_up_display->Render();
J2D::End(); J2D::End();
} }
Engine::Scene::~Scene() { Engine::Scene::~Scene() {
for (auto* f : FixedList) for (auto* f : fixed_list)
delete f; delete f;
for (auto* e : EntityList) for (auto* e : entity_list)
delete e; delete e;
delete HeadsUpDisplay; delete heads_up_display;
delete active_camera;
} }
void Engine::Scene::Unload() { void Engine::Scene::Unload() {
for (auto* f : FixedList) for (auto* f : fixed_list)
delete f; delete f;
for (auto* e : EntityList) for (auto* e : entity_list)
delete e; delete e;
delete HeadsUpDisplay; delete heads_up_display;
} }
void Engine::Scene::AppendEntity(Entity* entity) { void Engine::Scene::AppendEntity(Entity* entity) {
if (!EntityListContains(entity)) if (!EntityListContains(entity))
EntityList.push_back(entity); entity_list.push_back(entity);
} }
void Engine::Scene::AppendFixed(Fixed* fixed) { void Engine::Scene::AppendFixed(Fixed* fixed) {
if (!FixedListContains(fixed)) if (!FixedListContains(fixed))
FixedList.push_back(fixed); fixed_list.push_back(fixed);
} }
void Engine::Scene::DestroyEntity(Entity *entity) { void Engine::Scene::DestroyEntity(Entity *entity) {
auto it = std::find(EntityList.begin(), EntityList.end(), entity); auto it = std::find(entity_list.begin(), entity_list.end(), entity);
if (it != EntityList.end()) if (it != entity_list.end())
delete *it, EntityList.erase(it); delete *it, entity_list.erase(it);
} }
void Engine::Scene::DestroyFixed(Fixed* fixed) { void Engine::Scene::DestroyFixed(Fixed* fixed) {
auto it = std::find(FixedList.begin(), FixedList.end(), fixed); auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != FixedList.end()) if (it != fixed_list.end())
delete *it, FixedList.erase(it); delete *it, fixed_list.erase(it);
} }
void Engine::Scene::RemoveEntity(Entity* entity) { void Engine::Scene::RemoveEntity(Entity* entity) {
auto it = std::find(EntityList.begin(), EntityList.end(), entity); auto it = std::find(entity_list.begin(), entity_list.end(), entity);
if (it != EntityList.end()) if (it != entity_list.end())
EntityList.erase(it); entity_list.erase(it);
} }
void Engine::Scene::RemoveFixed(Fixed* fixed) { void Engine::Scene::RemoveFixed(Fixed* fixed) {
auto it = std::find(FixedList.begin(), FixedList.end(), fixed); auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != FixedList.end()) if (it != fixed_list.end())
FixedList.erase(it); fixed_list.erase(it);
} }
std::string Engine::Scene::GetName() const { std::string Engine::Scene::GetName() const {
return name; return name;
} }
Engine::Camera* Engine::Scene::GetActiveCamera() const {
return active_camera;
}

View File

@@ -5,6 +5,6 @@
void ControllableBox::Init() { void ControllableBox::Init() {
auto* hud = new Game::DemoGameHud(); auto* hud = new Game::DemoGameHud();
auto* b = new Game::Box({0, 0}); auto* b = new Game::Box({0, 0});
HeadsUpDisplay = hud; heads_up_display = hud;
AppendEntity(b); AppendEntity(b);
} }