add name field to record

This commit is contained in:
Ukendio 2024-05-12 05:25:26 +02:00
parent 0fd04c75c5
commit 5a3342271f

View file

@ -30,6 +30,7 @@ type Record = {
archetype: Archetype, archetype: Archetype,
row: number, row: number,
dense: i24, dense: i24,
name: string?,
} }
type EntityIndex = {dense: {[i24]: i53}, sparse: {[i53]: Record}} type EntityIndex = {dense: {[i24]: i53}, sparse: {[i53]: Record}}
@ -284,7 +285,6 @@ function World.new()
nextEntityId = 0, nextEntityId = 0,
ROOT_ARCHETYPE = (nil :: any) :: Archetype, ROOT_ARCHETYPE = (nil :: any) :: Archetype,
aliases = {}, aliases = {},
ids = {}
}, World) }, World)
return self return self
end end
@ -294,13 +294,14 @@ type World = typeof(World.new())
local function nextEntityId(world: World, index: i24, name: string?) local function nextEntityId(world: World, index: i24, name: string?)
local entityIndex = world.entityIndex local entityIndex = world.entityIndex
local id = newId(index, 0) local id = newId(index, 0)
entityIndex.sparse[id] = { local record = {
dense = index dense = index
} :: Record } :: Record
entityIndex.sparse[id] = record
entityIndex.dense[index] = id entityIndex.dense[index] = id
if name then if name then
world.aliases[name] = id world.aliases[name] = id
world.ids[id] = name record.name = name
end end
return id return id
@ -308,7 +309,7 @@ end
function World.component(world: World, name: string?) function World.component(world: World, name: string?)
if name then if name then
local entity = world.entityLookup.name[name] local entity = world.aliases[name]
if entity then if entity then
return entity return entity
end end
@ -341,8 +342,12 @@ function World.lookup(world: World, name: string): i53
return world.aliases[name] return world.aliases[name]
end end
function World.name(world: World, entity: i53): string function World.name(world: World, entity: i53): string?
return world.ids[entity] local record = world.entityIndex.sparse[entity]
if not record then
return nil
end
return record.name
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