Converting vehicle and building workflow to blender2ogre

This commit is contained in:
2025-09-20 05:52:24 +03:00
parent 62e14cf075
commit e967844558
143 changed files with 12687 additions and 51 deletions

View File

@@ -109,6 +109,14 @@ int luaLibraryLoader(lua_State *L)
return 1;
}
struct LuaChildEventTrigger {
flecs::entity parent_e;
Ogre::Vector3 position;
float halfheight;
float radius;
Ogre::String event;
};
static void installLibraryLoader(lua_State *L)
{
// Insert the c++ func 'luaLibraryLoader' into package.loaders.
@@ -262,8 +270,11 @@ LuaData::LuaData()
Ogre::Quaternion orientation(Ogre::Radian(yaw),
Ogre::Vector3(0, 1, 0));
Ogre::Vector3 position(x, y, z);
e.set<BoatType>({ "boat.glb", position, orientation });
lua_pushinteger(L, idmap.add_entity(e));
e.set<BoatType>(
{ "boat.scene", position, orientation });
int ret = idmap.add_entity(e);
lua_pushinteger(L, ret);
std::cout << "boat created: " << ret << std::endl;
return 1;
}
lua_pushinteger(L, -1);
@@ -301,23 +312,18 @@ LuaData::LuaData()
luaL_checktype(L, 6, LUA_TNUMBER); // halfh
luaL_checktype(L, 7, LUA_TNUMBER); // radius
int parent = lua_tointeger(L, 1);
std::cout << "parent: " << parent << std::endl;
flecs::entity parent_e = idmap.get_entity(parent);
Ogre::SceneNode *parentNode = nullptr;
if (parent_e.has<CharacterBase>())
parentNode = parent_e.get<CharacterBase>().mBodyNode;
else if (parent_e.has<BoatBase>())
parentNode = parent_e.get<BoatBase>().mNode;
flecs::entity e = ECS::get().entity().child_of(parent_e);
Ogre::String event = lua_tostring(L, 2);
float x = lua_tonumber(L, 3);
float y = lua_tonumber(L, 4);
float z = lua_tonumber(L, 5);
float h = lua_tonumber(L, 6);
float r = lua_tonumber(L, 7);
OgreAssert(parentNode, "bad parent");
Ogre::Vector3 position(x, y, z);
e.set<EventTrigger>({ parentNode, position, h, r, event });
flecs::entity e = ECS::get().entity().child_of(parent_e);
e.set<LuaChildEventTrigger>(
{ parent_e, position, h, r, event });
lua_pushinteger(L, idmap.add_entity(e));
return 1;
});
@@ -345,6 +351,54 @@ LuaData::LuaData()
return 1;
});
lua_setglobal(L, "ecs_npc_set");
lua_pushcfunction(L, [](lua_State *L) -> int {
OgreAssert(lua_gettop(L) == 1, "Bad parameters");
luaL_checktype(L, 1, LUA_TSTRING); // type
const char *fileName = lua_tostring(L, 1);
ECS::get<EngineData>().mScnMgr->getRootSceneNode()->saveChildren(
fileName);
return 0;
});
lua_setglobal(L, "ecs_save_scene_debug");
lua_pushcfunction(L, [](lua_State *L) -> int {
OgreAssert(lua_gettop(L) == 2, "Bad parameters");
luaL_checktype(L, 1, LUA_TNUMBER); // object
luaL_checktype(L, 2, LUA_TSTRING); // name
int object = lua_tointeger(L, 1);
flecs::entity object_e = idmap.get_entity(object);
const char *fileName = lua_tostring(L, 2);
Ogre::SceneNode *node = nullptr;
if (object_e.has<CharacterBase>())
node = object_e.get<CharacterBase>().mBodyNode;
else if (object_e.has<BoatBase>())
node = object_e.get<BoatBase>().mNode;
if (node)
node->saveChildren(fileName);
return 0;
});
lua_setglobal(L, "ecs_save_object_debug");
lua_pushcfunction(L, [](lua_State *L) -> int {
luaL_checktype(L, 1, LUA_TBOOLEAN); // object
ECS::get_mut<EngineData>().enableDbgDraw = lua_toboolean(L, 1);
ECS::modified<EngineData>();
return 0;
});
lua_setglobal(L, "ecs_set_debug_drawing");
lua_pushcfunction(L, [](lua_State *L) -> int {
OgreAssert(lua_gettop(L) == 2, "Bad parameters");
luaL_checktype(L, 1, LUA_TNUMBER); // object
luaL_checktype(L, 2, LUA_TBOOLEAN);
int object = lua_tointeger(L, 1);
flecs::entity object_e = idmap.get_entity(object);
bool enable = lua_toboolean(L, 2);
if (enable)
object_e.remove<CharacterDisablePhysics>();
else
object_e.add<CharacterDisablePhysics>();
object_e.add<CharacterUpdatePhysicsState>();
return 0;
});
lua_setglobal(L, "ecs_character_physics_control");
}
LuaData::~LuaData()
@@ -391,6 +445,7 @@ void LuaData::lateSetup()
LuaModule::LuaModule(flecs::world &ecs)
{
ecs.component<LuaChildEventTrigger>();
ecs.component<LuaBase>()
.on_add([](LuaBase &lua) {
lua.mLua = new LuaData;
@@ -399,6 +454,7 @@ LuaModule::LuaModule(flecs::world &ecs)
})
.add(flecs::Singleton);
ecs.add<LuaBase>();
ecs.system<const EngineData, LuaBase>("LuaUpdate")
.kind(flecs::OnUpdate)
.each([](const EngineData &eng, LuaBase &lua) {
@@ -414,5 +470,36 @@ LuaModule::LuaModule(flecs::world &ecs)
}
}
});
ecs.system<const EngineData, const LuaChildEventTrigger>(
"CreateChildTrigger")
.kind(flecs::OnUpdate)
.without<EventTrigger>()
.write<EventTrigger>()
.each([](flecs::entity e, const EngineData &env,
const LuaChildEventTrigger &lct) {
Ogre::SceneNode *parentNode = nullptr;
flecs::entity parent_e = lct.parent_e;
if (parent_e.has<CharacterBase>()) {
parentNode =
parent_e.get<CharacterBase>().mBodyNode;
OgreAssert(
parent_e.get<CharacterBase>().mBodyNode,
"bad node");
} else if (parent_e.has<BoatBase>()) {
parentNode = parent_e.get<BoatBase>().mNode;
OgreAssert(parent_e.get<BoatBase>().mNode,
"bad node");
} else
return;
EventTrigger &trigger = e.ensure<EventTrigger>();
OgreAssert(parentNode, "bad parent");
trigger.position = lct.position;
trigger.halfheight = lct.halfheight;
trigger.radius = lct.radius;
trigger.event = lct.event;
trigger.parent = parentNode;
e.modified<EventTrigger>();
});
}
}