mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 09:00:02 +00:00
Add tests for scheduler example
This commit is contained in:
parent
3b79717556
commit
ecdbe2d8ed
4 changed files with 254 additions and 7 deletions
247
test/tests.luau
247
test/tests.luau
|
@ -1243,4 +1243,251 @@ TEST("Hooks", function()
|
|||
end
|
||||
|
||||
end)
|
||||
|
||||
TEST("scheduler", function()
|
||||
type System = {
|
||||
callback: (world: World) -> ()
|
||||
}
|
||||
type Systems = { System }
|
||||
|
||||
|
||||
type Events = {
|
||||
RenderStepped: Systems,
|
||||
Heartbeat: Systems
|
||||
}
|
||||
|
||||
local scheduler_new: (w: World) -> {
|
||||
components: {
|
||||
Disabled: Entity,
|
||||
System: Entity<System>,
|
||||
Phase: Entity,
|
||||
DependsOn: Entity
|
||||
},
|
||||
|
||||
systems: {
|
||||
collect: {
|
||||
under_event: (event: Entity) -> Systems,
|
||||
all: () -> Events
|
||||
},
|
||||
run: (events: Events) -> ()
|
||||
},
|
||||
|
||||
phases: {
|
||||
RenderStepped: Entity,
|
||||
Heartbeat: Entity
|
||||
},
|
||||
|
||||
phase: (after: Entity) -> Entity
|
||||
}
|
||||
|
||||
do
|
||||
local world
|
||||
local Disabled
|
||||
local System
|
||||
local DependsOn
|
||||
local Phase
|
||||
local RenderStepped
|
||||
local Heartbeat
|
||||
|
||||
local function scheduler_collect_systems_under_phase_recursive(systems, phase)
|
||||
for _, system in world:query(System):with(pair(DependsOn, phase)) do
|
||||
table.insert(systems, system)
|
||||
end
|
||||
for dependant in world:query(Phase):with(pair(DependsOn, phase)) do
|
||||
scheduler_collect_systems_under_phase_recursive(systems, dependant)
|
||||
end
|
||||
end
|
||||
|
||||
local function scheduler_collect_systems_under_event(event)
|
||||
local systems = {}
|
||||
scheduler_collect_systems_under_phase_recursive(systems, event)
|
||||
return systems
|
||||
end
|
||||
|
||||
local function scheduler_collect_systems_all()
|
||||
local systems = {
|
||||
RenderStepped = scheduler_collect_systems_under_event(
|
||||
RenderStepped),
|
||||
Heartbeat = scheduler_collect_systems_under_event(
|
||||
Heartbeat)
|
||||
}
|
||||
return systems
|
||||
end
|
||||
|
||||
local function scheduler_run_systems(events)
|
||||
for _, system in events.RenderStepped do
|
||||
system.callback(world)
|
||||
end
|
||||
for _, system in events.Heartbeat do
|
||||
system.callback(world)
|
||||
end
|
||||
end
|
||||
|
||||
local function scheduler_phase_new(after)
|
||||
local phase = world:entity()
|
||||
world:add(phase, Phase)
|
||||
local dependency = pair(DependsOn, after)
|
||||
world:add(phase, dependency)
|
||||
return phase
|
||||
end
|
||||
|
||||
local function scheduler_systems_new(callback, phase)
|
||||
local system = world:entity()
|
||||
world:set(system, System, { callback = callback })
|
||||
world:add(system, pair(DependsOn, phase))
|
||||
return system
|
||||
end
|
||||
|
||||
function scheduler_new(w)
|
||||
world = w
|
||||
Disabled = world:component()
|
||||
System = world:component()
|
||||
Phase = world:component()
|
||||
DependsOn = world:component()
|
||||
|
||||
RenderStepped = world:component()
|
||||
Heartbeat = world:component()
|
||||
|
||||
world:add(RenderStepped, Phase)
|
||||
world:add(Heartbeat, Phase)
|
||||
|
||||
return {
|
||||
phase = scheduler_phase_new,
|
||||
|
||||
phases = {
|
||||
RenderStepped = RenderStepped,
|
||||
Heartbeat = Heartbeat,
|
||||
},
|
||||
|
||||
world = world,
|
||||
|
||||
collect_systems = {
|
||||
under_event = scheduler_collect_systems_under_event,
|
||||
all = scheduler_collect_systems_all
|
||||
},
|
||||
|
||||
run_systems = scheduler_run_systems,
|
||||
|
||||
components = {
|
||||
DependsOn = DependsOn,
|
||||
Disabled = Disabled,
|
||||
Heartbeat = Heartbeat,
|
||||
Phase = Phase,
|
||||
RenderStepped = RenderStepped,
|
||||
System = System,
|
||||
},
|
||||
|
||||
systems = {
|
||||
run = scheduler_run_systems,
|
||||
collect = {
|
||||
under_event = scheduler_collect_systems_under_event,
|
||||
all = scheduler_collect_systems_all
|
||||
},
|
||||
new = scheduler_systems_new,
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
do CASE "event dependant phase"
|
||||
|
||||
local world = jecs.World.new()
|
||||
local scheduler = scheduler_new(world)
|
||||
local components = scheduler.components
|
||||
local phases = scheduler.phases
|
||||
local Heartbeat = phases.Heartbeat
|
||||
local DependsOn = components.DependsOn
|
||||
|
||||
local Physics = scheduler.phase(Heartbeat)
|
||||
CHECK(world:target(Physics, DependsOn) == Heartbeat)
|
||||
end
|
||||
|
||||
do CASE "user-defined sub phases"
|
||||
local world = jecs.World.new()
|
||||
local scheduler = scheduler_new(world)
|
||||
local components = scheduler.components
|
||||
local phases = scheduler.phases
|
||||
local DependsOn = components.DependsOn
|
||||
|
||||
local A = scheduler.phase(phases.Heartbeat)
|
||||
local B = scheduler.phase(A)
|
||||
|
||||
CHECK(world:target(B, DependsOn) == A)
|
||||
end
|
||||
|
||||
do CASE "phase order"
|
||||
local world = jecs.World.new()
|
||||
local scheduler = scheduler_new(world)
|
||||
|
||||
local phases = scheduler.phases
|
||||
local Physics = scheduler.phase(phases.Heartbeat)
|
||||
local Collisions = scheduler.phase(Physics)
|
||||
|
||||
local order = "BEGIN"
|
||||
|
||||
local function move()
|
||||
order ..= "->move"
|
||||
end
|
||||
|
||||
local function hit()
|
||||
order ..= "->hit"
|
||||
end
|
||||
|
||||
local createSystem = scheduler.systems.new
|
||||
|
||||
createSystem(hit, Collisions)
|
||||
createSystem(move, Physics)
|
||||
|
||||
local events = scheduler.systems.collect.all()
|
||||
scheduler.systems.run(events)
|
||||
|
||||
order ..= "->END"
|
||||
|
||||
CHECK(order == "BEGIN->move->hit->END")
|
||||
end
|
||||
|
||||
do CASE "collect only systems under phase recursive"
|
||||
local world = jecs.World.new()
|
||||
local scheduler = scheduler_new(world)
|
||||
local phases = scheduler.phases
|
||||
local Heartbeat = phases.Heartbeat
|
||||
local RenderStepped = phases.RenderStepped
|
||||
local Render = scheduler.phase(RenderStepped)
|
||||
local Physics = scheduler.phase(Heartbeat)
|
||||
local Collisions = scheduler.phase(Physics)
|
||||
|
||||
local function move()
|
||||
end
|
||||
|
||||
local function hit()
|
||||
end
|
||||
|
||||
local function camera()
|
||||
end
|
||||
|
||||
local createSystem = scheduler.systems.new
|
||||
|
||||
createSystem(hit, Collisions)
|
||||
createSystem(move, Physics)
|
||||
createSystem(camera, Render)
|
||||
|
||||
local systems = scheduler.systems.collect.under_event(Collisions)
|
||||
|
||||
CHECK(#systems == 1)
|
||||
CHECK(systems[1].callback == hit)
|
||||
|
||||
systems = scheduler.systems.collect.under_event(Physics)
|
||||
|
||||
CHECK(#systems == 2)
|
||||
|
||||
systems = scheduler.systems.collect.under_event(Heartbeat)
|
||||
|
||||
CHECK(#systems == 2)
|
||||
|
||||
systems = scheduler.systems.collect.under_event(Render)
|
||||
|
||||
CHECK(#systems == 1)
|
||||
CHECK(systems[1].callback == camera)
|
||||
end
|
||||
end)
|
||||
FINISH()
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# Fdb version 4
|
||||
["pdflatex"] 1722383502.05394 "c:/Users/Marcus/Documents/packages/jecs/thesis/drafts/1/paper.tex" "paper.pdf" "paper" 1722383503.46586 0
|
||||
["pdflatex"] 1724165754.00483 "c:/Users/Marcus/Documents/packages/jecs/thesis/drafts/1/paper.tex" "paper.pdf" "paper" 1724165755.21979 0
|
||||
"../../images/archetype_graph.png" 1709688578 50172 8f93f7d24d4920bd8720f4b480771eb4 ""
|
||||
"../../images/insertion.png" 1720373630 158773 c2f9fb7fae25fea3afb7e426b1d318d6 ""
|
||||
"../../images/queries.png" 1720373630 205571 d976c9319fb29ae7dffc46ded3de4e55 ""
|
||||
|
@ -152,12 +152,12 @@
|
|||
"C:/Users/Marcus/AppData/Local/Programs/MiKTeX/tex/latex/url/url.sty" 1388490452 12796 8edb7d69a20b857904dd0ea757c14ec9 ""
|
||||
"C:/Users/Marcus/AppData/Local/Programs/MiKTeX/tex/latex/xcolor/xcolor.sty" 1700127522 55487 80a65caedd3722f4c20a14a69e785d8f ""
|
||||
"C:/Users/Marcus/AppData/Local/Programs/MiKTeX/tex/latex/xkeyval/xkeyval.sty" 1656236919 4937 4ce600ce9bd4ec84d0250eb6892fcf4f ""
|
||||
"c:/Users/Marcus/Documents/packages/jecs/thesis/drafts/1/paper.tex" 1722383500 33628 2358f35913ab57bac270409214a52615 ""
|
||||
"c:/Users/Marcus/Documents/packages/jecs/thesis/drafts/1/paper.tex" 1722532428 33628 2358f35913ab57bac270409214a52615 ""
|
||||
"listings-rust.sty" 1720461559 12349 f346af5561f91e34970cbe0b79654ec2 ""
|
||||
"paper.aux" 1722383503 5596 e71f1baf7c13471206b3537d383c78e2 "pdflatex"
|
||||
"paper.out" 1722383503 3695 a11dbc9d88dd30c22755dc5ebf6964ec "pdflatex"
|
||||
"paper.tex" 1722383500 33628 2358f35913ab57bac270409214a52615 ""
|
||||
"paper.toc" 1722383503 3025 f0a34bc8923dbdfdaeb8258045835a7e "pdflatex"
|
||||
"paper.aux" 1724165755 5596 e71f1baf7c13471206b3537d383c78e2 "pdflatex"
|
||||
"paper.out" 1724165755 3695 a11dbc9d88dd30c22755dc5ebf6964ec "pdflatex"
|
||||
"paper.tex" 1722532428 33628 2358f35913ab57bac270409214a52615 ""
|
||||
"paper.toc" 1724165755 3025 f0a34bc8923dbdfdaeb8258045835a7e "pdflatex"
|
||||
(generated)
|
||||
"paper.aux"
|
||||
"paper.log"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (MiKTeX 24.1) (preloaded format=pdflatex 2024.4.4) 31 JUL 2024 01:51
|
||||
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (MiKTeX 24.1) (preloaded format=pdflatex 2024.4.4) 20 AUG 2024 16:55
|
||||
entering extended mode
|
||||
restricted \write18 enabled.
|
||||
file:line:error style messages enabled.
|
||||
|
|
Binary file not shown.
Loading…
Reference in a new issue