We got GameObjects / Components and shit
This commit is contained in:
149
destrum/src/Scene/Scene.cpp
Normal file
149
destrum/src/Scene/Scene.cpp
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
83
destrum/src/Scene/SceneManager.cpp
Normal file
83
destrum/src/Scene/SceneManager.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <destrum/Scene/SceneManager.h>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <destrum/Scene/Scene.h>
|
||||
|
||||
void SceneManager::Update() {
|
||||
m_scenes[m_ActiveSceneIndex]->Update();
|
||||
}
|
||||
|
||||
void SceneManager::FixedUpdate() {
|
||||
m_scenes[m_ActiveSceneIndex]->FixedUpdate();
|
||||
}
|
||||
|
||||
void SceneManager::LateUpdate() {
|
||||
m_scenes[m_ActiveSceneIndex]->LateUpdate();
|
||||
}
|
||||
|
||||
void SceneManager::Render(const RenderContext& ctx) {
|
||||
m_scenes[m_ActiveSceneIndex]->Render(ctx);
|
||||
}
|
||||
|
||||
void SceneManager::RenderImgui() {
|
||||
m_scenes[m_ActiveSceneIndex]->RenderImgui();
|
||||
}
|
||||
|
||||
void SceneManager::HandleGameObjectDestroy() {
|
||||
for (const auto& scene : m_scenes) {
|
||||
scene->CleanupDestroyedGameObjects();
|
||||
}
|
||||
}
|
||||
|
||||
void SceneManager::DestroyGameObjects() {
|
||||
for (const auto& scene: m_scenes) {
|
||||
scene->DestroyGameObjects();
|
||||
}
|
||||
}
|
||||
|
||||
void SceneManager::UnloadAllScenes() {
|
||||
for (const auto& scene : m_scenes) {
|
||||
scene->Unload();
|
||||
}
|
||||
}
|
||||
|
||||
void SceneManager::HandleSceneDestroy() {
|
||||
for (auto it = m_scenes.begin(); it != m_scenes.end();) {
|
||||
if ((*it)->IsBeingUnloaded()) {
|
||||
it = m_scenes.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SceneManager::HandleScene() {
|
||||
DestroyGameObjects();
|
||||
HandleGameObjectDestroy();
|
||||
HandleSceneDestroy();
|
||||
}
|
||||
|
||||
void SceneManager::Destroy() {
|
||||
UnloadAllScenes();
|
||||
DestroyGameObjects();
|
||||
HandleGameObjectDestroy();
|
||||
HandleSceneDestroy();
|
||||
}
|
||||
|
||||
void SceneManager::SwitchScene(int index) {
|
||||
// InputManager::GetInstance().RemoveAllBindings();
|
||||
|
||||
if (index < 0 || index >= static_cast<int>(m_scenes.size())) {
|
||||
throw std::out_of_range("Scene index out of range");
|
||||
}
|
||||
m_scenes[m_ActiveSceneIndex]->UnloadBindings();
|
||||
m_ActiveSceneIndex = index;
|
||||
m_scenes[m_ActiveSceneIndex]->LoadBindings();
|
||||
}
|
||||
|
||||
Scene &SceneManager::CreateScene(const std::string &name) {
|
||||
const auto &scene = std::shared_ptr<Scene>(new Scene(name));
|
||||
m_scenes.push_back(scene);
|
||||
return *scene;
|
||||
}
|
||||
Reference in New Issue
Block a user