jecs/tools/entity_visualiser.luau
Ukendio de8e263828
Some checks are pending
analysis / Run Luau Analyze (push) Waiting to run
deploy-docs / build (push) Waiting to run
deploy-docs / Deploy (push) Blocked by required conditions
publish-npm / publish (push) Waiting to run
unit-testing / Run Luau Tests (push) Waiting to run
Add entity visualiser
2025-03-12 16:30:19 +01:00

50 lines
1.3 KiB
Text

local jecs = require("@jecs")
local ECS_GENERATION = jecs.ECS_GENERATION
local ECS_ID = jecs.ECS_ID
local ansi = require("@tools/ansi")
local function pe(e: any)
local gen = ECS_GENERATION(e)
return ansi.green(`e{ECS_ID(e)}`)..ansi.yellow(`v{gen}`)
end
local function name(world: jecs.World, id: any)
return world:get(id, jecs.Name) or `${id}`
end
local function components(world: jecs.World, entity: any)
local r = jecs.entity_index_try_get(world.entity_index, entity)
if not r then
return false
end
local archetype = r.archetype
local row = r.row
print(`Entity {pe(entity)}`)
print("-----------------------------------------------------")
for i, column in archetype.columns do
local component = archetype.types[i]
local n
if jecs.IS_PAIR(component) then
n = `({name(world, jecs.pair_first(world, component))}, {name(world, jecs.pair_second(world, component))})`
else
n = name(world, component)
end
local data = column[row] or "TAG"
print(`| {n} | {data} |`)
end
print("-----------------------------------------------------")
return true
end
local world = jecs.world()
local A = world:component()
world:set(A, jecs.Name, "A")
local e = world:entity()
world:set(e, A, true)
components(world, e)
return {
components = components,
prettify = pe
}