Create root archetype

This commit is contained in:
Ukendio 2024-05-13 00:20:50 +02:00
parent 1ae5a2ddf3
commit 8017f68993

View file

@ -298,8 +298,8 @@ World.__index = World
function World.new() function World.new()
local self = setmetatable({ local self = setmetatable({
archetypeIndex = {}; archetypeIndex = {};
archetypes = {}; archetypes = {} :: Archetypes;
componentIndex = {}; componentIndex = {} :: ComponentIndex;
entityIndex = { entityIndex = {
dense = {}, dense = {},
sparse = {} sparse = {}
@ -312,6 +312,7 @@ function World.new()
nextEntityId = 0; nextEntityId = 0;
ROOT_ARCHETYPE = (nil :: any) :: Archetype; ROOT_ARCHETYPE = (nil :: any) :: Archetype;
}, World) }, World)
self.ROOT_ARCHETYPE = archetypeOf(self, {})
return self return self
end end
@ -440,15 +441,7 @@ local function ensureEdge(archetype: Archetype, componentId: i53)
end end
local function archetypeTraverseAdd(world: World, componentId: i53, from: Archetype): Archetype local function archetypeTraverseAdd(world: World, componentId: i53, from: Archetype): Archetype
if not from then from = from or world.ROOT_ARCHETYPE
-- If there was no source archetype then it should return the ROOT_ARCHETYPE
local ROOT_ARCHETYPE = world.ROOT_ARCHETYPE
if not ROOT_ARCHETYPE then
ROOT_ARCHETYPE = archetypeOf(world, {}, nil)
world.ROOT_ARCHETYPE = ROOT_ARCHETYPE :: never
end
from = ROOT_ARCHETYPE
end
local edge = ensureEdge(from, componentId) local edge = ensureEdge(from, componentId)
local add = edge.add local add = edge.add
@ -674,14 +667,14 @@ function World.query(world: World, ...: i53): Query
function preparedQuery:__iter() function preparedQuery:__iter()
return function() return function()
local archetype = compatibleArchetype[1] local archetype = compatibleArchetype[1]
local row = next(archetype.entities, lastRow) local row: number = next(archetype.entities, lastRow) :: number
while row == nil do while row == nil do
lastArchetype, compatibleArchetype = next(compatibleArchetypes, lastArchetype) lastArchetype, compatibleArchetype = next(compatibleArchetypes, lastArchetype)
if lastArchetype == nil then if lastArchetype == nil then
return return
end end
archetype = compatibleArchetype[1] archetype = compatibleArchetype[1]
row = next(archetype.entities, row) row = next(archetype.entities, row) :: number
end end
lastRow = row lastRow = row