Register components as entities

This commit is contained in:
Ukendio 2024-05-10 20:35:41 +02:00
parent 076f0ca436
commit 87711eff19
2 changed files with 21 additions and 19 deletions

View file

@ -291,6 +291,16 @@ local function ecs_get_target(entityIndex, e)
return getAlive(entityIndex, ECS_PAIR_SECOND(e)) return getAlive(entityIndex, ECS_PAIR_SECOND(e))
end end
local function nextEntityId(entityIndex, index: i24)
local id = newId(index, 0)
entityIndex.sparse[id] = {
dense = index
} :: Record
entityIndex.dense[index] = id
return id
end
function World.component(world: World) function World.component(world: World)
local componentId = world.nextComponentId + 1 local componentId = world.nextComponentId + 1
if componentId > HI_COMPONENT_ID then if componentId > HI_COMPONENT_ID then
@ -299,21 +309,13 @@ function World.component(world: World)
error("Too many components, consider using world:entity() instead to create components.") error("Too many components, consider using world:entity() instead to create components.")
end end
world.nextComponentId = componentId world.nextComponentId = componentId
return componentId return nextEntityId(world.entityIndex, componentId)
end end
function World.entity(world: World) function World.entity(world: World)
local nextEntityId = world.nextEntityId + 1 local entityId = world.nextEntityId + 1
world.nextEntityId = nextEntityId world.nextEntityId = entityId
local index = nextEntityId + REST return nextEntityId(world.entityIndex, entityId + REST)
local id = newId(index, 0)
local entityIndex = world.entityIndex
entityIndex.sparse[id] = {
dense = index
} :: Record
entityIndex.dense[index] = id
return id
end end
-- should reuse this logic in World.set instead of swap removing in transition archetype -- should reuse this logic in World.set instead of swap removing in transition archetype

View file

@ -5,8 +5,8 @@ local ECS_GENERATION_INC = jecs.ECS_GENERATION_INC
local IS_PAIR = jecs.IS_PAIR local IS_PAIR = jecs.IS_PAIR
local ECS_PAIR = jecs.ECS_PAIR local ECS_PAIR = jecs.ECS_PAIR
local getAlive = jecs.getAlive local getAlive = jecs.getAlive
local ecs_pair_first = jecs.ecs_pair_first local ecs_get_source = jecs.ecs_get_source
local ecs_pair_second = jecs.ecs_pair_second local ecs_get_target = jecs.ecs_get_target
local REST = 256 + 4 local REST = 256 + 4
local TEST, CASE, CHECK, FINISH, SKIP = testkit.test() local TEST, CASE, CHECK, FINISH, SKIP = testkit.test()
@ -39,12 +39,12 @@ TEST("world", function()
elseif id == eAB then elseif id == eAB then
CHECK(data[A] == true) CHECK(data[A] == true)
CHECK(data[B] == true) CHECK(data[B] == true)
else
error("unknown entity", id)
end end
end end
CHECK(count == 3) -- components are registered in the entity index as well
-- so this test has to add 2 to account for them
CHECK(count == 3 + 2)
end end
do CASE "should query all matching entities" do CASE "should query all matching entities"
@ -199,8 +199,8 @@ TEST("world", function()
local pair = ECS_PAIR(e2, e3) local pair = ECS_PAIR(e2, e3)
CHECK(IS_PAIR(pair) == true) CHECK(IS_PAIR(pair) == true)
CHECK(ecs_pair_first(world.entityIndex, pair) == e2) CHECK(ecs_get_source(world.entityIndex, pair) == e2)
CHECK(ecs_pair_second(world.entityIndex, pair) == e3) CHECK(ecs_get_target(world.entityIndex, pair) == e3)
end end
end) end)