Camera
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
#include "J3ML/LinearAlgebra/Vector2.hpp"
|
||||
#include "Entity.h"
|
||||
#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) {}
|
||||
};
|
||||
|
@@ -7,21 +7,24 @@
|
||||
#include <JGL/types/RenderTarget.h>
|
||||
|
||||
namespace Engine {
|
||||
class Camera;
|
||||
class Scene;
|
||||
}
|
||||
|
||||
class Engine::Scene {
|
||||
protected:
|
||||
std::string name;
|
||||
Hud* HeadsUpDisplay = nullptr;
|
||||
std::vector<Fixed*> FixedList{};
|
||||
std::vector<Entity*> EntityList{};
|
||||
Hud* heads_up_display = nullptr;
|
||||
Camera* active_camera = nullptr;
|
||||
std::vector<Fixed*> fixed_list{};
|
||||
std::vector<Entity*> entity_list{};
|
||||
public:
|
||||
[[nodiscard]] bool EntityListContains(const Entity* entity) const;
|
||||
[[nodiscard]] bool FixedListContains(const Fixed* fixed) const;
|
||||
[[nodiscard]] size_t FixedCount() const;
|
||||
[[nodiscard]] size_t EntityCount() const;
|
||||
[[nodiscard]] std::string GetName() const;
|
||||
[[nodiscard]] Camera* GetActiveCamera() const;
|
||||
public:
|
||||
void AppendEntity(Entity* entity);
|
||||
void AppendFixed(Fixed* fixed);
|
||||
|
@@ -67,7 +67,6 @@ bool Engine::Entity::AppendChild(Entity* entity) {
|
||||
Engine::Entity::~Entity() {
|
||||
for (auto* e : children)
|
||||
delete e;
|
||||
children = {};
|
||||
}
|
||||
|
||||
void Engine::Entity::UpdateChildren() {
|
||||
|
@@ -1,105 +1,120 @@
|
||||
#include <Engine/Level/Scene.h>
|
||||
#include <Engine/Entity/Camera.h>
|
||||
|
||||
bool Engine::Scene::EntityListContains(const Entity* entity) const {
|
||||
for (auto* e : EntityList)
|
||||
for (auto* e : entity_list)
|
||||
if (e == entity)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Engine::Scene::FixedListContains(const Fixed* fixed) const {
|
||||
for (auto* f : FixedList)
|
||||
for (auto* f : fixed_list)
|
||||
if (f == fixed)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t Engine::Scene::FixedCount() const {
|
||||
return FixedList.size();
|
||||
return fixed_list.size();
|
||||
}
|
||||
|
||||
size_t Engine::Scene::EntityCount() const {
|
||||
return EntityList.size();
|
||||
return entity_list.size();
|
||||
}
|
||||
|
||||
void Engine::Scene::Update() {
|
||||
for (auto& e : EntityList)
|
||||
e->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) {
|
||||
for (auto& f : FixedList)
|
||||
for (auto& f : fixed_list)
|
||||
if (f->Enabled())
|
||||
f->Render();
|
||||
|
||||
// TODO Render order. In this system it's not possible for child entities to be rendered before the parent.
|
||||
|
||||
J2D::Begin(render_target, true);
|
||||
for (auto& e : EntityList)
|
||||
if (auto* r = dynamic_cast<Renderable*>(e))
|
||||
r->Render();
|
||||
|
||||
if (HeadsUpDisplay)
|
||||
HeadsUpDisplay->Render();
|
||||
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();
|
||||
|
||||
if (heads_up_display)
|
||||
heads_up_display->Render();
|
||||
J2D::End();
|
||||
}
|
||||
|
||||
Engine::Scene::~Scene() {
|
||||
for (auto* f : FixedList)
|
||||
for (auto* f : fixed_list)
|
||||
delete f;
|
||||
|
||||
for (auto* e : EntityList)
|
||||
for (auto* e : entity_list)
|
||||
delete e;
|
||||
|
||||
delete HeadsUpDisplay;
|
||||
delete heads_up_display;
|
||||
delete active_camera;
|
||||
}
|
||||
|
||||
void Engine::Scene::Unload() {
|
||||
for (auto* f : FixedList)
|
||||
for (auto* f : fixed_list)
|
||||
delete f;
|
||||
|
||||
for (auto* e : EntityList)
|
||||
for (auto* e : entity_list)
|
||||
delete e;
|
||||
|
||||
delete HeadsUpDisplay;
|
||||
delete heads_up_display;
|
||||
}
|
||||
|
||||
void Engine::Scene::AppendEntity(Entity* entity) {
|
||||
if (!EntityListContains(entity))
|
||||
EntityList.push_back(entity);
|
||||
entity_list.push_back(entity);
|
||||
}
|
||||
|
||||
void Engine::Scene::AppendFixed(Fixed* fixed) {
|
||||
if (!FixedListContains(fixed))
|
||||
FixedList.push_back(fixed);
|
||||
fixed_list.push_back(fixed);
|
||||
}
|
||||
|
||||
void Engine::Scene::DestroyEntity(Entity *entity) {
|
||||
auto it = std::find(EntityList.begin(), EntityList.end(), entity);
|
||||
if (it != EntityList.end())
|
||||
delete *it, EntityList.erase(it);
|
||||
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(FixedList.begin(), FixedList.end(), fixed);
|
||||
if (it != FixedList.end())
|
||||
delete *it, FixedList.erase(it);
|
||||
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(EntityList.begin(), EntityList.end(), entity);
|
||||
if (it != EntityList.end())
|
||||
EntityList.erase(it);
|
||||
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(FixedList.begin(), FixedList.end(), fixed);
|
||||
if (it != FixedList.end())
|
||||
FixedList.erase(it);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@@ -5,6 +5,6 @@
|
||||
void ControllableBox::Init() {
|
||||
auto* hud = new Game::DemoGameHud();
|
||||
auto* b = new Game::Box({0, 0});
|
||||
HeadsUpDisplay = hud;
|
||||
heads_up_display = hud;
|
||||
AppendEntity(b);
|
||||
}
|
||||
|
Reference in New Issue
Block a user