This commit is contained in:
2026-03-05 04:31:17 +03:00
parent 4290e99c61
commit 33104c3bca
15 changed files with 409 additions and 120 deletions

View File

@@ -67,6 +67,7 @@ void registerClasses()
// base thing
GetLuaState().GetGlobals().RegisterDirect("load_script", &luaLoadScript);
GetLuaState().DoString("g_factory = {}");
GetLuaState().DoString("g_entities = {}");
registerEngine();
}
@@ -196,11 +197,8 @@ void Game::LoadLevelXML(const char* mapname)
}
}
IEntityBase* entity = Lua_CreateEntity(classname.as_string());
// this is pure C++ entity :)
if (!entity)
entity = g_entityManager->CreateEntity(classname.as_string());
// Create entity and expose it to the engine
Entity* entity = static_cast<Entity*>(Lua_CreateEntity(classname.as_string()));
pugi::xml_node position = entitynode.child("Position");
if (position)
@@ -212,6 +210,22 @@ void Game::LoadLevelXML(const char* mapname)
entity->SetPosition(glm::vec3(x, y, z));
}
pugi::xml_node model = entitynode.child("Model");
if (model)
{
const char* filename = model.attribute("filename").as_string();
if (filename)
entity->LoadModel(filename);
}
pugi::xml_node physics = entitynode.child("Physics");
if (physics)
{
bool value = physics.attribute("value").as_bool();
if (value)
entity->CreateTestBody();
}
//IEntityBase* entity = g_entityManager->CreateEntity(classname.as_string());
g_world->AddEntity(entity);
}
@@ -221,13 +235,62 @@ IEntityBase* Game::Lua_CreateEntity(const char* classname)
{
using namespace LuaPlus;
//if (!classname)
// classname = "Entity";
SDL_assert_always(classname);
LuaPrototype* pluaprototype = nullptr;
// find prototype
LuaPrototype* luaprototype = Lua_FindPrototype(classname);
// create an entity
Entity* entity = nullptr;
if (luaprototype)
{
entity = static_cast<Entity*>(g_entityManager->CreateEntity(luaprototype->m_enginename.c_str()));
// override classname because of entity system is so dumb
entity->SetClassname(luaprototype->m_luaname.c_str());
}
else
{
entity = static_cast<Entity*>(g_entityManager->CreateEntity(classname));
}
SDL_assert_always(entity);
// get the factrory
LuaObject factory = GetLuaState().GetGlobal("g_factory");
// and the lookup table
LuaObject lookup = GetLuaState().GetGlobal("g_entities");
// generate name
char entityname[256];
snprintf(entityname, sizeof(entityname), "%s_%d", classname, factory.GetTableCount());
// create an table
LuaObject entityTable = GetLuaState().CreateTable();
entityTable.SetString("m_name", entityname);
entityTable.SetInteger("m_id", entity->GetID());
// assign prototype
if (luaprototype)
{
LuaObject prototype = GetLuaState().GetGlobal(luaprototype->m_luaname.c_str());
SDL_assert_always(!prototype.IsNil());
entityTable.SetMetatable(prototype);
}
// link to the entity
entity->InitFromTable(entityTable);
// push in to the factory
factory.SetObject(entityname, entityTable);
lookup.SetObject(entity->GetID(), entityTable);
return entity;
}
LuaPrototype* Game::Lua_FindPrototype(const char* classname)
{
// find a prototype
for (std::vector<LuaPrototype>::iterator it = g_prototypes.begin();
it != g_prototypes.end();
@@ -235,42 +298,9 @@ IEntityBase* Game::Lua_CreateEntity(const char* classname)
{
if (strcmp((*it).m_luaname.c_str(), classname) == 0)
{
pluaprototype = &(*it);
break;
return &(*it);
}
}
if (pluaprototype)
{
Entity* entity = static_cast<Entity*>(g_entityManager->CreateEntity(pluaprototype->m_enginename.c_str()));
SDL_assert_always(entity);
// get a prototype
LuaObject prototype = GetLuaState().GetGlobal(pluaprototype->m_luaname.c_str());
SDL_assert_always(!prototype.IsNil());
//prototype.SetObject("__index", prototype);
// generate table
LuaObject factory = GetLuaState().GetGlobal("g_factory");
// generate name
std::string entityname = pluaprototype->m_luaname + "_" + std::to_string(factory.GetTableCount());
// create an table
LuaObject entityTable = GetLuaState().GetGlobals().CreateTable(entityname.c_str());
entityTable.SetMetatable(prototype);
entityTable.SetString("m_name", entityname.c_str());
// push in to the factory
factory.SetObject(entityname.c_str(), entityTable);
// link to the entity
entity->InitFromTable(entityTable);
return entity;
}
//IEntityBase* entity = g_entityManager->CreateEntity(classname);
return nullptr;
}
@@ -278,3 +308,37 @@ IEntityBase* Game::Lua_CreateEntity(const char* classname)
void Game::Shutdown()
{
}
//LuaPrototype* pluaprototype = Lua_FindPrototype(classname);
//
//if (pluaprototype)
//{
// Entity* entity = static_cast<Entity*>(g_entityManager->CreateEntity(pluaprototype->m_enginename.c_str()));
// SDL_assert_always(entity);
// // get a prototype
// LuaObject prototype = GetLuaState().GetGlobal(pluaprototype->m_luaname.c_str());
// SDL_assert_always(!prototype.IsNil());
// //prototype.SetObject("__index", prototype);
// // generate table
// LuaObject factory = GetLuaState().GetGlobal("g_factory");
// // generate name
// std::string entityname = pluaprototype->m_luaname + "_" + std::to_string(factory.GetTableCount());
//
// // create an table
// LuaObject entityTable = GetLuaState().GetGlobals().CreateTable(entityname.c_str());
// entityTable.SetMetatable(prototype);
// entityTable.SetString("m_name", entityname.c_str());
//
// // push in to the factory
// factory.SetObject(entityname.c_str(), entityTable);
// // link to the entity
// entity->InitFromTable(entityTable);
// return entity;
//}
//IEntityBase* entity = g_entityManager->CreateEntity(classname);