Skip to content

Commit 057752e

Browse files
committed
Added timer plugin
1 parent 7fa0522 commit 057752e

3 files changed

Lines changed: 66 additions & 4 deletions

File tree

plugins/TimerPlugin/TimerPlugin.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module TimerPlugin
2+
3+
using ..Cruise
4+
5+
const TIMERPLUGIN = CRPlugin()
6+
7+
mutable struct CRTimer
8+
duration::Float32 # in seconds
9+
signal::CRSubject{Nothing}
10+
end
11+
12+
mutable struct TimerManager
13+
timers::Vector{CRTimer}
14+
paused::Bool
15+
end
16+
17+
TM = TimerManager(CRTimer[], false)
18+
19+
addtimer!(dur) = addtimer!(TM, dur)
20+
addtimer!(tm::TimerManager, dur) = begin
21+
timer = CRTimer(dur, CRSubject(nothing))
22+
addtimer!(tm, timer)
23+
return timer
24+
end
25+
addtimer!(tm::TimerManager, t::CRTimer) = push!(tm.timers, t)
26+
27+
ontimeout(f, t::CRTimer) = connect(f, t.signal)
28+
29+
id = add_system!(TIMERPLUGIN, TM)
30+
31+
function Cruise.update!(n::CRPluginNode{TimerManager})
32+
tm = n.obj
33+
tm.paused && return
34+
lvar = LOOP_VAR_REF[]
35+
36+
timers = tm.timers
37+
start, stop = 1, length(timers)
38+
dt = lvar.delta_seconds
39+
40+
while start <= stop
41+
timer = timers[start]
42+
timer.duration -= dt
43+
44+
if timer.duration <= 0
45+
notify!(timer.signal)
46+
timers[start], timers[stop] = timers[stop], timers[start]
47+
stop -= 1
48+
else
49+
start += 1
50+
end
51+
end
52+
53+
resize!(timers, stop)
54+
end
55+
56+
clear!(tm::TimerManager) = empty!(tm.timers)
57+
Cruise.shutdown!(n::CRPluginNode{TimerManager}) = clear!(n.obj)
58+
59+
export addtimer!, ontimeout, CRTimer, TIMERPLUGIN, TimerManager, clear!
60+
61+
end # module

src/Cruise.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ include("writer.jl")
2727

2828
include(joinpath("..", "plugins", "ODPlugin", "ODPlugin.jl"))
2929
include(joinpath("..", "plugins", "HZPlugin", "HZPlugin.jl"))
30+
include(joinpath("..", "plugins", "TimerPlugin", "TimerPlugin.jl"))
3031

3132
text_en = [
3233
"{speed:0.1}Everything begins somewhere.{pause:2}",

src/game_loop.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ enable_system(id) # Now your game logic will be able to run
246246
"""
247247
function enable_system(id::Integer, awake=true)
248248
app = CruiseApp()
249-
idtonode = app.idtonode
249+
idtonode = app.plugins.idtonode
250250

251251
!haskey(idtonode, id) && return
252-
node = app.idtonode[id]
252+
node = idtonode[id]
253253
node.enabled = true
254254
awake && awake!(node)
255255
end
@@ -271,10 +271,10 @@ disable_system(id) # Now your game logic will no more run
271271
"""
272272
function disable_system(id::Integer, shut=false)
273273
app = CruiseApp()
274-
idtonode = app.idtonode
274+
idtonode = app.plugins.idtonode
275275

276276
!haskey(idtonode, id) && return
277-
node = app.idtonode[id]
277+
node = idtonode[id]
278278
node.enabled = false
279279

280280
shut && shutdown!(node)

0 commit comments

Comments
 (0)