Skip to content

Commit

Permalink
Working on transform component
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Jul 19, 2023
1 parent 8e368c4 commit e92302d
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions Arcane/src/Arcane/Scripting/ScriptGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace Arcane
{
CreateVec2Table(L);
CreateVec3Table(L);
CreateEntityIdTable(L);
CreateTransformComponentTable(L);
}

void ScriptGlue::RegisterFunctions(lua_State* L)
Expand All @@ -84,13 +84,6 @@ namespace Arcane

lua_pushcfunction(L, l_InputManager_GetKeyPressed);
lua_setglobal(L, "GetKeyPressed");

// Component functions
lua_pushcfunction(L, ScriptingEngine::GetComponent);
lua_setglobal(L, "GetComponent");

lua_pushcfunction(L, ScriptingEngine::SetTransform);
lua_setglobal(L, "SetTransform");
}

void ScriptGlue::RegisterMetatables(lua_State* L)
Expand Down Expand Up @@ -162,7 +155,6 @@ namespace Arcane
lua_pushcfunction(L, Vector2NewIndex);
lua_settable(L, -3);


lua_pushstring(L, "__tostring");
lua_pushcfunction(L, Vec2ToString);
lua_settable(L, -3);
Expand Down Expand Up @@ -249,10 +241,50 @@ namespace Arcane

void ScriptGlue::CreateTransformComponentMetatable(lua_State* L)
{
luaL_newmetatable(L, "TransformComponentMetatable");

auto TransformComponentIndex = [](lua_State* L) -> int {
TransformComponent* transformComponent = (TransformComponent*)lua_touserdata(L, -2);
const char* index = lua_tostring(L, -1);

if (strcmp(index, "translation") == 0) {
// Create Transform Vector
glm::vec3* translation = (glm::vec3*)lua_newuserdata(L, sizeof(glm::vec3));
translation->x = transformComponent->pos.x;
translation->y = transformComponent->pos.y;
translation->z = transformComponent->pos.z;



return 1;
}
else {
lua_getglobal(L, "TransformComponent");
lua_pushstring(L, index);
lua_rawget(L, -2);
return 1;
}
};

}

void ScriptGlue::CreateTransformComponentTable(lua_State* L)
{
lua_newtable(L);
int transformComponentTable = lua_gettop(L);
lua_pushvalue(L, transformComponentTable);
lua_setglobal(L, "TransformComponent");

auto CreateTransformComponent = [](lua_State* L) -> int {
void* ptrToTransformComponent = lua_newuserdata(L, sizeof(TransformComponent));
new (ptrToTransformComponent) TransformComponent();
luaL_getmetatable(L, "TransformComponentMetatable");
lua_setmetatable(L, -2);
return 1;
};

lua_pushcfunction(L, CreateTransformComponent);
lua_setfield(L, -2, "new");
}

void ScriptGlue::CreateEntityIdTable(lua_State* L)
Expand Down

0 comments on commit e92302d

Please sign in to comment.