We got GameObjects / Components and shit
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
#include <destrum/Scene/Scene.h>
|
||||
#include <destrum/ObjectModel/GameObject.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <SDL_scancode.h>
|
||||
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
// #include "ServiceLocator.h"
|
||||
// #include "Input/InputManager.h"
|
||||
// #include "Managers/Renderer.h"
|
||||
|
||||
|
||||
unsigned int Scene::m_idCounter = 0;
|
||||
|
||||
Scene::Scene(const std::string& name) : m_name(name) {
|
||||
}
|
||||
|
||||
Scene::~Scene() = default;
|
||||
|
||||
void Scene::Add(std::shared_ptr<GameObject> object) {
|
||||
// m_objects.emplace_back(std::move(object));
|
||||
m_pendingAdditions.emplace_back(std::move(object));
|
||||
}
|
||||
|
||||
void Scene::Remove(const std::shared_ptr<GameObject>& object) {
|
||||
std::erase(m_objects, object);
|
||||
}
|
||||
|
||||
void Scene::RemoveAll() {
|
||||
m_objects.clear();
|
||||
}
|
||||
|
||||
void Scene::Load() {
|
||||
OnSceneLoaded.Invoke();
|
||||
if (m_registerBindings) {
|
||||
m_registerBindings();
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::Update() {
|
||||
if (!m_pendingAdditions.empty()) {
|
||||
for (auto& obj : m_pendingAdditions) {
|
||||
m_objects.emplace_back(std::move(obj));
|
||||
}
|
||||
m_pendingAdditions.clear();
|
||||
}
|
||||
|
||||
|
||||
for (const auto& object: m_objects) {
|
||||
if (object->IsActiveInHierarchy()) {
|
||||
object->Update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::FixedUpdate() {
|
||||
for (const auto& object: m_objects) {
|
||||
if (object->IsActiveInHierarchy()) {
|
||||
object->FixedUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::LateUpdate() {
|
||||
for (const auto& object: m_objects) {
|
||||
if (object->IsActiveInHierarchy()) {
|
||||
object->LateUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::Render(const RenderContext& ctx) const {
|
||||
for (const auto& object: m_objects) {
|
||||
if (object->IsActiveInHierarchy()) {
|
||||
object->Render(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// int width, height;
|
||||
// SDL_GetWindowSize(Renderer::GetInstance().GetSDLWindow(), &width, &height);
|
||||
//
|
||||
// Renderer::GetInstance().RenderLine(
|
||||
// static_cast<float>(width / 2), 0,
|
||||
// static_cast<float>(width / 2), static_cast<float>(height), SDL_Color(255, 0, 0, 255) // Red vertical line
|
||||
// );
|
||||
//
|
||||
// Renderer::GetInstance().RenderLine(
|
||||
// 0, static_cast<float>(height / 2), static_cast<float>(width), static_cast<float>(height / 2), SDL_Color(0, 255, 0, 255) // Green horizontal line
|
||||
// );
|
||||
}
|
||||
|
||||
void Scene::RenderImgui() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Scene::CleanupDestroyedGameObjects() {
|
||||
if (m_BeingUnloaded) {
|
||||
//Scene is gone anyways, kill everything
|
||||
m_objects.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& gameObject: m_objects) {
|
||||
//First check if a gameobjects components needs to be destroyed
|
||||
gameObject->CleanupComponents();
|
||||
}
|
||||
|
||||
// //Strange for loop since im deleting during looping over it
|
||||
// for (auto it = m_objects.begin(); it != m_objects.end();) {
|
||||
// if ((*it)->IsBeingDestroyed()) {
|
||||
// it = m_objects.erase(it);
|
||||
// } else {
|
||||
// ++it;
|
||||
// }
|
||||
// }
|
||||
|
||||
std::erase_if(m_objects, [] (const std::shared_ptr<GameObject>& gameObject) {
|
||||
return gameObject->IsBeingDestroyed();
|
||||
});
|
||||
}
|
||||
|
||||
void Scene::Unload() {
|
||||
if (m_unregisterBindings) {
|
||||
m_unregisterBindings();
|
||||
}
|
||||
m_BeingUnloaded = true;
|
||||
}
|
||||
|
||||
void Scene::DestroyGameObjects() {
|
||||
if (m_BeingUnloaded) {
|
||||
|
||||
for (auto& obj : m_pendingAdditions) {
|
||||
m_objects.emplace_back(std::move(obj));
|
||||
}
|
||||
m_pendingAdditions.clear();
|
||||
|
||||
//Scene is gone anyways, kill everything
|
||||
for (const auto& gameObject: m_objects) {
|
||||
gameObject->Destroy();
|
||||
}
|
||||
} else {
|
||||
assert(true && "Scene is being cleared but not unloaded? Wierd");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user