Updates
This commit is contained in:
133
src/gamedata/LuaData.cpp
Normal file
133
src/gamedata/LuaData.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "GameData.h"
|
||||
#include "Components.h"
|
||||
#include "GUIModule.h"
|
||||
#include "LuaData.h"
|
||||
namespace ECS
|
||||
{
|
||||
|
||||
int LuaData::setup_handler()
|
||||
{
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_pushvalue(L, 1);
|
||||
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
setup_handlers.push_back(ref);
|
||||
return 0;
|
||||
}
|
||||
int LuaData::call_handler(const Ogre::String &event)
|
||||
{
|
||||
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_pcall(L, 1, 0, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
LuaData::LuaData()
|
||||
: L(luaL_newstate())
|
||||
{
|
||||
luaopen_base(L);
|
||||
luaopen_package(L);
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
OgreAssert(false, "Crash function called");
|
||||
return 0;
|
||||
});
|
||||
lua_setglobal(L, "crash");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
ECS::get<LuaBase>().mLua->setup_handler();
|
||||
return 0;
|
||||
});
|
||||
lua_setglobal(L, "setup_handler");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
luaL_checktype(L, 1, LUA_TSTRING);
|
||||
size_t len;
|
||||
Ogre::String message(luaL_tolstring(L, 1, &len));
|
||||
if (message.length() == 0 && ECS::get_mut<GUI>().narrationBox) {
|
||||
ECS::get_mut<GUI>().enabled = false;
|
||||
ECS::get_mut<GUI>().grab = true;
|
||||
ECS::get_mut<GUI>().grabChanged = true;
|
||||
ECS::get_mut<GUI>().narrationText = message;
|
||||
ECS::get_mut<GUI>().narrationBox = false;
|
||||
ECS::modified<GUI>();
|
||||
} else {
|
||||
std::replace(message.begin(), message.end(), '\n', ' ');
|
||||
std::replace(message.begin(), message.end(), '\r', ' ');
|
||||
|
||||
std::cout << "narrator message: " << message
|
||||
<< std::endl;
|
||||
ECS::get_mut<GUI>().enabled = true;
|
||||
ECS::get_mut<GUI>().grab = false;
|
||||
ECS::get_mut<GUI>().grabChanged = true;
|
||||
ECS::get_mut<GUI>().narrationText = message;
|
||||
ECS::get_mut<GUI>().narrationBox = true;
|
||||
ECS::modified<GUI>();
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
lua_setglobal(L, "narrate");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
// ECS::get_mut<GUI>().mainMenu = true;
|
||||
ECS::get_mut<GUI>().enabled = true;
|
||||
ECS::get_mut<GUI>().mainMenu = true;
|
||||
ECS::get_mut<GUI>().grab = false;
|
||||
ECS::get_mut<GUI>().grabChanged = true;
|
||||
ECS::modified<GUI>();
|
||||
return 0;
|
||||
});
|
||||
lua_setglobal(L, "main_menu");
|
||||
}
|
||||
|
||||
LuaData::~LuaData()
|
||||
{
|
||||
lua_close(L);
|
||||
}
|
||||
|
||||
void LuaData::lateSetup()
|
||||
{
|
||||
Ogre::DataStreamList streams =
|
||||
Ogre::ResourceGroupManager::getSingleton().openResources(
|
||||
"*.lua", "LuaScripts");
|
||||
while (!streams.empty()) {
|
||||
Ogre::DataStreamPtr s = streams.front();
|
||||
std::cout << "stream: " << s->getAsString() << "\n";
|
||||
streams.pop_front();
|
||||
if (luaL_dostring(L, s->getAsString().c_str()) != LUA_OK) {
|
||||
std::cout << "error: " << lua_tostring(L, -1) << "\n";
|
||||
OgreAssert(false, "Script failure");
|
||||
}
|
||||
}
|
||||
const char *lua_code = "\n\
|
||||
function stuff()\n\
|
||||
return 4\n\
|
||||
end\n\
|
||||
x = stuff()\n\
|
||||
";
|
||||
luaL_dostring(L, lua_code);
|
||||
lua_getglobal(L, "x");
|
||||
int x = lua_tonumber(L, 1);
|
||||
std::cout << "lua: " << x << "\n";
|
||||
}
|
||||
|
||||
LuaModule::LuaModule(flecs::world &ecs)
|
||||
{
|
||||
ecs.component<LuaBase>().add(flecs::Singleton);
|
||||
ecs.set<LuaBase>({ OGRE_NEW LuaData, false, false });
|
||||
ecs.system<const EngineData, LuaBase>("LuaUpdate")
|
||||
.kind(flecs::OnUpdate)
|
||||
.each([](const EngineData &eng, LuaBase &lua) {
|
||||
if (!lua.setup_called) {
|
||||
lua.mLua->lateSetup();
|
||||
lua.mLua->call_handler("setup");
|
||||
lua.setup_called = true;
|
||||
}
|
||||
if (!lua.startup_called) {
|
||||
if (eng.startupDelay <= 0.0f) {
|
||||
lua.mLua->call_handler("startup");
|
||||
lua.startup_called = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user