mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Add a hooks cache
This commit is contained in:
parent
e234bd82ee
commit
1503d7e462
5 changed files with 657 additions and 483 deletions
76
demo/default.project.json
Normal file
76
demo/default.project.json
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
{
|
||||||
|
"name": "demo",
|
||||||
|
"tree": {
|
||||||
|
"$className": "DataModel",
|
||||||
|
"ReplicatedStorage": {
|
||||||
|
"$className": "ReplicatedStorage",
|
||||||
|
"$path": "src/ReplicatedStorage",
|
||||||
|
"ecs": {
|
||||||
|
"$path": "../src"
|
||||||
|
},
|
||||||
|
"net": {
|
||||||
|
"$path": "net/client.luau"
|
||||||
|
},
|
||||||
|
"Packages": {
|
||||||
|
"$path": "Packages"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ServerScriptService": {
|
||||||
|
"$className": "ServerScriptService",
|
||||||
|
"$path": "src/ServerScriptService",
|
||||||
|
"net": {
|
||||||
|
"$path": "net/server.luau"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Workspace": {
|
||||||
|
"$properties": {
|
||||||
|
"FilteringEnabled": true
|
||||||
|
},
|
||||||
|
"Baseplate": {
|
||||||
|
"$className": "Part",
|
||||||
|
"$properties": {
|
||||||
|
"Anchored": true,
|
||||||
|
"Color": [
|
||||||
|
0.38823,
|
||||||
|
0.37254,
|
||||||
|
0.38823
|
||||||
|
],
|
||||||
|
"Locked": true,
|
||||||
|
"Position": [
|
||||||
|
0,
|
||||||
|
-10,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"Size": [
|
||||||
|
512,
|
||||||
|
20,
|
||||||
|
512
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Lighting": {
|
||||||
|
"$properties": {
|
||||||
|
"Ambient": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"Brightness": 2,
|
||||||
|
"GlobalShadows": true,
|
||||||
|
"Outlines": false,
|
||||||
|
"Technology": "Voxel"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"SoundService": {
|
||||||
|
"$properties": {
|
||||||
|
"RespectFilteringEnabled": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"StarterPlayer": {
|
||||||
|
"StarterPlayerScripts": {
|
||||||
|
"$path": "src/StarterPlayer/StarterPlayerScripts"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
demo/src/ReplicatedStorage/std/hooks.luau
Normal file
32
demo/src/ReplicatedStorage/std/hooks.luau
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
--!native
|
||||||
|
--!optimize 2
|
||||||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
|
local jecs = require(ReplicatedStorage.ecs)
|
||||||
|
|
||||||
|
local function create_cache(hook)
|
||||||
|
local columns = setmetatable({}, {
|
||||||
|
__index = function(self, component)
|
||||||
|
local column = {}
|
||||||
|
self[component] = column
|
||||||
|
return column
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
return function(world, component, fn)
|
||||||
|
local column = columns[component]
|
||||||
|
table.insert(column, fn)
|
||||||
|
world:set(component, hook, function(entity, value)
|
||||||
|
for _, callback in column do
|
||||||
|
callback(entity, value)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local hooks = {
|
||||||
|
OnSet = create_cache(jecs.OnSet),
|
||||||
|
OnAdd = create_cache(jecs.OnAdd),
|
||||||
|
OnRemove = create_cache(jecs.OnRemove)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hooks
|
|
@ -19,6 +19,7 @@ local std = {
|
||||||
world = world :: World,
|
world = world :: World,
|
||||||
pair = jecs.pair,
|
pair = jecs.pair,
|
||||||
__ = jecs.w,
|
__ = jecs.w,
|
||||||
|
hooks = require(script.hooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
return std
|
return std
|
||||||
|
|
|
@ -4,6 +4,8 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
local jabby = require(ReplicatedStorage.Packages.jabby)
|
local jabby = require(ReplicatedStorage.Packages.jabby)
|
||||||
local jecs = require(ReplicatedStorage.ecs)
|
local jecs = require(ReplicatedStorage.ecs)
|
||||||
local pair = jecs.pair
|
local pair = jecs.pair
|
||||||
|
local Name = jecs.Name
|
||||||
|
|
||||||
type World = jecs.World
|
type World = jecs.World
|
||||||
type Entity<T=nil> = jecs.Entity<T>
|
type Entity<T=nil> = jecs.Entity<T>
|
||||||
|
|
||||||
|
@ -43,20 +45,20 @@ export type Scheduler = {
|
||||||
Heartbeat: Entity,
|
Heartbeat: Entity,
|
||||||
},
|
},
|
||||||
|
|
||||||
phase: (after: Entity) -> Entity
|
phase: (after: Entity) -> Entity,
|
||||||
|
|
||||||
|
debugging: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
local scheduler_new: (w: World, components: { [string]: Entity }) -> Scheduler
|
local scheduler_new: (w: World, components: { [string]: Entity }) -> Scheduler
|
||||||
|
|
||||||
|
|
||||||
do
|
do
|
||||||
local world: World
|
local world: World
|
||||||
local Disabled: Entity
|
local Disabled: Entity
|
||||||
local System: Entity<{}>
|
local System: Entity<System>
|
||||||
local DependsOn
|
local DependsOn: Entity
|
||||||
local Phase
|
local Phase: Entity
|
||||||
local Event
|
local Event: Entity<RBXScriptSignal>
|
||||||
local Name
|
|
||||||
|
|
||||||
local scheduler
|
local scheduler
|
||||||
|
|
||||||
|
@ -65,18 +67,19 @@ do
|
||||||
local PreAnimation
|
local PreAnimation
|
||||||
local PreSimulation
|
local PreSimulation
|
||||||
|
|
||||||
local system: System
|
local sys: System
|
||||||
local dt
|
local dt
|
||||||
|
|
||||||
local function run()
|
local function run()
|
||||||
local id = system.id
|
local id = sys.id
|
||||||
local system_data = scheduler.system_data[id]
|
local system_data = scheduler.system_data[id]
|
||||||
if system_data.paused then return end
|
if system_data.paused then return end
|
||||||
|
|
||||||
scheduler:mark_system_frame_start(id)
|
scheduler:mark_system_frame_start(id)
|
||||||
system.callback(dt)
|
sys.callback(dt)
|
||||||
scheduler:mark_system_frame_end(id)
|
scheduler:mark_system_frame_end(id)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function panic(str)
|
local function panic(str)
|
||||||
-- We don't want to interrupt the loop when we error
|
-- We don't want to interrupt the loop when we error
|
||||||
task.spawn(error, str)
|
task.spawn(error, str)
|
||||||
|
@ -91,10 +94,10 @@ do
|
||||||
dt = event:Wait()
|
dt = event:Wait()
|
||||||
|
|
||||||
debug.profilebegin(event_name)
|
debug.profilebegin(event_name)
|
||||||
for _, sys in systems do
|
for _, s in systems do
|
||||||
system = sys
|
sys = s
|
||||||
local didNotYield, why = xpcall(function()
|
local didNotYield, why = xpcall(function()
|
||||||
for _ in run do end
|
for _ in run do break end
|
||||||
end, debug.traceback)
|
end, debug.traceback)
|
||||||
|
|
||||||
if didNotYield then
|
if didNotYield then
|
||||||
|
@ -105,7 +108,7 @@ do
|
||||||
panic("Not allowed to yield in the systems."
|
panic("Not allowed to yield in the systems."
|
||||||
.. "\n"
|
.. "\n"
|
||||||
.. "System: "
|
.. "System: "
|
||||||
.. debug.info(system.callback, "n")
|
.. debug.info(s.callback, "n")
|
||||||
.. " has been ejected"
|
.. " has been ejected"
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
@ -121,13 +124,13 @@ do
|
||||||
|
|
||||||
local function scheduler_collect_systems_under_phase_recursive(systems, phase)
|
local function scheduler_collect_systems_under_phase_recursive(systems, phase)
|
||||||
local phase_name = world:get(phase, Name)
|
local phase_name = world:get(phase, Name)
|
||||||
for _, system in world:query(System):with(pair(DependsOn, phase)) do
|
for _, s in world:query(System):with(pair(DependsOn, phase)) do
|
||||||
table.insert(systems, {
|
table.insert(systems, {
|
||||||
id = scheduler:register_system({
|
id = scheduler:register_system({
|
||||||
name = system.name,
|
name = s.name,
|
||||||
phase = phase_name
|
phase = phase_name
|
||||||
}),
|
}),
|
||||||
callback = system.callback
|
callback = s.callback
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
for after in world:query(Phase):with(pair(DependsOn, phase)) do
|
for after in world:query(Phase):with(pair(DependsOn, phase)) do
|
||||||
|
@ -172,7 +175,6 @@ do
|
||||||
Phase = world:component()
|
Phase = world:component()
|
||||||
DependsOn = world:component()
|
DependsOn = world:component()
|
||||||
Event = world:component()
|
Event = world:component()
|
||||||
Name = world:component()
|
|
||||||
|
|
||||||
RenderStepped = world:component()
|
RenderStepped = world:component()
|
||||||
Heartbeat = world:component()
|
Heartbeat = world:component()
|
||||||
|
@ -193,6 +195,7 @@ do
|
||||||
|
|
||||||
world:add(PreAnimation, Phase)
|
world:add(PreAnimation, Phase)
|
||||||
world:set(PreAnimation, Event, RunService.PreAnimation)
|
world:set(PreAnimation, Event, RunService.PreAnimation)
|
||||||
|
|
||||||
for name, component in components do
|
for name, component in components do
|
||||||
world:set(component, Name, name)
|
world:set(component, Name, name)
|
||||||
end
|
end
|
||||||
|
@ -210,6 +213,7 @@ do
|
||||||
scheduler = jabby.scheduler.create("scheduler")
|
scheduler = jabby.scheduler.create("scheduler")
|
||||||
|
|
||||||
table.insert(jabby.public, scheduler)
|
table.insert(jabby.public, scheduler)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
phase = scheduler_phase_new,
|
phase = scheduler_phase_new,
|
||||||
|
|
||||||
|
|
987
src/init.luau
987
src/init.luau
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue