mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Add start
This commit is contained in:
parent
e7bbf08713
commit
17415f27f3
7 changed files with 74 additions and 42 deletions
BIN
demo.rbxl
BIN
demo.rbxl
Binary file not shown.
|
@ -1,8 +1,5 @@
|
||||||
local jecs = require(game:GetService("ReplicatedStorage").ecs)
|
local jecs = require(game:GetService("ReplicatedStorage").ecs)
|
||||||
|
type World = jecs.World
|
||||||
local world = require(script.Parent.world)
|
|
||||||
local sparse = ((world :: any) :: jecs.World).entityIndex.sparse
|
|
||||||
type World = world.World
|
|
||||||
|
|
||||||
type Tracker<T> = { track: (world: World, fn: (changes: {
|
type Tracker<T> = { track: (world: World, fn: (changes: {
|
||||||
added: () -> () -> (number, T),
|
added: () -> () -> (number, T),
|
||||||
|
@ -32,7 +29,8 @@ local function diff(a, b)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local function ChangeTracker<T>(T: Entity<T>): Tracker<T>
|
local function ChangeTracker<T>(world: World, T: Entity<T>): Tracker<T>
|
||||||
|
local sparse = world.entityIndex.sparse
|
||||||
local PreviousT = jecs.pair(jecs.Rest, T)
|
local PreviousT = jecs.pair(jecs.Rest, T)
|
||||||
local add = {}
|
local add = {}
|
||||||
local added
|
local added
|
||||||
|
|
|
@ -3,12 +3,9 @@ local jecs = require(game:GetService("ReplicatedStorage").ecs)
|
||||||
local world = require(script.world) :: jecs.World
|
local world = require(script.world) :: jecs.World
|
||||||
export type World = jecs.World
|
export type World = jecs.World
|
||||||
|
|
||||||
local scheduler = require(script.scheduler)
|
|
||||||
export type Scheduler = scheduler.Scheduler
|
|
||||||
|
|
||||||
local std = {
|
local std = {
|
||||||
ChangeTracker = require(script.changetracker),
|
ChangeTracker = require(script.changetracker),
|
||||||
Scheduler = scheduler.new(world),
|
Scheduler = require(script.scheduler),
|
||||||
bt = require(script.bt),
|
bt = require(script.bt),
|
||||||
collect = require(script.collect),
|
collect = require(script.collect),
|
||||||
components = require(script.components),
|
components = require(script.components),
|
||||||
|
@ -19,6 +16,7 @@ local std = {
|
||||||
world = world :: World,
|
world = world :: World,
|
||||||
pair = jecs.pair,
|
pair = jecs.pair,
|
||||||
__ = jecs.w,
|
__ = jecs.w,
|
||||||
|
start = require(script.start)
|
||||||
}
|
}
|
||||||
|
|
||||||
return std
|
return std
|
||||||
|
|
|
@ -10,9 +10,7 @@ type Entity<T=nil> = jecs.Entity<T>
|
||||||
|
|
||||||
type System = {
|
type System = {
|
||||||
callback: (world: World) -> (),
|
callback: (world: World) -> (),
|
||||||
name: string,
|
id: number,
|
||||||
rate: number?,
|
|
||||||
interval: number?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Systems = { System }
|
type Systems = { System }
|
||||||
|
@ -71,9 +69,14 @@ do
|
||||||
local system: System
|
local system: System
|
||||||
local dt
|
local dt
|
||||||
local function run()
|
local function run()
|
||||||
debug.profilebegin(system.name)
|
local id = system.id
|
||||||
|
local system_data = scheduler.system_data[id]
|
||||||
|
if system_data.paused then return end
|
||||||
|
|
||||||
|
scheduler:mark_system_frame_start(id)
|
||||||
system.callback(dt)
|
system.callback(dt)
|
||||||
debug.profileend()
|
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
|
||||||
|
@ -90,7 +93,25 @@ do
|
||||||
|
|
||||||
debug.profilebegin(event_name)
|
debug.profilebegin(event_name)
|
||||||
for _, sys in systems do
|
for _, sys in systems do
|
||||||
scheduler:run(sys.id, sys.system, dt)
|
system = sys
|
||||||
|
local didNotYield, why = xpcall(function()
|
||||||
|
for _ in run do end
|
||||||
|
end, debug.traceback)
|
||||||
|
|
||||||
|
if didNotYield then
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
|
||||||
|
if string.find(why, "thread is not yieldable") then
|
||||||
|
panic("Not allowed to yield in the systems."
|
||||||
|
.. "\n"
|
||||||
|
.. "System: "
|
||||||
|
.. debug.info(system.callback, "n")
|
||||||
|
.. " has been ejected"
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
panic(why)
|
||||||
end
|
end
|
||||||
debug.profileend()
|
debug.profileend()
|
||||||
end
|
end
|
||||||
|
@ -107,7 +128,7 @@ do
|
||||||
name = system.name,
|
name = system.name,
|
||||||
phase = phase_name
|
phase = phase_name
|
||||||
}),
|
}),
|
||||||
system = system.callback
|
callback = system.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
|
||||||
|
@ -166,7 +187,6 @@ do
|
||||||
end
|
end
|
||||||
|
|
||||||
world:add(Heartbeat, Phase)
|
world:add(Heartbeat, Phase)
|
||||||
world:set(Heartbeat, Name)
|
|
||||||
world:set(Heartbeat, Event, RunService.Heartbeat)
|
world:set(Heartbeat, Event, RunService.Heartbeat)
|
||||||
|
|
||||||
world:add(PreSimulation, Phase)
|
world:add(PreSimulation, Phase)
|
||||||
|
@ -174,20 +194,20 @@ 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
|
||||||
|
world:set(component, Name, name)
|
||||||
|
end
|
||||||
|
|
||||||
jabby.public.updated = true
|
|
||||||
table.insert(jabby.public, {
|
table.insert(jabby.public, {
|
||||||
class_name = "World",
|
class_name = "World",
|
||||||
name = "MyWorld",
|
name = "MyWorld",
|
||||||
world = world,
|
world = world,
|
||||||
debug = Name,
|
debug = Name,
|
||||||
entities = {}
|
entities = {}
|
||||||
})
|
} )
|
||||||
for name, component in components do
|
|
||||||
world:set(component, Name, name)
|
|
||||||
print(Name, name)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
jabby.public.updated = true
|
||||||
scheduler = jabby.scheduler.create("scheduler")
|
scheduler = jabby.scheduler.create("scheduler")
|
||||||
|
|
||||||
table.insert(jabby.public, scheduler)
|
table.insert(jabby.public, scheduler)
|
||||||
|
|
31
demo/src/ReplicatedStorage/std/start.luau
Normal file
31
demo/src/ReplicatedStorage/std/start.luau
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
local RunService = game:GetService("RunService")
|
||||||
|
local UserInputService = game:GetService("UserInputService")
|
||||||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
|
local jabby = require(ReplicatedStorage.Packages.jabby)
|
||||||
|
local Scheduler = require(ReplicatedStorage.std.scheduler)
|
||||||
|
local world = require(ReplicatedStorage.std.world)
|
||||||
|
|
||||||
|
local function start(modules)
|
||||||
|
local scheduler = Scheduler.new(world)
|
||||||
|
for _, module in modules do
|
||||||
|
require(module)(scheduler)
|
||||||
|
end
|
||||||
|
local events = scheduler.collect.all()
|
||||||
|
scheduler.systems.begin(events)
|
||||||
|
if RunService:IsClient() then
|
||||||
|
local client = jabby.obtain_client()
|
||||||
|
local player = game:GetService("Players").LocalPlayer
|
||||||
|
local apps = player:WaitForChild("PlayerGui")
|
||||||
|
local dtor
|
||||||
|
UserInputService.InputBegan:Connect(function(input)
|
||||||
|
if input.KeyCode == Enum.KeyCode.F4 then
|
||||||
|
if dtor then
|
||||||
|
dtor()
|
||||||
|
end
|
||||||
|
dtor = client.spawn_app(client.apps.home)
|
||||||
|
end
|
||||||
|
end )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return start
|
|
@ -1,11 +1,4 @@
|
||||||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
local std = require(ReplicatedStorage.std)
|
local start = require(ReplicatedStorage.std.start)
|
||||||
local components = std.components
|
|
||||||
local world = std.world
|
|
||||||
|
|
||||||
local scheduler = std.Scheduler
|
start(script.Parent:WaitForChild("systems"):GetChildren())
|
||||||
for _, module in script.Parent.systems:GetChildren() do
|
|
||||||
require(module)(scheduler)
|
|
||||||
end
|
|
||||||
local events = scheduler.collect.all()
|
|
||||||
scheduler.systems.begin(events)
|
|
||||||
|
|
|
@ -1,12 +1,4 @@
|
||||||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
local std = require(ReplicatedStorage.std)
|
local start = require(ReplicatedStorage.std.start)
|
||||||
local scheduler = std.Scheduler
|
|
||||||
for _, module in script.Parent:WaitForChild("systems"):GetChildren() do
|
|
||||||
require(module)(scheduler)
|
|
||||||
end
|
|
||||||
local events = scheduler.collect.all()
|
|
||||||
|
|
||||||
scheduler.systems.begin(events)
|
start(script.Parent:WaitForChild("systems"):GetChildren())
|
||||||
local jabby = require(ReplicatedStorage.Packages.jabby)
|
|
||||||
local client = jabby.obtain_client()
|
|
||||||
client.spawn_app(client.apps.home)
|
|
||||||
|
|
Loading…
Reference in a new issue