feat: save rules
This commit is contained in:
parent
6602584c13
commit
8c8c844dd4
@ -208,10 +208,16 @@ function _rules.listen(name, handler)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function _rules.create(name, value, handler)
|
function _rules.create(name, value, handler)
|
||||||
_rules.set(name, value)
|
local handlerid
|
||||||
if handler ~= nil then
|
if handler ~= nil then
|
||||||
return _rules.listen(name, handler)
|
handlerid = _rules.listen(name, handler)
|
||||||
end
|
end
|
||||||
|
if _rules.get(name) == nil then
|
||||||
|
_rules.set(name, value)
|
||||||
|
else
|
||||||
|
handler(_rules.get(name))
|
||||||
|
end
|
||||||
|
return handlerid
|
||||||
end
|
end
|
||||||
|
|
||||||
function _rules.unlisten(name, id)
|
function _rules.unlisten(name, id)
|
||||||
@ -240,13 +246,36 @@ function __vc_create_hud_rules()
|
|||||||
input.set_enabled("player.attack", value)
|
input.set_enabled("player.attack", value)
|
||||||
end)
|
end)
|
||||||
_rules.create("allow-cheat-movement", true, function(value)
|
_rules.create("allow-cheat-movement", true, function(value)
|
||||||
input.set_enabled("player.cheat", value)
|
input.set_enabled("movement.cheat", value)
|
||||||
end)
|
end)
|
||||||
_rules.create("allow-debug-cheats", true, function(value)
|
_rules.create("allow-debug-cheats", true, function(value)
|
||||||
hud._set_debug_cheats(value)
|
hud._set_debug_cheats(value)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local RULES_FILE = "world:rules.toml"
|
||||||
|
function __vc_on_world_open()
|
||||||
|
if not file.exists(RULES_FILE) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local rule_values = toml.parse(file.read(RULES_FILE))
|
||||||
|
for name, value in pairs(rule_values) do
|
||||||
|
_rules.set(name, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function __vc_on_world_save()
|
||||||
|
local rule_values = {}
|
||||||
|
for name, rule in pairs(rules.rules) do
|
||||||
|
rule_values[name] = rule.value
|
||||||
|
end
|
||||||
|
file.write(RULES_FILE, toml.tostring(rule_values))
|
||||||
|
end
|
||||||
|
|
||||||
|
function __vc_on_world_quit()
|
||||||
|
_rules.clear()
|
||||||
|
end
|
||||||
|
|
||||||
-- --------- Deprecated functions ------ --
|
-- --------- Deprecated functions ------ --
|
||||||
local function wrap_deprecated(func, name, alternatives)
|
local function wrap_deprecated(func, name, alternatives)
|
||||||
return function (...)
|
return function (...)
|
||||||
|
|||||||
@ -360,7 +360,7 @@ void Engine::loadContent() {
|
|||||||
for (auto& pack : contentPacks) {
|
for (auto& pack : contentPacks) {
|
||||||
ContentLoader(&pack, contentBuilder, *resPaths).load();
|
ContentLoader(&pack, contentBuilder, *resPaths).load();
|
||||||
load_configs(pack.folder);
|
load_configs(pack.folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
content = contentBuilder.build();
|
content = contentBuilder.build();
|
||||||
|
|
||||||
@ -421,8 +421,6 @@ double Engine::getDelta() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::setScreen(std::shared_ptr<Screen> screen) {
|
void Engine::setScreen(std::shared_ptr<Screen> screen) {
|
||||||
// unblock all bindings
|
|
||||||
Events::enableBindings();
|
|
||||||
// reset audio channels (stop all sources)
|
// reset audio channels (stop all sources)
|
||||||
audio::reset_channel(audio::get_channel_index("regular"));
|
audio::reset_channel(audio::get_channel_index("regular"));
|
||||||
audio::reset_channel(audio::get_channel_index("ambient"));
|
audio::reset_channel(audio::get_channel_index("ambient"));
|
||||||
|
|||||||
@ -87,6 +87,8 @@ void LevelScreen::initializePack(ContentPackRuntime* pack) {
|
|||||||
LevelScreen::~LevelScreen() {
|
LevelScreen::~LevelScreen() {
|
||||||
saveWorldPreview();
|
saveWorldPreview();
|
||||||
scripting::on_frontend_close();
|
scripting::on_frontend_close();
|
||||||
|
// unblock all bindings
|
||||||
|
Events::enableBindings();
|
||||||
controller->onWorldQuit();
|
controller->onWorldQuit();
|
||||||
engine->getPaths()->setCurrentWorldFolder(fs::path());
|
engine->getPaths()->setCurrentWorldFolder(fs::path());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -150,7 +150,7 @@ void scripting::initialize(Engine* engine) {
|
|||||||
void scripting::process_post_runnables() {
|
void scripting::process_post_runnables() {
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
if (lua::getglobal(L, "__process_post_runnables")) {
|
if (lua::getglobal(L, "__process_post_runnables")) {
|
||||||
lua::call_nothrow(L, 0);
|
lua::call_nothrow(L, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,9 +160,13 @@ void scripting::on_world_load(LevelController* controller) {
|
|||||||
scripting::indices = level->content->getIndices();
|
scripting::indices = level->content->getIndices();
|
||||||
scripting::blocks = controller->getBlocksController();
|
scripting::blocks = controller->getBlocksController();
|
||||||
scripting::controller = controller;
|
scripting::controller = controller;
|
||||||
load_script("world.lua", false);
|
|
||||||
|
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
|
if (lua::getglobal(L, "__vc_on_world_open")) {
|
||||||
|
lua::call_nothrow(L, 0, 0);
|
||||||
|
}
|
||||||
|
load_script("world.lua", false);
|
||||||
|
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ":.worldopen");
|
lua::emit_event(L, pack.id + ":.worldopen");
|
||||||
}
|
}
|
||||||
@ -180,6 +184,9 @@ void scripting::on_world_save() {
|
|||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ":.worldsave");
|
lua::emit_event(L, pack.id + ":.worldsave");
|
||||||
}
|
}
|
||||||
|
if (lua::getglobal(L, "__vc_on_world_save")) {
|
||||||
|
lua::call_nothrow(L, 0, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_quit() {
|
void scripting::on_world_quit() {
|
||||||
@ -187,6 +194,9 @@ void scripting::on_world_quit() {
|
|||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ":.worldquit");
|
lua::emit_event(L, pack.id + ":.worldquit");
|
||||||
}
|
}
|
||||||
|
if (lua::getglobal(L, "__vc_on_world_quit")) {
|
||||||
|
lua::call_nothrow(L, 0, 0);
|
||||||
|
}
|
||||||
scripting::level = nullptr;
|
scripting::level = nullptr;
|
||||||
scripting::content = nullptr;
|
scripting::content = nullptr;
|
||||||
scripting::indices = nullptr;
|
scripting::indices = nullptr;
|
||||||
@ -204,11 +214,6 @@ void scripting::cleanup() {
|
|||||||
}
|
}
|
||||||
lua::pop(L);
|
lua::pop(L);
|
||||||
|
|
||||||
lua::getglobal(L, "rules");
|
|
||||||
lua::getfield(L, "clear");
|
|
||||||
lua::call_nothrow(L, 0);
|
|
||||||
lua::pop(L);
|
|
||||||
|
|
||||||
if (lua::getglobal(L, "__scripts_cleanup")) {
|
if (lua::getglobal(L, "__scripts_cleanup")) {
|
||||||
lua::call_nothrow(L, 0);
|
lua::call_nothrow(L, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user