Separate ids and add tests

This commit is contained in:
Ukendio 2024-04-30 22:47:15 +02:00
parent 275aa6ec6d
commit 18efea6ce9
3 changed files with 23 additions and 14 deletions

View file

@ -30,14 +30,14 @@ local B6 = ecr.component()
local B7 = ecr.component()
local B8 = ecr.component()
local D1 = ecs:entity()
local D2 = ecs:entity()
local D3 = ecs:entity()
local D4 = ecs:entity()
local D5 = ecs:entity()
local D6 = ecs:entity()
local D7 = ecs:entity()
local D8 = ecs:entity()
local D1 = ecs:component()
local D2 = ecs:component()
local D3 = ecs:component()
local D4 = ecs:component()
local D5 = ecs:component()
local D6 = ecs:component()
local D7 = ecs:component()
local D8 = ecs:component()
local E1 = mcs:entity()

View file

@ -159,7 +159,8 @@ function World.new()
archetypes = {},
archetypeIndex = {},
ROOT_ARCHETYPE = (nil :: any) :: Archetype,
nextId = 0,
nextEntityId = 0,
nextComponentId = 0,
nextArchetypeId = 0,
hooks = {
[ON_ADD] = {}
@ -521,16 +522,17 @@ function World.query(world: World, ...: i53): any
end
function World.component(world: World)
local id = world.nextId + 1
if id > HI_COMPONENT_ID then
local componentId = world.nextComponentId + 1
if componentId > HI_COMPONENT_ID then
error("Too many components")
end
return id
world.nextComponentId = componentId
return componentId
end
function World.entity(world: World)
world.nextId += 1
return world.nextId + REST
world.nextEntityId += 1
return world.nextEntityId + REST
end
function World.observer(world: World, ...)

View file

@ -157,6 +157,13 @@ return function()
expect(withoutCount).to.equal(1)
end)
it("should allow calling world:entity before world:component", function()
for _ = 1, 256 do
world:entity()
end
expect(world:component()).to.be.ok()
end)
it("should skip iteration", function()
local Position, Velocity = world:entity(), world:entity()
local e = world:entity()