feature/imp-61-time-based-trigger #29
@@ -5,6 +5,7 @@ globals = {
|
|||||||
"Focus",
|
"Focus",
|
||||||
"Day",
|
"Day",
|
||||||
"Timer",
|
"Timer",
|
||||||
|
"Trigger",
|
||||||
"Util",
|
"Util",
|
||||||
"Decision",
|
"Decision",
|
||||||
"Situation",
|
"Situation",
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ logic/logic.meter.lua
|
|||||||
logic/logic.focus.lua
|
logic/logic.focus.lua
|
||||||
logic/logic.day.lua
|
logic/logic.day.lua
|
||||||
logic/logic.timer.lua
|
logic/logic.timer.lua
|
||||||
|
logic/logic.trigger.lua
|
||||||
logic/logic.minigame.lua
|
logic/logic.minigame.lua
|
||||||
system/system.ui.lua
|
system/system.ui.lua
|
||||||
audio/audio.manager.lua
|
audio/audio.manager.lua
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ Context = {}
|
|||||||
--- * minigame_button_mash (table) Button mash minigame state (see Minigame.get_default_button_mash).<br/>
|
--- * minigame_button_mash (table) Button mash minigame state (see Minigame.get_default_button_mash).<br/>
|
||||||
--- * minigame_rhythm (table) Rhythm minigame state (see Minigame.get_default_rhythm).<br/>
|
--- * minigame_rhythm (table) Rhythm minigame state (see Minigame.get_default_rhythm).<br/>
|
||||||
--- * meters (table) Meter values (see Meter.get_initial).<br/>
|
--- * meters (table) Meter values (see Meter.get_initial).<br/>
|
||||||
|
--- * triggers (table) Active trigger runtime state, keyed by trigger ID.<br/>
|
||||||
--- * stat_screen_active (boolean) Whether the stat screen overlay is currently shown.<br/>
|
--- * stat_screen_active (boolean) Whether the stat screen overlay is currently shown.<br/>
|
||||||
--- * game (table) Current game progress state. Contains: `current_screen` (string) active screen ID, `current_situation` (string|nil) active situation ID.<br/>
|
--- * game (table) Current game progress state. Contains: `current_screen` (string) active screen ID, `current_situation` (string|nil) active situation ID.<br/>
|
||||||
function Context.initial_data()
|
function Context.initial_data()
|
||||||
@@ -35,6 +36,7 @@ function Context.initial_data()
|
|||||||
minigame_rhythm = {},
|
minigame_rhythm = {},
|
||||||
meters = Meter.get_initial(),
|
meters = Meter.get_initial(),
|
||||||
timer = Timer.get_initial(),
|
timer = Timer.get_initial(),
|
||||||
|
triggers = {},
|
||||||
game = {
|
game = {
|
||||||
current_screen = "home",
|
current_screen = "home",
|
||||||
current_situation = nil,
|
current_situation = nil,
|
||||||
|
|||||||
@@ -13,4 +13,5 @@ Sprite = {}
|
|||||||
Audio = {}
|
Audio = {}
|
||||||
Focus = {}
|
Focus = {}
|
||||||
Day = {}
|
Day = {}
|
||||||
Timer = {}
|
Timer = {}
|
||||||
|
Trigger = {}
|
||||||
135
inc/logic/logic.trigger.lua
Normal file
135
inc/logic/logic.trigger.lua
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
--- @section Trigger
|
||||||
|
local triggers = {}
|
||||||
|
|
||||||
|
--- @within Trigger
|
||||||
|
--- @param trigger table The trigger data table.
|
||||||
|
--- @param trigger.id string Unique trigger identifier.
|
||||||
|
--- @param trigger.duration number Duration in frames before the trigger fires.
|
||||||
|
--- @param[opt] trigger.on_start function Called when the trigger starts. Defaults to noop.
|
||||||
|
--- @param[opt] trigger.on_stop function Called when the trigger fires or is manually stopped. Defaults to noop.
|
||||||
|
--- @param[opt] trigger.repeating boolean If true, trigger restarts after firing. Defaults to false.
|
||||||
|
function Trigger.register(trigger)
|
||||||
|
if not trigger or not trigger.id then
|
||||||
|
trace("Error: Invalid trigger registered (missing id)!")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not trigger.duration or trigger.duration <= 0 then
|
||||||
|
trace("Error: Invalid trigger registered (missing or invalid duration)!")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if not trigger.on_start then
|
||||||
|
trigger.on_start = function() end
|
||||||
|
end
|
||||||
|
if not trigger.on_stop then
|
||||||
|
trigger.on_stop = function() end
|
||||||
|
end
|
||||||
|
if trigger.repeating == nil then
|
||||||
|
trigger.repeating = false
|
||||||
|
end
|
||||||
|
if triggers[trigger.id] then
|
||||||
|
trace("Warning: Overwriting trigger with id: " .. trigger.id)
|
||||||
|
end
|
||||||
|
triggers[trigger.id] = trigger
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @within Trigger
|
||||||
|
--- @param id string The trigger ID.
|
||||||
|
--- @return table|nil result The trigger definition or nil.
|
||||||
|
function Trigger.get_by_id(id)
|
||||||
|
return triggers[id]
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @within Trigger
|
||||||
|
--- @return table result All trigger definitions keyed by ID.
|
||||||
|
function Trigger.get_all()
|
||||||
|
return triggers
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @within Trigger
|
||||||
|
--- @param id string The trigger ID.
|
||||||
|
--- @return boolean active True if the trigger is running.
|
||||||
|
function Trigger.is_active(id)
|
||||||
|
if not Context or not Context.triggers then return false end
|
||||||
|
return Context.triggers[id] ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
--- If already active, restarts from 0.
|
||||||
|
--- @within Trigger
|
||||||
|
--- @param id string The trigger ID.
|
||||||
|
function Trigger.start(id)
|
||||||
|
if not Context or not Context.triggers then return end
|
||||||
|
local trigger = triggers[id]
|
||||||
|
if not trigger then
|
||||||
|
trace("Error: Cannot start unknown trigger: " .. tostring(id))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
Context.triggers[id] = { elapsed = 0 }
|
||||||
|
trigger.on_start()
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @within Trigger
|
||||||
|
--- @param id string The trigger ID.
|
||||||
|
function Trigger.stop(id)
|
||||||
|
if not Context or not Context.triggers then return end
|
||||||
|
local trigger = triggers[id]
|
||||||
|
if not trigger then
|
||||||
|
trace("Error: Cannot stop unknown trigger: " .. tostring(id))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not Context.triggers[id] then return end
|
||||||
|
|
||||||
|
Context.triggers[id] = nil
|
||||||
|
trigger.on_stop()
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Resets elapsed time to 0 without calling handlers. No-op if inactive.
|
||||||
|
--- @within Trigger
|
||||||
|
--- @param id string The trigger ID.
|
||||||
|
function Trigger.reset(id)
|
||||||
|
if not Context or not Context.triggers then return end
|
||||||
|
if not triggers[id] then
|
||||||
|
trace("Error: Cannot reset unknown trigger: " .. tostring(id))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not Context.triggers[id] then return end
|
||||||
|
|
||||||
|
Context.triggers[id].elapsed = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Pauses during minigames.
|
||||||
|
--- @within Trigger
|
||||||
|
function Trigger.update()
|
||||||
|
if not Context or not Context.game_in_progress or not Context.triggers then return end
|
||||||
|
|
||||||
|
local in_minigame = string.find(Window.get_current_id(), "^minigame_") ~= nil
|
||||||
|
if in_minigame then return end
|
||||||
|
|
||||||
|
local fired = {}
|
||||||
|
for id, state in pairs(Context.triggers) do
|
||||||
|
local trigger = triggers[id]
|
||||||
|
if trigger then
|
||||||
|
state.elapsed = state.elapsed + 1
|
||||||
|
if state.elapsed >= trigger.duration then
|
||||||
|
table.insert(fired, id)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
table.insert(fired, id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, id in ipairs(fired) do
|
||||||
|
local trigger = triggers[id]
|
||||||
|
if trigger then
|
||||||
|
trigger.on_stop()
|
||||||
|
if trigger.repeating then
|
||||||
|
Context.triggers[id] = { elapsed = 0 }
|
||||||
|
else
|
||||||
|
Context.triggers[id] = nil
|
||||||
|
end
|
||||||
|
else
|
||||||
|
Context.triggers[id] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -23,6 +23,7 @@ function TIC()
|
|||||||
end
|
end
|
||||||
Meter.update()
|
Meter.update()
|
||||||
Timer.update()
|
Timer.update()
|
||||||
|
Trigger.update()
|
||||||
if Context.game_in_progress then
|
if Context.game_in_progress then
|
||||||
Meter.draw()
|
Meter.draw()
|
||||||
Timer.draw()
|
Timer.draw()
|
||||||
|
|||||||
Reference in New Issue
Block a user