Update scheduler

This commit is contained in:
Ukendio 2024-08-27 21:27:26 +02:00
parent dcfa34df4e
commit 8d83bd6322

View file

@ -6,7 +6,10 @@ type World = jecs.World
type Entity<T=nil> = jecs.Entity<T>
type System = {
callback: (world: World) -> ()
callback: (world: World) -> (),
name: string,
rate: number?,
interval: number?
}
type Systems = { System }
@ -31,7 +34,7 @@ export type Scheduler = {
},
systems: {
begin: (events: Events) -> (),
begin: (events: Events) -> { [Entity]: thread },
new: (callback: (dt: number) -> (), phase: Entity) -> Entity
},
@ -59,7 +62,7 @@ do
local PreAnimation
local PreSimulation
local system
local system: System
local dt
local function run()
debug.profilebegin(system.name)
@ -70,17 +73,18 @@ do
-- We don't want to interrupt the loop when we error
task.spawn(error, str)
end
local function begin(events)
local connections = {}
local function begin(events: { Systems })
local threads = {}
for event, systems in events do
if not event then continue end
local event_name = tostring(event)
connections[event] = event:Connect(function(last)
threads[event] = task.spawn(function()
while true do
dt = event:Wait()
debug.profilebegin(event_name)
for _, sys in systems do
system = sys
dt = last
local didNotYield, why = xpcall(function()
for _ in run do end
@ -97,17 +101,18 @@ do
end
end
debug.profileend()
end
end)
end
return connections
return threads
end
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)
for after in world:query(Phase):with(pair(DependsOn, phase)) do
scheduler_collect_systems_under_phase_recursive(systems, after)
end
end