Browse Source

Add script system

master
Riyyi 4 years ago
parent
commit
f09e998d52
  1. 19
      inferno/src/inferno/scene/scene.cpp
  2. 65
      inferno/src/inferno/systems/script.cpp
  3. 30
      inferno/src/inferno/systems/script.h

19
inferno/src/inferno/scene/scene.cpp

@ -5,6 +5,7 @@
#include "inferno/script/nativescript.h"
#include "inferno/systems/camera.h"
#include "inferno/systems/render.h"
#include "inferno/systems/script.h"
#include "inferno/systems/transform.h"
namespace Inferno {
@ -34,6 +35,10 @@ namespace Inferno {
renderSystem->initialize();
RenderSystem::the().setRegistry(m_registry);
ScriptSystem* scriptSystem = new ScriptSystem();
scriptSystem->initialize();
ScriptSystem::the().setScene(this);
// Load assets
// ---------------------------------
@ -61,7 +66,7 @@ namespace Inferno {
void Scene::update(float deltaTime)
{
(void)deltaTime;
ScriptSystem::the().update(deltaTime);
TransformSystem::the().update();
CameraSystem::the().update();
@ -74,6 +79,7 @@ namespace Inferno {
void Scene::destroy()
{
ScriptSystem::the().destroy();
RenderSystem::the().destroy();
CameraSystem::the().destroy();
TransformSystem::the().destroy();
@ -88,14 +94,13 @@ namespace Inferno {
return entity;
}
Entity Scene::createEntity(entt::entity handle)
void Scene::destroyEntity(uint32_t entity)
{
return Entity(m_registry, handle);
}
if (hasComponent<NativeScript>(entity)) {
ScriptSystem::the().cleanup(entity);
}
Entity Scene::createEntity(uint32_t handle)
{
return Entity(m_registry, handle);
m_registry->destroy(entt::entity { entity });
}
glm::mat4 Scene::cameraProjectionView()

65
inferno/src/inferno/systems/script.cpp

@ -0,0 +1,65 @@
#include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "inferno/assertions.h"
#include "inferno/scene/components.h"
#include "inferno/script/nativescript.h"
#include "inferno/systems/script.h"
#include <bits/stdint-uintn.h>
namespace Inferno {
ScriptSystem* ScriptSystem::s_instance = nullptr;
void ScriptSystem::initialize()
{
ASSERT(!s_instance, "ScriptSystem already exists!");
s_instance = this;
dbg(Log::Info) << "ScriptSystem initialized";
}
void ScriptSystem::destroy()
{
auto view = m_scene->registry()->view<NativeScriptComponent>();
// @Todo change the getComponent() call to retrieve from the view for more performance
for (auto entity : view) {
cleanup(static_cast<uint32_t>(entity));
}
delete s_instance;
s_instance = nullptr;
}
void ScriptSystem::update(float deltaTime)
{
// @Todo figure out why group doesn't work here
auto view = m_scene->registry()->view<TransformComponent, NativeScriptComponent>();
for (auto [entity, transform, nativeScript] : view.each()) {
// Create script if not initialized
if (!nativeScript.instance) {
nativeScript.initialize();
nativeScript.instance->transform = &transform;
nativeScript.instance->m_scene = m_scene;
nativeScript.instance->m_entity = static_cast<uint32_t>(entity);
nativeScript.instance->initialize();
}
nativeScript.instance->transform = &transform;
nativeScript.instance->update(deltaTime);
}
}
void ScriptSystem::cleanup(uint32_t entity)
{
auto& nativeScript = m_scene->getComponent<NativeScriptComponent>(entity);
if (!nativeScript.instance) {
nativeScript.instance->destroy();
nativeScript.destroy();
}
}
}

30
inferno/src/inferno/systems/script.h

@ -0,0 +1,30 @@
#ifndef SCRIPT_SYSTEM_H
#define SCRIPT_SYSTEM_H
#include <memory> //std::shared_ptr
namespace Inferno {
class Scene;
class ScriptSystem {
public:
void initialize();
void destroy();
void update(float deltaTime);
void cleanup(uint32_t entity);
void setScene(Scene* scene) { m_scene = scene; }
static inline ScriptSystem& the() { return *s_instance; }
private:
Scene* m_scene;
static ScriptSystem* s_instance;
};
}
#endif // SCRIPT_SYSTEM_H
Loading…
Cancel
Save