mirror of
https://github.com/Ukendio/jecs.git
synced 2025-08-05 03:39:17 +00:00
Compare commits
10 commits
cb238ab29e
...
d3b032666b
Author | SHA1 | Date | |
---|---|---|---|
|
d3b032666b | ||
|
b1a2bc48a7 | ||
|
1002139fc9 | ||
|
d237b176e6 | ||
|
0816c645da | ||
|
6a92d4e3c6 | ||
|
50363ac474 | ||
|
5b9c032451 | ||
|
5b31a1af22 | ||
|
846722ba58 |
5 changed files with 179 additions and 46 deletions
|
@ -11,6 +11,7 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
|
|||
## [Unreleased]
|
||||
|
||||
- `[world]`:
|
||||
- Added `world:range` to allow for creating
|
||||
- Changed `world:clear` to also look through the component record for the cleared `ID`
|
||||
- Removes the cleared ID from every entity that has it
|
||||
- Changed entity ID layouts by putting the index in the lower bits, which should make every world function 1-5 nanoseconds faster
|
||||
|
|
|
@ -24,7 +24,7 @@ local function observers_new(world, description)
|
|||
end
|
||||
|
||||
local entity_index = world.entity_index :: any
|
||||
local function emplaced(entity: jecs.Entity)
|
||||
local function emplaced(entity, id, value)
|
||||
local r = jecs.entity_index_try_get_fast(
|
||||
entity_index, entity :: any)
|
||||
|
||||
|
@ -45,6 +45,106 @@ local function observers_new(world, description)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
local function join(world, component)
|
||||
local sparse_array = {}
|
||||
local dense_array = {}
|
||||
local values = {}
|
||||
local max_id = 0
|
||||
|
||||
world:added(component, function(entity, id, value)
|
||||
max_id += 1
|
||||
sparse_array[entity] = max_id
|
||||
dense_array[max_id] = entity
|
||||
values[max_id] = value
|
||||
end)
|
||||
|
||||
world:removed(component, function(entity, id)
|
||||
local e_swap = dense_array[max_id]
|
||||
local v_swap = values[max_id]
|
||||
|
||||
local dense = sparse_array[entity]
|
||||
dense_array[dense] = e_swap
|
||||
values[dense] = v_swap
|
||||
|
||||
sparse_array[entity] = nil
|
||||
dense_array[max_id] = nil
|
||||
values[max_id] = nil
|
||||
end)
|
||||
|
||||
world:changed(component, function(entity, id, value)
|
||||
values[sparse_array[entity]] = value
|
||||
end)
|
||||
|
||||
return function()
|
||||
local i = max_id
|
||||
return function(): ...any
|
||||
i -= 1
|
||||
if i == 0 then
|
||||
return nil
|
||||
end
|
||||
local e = dense_array[i]
|
||||
return e, values[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function query_changed(world, component)
|
||||
assert(jecs.IS_PAIR(component) == false)
|
||||
local callerid = debug.info(2, "sl")
|
||||
|
||||
local tracker = world.trackers[callerid]
|
||||
if not tracker then
|
||||
local records = {}
|
||||
local connections = {}
|
||||
tracker = {
|
||||
records = records,
|
||||
connections = connections
|
||||
}
|
||||
world.trackers[callerid] = tracker
|
||||
|
||||
table.insert(connections, world:added(component, function(entity, id, v)
|
||||
tracker[entity] = {
|
||||
new = v
|
||||
}
|
||||
end))
|
||||
table.insert(connections, world:changed(component, function(entity, id, v)
|
||||
local record = tracker[entity]
|
||||
record.old = record.new
|
||||
record.new = v
|
||||
end))
|
||||
|
||||
table.insert(connections, world:removed(component, function(entity, id)
|
||||
local record = tracker[entity]
|
||||
record.old = record.new
|
||||
record.new = nil
|
||||
end))
|
||||
end
|
||||
|
||||
local entity = nil
|
||||
local record = nil
|
||||
return function()
|
||||
entity, record = next(tracker, entity)
|
||||
if entity == nil then
|
||||
return
|
||||
end
|
||||
return entity, record
|
||||
end
|
||||
end
|
||||
|
||||
local function spy_on_world_delete(world)
|
||||
local world_delete = world.delete
|
||||
world.delete = function(world, entity)
|
||||
world_delete(world, entity)
|
||||
for _, tracker in world.trackers do
|
||||
tracker.records[entity] = nil
|
||||
for _, connection in tracker.connections do
|
||||
connection()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function monitors_new(world, description)
|
||||
local query = description.query
|
||||
local callback = description.callback
|
||||
|
@ -93,10 +193,11 @@ local function monitors_new(world, description)
|
|||
end
|
||||
|
||||
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld
|
||||
type Signal = { [jecs.Entity]: { (...any) -> () } }
|
||||
local signals = {
|
||||
added = {},
|
||||
emplaced = {},
|
||||
removed = {}
|
||||
added = {} :: Signal,
|
||||
emplaced = {} :: Signal,
|
||||
removed = {} :: Signal
|
||||
}
|
||||
|
||||
world.added = function(_, component, fn)
|
||||
|
@ -115,6 +216,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
world:set(component, jecs.OnAdd, on_add)
|
||||
end
|
||||
table.insert(listeners, fn)
|
||||
return function()
|
||||
local n = #listeners
|
||||
local i = table.find(listeners, fn)
|
||||
listeners[i] = listeners[n]
|
||||
listeners[n] = nil
|
||||
end
|
||||
end
|
||||
|
||||
world.changed = function(_, component, fn)
|
||||
|
@ -132,6 +239,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
world:set(component, jecs.OnChange, on_change)
|
||||
end
|
||||
table.insert(listeners, fn)
|
||||
return function()
|
||||
local n = #listeners
|
||||
local i = table.find(listeners, fn)
|
||||
listeners[i] = listeners[n]
|
||||
listeners[n] = nil
|
||||
end
|
||||
end
|
||||
|
||||
world.removed = function(_, component, fn)
|
||||
|
@ -149,6 +262,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
world:set(component, jecs.OnRemove, on_remove)
|
||||
end
|
||||
table.insert(listeners, fn)
|
||||
return function()
|
||||
local n = #listeners
|
||||
local i = table.find(listeners, fn)
|
||||
listeners[i] = listeners[n]
|
||||
listeners[n] = nil
|
||||
end
|
||||
end
|
||||
|
||||
world.signals = signals
|
||||
|
@ -157,6 +276,8 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
|
||||
world.monitor = monitors_new
|
||||
|
||||
world.trackers = {}
|
||||
|
||||
return world :: PatchedWorld
|
||||
end
|
||||
|
||||
|
|
|
@ -5,23 +5,29 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|||
local jecs = require(ReplicatedStorage.Lib:Clone())
|
||||
local mirror = require(ReplicatedStorage.mirror:Clone())
|
||||
|
||||
local ecs = jecs.World.new()
|
||||
local mcs = mirror.world()
|
||||
|
||||
|
||||
return {
|
||||
ParameterGenerator = function()
|
||||
local ecs = jecs.world()
|
||||
ecs:range(1000, 20000)
|
||||
local mcs = mirror.World.new()
|
||||
return ecs, mcs
|
||||
end,
|
||||
|
||||
Functions = {
|
||||
Mirror = function()
|
||||
for i = 1000, 1100 do
|
||||
mcs:entity(i)
|
||||
Mirror = function(_, ecs, mcs)
|
||||
for i = 1, 100 do
|
||||
|
||||
mcs:entity()
|
||||
end
|
||||
end,
|
||||
Jecs = function()
|
||||
for i = 1, 1000 do
|
||||
ecs:entity(i)
|
||||
|
||||
Jecs = function(_, ecs, mcs)
|
||||
for i = 1, 100 do
|
||||
|
||||
ecs:entity()
|
||||
end
|
||||
end,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
49
jecs.luau
49
jecs.luau
|
@ -120,7 +120,6 @@ local ECS_GENERATION_MASK = bit32.lshift(1, 16)
|
|||
|
||||
local NULL_ARRAY = table.freeze({}) :: Column
|
||||
local NULL = newproxy(false)
|
||||
local NULL_RECORD = table.freeze({ dense = 0 }) :: ecs_record_t
|
||||
|
||||
local ECS_INTERNAL_ERROR = [[
|
||||
This is an internal error, please file a bug report via the following link:
|
||||
|
@ -128,6 +127,12 @@ local ECS_INTERNAL_ERROR = [[
|
|||
https://github.com/Ukendio/jecs/issues/new?template=BUG-REPORT.md
|
||||
]]
|
||||
|
||||
local function ecs_assert(condition, msg: string?)
|
||||
if not condition then
|
||||
error(msg)
|
||||
end
|
||||
end
|
||||
|
||||
local ecs_metadata: Map<i53, Map<i53, any>> = {}
|
||||
local ecs_max_component_id = 0
|
||||
local ecs_max_tag_id = EcsRest
|
||||
|
@ -287,6 +292,8 @@ local function ecs_get_alive(world, entity)
|
|||
return current
|
||||
end
|
||||
|
||||
local ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY = "Entity is outside range"
|
||||
|
||||
local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
|
||||
local dense_array = entity_index.dense_array
|
||||
local alive_count = entity_index.alive_count
|
||||
|
@ -302,9 +309,8 @@ local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
|
|||
|
||||
local id = max_id + 1
|
||||
local range_end = entity_index.range_end
|
||||
if range_end then
|
||||
assert(id < range_end)
|
||||
end
|
||||
ecs_assert(range_end == nil or id < range_end, ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY)
|
||||
|
||||
entity_index.max_id = id
|
||||
alive_count += 1
|
||||
entity_index.alive_count = alive_count
|
||||
|
@ -596,10 +602,10 @@ local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t
|
|||
local is_pair = ECS_IS_PAIR(id)
|
||||
if is_pair then
|
||||
relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53
|
||||
assert(relation and entity_index_is_alive(
|
||||
ecs_assert(relation and entity_index_is_alive(
|
||||
entity_index, relation), ECS_INTERNAL_ERROR)
|
||||
target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53
|
||||
assert(target and entity_index_is_alive(
|
||||
ecs_assert(target and entity_index_is_alive(
|
||||
entity_index, target), ECS_INTERNAL_ERROR)
|
||||
end
|
||||
|
||||
|
@ -734,37 +740,31 @@ end
|
|||
|
||||
local function world_range(world: ecs_world_t, range_begin: number, range_end: number?)
|
||||
local entity_index = world.entity_index
|
||||
if range_end then
|
||||
range_end += 1
|
||||
end
|
||||
|
||||
entity_index.range_begin = range_begin
|
||||
entity_index.range_end = range_end
|
||||
|
||||
local max_id = entity_index.max_id
|
||||
|
||||
if range_begin > max_id then
|
||||
local dense_array = entity_index.dense_array
|
||||
local sparse_array = entity_index.sparse_array
|
||||
|
||||
if range_begin > max_id then
|
||||
for i = max_id, range_begin - 1 do
|
||||
dense_array[i] = 0
|
||||
sparse_array[i] = NULL_RECORD
|
||||
for i = max_id, range_begin do
|
||||
dense_array[i] = i
|
||||
sparse_array[i] = {
|
||||
dense = 0
|
||||
} :: ecs_record_t
|
||||
end
|
||||
sparse_array[range_begin] = { dense = 0 } :: ecs_record_t
|
||||
entity_index.max_id = range_begin - 1
|
||||
entity_index.alive_count = range_begin - 1
|
||||
end
|
||||
|
||||
entity_index.max_id = range_begin
|
||||
entity_index.alive_count = range_begin
|
||||
end
|
||||
|
||||
local function world_entity(world: ecs_world_t, entity: i53?): i53
|
||||
local entity_index = world.entity_index
|
||||
if entity then
|
||||
local index = ECS_ID(entity)
|
||||
local range_begin = entity_index.range_begin
|
||||
if range_begin then
|
||||
assert(index > range_begin)
|
||||
end
|
||||
local max_id = entity_index.max_id
|
||||
local sparse_array = entity_index.sparse_array
|
||||
local dense_array = entity_index.dense_array
|
||||
|
@ -805,10 +805,6 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
|
|||
dense_array[dense] = e_swap
|
||||
return any
|
||||
else
|
||||
local range_end = entity_index.range_end
|
||||
if range_end then
|
||||
assert(index < range_end)
|
||||
end
|
||||
for i = max_id + 1, index do
|
||||
sparse_array[i] = { dense = i } :: ecs_record_t
|
||||
dense_array[i] = i
|
||||
|
@ -2572,6 +2568,9 @@ export type World = {
|
|||
|
||||
observable: any,
|
||||
|
||||
--- Enforce a check on entities to be created within desired range
|
||||
range: (self: World, range_begin: number, range_end: number?) -> (),
|
||||
|
||||
--- Creates a new entity
|
||||
entity: (self: World, id: Entity?) -> Entity,
|
||||
--- Creates a new entity located in the first 256 ids.
|
||||
|
|
|
@ -633,24 +633,30 @@ TEST("world:each()", function()
|
|||
end
|
||||
end)
|
||||
|
||||
TEST("world:entity()", function()
|
||||
do CASE "range"
|
||||
TEST("world:range()", function()
|
||||
do CASE ""
|
||||
local world = jecs.world()
|
||||
world = lifetime_tracker_add(world, {})
|
||||
world:range(1000, 2000)
|
||||
local e = world:entity()
|
||||
CHECK(e == 1000)
|
||||
|
||||
world:entity(1590)
|
||||
CHECK_EXPECT_ERR(function()
|
||||
|
||||
world:entity(5000)
|
||||
end)
|
||||
CHECK(world:entity(5000) == 5000)
|
||||
|
||||
CHECK(world:contains(1590))
|
||||
world:set(591, jecs.Name, "9888")
|
||||
CHECK(not world:contains(591))
|
||||
CHECK(not world:contains(5000))
|
||||
CHECK(world:contains(5000))
|
||||
CHECK(not world:contains(988))
|
||||
|
||||
local e = world:entity(2000)
|
||||
CHECK(e == 2000)
|
||||
end
|
||||
end)
|
||||
|
||||
TEST("world:entity()", function()
|
||||
do CASE "desired id"
|
||||
local world = jecs.world()
|
||||
local id = world:entity()
|
||||
|
|
Loading…
Reference in a new issue