Skip to content

Commit

Permalink
Added initial native scripting support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCherno committed Aug 13, 2020
1 parent 8a81a7e commit 3177aca
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
1 change: 1 addition & 0 deletions Hazel/src/Hazel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "Hazel/Scene/Scene.h"
#include "Hazel/Scene/Entity.h"
#include "Hazel/Scene/ScriptableEntity.h"
#include "Hazel/Scene/Components.h"

// ---Renderer------------------------
Expand Down
24 changes: 24 additions & 0 deletions Hazel/src/Hazel/Scene/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <glm/glm.hpp>

#include "SceneCamera.h"
#include "ScriptableEntity.h"

namespace Hazel {

Expand Down Expand Up @@ -49,4 +50,27 @@ namespace Hazel {
CameraComponent(const CameraComponent&) = default;
};

struct NativeScriptComponent
{
ScriptableEntity* Instance = nullptr;

std::function<void()> InstantiateFunction;
std::function<void()> DestroyInstanceFunction;

std::function<void(ScriptableEntity*)> OnCreateFunction;
std::function<void(ScriptableEntity*)> OnDestroyFunction;
std::function<void(ScriptableEntity*, Timestep)> OnUpdateFunction;

template<typename T>
void Bind()
{
InstantiateFunction = [&]() { Instance = new T(); };
DestroyInstanceFunction = [&]() { delete (T*)Instance; Instance = nullptr; };

OnCreateFunction = [](ScriptableEntity* instance) { ((T*)instance)->OnCreate(); };
OnDestroyFunction = [](ScriptableEntity* instance) { ((T*)instance)->OnDestroy(); };
OnUpdateFunction = [](ScriptableEntity* instance, Timestep ts) { ((T*)instance)->OnUpdate(ts); };
}
};

}
18 changes: 18 additions & 0 deletions Hazel/src/Hazel/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ namespace Hazel {

void Scene::OnUpdate(Timestep ts)
{
// Update scripts
{
m_Registry.view<NativeScriptComponent>().each([=](auto entity, auto& nsc)
{
if (!nsc.Instance)
{
nsc.InstantiateFunction();
nsc.Instance->m_Entity = Entity{ entity, this };

if (nsc.OnCreateFunction)
nsc.OnCreateFunction(nsc.Instance);
}

if (nsc.OnUpdateFunction)
nsc.OnUpdateFunction(nsc.Instance, ts);
});
}

// Render 2D
Camera* mainCamera = nullptr;
glm::mat4* cameraTransform = nullptr;
Expand Down
21 changes: 21 additions & 0 deletions Hazel/src/Hazel/Scene/ScriptableEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "Entity.h"

namespace Hazel {

class ScriptableEntity
{
public:
template<typename T>
T& GetComponent()
{
return m_Entity.GetComponent<T>();
}
private:
Entity m_Entity;
friend class Scene;
};

}

12 changes: 6 additions & 6 deletions Hazelnut/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ Collapsed=0

[Window][Settings]
Pos=0,19
Size=899,701
Size=353,701
Collapsed=0
DockId=0x00000001,0

[Window][Viewport]
Pos=901,19
Size=379,701
Pos=355,19
Size=925,701
Collapsed=0
DockId=0x00000002,0

[Docking][Data]
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=437,391 Size=1280,701 Split=X Selected=0x995B0CF8
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=899,701 Selected=0x1C33C293
DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=1659,701 CentralNode=1 HiddenTabBar=1 Selected=0x995B0CF8
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=216,258 Size=1280,701 Split=X Selected=0x995B0CF8
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=353,701 Selected=0x1C33C293
DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=925,701 CentralNode=1 HiddenTabBar=1 Selected=0x995B0CF8

30 changes: 30 additions & 0 deletions Hazelnut/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@ namespace Hazel {
m_SecondCamera = m_ActiveScene->CreateEntity("Clip-Space Entity");
auto& cc = m_SecondCamera.AddComponent<CameraComponent>();
cc.Primary = false;

class CameraController : public ScriptableEntity
{
public:
void OnCreate()
{
}

void OnDestroy()
{
}

void OnUpdate(Timestep ts)
{
auto& transform = GetComponent<TransformComponent>().Transform;
float speed = 5.0f;

if (Input::IsKeyPressed(KeyCode::A))
transform[3][0] -= speed * ts;
if (Input::IsKeyPressed(KeyCode::D))
transform[3][0] += speed * ts;
if (Input::IsKeyPressed(KeyCode::W))
transform[3][1] += speed * ts;
if (Input::IsKeyPressed(KeyCode::S))
transform[3][1] -= speed * ts;
}
};

m_CameraEntity.AddComponent<NativeScriptComponent>().Bind<CameraController>();

}

void EditorLayer::OnDetach()
Expand Down

0 comments on commit 3177aca

Please sign in to comment.