Merge pull request #1 from Ukendio/main

Change pair function names
This commit is contained in:
Conz 2024-08-10 17:50:48 -05:00 committed by GitHub
commit 2b1775ae1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 31 deletions

View file

@ -56,6 +56,17 @@ type ArchetypeDiff = {
removed: Ty, removed: Ty,
} }
export type World = {
archetypeIndex: { [string]: Archetype },
archetypes: Archetypes,
componentIndex: ComponentIndex,
entityIndex: EntityIndex,
nextArchetypeId: number,
nextComponentId: number,
nextEntityId: number,
ROOT_ARCHETYPE: Archetype
}
local HI_COMPONENT_ID = 256 local HI_COMPONENT_ID = 256
local EcsOnAdd = HI_COMPONENT_ID + 1 local EcsOnAdd = HI_COMPONENT_ID + 1
@ -119,19 +130,11 @@ local function ECS_ENTITY_T_HI(e: i53): i24
return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) % ECS_ENTITY_MASK else e return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) % ECS_ENTITY_MASK else e
end end
local function ECS_PAIR_FIRST(e)
return ECS_ENTITY_T_HI(e)
end
-- SECOND -- SECOND
local function ECS_ENTITY_T_LO(e: i53): i24 local function ECS_ENTITY_T_LO(e: i53): i24
return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) // ECS_ENTITY_MASK else e return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) // ECS_ENTITY_MASK else e
end end
local function ECS_PAIR_SECCOND(e)
return ECS_PAIR_SECCOND(e)
end
local function STRIP_GENERATION(e: i53): i24 local function STRIP_GENERATION(e: i53): i24
return ECS_ENTITY_T_LO(e) return ECS_ENTITY_T_LO(e)
end end
@ -165,12 +168,12 @@ local function entity_index_sparse_get(entityIndex, id)
end end
-- ECS_PAIR_FIRST, gets the relationship target / obj / HIGH bits -- ECS_PAIR_FIRST, gets the relationship target / obj / HIGH bits
local function ecs_pair_relation(world, e) local function ecs_pair_first(world, e)
return entity_index_get_alive(world.entityIndex, ECS_ENTITY_T_HI(e)) return entity_index_get_alive(world.entityIndex, ECS_ENTITY_T_HI(e))
end end
-- ECS_PAIR_SECOND gets the relationship / pred / LOW bits -- ECS_PAIR_SECOND gets the relationship / pred / LOW bits
local function ecs_pair_object(world, e) local function ecs_pair_second(world, e)
return entity_index_get_alive(world.entityIndex, ECS_ENTITY_T_LO(e)) return entity_index_get_alive(world.entityIndex, ECS_ENTITY_T_LO(e))
end end
@ -302,8 +305,8 @@ local function archetype_create(world: any, types: { i24 }, prev: Archetype?): A
idr.size += 1 idr.size += 1
records[componentId] = tr records[componentId] = tr
if ECS_IS_PAIR(componentId) then if ECS_IS_PAIR(componentId) then
local relation = ecs_pair_relation(world, componentId) local relation = ecs_pair_first(world, componentId)
local object = ecs_pair_object(world, componentId) local object = ecs_pair_second(world, componentId)
local r = ECS_PAIR(relation, EcsWildcard) local r = ECS_PAIR(relation, EcsWildcard)
local idr_r = id_record_ensure(componentIndex, r) local idr_r = id_record_ensure(componentIndex, r)
@ -339,17 +342,6 @@ local function archetype_create(world: any, types: { i24 }, prev: Archetype?): A
return archetype return archetype
end end
export type World = {
archetypeIndex: { [string]: Archetype },
archetypes: Archetypes,
componentIndex: ComponentIndex,
entityIndex: EntityIndex,
nextArchetypeId: number,
nextComponentId: number,
nextEntityId: number,
ROOT_ARCHETYPE: Archetype
}
local function world_entity(world: World): i53 local function world_entity(world: World): i53
local entityId = world.nextEntityId + 1 local entityId = world.nextEntityId + 1
world.nextEntityId = entityId world.nextEntityId = entityId
@ -376,7 +368,7 @@ local function world_target(world: World, entity: i53, relation: i24--[[, nth: n
return nil return nil
end end
return ecs_pair_object(world, archetype.types[tr.column]) return ecs_pair_second(world, archetype.types[tr.column])
end end
local function world_parent(world: World, entity: i53) local function world_parent(world: World, entity: i53)
@ -1414,12 +1406,12 @@ return {
pair = (ECS_PAIR :: any) :: <R, T>(pred: Entity, obj: Entity) -> number, pair = (ECS_PAIR :: any) :: <R, T>(pred: Entity, obj: Entity) -> number,
-- Inwards facing API for testing -- Inwards facing API for testing
IS_PAIR = ECS_IS_PAIR,
ECS_ID = ECS_ENTITY_T_LO, ECS_ID = ECS_ENTITY_T_LO,
ECS_GENERATION_INC = ECS_GENERATION_INC, ECS_GENERATION_INC = ECS_GENERATION_INC,
ECS_GENERATION = ECS_GENERATION, ECS_GENERATION = ECS_GENERATION,
ecs_pair_relation = ecs_pair_relation, IS_PAIR = ECS_IS_PAIR,
ecs_pair_object = ecs_pair_object, pair_first = ecs_pair_first,
pair_second = ecs_pair_second,
entity_index_get_alive = entity_index_get_alive, entity_index_get_alive = entity_index_get_alive,
} }

View file

@ -6,8 +6,8 @@ local ECS_GENERATION_INC = jecs.ECS_GENERATION_INC
local IS_PAIR = jecs.IS_PAIR local IS_PAIR = jecs.IS_PAIR
local pair = jecs.pair local pair = jecs.pair
local getAlive = jecs.entity_index_get_alive local getAlive = jecs.entity_index_get_alive
local ecs_pair_relation = jecs.ecs_pair_relation local ecs_pair_first = jecs.pair_first
local ecs_pair_object = jecs.ecs_pair_object local ecs_pair_second = jecs.pair_second
local TEST, CASE, CHECK, FINISH, SKIP = testkit.test() local TEST, CASE, CHECK, FINISH, SKIP = testkit.test()
local function CHECK_NO_ERR<T...>(s: string, fn: (T...) -> (), ...: T...) local function CHECK_NO_ERR<T...>(s: string, fn: (T...) -> (), ...: T...)
@ -96,8 +96,8 @@ TEST("world:entity()", function()
local pair = pair(e2, e3) local pair = pair(e2, e3)
CHECK(IS_PAIR(pair) == true) CHECK(IS_PAIR(pair) == true)
CHECK(ecs_pair_relation(world, pair) == e2) CHECK(ecs_pair_first(world, pair) == e2)
CHECK(ecs_pair_object(world, pair) == e3) CHECK(ecs_pair_second(world, pair) == e3)
end end
end) end)