Skip to content

Commit

Permalink
Massive scripting overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Aug 8, 2023
1 parent 8d01d24 commit 7002d14
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 230 deletions.
2 changes: 2 additions & 0 deletions Arcane/src/Arcane/ECS/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ namespace Arcane
{
glm::vec3 color = {1.0f, 1.0f, 1.0f};
Texture* sprite = nullptr;
bool flipX = false;
bool flipY = false;
};

struct RigidBodyComponent
Expand Down
30 changes: 27 additions & 3 deletions Arcane/src/Arcane/Renderer/SceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,20 @@ namespace Arcane

for (int i = 0; i < quadSize; i++) {
QuadVertex v;
v.position = translation * rotation * scale * s_Data.quadBase[i];

glm::vec4 quadVertex = s_Data.quadBase[i];
if (spriteRendererComponent.flipX == true)
{
quadVertex.x *= -1;
}

if (spriteRendererComponent.flipY == true)
{
quadVertex.y *= -1;
}


v.position = translation * rotation * scale * quadVertex;
v.color = spriteRendererComponent.color;
v.texCoord = textureCoords[i];
v.texId = textureId;
Expand All @@ -643,10 +656,21 @@ namespace Arcane
};

std::vector<AnimatedQuadVertex> vertices;

for (int i = 0; i < quadSize; i++) {
AnimatedQuadVertex v;
v.position = translation * rotation * scale * s_Data.quadBase[i];

glm::vec4 quadVertex = s_Data.quadBase[i];
if (spriteRendererComponent.flipX == true)
{
quadVertex.x *= -1;
}

if (spriteRendererComponent.flipY == true)
{
quadVertex.y *= -1;
}

v.position = translation * rotation * scale * quadVertex;
v.color = spriteRendererComponent.color;
v.texCoord = textureCoords[i];
vertices.push_back(v);
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ namespace Arcane
Script* script = scriptComponent.script;

if (scriptComponent.updateProperties == true) {
script->LoadScriptProperites();
script->WriteProperties();
scriptComponent.updateProperties = false;
m_Registry.emplace_or_replace<ScriptComponent>(entity, scriptComponent);
}
Expand Down
124 changes: 24 additions & 100 deletions Arcane/src/Arcane/Scripting/Script.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Script.h"
#include "Arcane/Scene/Scene.h"
#include "Arcane/ECS/Entity.h"

#include "ScriptGlue.h"
namespace Arcane
{
static void PrintStack(lua_State* L)
Expand Down Expand Up @@ -40,52 +40,10 @@ namespace Arcane
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)
{
lua_settop(ScriptingEngine::GetLuaState(), 0);

// Places the entity table on the lua stack
luaL_dofile(ScriptingEngine::GetLuaState(), name.c_str());

Expand All @@ -98,103 +56,69 @@ namespace Arcane
void Script::OnStart()
{
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_StartIndex);
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_EntityIdIndex);
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_ObjectIndex);
lua_pcall(ScriptingEngine::GetLuaState(), 1, 0, 0);
}

void Script::OnUpdate(float deltaTime)
{
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_UpdateIndex);
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_EntityIdIndex);
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_ObjectIndex);
lua_pushnumber(ScriptingEngine::GetLuaState(), deltaTime);
lua_pcall(ScriptingEngine::GetLuaState(), 2, 0, 0);
}

void Script::SetEntity(Entity& entity)
{
lua_State* L = ScriptingEngine::GetLuaState();
lua_newtable(L);
int entityTableIdx = lua_gettop(L);
PrintStack(L);

Entity* entityPtr = (Entity*)lua_newuserdata(L, sizeof(Entity));
*entityPtr = entity;
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_ObjectIndex);
PrintStack(L);

lua_setfield(L, -2, "EntityId");
Entity* entityUserData = (Entity*)lua_newuserdata(L, sizeof(Entity));
*entityUserData = entity;
PrintStack(L);

lua_pushcfunction(L, ScriptingEngine::GetComponent);
PrintStack(L);

lua_setfield(L, -2, "GetComponent");
PrintStack(L);

lua_pushcfunction(L, ScriptingEngine::HasComponent);
PrintStack(L);

lua_setfield(L, -2, "HasComponent");
PrintStack(L);

lua_pushcfunction(L, ScriptingEngine::SetTransform);
PrintStack(L);

lua_setfield(L, -2, "SetTranslation");
PrintStack(L);

lua_rawgeti(L, LUA_REGISTRYINDEX, m_ObjectIndex);
lua_setfield(L, -2, "EntityId");
PrintStack(L);

// Copy Across Properties
lua_getfield(L, -1, "Properties");
luaL_setmetatable(L, "EntityMetatable");
PrintStack(L);

lua_setfield(L, -3, "Properties");
// Get Start and Update Methods
lua_getfield(L, -1, "OnStart");
m_StartIndex = luaL_ref(L, LUA_REGISTRYINDEX);
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);
lua_getfield(L, -1, "OnUpdate");
m_UpdateIndex = luaL_ref(L, LUA_REGISTRYINDEX);
PrintStack(L);

lua_pushstring(L, "__index");
PrintStack(L);
lua_pushcfunction(L, l_Entity_Index);
// Set Entity Specific Methods
lua_pushcfunction(L, ScriptGlue::GetComponent);
PrintStack(L);
lua_settable(L, -3);


// Set Metatable
lua_setmetatable(L, -2);
lua_setfield(L, -2, "GetComponent");
PrintStack(L);

// Get OnStart and Update metamethods
luaL_getmetafield(L, -1, "OnStart");
PrintStack(L);
m_StartIndex = luaL_ref(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX);
lua_pushcfunction(L, ScriptGlue::SetComponent);
PrintStack(L);

luaL_getmetafield(L, -1, "OnUpdate");
lua_setfield(L, -2, "SetComponent");
PrintStack(L);
m_UpdateIndex = luaL_ref(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX);
PrintStack(L);

m_EntityIdIndex = luaL_ref(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX);

lua_settop(L, 0);
LoadProperties();
ReadProperties();
}

void Script::LoadScriptProperites()
void Script::WriteProperties()
{
// Clear lua stack
lua_settop(ScriptingEngine::GetLuaState(), 0);

// need to push object onto stack
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_EntityIdIndex);
lua_rawgeti(ScriptingEngine::GetLuaState(), LUA_REGISTRYINDEX, m_ObjectIndex);

// Get properties
lua_pushstring(ScriptingEngine::GetLuaState(), "Properties");
Expand Down Expand Up @@ -254,11 +178,11 @@ namespace Arcane
}
}

void Script::LoadProperties()
void Script::ReadProperties()
{
lua_State* L = ScriptingEngine::GetLuaState();
PrintStack(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, m_EntityIdIndex);
lua_rawgeti(L, LUA_REGISTRYINDEX, m_ObjectIndex);
PrintStack(L);

lua_getfield(L, -1, "Properties");
Expand Down
4 changes: 2 additions & 2 deletions Arcane/src/Arcane/Scripting/Script.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ namespace Arcane
std::unordered_map<std::string, ScriptProperty> GetProperties() { return m_Properties; }
void SetPropertyValue(std::string key, std::any newValue) { m_Properties[key].value = newValue; }

void LoadScriptProperites();
void WriteProperties();
void SetEntity(Entity& entity);
private:
void LoadProperties();
void ReadProperties();
private:
lua_State* m_LuaState;
std::unordered_map<std::string, ScriptProperty> m_Properties;
Expand Down

0 comments on commit 7002d14

Please sign in to comment.