mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Merge cb238ab29e
into 8822be58a9
This commit is contained in:
commit
e8572c7351
8 changed files with 209 additions and 48 deletions
|
@ -106,12 +106,14 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
||||||
if not listeners then
|
if not listeners then
|
||||||
listeners = {}
|
listeners = {}
|
||||||
signals.added[component] = listeners
|
signals.added[component] = listeners
|
||||||
|
|
||||||
local function on_add(entity: number, id: number, value: any)
|
local function on_add(entity: number, id: number, value: any)
|
||||||
for _, listener in listeners :: any do
|
for _, listener in listeners :: any do
|
||||||
listener(entity, id, value)
|
listener(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
world:set(component, jecs.OnAdd, on_add) end
|
world:set(component, jecs.OnAdd, on_add)
|
||||||
|
end
|
||||||
table.insert(listeners, fn)
|
table.insert(listeners, fn)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,9 @@
|
||||||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
local Matter = require(ReplicatedStorage.DevPackages.Matter)
|
local Matter = require(ReplicatedStorage.DevPackages.Matter)
|
||||||
local ecr = require(ReplicatedStorage.DevPackages.ecr)
|
local ecr = require(ReplicatedStorage.DevPackages.ecr)
|
||||||
local jecs = require(ReplicatedStorage.Lib)
|
local jecs = require(ReplicatedStorage.Lib:Clone())
|
||||||
local newWorld = Matter.World.new()
|
|
||||||
local ecs = jecs.World.new()
|
local ecs = jecs.World.new()
|
||||||
local mirror = require(ReplicatedStorage.mirror)
|
local mirror = require(ReplicatedStorage.mirror:Clone())
|
||||||
local mcs = mirror.World.new()
|
local mcs = mirror.World.new()
|
||||||
|
|
||||||
local A1 = Matter.component()
|
local A1 = Matter.component()
|
||||||
|
|
|
@ -2,35 +2,25 @@
|
||||||
--!native
|
--!native
|
||||||
|
|
||||||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
local Matter = require(ReplicatedStorage.DevPackages.Matter)
|
local jecs = require(ReplicatedStorage.Lib:Clone())
|
||||||
local ecr = require(ReplicatedStorage.DevPackages.ecr)
|
local mirror = require(ReplicatedStorage.mirror:Clone())
|
||||||
local jecs = require(ReplicatedStorage.Lib)
|
|
||||||
local newWorld = Matter.World.new()
|
|
||||||
local ecs = jecs.World.new()
|
local ecs = jecs.World.new()
|
||||||
|
local mcs = mirror.world()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ParameterGenerator = function()
|
ParameterGenerator = function()
|
||||||
local registry2 = ecr.registry()
|
|
||||||
|
|
||||||
return registry2
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
Functions = {
|
Functions = {
|
||||||
Matter = function()
|
Mirror = function()
|
||||||
for i = 1, 1000 do
|
for i = 1000, 1100 do
|
||||||
newWorld:spawn()
|
mcs:entity(i)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
ECR = function(_, registry2)
|
|
||||||
for i = 1, 1000 do
|
|
||||||
registry2.create()
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
|
|
||||||
Jecs = function()
|
Jecs = function()
|
||||||
for i = 1, 1000 do
|
for i = 1, 1000 do
|
||||||
ecs:entity()
|
ecs:entity(i)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
154
jecs.luau
154
jecs.luau
|
@ -62,6 +62,8 @@ type ecs_entity_index_t = {
|
||||||
sparse_array: Map<i24, ecs_record_t>,
|
sparse_array: Map<i24, ecs_record_t>,
|
||||||
alive_count: number,
|
alive_count: number,
|
||||||
max_id: number,
|
max_id: number,
|
||||||
|
range_begin: number?,
|
||||||
|
range_end: number?
|
||||||
}
|
}
|
||||||
|
|
||||||
type ecs_query_data_t = {
|
type ecs_query_data_t = {
|
||||||
|
@ -118,6 +120,7 @@ local ECS_GENERATION_MASK = bit32.lshift(1, 16)
|
||||||
|
|
||||||
local NULL_ARRAY = table.freeze({}) :: Column
|
local NULL_ARRAY = table.freeze({}) :: Column
|
||||||
local NULL = newproxy(false)
|
local NULL = newproxy(false)
|
||||||
|
local NULL_RECORD = table.freeze({ dense = 0 }) :: ecs_record_t
|
||||||
|
|
||||||
local ECS_INTERNAL_ERROR = [[
|
local ECS_INTERNAL_ERROR = [[
|
||||||
This is an internal error, please file a bug report via the following link:
|
This is an internal error, please file a bug report via the following link:
|
||||||
|
@ -185,6 +188,10 @@ local function ECS_ENTITY_T_LO(e: i53): i24
|
||||||
return e % ECS_ENTITY_MASK
|
return e % ECS_ENTITY_MASK
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function ECS_ID(e: i53)
|
||||||
|
return e % ECS_ENTITY_MASK
|
||||||
|
end
|
||||||
|
|
||||||
local function ECS_GENERATION(e: i53)
|
local function ECS_GENERATION(e: i53)
|
||||||
return e // ECS_ENTITY_MASK
|
return e // ECS_ENTITY_MASK
|
||||||
end
|
end
|
||||||
|
@ -249,10 +256,10 @@ local function entity_index_is_alive(entity_index: ecs_entity_index_t, entity: i
|
||||||
return entity_index_try_get(entity_index, entity) ~= nil
|
return entity_index_try_get(entity_index, entity) ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53?
|
local function entity_index_get_alive(entity_index: ecs_entity_index_t, entity: i53): i53?
|
||||||
local r = entity_index_try_get_any(index, entity)
|
local r = entity_index_try_get_any(entity_index, entity)
|
||||||
if r then
|
if r then
|
||||||
return index.dense_array[r.dense]
|
return entity_index.dense_array[r.dense]
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
@ -283,8 +290,10 @@ end
|
||||||
local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
|
local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
|
||||||
local dense_array = entity_index.dense_array
|
local dense_array = entity_index.dense_array
|
||||||
local alive_count = entity_index.alive_count
|
local alive_count = entity_index.alive_count
|
||||||
|
local sparse_array = entity_index.sparse_array
|
||||||
local max_id = entity_index.max_id
|
local max_id = entity_index.max_id
|
||||||
if alive_count ~= max_id then
|
|
||||||
|
if alive_count < max_id then
|
||||||
alive_count += 1
|
alive_count += 1
|
||||||
entity_index.alive_count = alive_count
|
entity_index.alive_count = alive_count
|
||||||
local id = dense_array[alive_count]
|
local id = dense_array[alive_count]
|
||||||
|
@ -292,11 +301,15 @@ local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
|
||||||
end
|
end
|
||||||
|
|
||||||
local id = max_id + 1
|
local id = max_id + 1
|
||||||
|
local range_end = entity_index.range_end
|
||||||
|
if range_end then
|
||||||
|
assert(id < range_end)
|
||||||
|
end
|
||||||
entity_index.max_id = id
|
entity_index.max_id = id
|
||||||
alive_count += 1
|
alive_count += 1
|
||||||
entity_index.alive_count = alive_count
|
entity_index.alive_count = alive_count
|
||||||
dense_array[alive_count] = id
|
dense_array[alive_count] = id
|
||||||
entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t
|
sparse_array[id] = { dense = alive_count } :: ecs_record_t
|
||||||
|
|
||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
@ -719,8 +732,110 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev:
|
||||||
return archetype
|
return archetype
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_entity(world: ecs_world_t): i53
|
local function world_range(world: ecs_world_t, range_begin: number, range_end: number?)
|
||||||
return entity_index_new_id(world.entity_index)
|
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
|
||||||
|
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
|
||||||
|
end
|
||||||
|
sparse_array[range_begin] = { dense = 0 } :: ecs_record_t
|
||||||
|
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
|
||||||
|
local alive_count = entity_index.alive_count
|
||||||
|
local r = sparse_array[index]
|
||||||
|
if r then
|
||||||
|
local dense = r.dense
|
||||||
|
if not dense or dense == 0 then
|
||||||
|
dense = index
|
||||||
|
end
|
||||||
|
local any = dense_array[dense]
|
||||||
|
if any == entity then
|
||||||
|
if alive_count > dense then
|
||||||
|
return entity
|
||||||
|
end
|
||||||
|
local e_swap = dense_array[alive_count]
|
||||||
|
local r_swap = sparse_array[alive_count]
|
||||||
|
r_swap.dense = dense
|
||||||
|
r.dense = alive_count
|
||||||
|
dense_array[alive_count] = entity
|
||||||
|
dense_array[dense] = e_swap
|
||||||
|
return entity
|
||||||
|
end
|
||||||
|
|
||||||
|
-- assert(any ~= 0) should never happen
|
||||||
|
|
||||||
|
local e_swap = dense_array[alive_count]
|
||||||
|
local r_swap = sparse_array[alive_count]
|
||||||
|
|
||||||
|
if dense <= alive_count then
|
||||||
|
alive_count += 1
|
||||||
|
entity_index.alive_count = alive_count
|
||||||
|
end
|
||||||
|
|
||||||
|
r_swap.dense = dense
|
||||||
|
r.dense = alive_count
|
||||||
|
dense_array[alive_count] = any
|
||||||
|
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
|
||||||
|
end
|
||||||
|
entity_index.max_id = index
|
||||||
|
|
||||||
|
local e_swap = dense_array[alive_count]
|
||||||
|
local r_swap = sparse_array[alive_count]
|
||||||
|
r_swap.dense = index
|
||||||
|
|
||||||
|
alive_count += 1
|
||||||
|
entity_index.alive_count = alive_count
|
||||||
|
|
||||||
|
r = sparse_array[index]
|
||||||
|
|
||||||
|
r.dense = alive_count
|
||||||
|
|
||||||
|
sparse_array[index] = r
|
||||||
|
|
||||||
|
dense_array[index] = e_swap
|
||||||
|
dense_array[alive_count] = entity
|
||||||
|
|
||||||
|
|
||||||
|
return entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return entity_index_new_id(entity_index, entity)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_parent(world: ecs_world_t, entity: i53)
|
local function world_parent(world: ecs_world_t, entity: i53)
|
||||||
|
@ -2311,6 +2426,8 @@ export type EntityIndex = {
|
||||||
sparse_array: Map<i24, Record>,
|
sparse_array: Map<i24, Record>,
|
||||||
alive_count: number,
|
alive_count: number,
|
||||||
max_id: number,
|
max_id: number,
|
||||||
|
range_begin: number?,
|
||||||
|
range_end: number?
|
||||||
}
|
}
|
||||||
|
|
||||||
local World = {}
|
local World = {}
|
||||||
|
@ -2332,6 +2449,7 @@ World.contains = world_contains
|
||||||
World.cleanup = world_cleanup
|
World.cleanup = world_cleanup
|
||||||
World.each = world_each
|
World.each = world_each
|
||||||
World.children = world_children
|
World.children = world_children
|
||||||
|
World.range = world_range
|
||||||
|
|
||||||
local function world_new()
|
local function world_new()
|
||||||
local entity_index = {
|
local entity_index = {
|
||||||
|
@ -2372,16 +2490,6 @@ local function world_new()
|
||||||
entity_index_new_id(entity_index)
|
entity_index_new_id(entity_index)
|
||||||
end
|
end
|
||||||
|
|
||||||
for i, bundle in ecs_metadata do
|
|
||||||
for ty, value in bundle do
|
|
||||||
if value == NULL then
|
|
||||||
world_add(self, i, ty)
|
|
||||||
else
|
|
||||||
world_set(self, i, ty, value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
world_add(self, EcsName, EcsComponent)
|
world_add(self, EcsName, EcsComponent)
|
||||||
world_add(self, EcsOnChange, EcsComponent)
|
world_add(self, EcsOnChange, EcsComponent)
|
||||||
world_add(self, EcsOnAdd, EcsComponent)
|
world_add(self, EcsOnAdd, EcsComponent)
|
||||||
|
@ -2404,6 +2512,16 @@ local function world_new()
|
||||||
|
|
||||||
world_add(self, EcsChildOf, ECS_PAIR(EcsOnDeleteTarget, EcsDelete))
|
world_add(self, EcsChildOf, ECS_PAIR(EcsOnDeleteTarget, EcsDelete))
|
||||||
|
|
||||||
|
for i, bundle in ecs_metadata do
|
||||||
|
for ty, value in bundle do
|
||||||
|
if value == NULL then
|
||||||
|
world_add(self, i, ty)
|
||||||
|
else
|
||||||
|
world_set(self, i, ty, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -2558,6 +2676,8 @@ return {
|
||||||
ECS_META_RESET = ECS_META_RESET,
|
ECS_META_RESET = ECS_META_RESET,
|
||||||
|
|
||||||
IS_PAIR = (ECS_IS_PAIR :: any) :: <P, O>(pair: Pair<P, O>) -> boolean,
|
IS_PAIR = (ECS_IS_PAIR :: any) :: <P, O>(pair: Pair<P, O>) -> boolean,
|
||||||
|
ECS_PAIR_FIRST = ECS_PAIR_FIRST,
|
||||||
|
ECS_PAIR_SECOND = ECS_PAIR_SECOND,
|
||||||
pair_first = (ecs_pair_first :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
|
pair_first = (ecs_pair_first :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
|
||||||
pair_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
|
pair_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
|
||||||
entity_index_get_alive = entity_index_get_alive,
|
entity_index_get_alive = entity_index_get_alive,
|
||||||
|
|
|
@ -87,10 +87,10 @@ TEST("addons/observers", function()
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
|
|
||||||
local callcount = 0
|
local callcount = 0
|
||||||
world:added(A, function(entity)
|
world:added(A, function(entity)
|
||||||
callcount += 1
|
callcount += 1
|
||||||
end)
|
end)
|
||||||
world:added(A, function(entity)
|
world:added(A, function(entity)
|
||||||
callcount += 1
|
callcount += 1
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
|
@ -247,6 +247,7 @@ TEST("world:component()", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("world:contains()", function()
|
TEST("world:contains()", function()
|
||||||
|
|
||||||
local tag = jecs.tag()
|
local tag = jecs.tag()
|
||||||
local world = jecs.world()
|
local world = jecs.world()
|
||||||
local id = world:entity()
|
local id = world:entity()
|
||||||
|
@ -258,6 +259,7 @@ TEST("world:contains()", function()
|
||||||
CHECK(not world:contains(id))
|
CHECK(not world:contains(id))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
CHECK(world:contains(tag))
|
CHECK(world:contains(tag))
|
||||||
jecs.ECS_META_RESET()
|
jecs.ECS_META_RESET()
|
||||||
end)
|
end)
|
||||||
|
@ -632,6 +634,55 @@ TEST("world:each()", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("world:entity()", function()
|
TEST("world:entity()", function()
|
||||||
|
do CASE "range"
|
||||||
|
local world = jecs.world()
|
||||||
|
world = lifetime_tracker_add(world, {})
|
||||||
|
world:range(1000, 2000)
|
||||||
|
|
||||||
|
world:entity(1590)
|
||||||
|
CHECK_EXPECT_ERR(function()
|
||||||
|
|
||||||
|
world:entity(5000)
|
||||||
|
end)
|
||||||
|
|
||||||
|
CHECK(world:contains(1590))
|
||||||
|
world:set(591, jecs.Name, "9888")
|
||||||
|
CHECK(not world:contains(591))
|
||||||
|
CHECK(not world:contains(5000))
|
||||||
|
CHECK(not world:contains(988))
|
||||||
|
end
|
||||||
|
do CASE "desired id"
|
||||||
|
local world = jecs.world()
|
||||||
|
local id = world:entity()
|
||||||
|
local e = world:entity(id + 5)
|
||||||
|
CHECK(e == id + 5)
|
||||||
|
CHECK(world:contains(e))
|
||||||
|
local e2 = world:entity()
|
||||||
|
CHECK(world:contains(e2))
|
||||||
|
|
||||||
|
-- world:print_entity_index()
|
||||||
|
local e3 = world:entity(275)
|
||||||
|
|
||||||
|
CHECK(e3 == 275)
|
||||||
|
CHECK(world:contains(e3))
|
||||||
|
|
||||||
|
CHECK(e3 == world:entity(e3))
|
||||||
|
|
||||||
|
world:delete(e3)
|
||||||
|
|
||||||
|
local e3v1 = world:entity(275)
|
||||||
|
CHECK(not world:contains(275))
|
||||||
|
CHECK(jecs.ECS_GENERATION(e3v1) == 1)
|
||||||
|
CHECK(jecs.ECS_ID(e3v1) == 275)
|
||||||
|
|
||||||
|
|
||||||
|
CHECK(world:contains(e3v1))
|
||||||
|
-- world:print_entity_index()
|
||||||
|
world:entity(e3)
|
||||||
|
|
||||||
|
world:entity(e3)
|
||||||
|
world:entity(275)
|
||||||
|
end
|
||||||
local N = 2^8
|
local N = 2^8
|
||||||
|
|
||||||
do CASE "unique IDs"
|
do CASE "unique IDs"
|
||||||
|
|
|
@ -28,7 +28,7 @@ end
|
||||||
local padding_enabled = false
|
local padding_enabled = false
|
||||||
local function pad()
|
local function pad()
|
||||||
if padding_enabled then
|
if padding_enabled then
|
||||||
print("")
|
print("------------------")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,7 +64,8 @@ local function lifetime_tracker_add(world: jecs.World, opt): PatchedWorld
|
||||||
world.print_entity_index = function()
|
world.print_entity_index = function()
|
||||||
local max_id = entity_index.max_id
|
local max_id = entity_index.max_id
|
||||||
local alive_count = entity_index.alive_count
|
local alive_count = entity_index.alive_count
|
||||||
local alive = table.move(dense_array, 1 + jecs.Rest :: any, alive_count, 1, {})
|
local range_begin = entity_index.range_begin or jecs.Rest + 1
|
||||||
|
local alive = table.move(dense_array, range_begin :: any, alive_count, 1, {})
|
||||||
local dead = table.move(dense_array, alive_count + 1, max_id, 1, {})
|
local dead = table.move(dense_array, alive_count + 1, max_id, 1, {})
|
||||||
|
|
||||||
local sep = "|--------|"
|
local sep = "|--------|"
|
||||||
|
|
|
@ -229,7 +229,6 @@ end
|
||||||
local test_focused = false
|
local test_focused = false
|
||||||
|
|
||||||
local function TEST(name: string, fn: () -> ())
|
local function TEST(name: string, fn: () -> ())
|
||||||
|
|
||||||
test = {
|
test = {
|
||||||
name = name,
|
name = name,
|
||||||
cases = {},
|
cases = {},
|
||||||
|
@ -237,19 +236,18 @@ local function TEST(name: string, fn: () -> ())
|
||||||
focus = false,
|
focus = false,
|
||||||
fn = fn
|
fn = fn
|
||||||
}
|
}
|
||||||
|
|
||||||
local t = test
|
local t = test
|
||||||
|
|
||||||
if check_for_focused and not test_focused then
|
if check_for_focused and not test_focused then
|
||||||
test.focus = true
|
test.focus = true
|
||||||
test_focused = true
|
test_focused = true
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(tests, t)
|
table.insert(tests, t)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function FOCUS()
|
local function FOCUS()
|
||||||
assert(test, "no active test")
|
|
||||||
|
|
||||||
check_for_focused = true
|
check_for_focused = true
|
||||||
test_focused = false
|
test_focused = false
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue