New models; using raft
This commit is contained in:
@@ -12,11 +12,17 @@ int luaopen_lpeg(lua_State *L);
|
||||
}
|
||||
namespace ECS
|
||||
{
|
||||
struct LuaEcsEntity {
|
||||
int id;
|
||||
flecs::entity e;
|
||||
};
|
||||
struct idmap {
|
||||
std::unordered_map<int, flecs::entity> id2entity;
|
||||
std::unordered_map<flecs::entity_t, int> entity2id;
|
||||
int next_id;
|
||||
idmap()
|
||||
: id2entity({})
|
||||
, entity2id({})
|
||||
, next_id(0)
|
||||
{
|
||||
}
|
||||
@@ -27,8 +33,11 @@ struct idmap {
|
||||
}
|
||||
int add_entity(flecs::entity e)
|
||||
{
|
||||
if (entity2id.find(e.id()) != entity2id.end())
|
||||
return entity2id[e.id()];
|
||||
int id = get_next_id();
|
||||
id2entity[id] = e;
|
||||
entity2id[e.id()] = id;
|
||||
return id;
|
||||
}
|
||||
flecs::entity get_entity(int id)
|
||||
@@ -53,7 +62,28 @@ int LuaData::call_handler(const Ogre::String &event)
|
||||
for (i = 0; i < setup_handlers.size(); i++) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, setup_handlers[i]);
|
||||
lua_pushstring(L, event.c_str());
|
||||
lua_pcall(L, 1, 0, 0);
|
||||
lua_pushinteger(L, -1);
|
||||
if (lua_pcall(L, 2, 0, 0)) {
|
||||
Ogre::LogManager::getSingleton().stream()
|
||||
<< lua_tostring(L, -1);
|
||||
OgreAssert(false, "Lua error");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LuaData::call_handler(const Ogre::String &event, flecs::entity e)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < setup_handlers.size(); i++) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, setup_handlers[i]);
|
||||
lua_pushstring(L, event.c_str());
|
||||
lua_pushinteger(L, idmap.add_entity(e));
|
||||
if (lua_pcall(L, 2, 0, 0)) {
|
||||
Ogre::LogManager::getSingleton().stream()
|
||||
<< lua_tostring(L, -1);
|
||||
OgreAssert(false, "Lua error");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -276,6 +306,21 @@ LuaData::LuaData()
|
||||
lua_pushinteger(L, ret);
|
||||
std::cout << "boat created: " << ret << std::endl;
|
||||
return 1;
|
||||
} else if (what == "raft") {
|
||||
float yaw = lua_tonumber(L, 5);
|
||||
float x = lua_tonumber(L, 2);
|
||||
float y = lua_tonumber(L, 3);
|
||||
float z = lua_tonumber(L, 4);
|
||||
flecs::entity e = ECS::get().entity();
|
||||
Ogre::Quaternion orientation(Ogre::Radian(yaw),
|
||||
Ogre::Vector3(0, 1, 0));
|
||||
Ogre::Vector3 position(x, y, z);
|
||||
e.set<BoatType>(
|
||||
{ "raft.scene", position, orientation });
|
||||
int ret = idmap.add_entity(e);
|
||||
lua_pushinteger(L, ret);
|
||||
std::cout << "raft created: " << ret << std::endl;
|
||||
return 1;
|
||||
}
|
||||
lua_pushinteger(L, -1);
|
||||
return 1;
|
||||
@@ -413,6 +458,118 @@ LuaData::LuaData()
|
||||
return 0;
|
||||
});
|
||||
lua_setglobal(L, "ecs_set_slot");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
luaL_checktype(L, 1, LUA_TNUMBER); // entity id
|
||||
int id = lua_tointeger(L, 1);
|
||||
flecs::entity e = idmap.get_entity(id);
|
||||
LuaEcsEntity *edata = static_cast<LuaEcsEntity *>(
|
||||
lua_newuserdata(L, sizeof(LuaEcsEntity)));
|
||||
new (edata) LuaEcsEntity();
|
||||
edata->e = e;
|
||||
edata->id = e;
|
||||
luaL_getmetatable(L, "FlecsEntityMetaTable");
|
||||
lua_setmetatable(L, -2);
|
||||
return 1;
|
||||
});
|
||||
lua_setglobal(L, "ecs_get_entity");
|
||||
luaL_newmetatable(L, "FlecsEntityMetaTable");
|
||||
lua_pushstring(L, "__index");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA); //metatable
|
||||
luaL_checktype(L, 2, LUA_TSTRING); //function
|
||||
Ogre::String component = lua_tostring(L, 2);
|
||||
if (component == "components") {
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushcclosure(
|
||||
L,
|
||||
[](lua_State *L) -> int {
|
||||
luaL_checktype(L, lua_upvalueindex(1),
|
||||
LUA_TUSERDATA);
|
||||
LuaEcsEntity *ent = static_cast<
|
||||
LuaEcsEntity *>(lua_touserdata(
|
||||
L, lua_upvalueindex(1)));
|
||||
std::cout << ent->id << " called!!!\n";
|
||||
ent->e.each([&](flecs::id id) {
|
||||
flecs::entity cmp =
|
||||
ECS::get().entity(id);
|
||||
if (cmp.is_alive()) {
|
||||
const char *name =
|
||||
cmp.name();
|
||||
if (name)
|
||||
std::cout
|
||||
<< "component: "
|
||||
<< name
|
||||
<< std::endl;
|
||||
}
|
||||
});
|
||||
return 0;
|
||||
},
|
||||
1);
|
||||
} else if (component == "is_trigger") {
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushcclosure(
|
||||
L,
|
||||
[](lua_State *L) -> int {
|
||||
luaL_checktype(L, lua_upvalueindex(1),
|
||||
LUA_TUSERDATA);
|
||||
LuaEcsEntity *ent = static_cast<
|
||||
LuaEcsEntity *>(lua_touserdata(
|
||||
L, lua_upvalueindex(1)));
|
||||
lua_pushboolean(
|
||||
L, ent->e.has<EventTrigger>());
|
||||
return 1;
|
||||
},
|
||||
1);
|
||||
} else if (component == "is_character") {
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushcclosure(
|
||||
L,
|
||||
[](lua_State *L) -> int {
|
||||
luaL_checktype(L, lua_upvalueindex(1),
|
||||
LUA_TUSERDATA);
|
||||
LuaEcsEntity *ent = static_cast<
|
||||
LuaEcsEntity *>(lua_touserdata(
|
||||
L, lua_upvalueindex(1)));
|
||||
lua_pushboolean(
|
||||
L, ent->e.has<Character>());
|
||||
return 1;
|
||||
},
|
||||
1);
|
||||
} else if (component == "is_boat") {
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushcclosure(
|
||||
L,
|
||||
[](lua_State *L) -> int {
|
||||
luaL_checktype(L, lua_upvalueindex(1),
|
||||
LUA_TUSERDATA);
|
||||
LuaEcsEntity *ent = static_cast<
|
||||
LuaEcsEntity *>(lua_touserdata(
|
||||
L, lua_upvalueindex(1)));
|
||||
lua_pushboolean(L,
|
||||
ent->e.has<BoatBase>());
|
||||
return 1;
|
||||
},
|
||||
1);
|
||||
} else if (component == "is_player") {
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushcclosure(
|
||||
L,
|
||||
[](lua_State *L) -> int {
|
||||
luaL_checktype(L, lua_upvalueindex(1),
|
||||
LUA_TUSERDATA);
|
||||
LuaEcsEntity *ent = static_cast<
|
||||
LuaEcsEntity *>(lua_touserdata(
|
||||
L, lua_upvalueindex(1)));
|
||||
lua_pushboolean(L,
|
||||
ent->e.has<Player>());
|
||||
return 1;
|
||||
},
|
||||
1);
|
||||
} else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
});
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
LuaData::~LuaData()
|
||||
|
||||
Reference in New Issue
Block a user