mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
add name field to record
This commit is contained in:
parent
0fd04c75c5
commit
5a3342271f
1 changed files with 11 additions and 6 deletions
17
lib/init.lua
17
lib/init.lua
|
@ -30,6 +30,7 @@ type Record = {
|
|||
archetype: Archetype,
|
||||
row: number,
|
||||
dense: i24,
|
||||
name: string?,
|
||||
}
|
||||
|
||||
type EntityIndex = {dense: {[i24]: i53}, sparse: {[i53]: Record}}
|
||||
|
@ -284,7 +285,6 @@ function World.new()
|
|||
nextEntityId = 0,
|
||||
ROOT_ARCHETYPE = (nil :: any) :: Archetype,
|
||||
aliases = {},
|
||||
ids = {}
|
||||
}, World)
|
||||
return self
|
||||
end
|
||||
|
@ -294,13 +294,14 @@ type World = typeof(World.new())
|
|||
local function nextEntityId(world: World, index: i24, name: string?)
|
||||
local entityIndex = world.entityIndex
|
||||
local id = newId(index, 0)
|
||||
entityIndex.sparse[id] = {
|
||||
local record = {
|
||||
dense = index
|
||||
} :: Record
|
||||
entityIndex.sparse[id] = record
|
||||
entityIndex.dense[index] = id
|
||||
if name then
|
||||
world.aliases[name] = id
|
||||
world.ids[id] = name
|
||||
record.name = name
|
||||
end
|
||||
|
||||
return id
|
||||
|
@ -308,7 +309,7 @@ end
|
|||
|
||||
function World.component(world: World, name: string?)
|
||||
if name then
|
||||
local entity = world.entityLookup.name[name]
|
||||
local entity = world.aliases[name]
|
||||
if entity then
|
||||
return entity
|
||||
end
|
||||
|
@ -341,8 +342,12 @@ function World.lookup(world: World, name: string): i53
|
|||
return world.aliases[name]
|
||||
end
|
||||
|
||||
function World.name(world: World, entity: i53): string
|
||||
return world.ids[entity]
|
||||
function World.name(world: World, entity: i53): string?
|
||||
local record = world.entityIndex.sparse[entity]
|
||||
if not record then
|
||||
return nil
|
||||
end
|
||||
return record.name
|
||||
end
|
||||
|
||||
-- should reuse this logic in World.set instead of swap removing in transition archetype
|
||||
|
|
Loading…
Reference in a new issue