Skip to content

Commit

Permalink
Can change translation of object now from lua script
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Jul 25, 2023
1 parent 68c8084 commit 4280f12
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 24 deletions.
12 changes: 12 additions & 0 deletions Arcane/src/Arcane/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ namespace Arcane
// Update Physics Engine
m_PhysicsWorld->Step(1.0f / 60.0f);

// Sprite Render Component
{
auto view = m_Registry.view<TransformComponent, SpriteRendererComponent>();

for (auto& entity : view)
{
auto& spriteRendererComponent = view.get<SpriteRendererComponent>(entity);
auto& transformComponent = view.get<TransformComponent>(entity);
m_SceneRenderer->SubmitQuad(transformComponent, spriteRendererComponent);
}
}

// Render Mesh
{
auto view = m_Registry.view<MeshComponent, TransformComponent, MeshRendererComponent>();
Expand Down
59 changes: 58 additions & 1 deletion Arcane/src/Arcane/Scripting/Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,49 @@ namespace Arcane

str = str + "\n";
std::cout << str;
}

static int l_Entity_Index(lua_State* L)
{
const char* index = lua_tostring(L, -1);
lua_getfield(L, -2, "EntityId");

Entity entity = *(Entity*)lua_touserdata(L, -1);

if (strcmp(index, "translation") == 0)
{
glm::vec3* newValue = (glm::vec3*)lua_newuserdata(L, sizeof(glm::vec3));

TransformComponent& transformComponent = entity.GetComponent<TransformComponent>();
newValue->x = transformComponent.pos.x;
newValue->y = transformComponent.pos.y;
newValue->z = transformComponent.pos.z;

luaL_getmetatable(L, "Vector3Metatable");
lua_setmetatable(L, -2);

return 1;
}

return 1;
}

static int l_Entity_NewIndex(lua_State* L)
{
glm::vec3* vec3 = (glm::vec3*)lua_touserdata(L, -1);
const char* index = lua_tostring(L, -2);

lua_getfield(L, -3, "EntityId");
Entity entity = *(Entity*)lua_touserdata(L, -1);

if (strcmp(index, "translation") == 0)
{
entity.GetComponent<TransformComponent>().pos.x = vec3->x;
entity.GetComponent<TransformComponent>().pos.y = vec3->y;
entity.GetComponent<TransformComponent>().pos.z = vec3->z;
}

return 0;
}

Script::Script(std::string name)
Expand Down Expand Up @@ -99,7 +142,6 @@ namespace Arcane
lua_setfield(L, -2, "SetTranslation");
PrintStack(L);


lua_rawgeti(L, LUA_REGISTRYINDEX, m_ObjectIndex);
PrintStack(L);

Expand All @@ -110,6 +152,21 @@ namespace Arcane
lua_setfield(L, -3, "Properties");
PrintStack(L);

// Add Extra Metamethods like index and newindex to entity (at this point script table is at the top (-1))
lua_pushstring(L, "__newindex");
PrintStack(L);
lua_pushcfunction(L, l_Entity_NewIndex);
PrintStack(L);
lua_settable (L, -3);
PrintStack(L);

lua_pushstring(L, "__index");
PrintStack(L);
lua_pushcfunction(L, l_Entity_Index);
PrintStack(L);
lua_settable(L, -3);


// Set Metatable
lua_setmetatable(L, -2);
PrintStack(L);
Expand Down
1 change: 1 addition & 0 deletions Arcane/src/Arcane/Scripting/ScriptGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace Arcane
return 1;
}


void ScriptGlue::RegisterTables(lua_State* L)
{
CreateVec2Table(L);
Expand Down
32 changes: 16 additions & 16 deletions EnchantingTable/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DockId=0x39930E19,0

[Window][DockSpace Demo]
Pos=0,0
Size=1920,974
Size=1200,800
Collapsed=0

[Window][Entity Panel]
Expand All @@ -33,14 +33,14 @@ Collapsed=0
DockId=0x00000005,0

[Window][File Browser]
Pos=287,681
Size=1288,293
Pos=166,460
Size=802,340
Collapsed=0
DockId=0x00000008,0
DockId=0x0000000D,0

[Window][Viewport]
Pos=287,24
Size=1288,655
Pos=166,24
Size=802,434
Collapsed=0
DockId=0x00000007,0

Expand All @@ -51,8 +51,8 @@ Collapsed=0
DockId=0x00000009,0

[Window][Entity]
Pos=1577,24
Size=343,950
Pos=970,24
Size=230,776
Collapsed=0
DockId=0x0000000C,0

Expand All @@ -64,25 +64,25 @@ DockId=0x0000000C,1

[Window][Default Scene]
Pos=0,24
Size=285,950
Size=164,776
Collapsed=0
DockId=0x0000000F,0

[Docking][Data]
DockSpace ID=0x39930E19 Pos=184,152 Size=1072,842 CentralNode=1 Selected=0x4A17B156
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,24 Size=1920,950 Split=X
DockNode ID=0x0000000F Parent=0x3BC79352 SizeRef=285,950 Selected=0xB8946031
DockNode ID=0x00000010 Parent=0x3BC79352 SizeRef=1633,950 Split=X
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,24 Size=1200,776 Split=X
DockNode ID=0x0000000F Parent=0x3BC79352 SizeRef=164,950 Selected=0xB8946031
DockNode ID=0x00000010 Parent=0x3BC79352 SizeRef=1034,950 Split=X
DockNode ID=0x00000009 Parent=0x00000010 SizeRef=253,1181 Selected=0x18B8C0DE
DockNode ID=0x0000000A Parent=0x00000010 SizeRef=1665,1181 Split=X
DockNode ID=0x0000000B Parent=0x0000000A SizeRef=1288,1358 Split=X
DockNode ID=0x0000000B Parent=0x0000000A SizeRef=802,1358 Split=X
DockNode ID=0x00000003 Parent=0x0000000B SizeRef=1857,1181 Split=X
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=409,1181 Selected=0x18F2AEBA
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=1774,1181 Split=X
DockNode ID=0x00000001 Parent=0x00000006 SizeRef=1262,1181 Split=Y Selected=0x995B0CF8
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=1178,690 CentralNode=1 Selected=0x995B0CF8
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=1178,293 Selected=0x2389D759
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=761,434 CentralNode=1 Selected=0x995B0CF8
DockNode ID=0x0000000D Parent=0x00000001 SizeRef=761,340 Selected=0x2389D759
DockNode ID=0x00000002 Parent=0x00000006 SizeRef=336,1181 Selected=0x4A17B156
DockNode ID=0x00000004 Parent=0x0000000B SizeRef=360,1181 Selected=0x2DDDAD1E
DockNode ID=0x0000000C Parent=0x0000000A SizeRef=343,1358 Selected=0x0727BC04
DockNode ID=0x0000000C Parent=0x0000000A SizeRef=230,1358 Selected=0x0984415E

14 changes: 7 additions & 7 deletions EnchantingTable/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ void EditorLayer::OnImGuiRender()
}

if (Arcane::InputManager::GetKeyReleased(32)) {
m_SceneRenderer->ReadGeometryFramebufferPixel(1);
//if (m_State == SceneState::EDIT) {
// OnScenePlay();
//}
//else if (m_State == SceneState::PLAY) {
// OnSceneStop();
//}
//m_SceneRenderer->ReadGeometryFramebufferPixel(1);
if (m_State == SceneState::EDIT) {
OnScenePlay();
}
else if (m_State == SceneState::PLAY) {
OnSceneStop();
}
}
}

Expand Down

0 comments on commit 4280f12

Please sign in to comment.