From 2cf42c7dea69851c00dd7e83218260782ec0446c Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 24 Jan 2021 02:14:51 +0100 Subject: [PATCH] Implement lua script into script system --- inferno/src/inferno/scene/scene.cpp | 19 ++++---- inferno/src/inferno/systems/script.cpp | 61 ++++++++++++++++++++++---- inferno/src/inferno/systems/script.h | 7 ++- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/inferno/src/inferno/scene/scene.cpp b/inferno/src/inferno/scene/scene.cpp index f562cd2..34989e7 100644 --- a/inferno/src/inferno/scene/scene.cpp +++ b/inferno/src/inferno/scene/scene.cpp @@ -24,12 +24,6 @@ namespace Inferno { cameraSystem->initialize(); CameraSystem::the().setRegistry(m_registry); - uint32_t camera = createEntity("Camera Entity"); - auto& cameraTransform = getComponent(camera); - cameraTransform.translate.z = 1.0f; - addComponent(camera, CameraType::Perspective); - addComponent(camera).bind(); - RenderSystem* renderSystem = new RenderSystem(); renderSystem->initialize(); RenderSystem::the().setRegistry(m_registry); @@ -47,6 +41,14 @@ namespace Inferno { // Construct entities // --------------------------------- + uint32_t camera = createEntity("Camera Entity"); + auto& cameraTransform = getComponent(camera); + cameraTransform.rotate.z = -1.0f; + cameraTransform.translate.z = 1.0f; + addComponent(camera, CameraType::Perspective); + addComponent(camera).bind(); + // addComponent(camera, "assets/lua/cameracontroller.lua"); + uint32_t quad = createEntity("Quad"); addComponent(quad, glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture); @@ -95,10 +97,7 @@ namespace Inferno { void Scene::destroyEntity(uint32_t entity) { - if (hasComponent(entity)) { - ScriptSystem::the().cleanup(entity); - } - + ScriptSystem::the().cleanup(entity); m_registry->destroy(entt::entity { entity }); } diff --git a/inferno/src/inferno/systems/script.cpp b/inferno/src/inferno/systems/script.cpp index f98d3f1..dd9e803 100644 --- a/inferno/src/inferno/systems/script.cpp +++ b/inferno/src/inferno/systems/script.cpp @@ -2,9 +2,10 @@ #include "inferno/assertions.h" #include "inferno/scene/components.h" +#include "inferno/scene/scene.h" +#include "inferno/script/lua.h" #include "inferno/script/nativescript.h" #include "inferno/systems/script.h" -#include namespace Inferno { @@ -20,11 +21,16 @@ namespace Inferno { void ScriptSystem::destroy() { - auto view = m_scene->registry()->view(); + auto nativeScriptView = m_scene->registry()->view(); - // @Todo change the getComponent() call to retrieve from the view for more performance - for (auto entity : view) { - cleanup(static_cast(entity)); + for (auto entity : nativeScriptView) { + cleanup(nativeScriptView.get(entity)); + } + + auto luaScriptView = m_scene->registry()->view(); + + for (auto entity : luaScriptView) { + cleanup(luaScriptView.get(entity)); } delete s_instance; @@ -34,11 +40,11 @@ namespace Inferno { void ScriptSystem::update(float deltaTime) { // @Todo figure out why group doesn't work here - auto view = m_scene->registry()->view(); + auto nativeScriptView = m_scene->registry()->view(); - for (auto [entity, transform, nativeScript] : view.each()) { + for (auto [entity, transform, nativeScript] : nativeScriptView.each()) { - // Create script if not initialized + // Create native script if not initialized if (!nativeScript.instance) { nativeScript.instance = nativeScript.initialize(); nativeScript.instance->transform = &transform; @@ -50,16 +56,53 @@ namespace Inferno { nativeScript.instance->transform = &transform; nativeScript.instance->update(deltaTime); } + + auto luaScriptView = m_scene->registry()->view(); + + for (auto [entity, transform, luaScript] : luaScriptView.each()) { + + // Create Lua script if not initialized + if (!luaScript.instance) { + luaScript.instance = new Lua(); + luaScript.instance->transform = &transform; + luaScript.instance->m_scene = m_scene; + luaScript.instance->m_entity = static_cast(entity); + luaScript.instance->m_path = luaScript.path; + luaScript.instance->initialize(); + } + + luaScript.instance->update(deltaTime); + } + } void ScriptSystem::cleanup(uint32_t entity) { - auto& nativeScript = m_scene->getComponent(entity); + if (m_scene->hasComponent(entity)) { + auto& nativeScript = m_scene->getComponent(entity); + cleanup(nativeScript); + } + if (m_scene->hasComponent(entity)) { + auto& luaScript = m_scene->getComponent(entity); + cleanup(luaScript); + } + } + + void ScriptSystem::cleanup(NativeScriptComponent& nativeScript) + { if (nativeScript.instance) { nativeScript.instance->destroy(); nativeScript.destroy(); } } + void ScriptSystem::cleanup(LuaScriptComponent& luaScript) + { + if (luaScript.instance) { + luaScript.instance->destroy(); + delete luaScript.instance; + } + } + } diff --git a/inferno/src/inferno/systems/script.h b/inferno/src/inferno/systems/script.h index c61a5c7..4421af0 100644 --- a/inferno/src/inferno/systems/script.h +++ b/inferno/src/inferno/systems/script.h @@ -1,10 +1,13 @@ #ifndef SCRIPT_SYSTEM_H #define SCRIPT_SYSTEM_H -#include //std::shared_ptr +#include // uint32_t namespace Inferno { + struct NativeScriptComponent; + struct LuaScriptComponent; + class Scene; class ScriptSystem { @@ -14,6 +17,8 @@ namespace Inferno { void update(float deltaTime); void cleanup(uint32_t entity); + void cleanup(NativeScriptComponent& nativeScript); + void cleanup(LuaScriptComponent& luaScript); void setScene(Scene* scene) { m_scene = scene; }