mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Ensure entity exists
This commit is contained in:
parent
9fad8311c4
commit
2b0d55db83
2 changed files with 22 additions and 8 deletions
22
lib/init.lua
22
lib/init.lua
|
@ -302,20 +302,21 @@ local function nextEntityId(world: World, index: i24, name: string?)
|
|||
entityIndex.dense[index] = id
|
||||
local entityLookup = world.entityLookup
|
||||
if name then
|
||||
local entityLookupName = entityLookup.name
|
||||
local entity = entityLookupName[name]
|
||||
if entity then
|
||||
return entity
|
||||
end
|
||||
|
||||
entityLookup.id[id] = name
|
||||
entityLookupName[name] = id
|
||||
entityLookup.name[name] = id
|
||||
end
|
||||
|
||||
return id
|
||||
end
|
||||
|
||||
function World.component(world: World, name: string?)
|
||||
if name then
|
||||
local entity = world.entityLookup.name[name]
|
||||
if entity then
|
||||
return entity
|
||||
end
|
||||
end
|
||||
|
||||
local componentId = world.nextComponentId + 1
|
||||
if componentId > HI_COMPONENT_ID then
|
||||
-- IDs are partitioned into ranges because component IDs are not nominal,
|
||||
|
@ -327,6 +328,13 @@ function World.component(world: World, name: string?)
|
|||
end
|
||||
|
||||
function World.entity(world: World, name: string?)
|
||||
if name then
|
||||
local entity = world.entityLookup.name[name]
|
||||
if entity then
|
||||
return entity
|
||||
end
|
||||
end
|
||||
|
||||
local entityId = world.nextEntityId + 1
|
||||
world.nextEntityId = entityId
|
||||
return nextEntityId(world, entityId + REST, name)
|
||||
|
|
|
@ -222,7 +222,13 @@ TEST("world", function()
|
|||
|
||||
CHECK(world:lookup("Eats") == Eats)
|
||||
CHECK(world:name(Apples) == "Apples")
|
||||
|
||||
end
|
||||
|
||||
do CASE "should return existing entity"
|
||||
local world = jecs.World.new()
|
||||
local Eats = world:entity("Eats")
|
||||
|
||||
CHECK(Eats == world:entity("Eats"))
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
Loading…
Reference in a new issue