add Schedule class & add time.schedule.world schedule group
This commit is contained in:
parent
cd2bc8fbf6
commit
b28aa71845
37
res/modules/schedule.lua
Normal file
37
res/modules/schedule.lua
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
local Schedule = {
|
||||||
|
__index = {
|
||||||
|
set_interval = function(self, ms, callback)
|
||||||
|
local id = self._next_interval
|
||||||
|
self._intervals[id] = {
|
||||||
|
last_called = 0.0,
|
||||||
|
delay = ms / 1000.0,
|
||||||
|
callback = callback,
|
||||||
|
}
|
||||||
|
self._next_interval = id + 1
|
||||||
|
return id
|
||||||
|
end,
|
||||||
|
tick = function(self, dt)
|
||||||
|
local timer = self._timer + dt
|
||||||
|
for id, interval in pairs(self._intervals) do
|
||||||
|
if timer - interval.last_called >= interval.delay then
|
||||||
|
xpcall(interval.callback, function(s)
|
||||||
|
debug.error(s..'\n'..debug.traceback())
|
||||||
|
end)
|
||||||
|
interval.last_called = timer
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self._timer = timer
|
||||||
|
end,
|
||||||
|
remove_interval = function (self, id)
|
||||||
|
self._intervals[id] = nil
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return function ()
|
||||||
|
return setmetatable({
|
||||||
|
_next_interval = 1,
|
||||||
|
_timer = 0.0,
|
||||||
|
_intervals = {},
|
||||||
|
}, Schedule)
|
||||||
|
end
|
||||||
@ -450,8 +450,37 @@ function __vc_on_hud_open()
|
|||||||
hud.open_permanent("core:ingame_chat")
|
hud.open_permanent("core:ingame_chat")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local ScheduleGroup_mt = {
|
||||||
|
__index = {
|
||||||
|
publish = function(self, schedule)
|
||||||
|
local id = self._next_schedule
|
||||||
|
self._schedules[id] = schedule
|
||||||
|
self._next_schedule = id + 1
|
||||||
|
end,
|
||||||
|
tick = function(self, dt)
|
||||||
|
for id, schedule in pairs(self._schedules) do
|
||||||
|
schedule:tick(dt)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
remove = function(self, id)
|
||||||
|
self._schedules[id] = nil
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
local function ScheduleGroup()
|
||||||
|
return setmetatable({
|
||||||
|
_next_schedule = 1,
|
||||||
|
_schedules = {},
|
||||||
|
}, ScheduleGroup_mt)
|
||||||
|
end
|
||||||
|
|
||||||
|
time.schedules = {}
|
||||||
|
|
||||||
local RULES_FILE = "world:rules.toml"
|
local RULES_FILE = "world:rules.toml"
|
||||||
function __vc_on_world_open()
|
function __vc_on_world_open()
|
||||||
|
time.schedules.world = ScheduleGroup()
|
||||||
|
|
||||||
if not file.exists(RULES_FILE) then
|
if not file.exists(RULES_FILE) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -461,6 +490,10 @@ function __vc_on_world_open()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function __vc_on_world_tick()
|
||||||
|
time.schedules.world:tick(1.0 / 20.0)
|
||||||
|
end
|
||||||
|
|
||||||
function __vc_on_world_save()
|
function __vc_on_world_save()
|
||||||
local rule_values = {}
|
local rule_values = {}
|
||||||
for name, rule in pairs(rules.rules) do
|
for name, rule in pairs(rules.rules) do
|
||||||
|
|||||||
@ -299,6 +299,9 @@ void scripting::on_world_load(LevelController* controller) {
|
|||||||
|
|
||||||
void scripting::on_world_tick() {
|
void scripting::on_world_tick() {
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
|
if (lua::getglobal(L, "__vc_on_world_tick")) {
|
||||||
|
lua::call_nothrow(L, 0, 0);
|
||||||
|
}
|
||||||
for (auto& pack : content_control->getAllContentPacks()) {
|
for (auto& pack : content_control->getAllContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ":.worldtick");
|
lua::emit_event(L, pack.id + ":.worldtick");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user