Compare commits

..

1 commit

Author SHA1 Message Date
Clown
8ffc1486e7
Merge 96bed9bd7e into bee92f489c 2025-06-27 21:44:01 +01:00
5 changed files with 40 additions and 135 deletions

4
jecs.d.ts vendored
View file

@ -105,7 +105,7 @@ export class World {
/** /**
* Creates a new World. * Creates a new World.
*/ */
private constructor(); constructor();
/** /**
* Enforces a check for entities to be created within a desired range. * Enforces a check for entities to be created within a desired range.
@ -249,8 +249,6 @@ export class World {
query<T extends Id[]>(...components: T): Query<InferComponents<T>>; query<T extends Id[]>(...components: T): Query<InferComponents<T>>;
} }
export function world(): World;
export function component<T>(): Entity<T>; export function component<T>(): Entity<T>;
export function tag(): Tag; export function tag(): Tag;

104
jecs.luau
View file

@ -52,8 +52,6 @@ export type Query<T...> = typeof(setmetatable(
} }
)) ))
type QueryArm<T...> = () -> ()
export type Observer = { export type Observer = {
callback: (archetype: Archetype) -> (), callback: (archetype: Archetype) -> (),
query: QueryInner, query: QueryInner,
@ -660,9 +658,6 @@ local function id_record_ensure(world: World, id: Entity): ComponentRecord
local relation = id local relation = id
local target = 0 local target = 0
local is_pair = ECS_IS_PAIR(id :: number) local is_pair = ECS_IS_PAIR(id :: number)
local has_delete = false
if is_pair then if is_pair then
relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id :: number)) :: i53 relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id :: number)) :: i53
ecs_assert(relation and entity_index_is_alive( ecs_assert(relation and entity_index_is_alive(
@ -670,18 +665,15 @@ local function id_record_ensure(world: World, id: Entity): ComponentRecord
target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id :: number)) :: i53 target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id :: number)) :: i53
ecs_assert(target and entity_index_is_alive( ecs_assert(target and entity_index_is_alive(
entity_index, target), ECS_INTERNAL_ERROR) entity_index, target), ECS_INTERNAL_ERROR)
end
local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0) local cleanup_policy = world_target(world, relation, EcsOnDelete, 0)
local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0)
if cleanup_policy_target == EcsDelete then local has_delete = false
has_delete = true
end
else
local cleanup_policy = world_target(world, relation, EcsOnDelete, 0)
if cleanup_policy == EcsDelete then if cleanup_policy == EcsDelete or cleanup_policy_target == EcsDelete then
has_delete = true has_delete = true
end
end end
local on_add, on_change, on_remove = world_get(world, local on_add, on_change, on_remove = world_get(world,
@ -2034,16 +2026,18 @@ local function ecs_bulk_insert(world: World, entity: Entity, ids: { Entity }, va
local value = values[i] :: any local value = values[i] :: any
local on_add = idr.hooks.on_add local on_add = idr.hooks.on_add
local on_change = idr.hooks.on_change
if value ~= nil then if value then
columns_map[id][row] = value columns_map[id][row] = value
local on_change = idr.hooks.on_change
local hook = if set then on_change else on_add local hook = if set then on_change else on_add
if hook then if hook then
hook(entity, id, value :: any) hook(entity, id, value :: any)
end end
elseif on_add then else
on_add(entity, id) if on_add then
on_add(entity, id, value)
end
end end
end end
end end
@ -2146,41 +2140,38 @@ local function world_new()
return r return r
end end
-- local function inner_entity_index_try_get(entity: number): Record? -- local function entity_index_try_get_safe(entity: number): Record?
-- local r = inner_entity_index_try_get_any(entity) -- local r = entity_index_try_get_any_fast(entity_index, entity)
-- if r then -- if r then
-- local r_dense = r.dense -- local r_dense = r.dense
-- if r_dense > entity_index.alive_count then -- if r_dense > entity_index.alive_count then
-- return nil -- return nil
-- end -- end
-- if eindex_dense_array[r_dense] ~= entity then -- if entity_index.dense_array[r_dense] ~= entity then
-- return nil -- return nil
-- end -- end
-- end -- end
-- return r -- return r
-- end -- end
local function inner_entity_index_try_get_unsafe(entity: number): Record? local function inner_entity_index_try_get(entity: number): Record?
local r = inner_entity_index_try_get_any(entity) local r = eindex_sparse_array[ECS_ENTITY_T_LO(entity)]
if r then if r then
local r_dense = r.dense if eindex_dense_array[r.dense] ~= entity then
-- if r_dense > entity_index.alive_count then
-- return nil
-- end
if eindex_dense_array[r_dense] ~= entity then
return nil return nil
end end
end end
return r return r
end end
local function inner_world_add<T, a>( local function inner_world_add<T, a>(
world: World, world: World,
entity: Entity<T>, entity: Entity<T>,
id: Id<a> id: Id<a>
): () ): ()
local entity_index = world.entity_index local entity_index = world.entity_index
local record = inner_entity_index_try_get_unsafe(entity :: number) local record = inner_entity_index_try_get(entity :: number)
if not record then if not record then
return return
end end
@ -2208,7 +2199,7 @@ local function world_new()
local function inner_world_get(world: World, entity: Entity, local function inner_world_get(world: World, entity: Entity,
a: Id, b: Id?, c: Id?, d: Id?, e: Id?): ...any a: Id, b: Id?, c: Id?, d: Id?, e: Id?): ...any
local record = inner_entity_index_try_get_unsafe(entity::number) local record = inner_entity_index_try_get(entity::number)
if not record then if not record then
return nil return nil
end end
@ -2239,7 +2230,7 @@ local function world_new()
local function inner_world_has(world: World, entity: i53, local function inner_world_has(world: World, entity: i53,
a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean
local record = inner_entity_index_try_get_unsafe(entity) local record = inner_entity_index_try_get(entity)
if not record then if not record then
return false return false
end end
@ -2259,7 +2250,7 @@ local function world_new()
end end
local function inner_world_target<T, a>(world: World, entity: Entity<T>, relation: Id<a>, index: number?): Entity? local function inner_world_target<T, a>(world: World, entity: Entity<T>, relation: Id<a>, index: number?): Entity?
local record = inner_entity_index_try_get_unsafe(entity :: number) local record = inner_entity_index_try_get(entity :: number)
if not record then if not record then
return nil return nil
end end
@ -2321,7 +2312,7 @@ local function world_new()
end end
local function inner_world_set<T, a>(world: World, entity: Entity<T>, id: Id<a>, data: a): () local function inner_world_set<T, a>(world: World, entity: Entity<T>, id: Id<a>, data: a): ()
local record = inner_entity_index_try_get_unsafe(entity :: number) local record = inner_entity_index_try_get(entity :: number)
if not record then if not record then
return return
end end
@ -2422,7 +2413,7 @@ local function world_new()
end end
local function inner_world_remove<T, a>(world: World, entity: Entity<T>, id: Id<a>) local function inner_world_remove<T, a>(world: World, entity: Entity<T>, id: Id<a>)
local record = inner_entity_index_try_get_unsafe(entity :: number) local record = inner_entity_index_try_get(entity :: number)
if not record then if not record then
return return
end end
@ -2542,12 +2533,11 @@ local function world_new()
end end
end end
end end
end end
local function inner_world_delete<T>(world: World, entity: Entity<T>) local function inner_world_delete<T>(world: World, entity: Entity<T>)
local entity_index = world.entity_index local entity_index = world.entity_index
local record = inner_entity_index_try_get_unsafe(entity::number) local record = inner_entity_index_try_get(entity::number)
if not record then if not record then
return return
end end
@ -2585,41 +2575,15 @@ local function world_new()
archetype_destroy(world, idr_archetype) archetype_destroy(world, idr_archetype)
end end
else else
local on_remove = idr.hooks.on_remove for archetype_id in idr.records do
if on_remove then local idr_archetype = archetypes[archetype_id]
for archetype_id in idr.records do local entities = idr_archetype.entities
local idr_archetype = archetypes[archetype_id] local n = #entities
local to = archetype_traverse_remove(world, entity, idr_archetype) for i = n, 1, -1 do
local entities = idr_archetype.entities inner_world_remove(world, entities[i], entity)
local n = #entities
for i = n, 1, -1 do
local e = entities[i]
on_remove(e, entity)
local r = eindex_sparse_array[ECS_ID(e :: number)]
local from = r.archetype
if from ~= idr_archetype then
-- unfortunately the on_remove hook allows a window where `e` can have changed archetype
-- this is hypothetically not that expensive of an operation anyways
to = archetype_traverse_remove(world, entity, from)
end
entity_move(entity_index, e, r, to)
end
archetype_destroy(world, idr_archetype)
end end
else
for archetype_id in idr.records do
local idr_archetype = archetypes[archetype_id]
local to = archetype_traverse_remove(world, entity, idr_archetype)
local entities = idr_archetype.entities
local n = #entities
for i = n, 1, -1 do
local e = entities[i]
entity_move(entity_index, e, eindex_sparse_array[ECS_ID(e :: number)], to)
end
archetype_destroy(world, idr_archetype) archetype_destroy(world, idr_archetype)
end
end end
end end
end end
@ -2839,7 +2803,7 @@ local function world_new()
if value == NULL then if value == NULL then
inner_world_add(world, i, ty) inner_world_add(world, i, ty)
else else
inner_world_set(world, i, ty, value) inner_world_add(world, i, ty, value)
end end
end end
end end

View file

@ -1,6 +1,6 @@
{ {
"name": "@rbxts/jecs", "name": "@rbxts/jecs",
"version": "0.7.3", "version": "0.7.2",
"description": "Stupidly fast Entity Component System", "description": "Stupidly fast Entity Component System",
"main": "jecs.luau", "main": "jecs.luau",
"repository": { "repository": {

View file

@ -25,57 +25,6 @@ local entity_visualiser = require("@tools/entity_visualiser")
local lifetime_tracker_add = require("@tools/lifetime_tracker") local lifetime_tracker_add = require("@tools/lifetime_tracker")
local dwi = entity_visualiser.stringify local dwi = entity_visualiser.stringify
TEST("repro#", function()
do CASE "pair(OnDelete, Delete)"
local world = jecs.world()
local ct = world:component()
world:add(ct, jecs.pair(jecs.OnDelete, jecs.Delete))
local e1 = world:entity()
local e2 = world:entity()
local dummy = world:entity()
world:add(e1, ct)
world:add(e2, jecs.pair(ct, dummy))
world:delete(dummy)
CHECK(world:contains(e2))
world:delete(ct)
CHECK(not world:contains(e1))
end
do CASE "pair(OnDeleteTarget, Delete)"
print("start")
local world = jecs.world()
local ct = world:component()
world:add(ct, jecs.pair(jecs.OnDeleteTarget, jecs.Delete))
local e1 = world:entity()
local e2 = world:entity()
-- local dummy = world:entity()
print("flags")
world:add(e1, ct)
print(world.component_index[ct].flags)
-- world:add(e2, jecs.pair(ct, dummy))
-- world:delete(dummy)
-- CHECK(not world:contains(e2))
world:delete(ct)
CHECK(world:contains(e1))
end
end)
TEST("bulk", function() TEST("bulk", function()
local world = jecs.world() local world = jecs.world()
local A = world:component() local A = world:component()
@ -93,10 +42,7 @@ TEST("bulk", function()
CHECK(world:get(e, B) == 2) CHECK(world:get(e, B) == 2)
CHECK(world:get(e, C) == 3) CHECK(world:get(e, C) == 3)
jecs.bulk_insert(world, e, jecs.bulk_insert(world, e, { D, E, F }, { 4, nil, 5 })
{ D, E, F },
{ 4, nil, 5 }
)
CHECK(world:get(e, A) == 1) CHECK(world:get(e, A) == 1)
CHECK(world:get(e, B) == 2) CHECK(world:get(e, B) == 2)
CHECK(world:get(e, C) == 3) CHECK(world:get(e, C) == 3)
@ -105,10 +51,7 @@ TEST("bulk", function()
CHECK(world:get(e, E) == nil and world:has(e, E)) CHECK(world:get(e, E) == nil and world:has(e, E))
CHECK(world:get(e, F) == 5) CHECK(world:get(e, F) == 5)
jecs.bulk_insert(world, e, jecs.bulk_insert(world, e, { A, D, E, F, C }, { 10, 40, nil, 50, 30 })
{ A, D, E, F, C },
{ 10, 40, nil, 50, 30 }
)
CHECK(world:get(e, A) == 10) CHECK(world:get(e, A) == 10)
CHECK(world:get(e, B) == 2) CHECK(world:get(e, B) == 2)
@ -498,7 +441,7 @@ TEST("world:delete()", function()
local A = world:entity() local A = world:entity()
local B = world:entity() local B = world:entity()
world:add(Relation, pair(jecs.OnDeleteTarget, jecs.Delete)) world:add(Relation, pair(jecs.OnDelete, jecs.Delete))
local entity = world:entity() local entity = world:entity()

View file

@ -1,6 +1,6 @@
[package] [package]
name = "ukendio/jecs" name = "ukendio/jecs"
version = "0.7.3" version = "0.7.2"
registry = "https://github.com/UpliftGames/wally-index" registry = "https://github.com/UpliftGames/wally-index"
realm = "shared" realm = "shared"
license = "MIT" license = "MIT"