mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 01:20:04 +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
|
entityIndex.dense[index] = id
|
||||||
local entityLookup = world.entityLookup
|
local entityLookup = world.entityLookup
|
||||||
if name then
|
if name then
|
||||||
local entityLookupName = entityLookup.name
|
|
||||||
local entity = entityLookupName[name]
|
|
||||||
if entity then
|
|
||||||
return entity
|
|
||||||
end
|
|
||||||
|
|
||||||
entityLookup.id[id] = name
|
entityLookup.id[id] = name
|
||||||
entityLookupName[name] = id
|
entityLookup.name[name] = id
|
||||||
end
|
end
|
||||||
|
|
||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
|
||||||
function World.component(world: World, name: string?)
|
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
|
local componentId = world.nextComponentId + 1
|
||||||
if componentId > HI_COMPONENT_ID then
|
if componentId > HI_COMPONENT_ID then
|
||||||
-- IDs are partitioned into ranges because component IDs are not nominal,
|
-- IDs are partitioned into ranges because component IDs are not nominal,
|
||||||
|
@ -327,6 +328,13 @@ function World.component(world: World, name: string?)
|
||||||
end
|
end
|
||||||
|
|
||||||
function World.entity(world: World, name: string?)
|
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
|
local entityId = world.nextEntityId + 1
|
||||||
world.nextEntityId = entityId
|
world.nextEntityId = entityId
|
||||||
return nextEntityId(world, entityId + REST, name)
|
return nextEntityId(world, entityId + REST, name)
|
||||||
|
|
|
@ -222,7 +222,13 @@ TEST("world", function()
|
||||||
|
|
||||||
CHECK(world:lookup("Eats") == Eats)
|
CHECK(world:lookup("Eats") == Eats)
|
||||||
CHECK(world:name(Apples) == "Apples")
|
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
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue