mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 09:30:03 +00:00
Initial commit
This commit is contained in:
parent
0292574b5f
commit
b4a10b10b9
3 changed files with 202 additions and 157 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -53,7 +53,7 @@ WallyPatches
|
||||||
# Misc
|
# Misc
|
||||||
roblox.toml
|
roblox.toml
|
||||||
sourcemap.json
|
sourcemap.json
|
||||||
drafts/*.lua
|
drafts/
|
||||||
|
|
||||||
# Cached Vitepress (docs)
|
# Cached Vitepress (docs)
|
||||||
|
|
||||||
|
@ -61,4 +61,4 @@ drafts/*.lua
|
||||||
/docs/.vitepress/dist
|
/docs/.vitepress/dist
|
||||||
|
|
||||||
.vitepress/cache
|
.vitepress/cache
|
||||||
.vitepress/dist
|
.vitepress/dist
|
||||||
|
|
349
src/init.luau
349
src/init.luau
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
--!optimize 2
|
--!optimize 2
|
||||||
--!native
|
--!native
|
||||||
--!strict
|
--!strict
|
||||||
|
@ -17,6 +16,7 @@ type ArchetypeEdge = {
|
||||||
remove: Archetype,
|
remove: Archetype,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type Archetype = {
|
type Archetype = {
|
||||||
id: number,
|
id: number,
|
||||||
edges: { [i53]: ArchetypeEdge },
|
edges: { [i53]: ArchetypeEdge },
|
||||||
|
@ -669,6 +669,32 @@ do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local world_has: () -> boolean
|
||||||
|
do
|
||||||
|
function world_has(world, entity_id, ...)
|
||||||
|
local id = entity_id
|
||||||
|
local record = world.entityIndex.sparse[id]
|
||||||
|
if not record then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local archetype = record.archetype
|
||||||
|
if not archetype then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local tr = archetype.records
|
||||||
|
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
if not tr[select(i, ...)] then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
type Item = () -> (number, ...any)
|
type Item = () -> (number, ...any)
|
||||||
export type Query = typeof({
|
export type Query = typeof({
|
||||||
__iter = function(): Item
|
__iter = function(): Item
|
||||||
|
@ -678,8 +704,8 @@ export type Query = typeof({
|
||||||
end,
|
end,
|
||||||
}) & {
|
}) & {
|
||||||
next: Item,
|
next: Item,
|
||||||
replace: (Query, ...any) -> (),
|
without: (Query) -> Query,
|
||||||
without: (Query) -> Query
|
replace: (Query, (...any) -> (...any)) -> ()
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
||||||
|
@ -688,135 +714,145 @@ local world_query: (World, ...i53) -> Query
|
||||||
do
|
do
|
||||||
|
|
||||||
local noop: Item = function()
|
local noop: Item = function()
|
||||||
return nil :: any
|
return nil :: any
|
||||||
end
|
end
|
||||||
|
|
||||||
local EmptyQuery: Query = {
|
local EmptyQuery: Query = {
|
||||||
__iter = function(): Item
|
__iter = function(): Item
|
||||||
return noop
|
return noop
|
||||||
end,
|
end,
|
||||||
next = noop :: Item,
|
next = noop :: Item,
|
||||||
replace = noop :: (Query, ...any) -> (),
|
replace = noop :: (Query, ...any) -> (),
|
||||||
without = function(self: Query, ...)
|
without = function(self: Query, ...)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable(EmptyQuery, EmptyQuery)
|
setmetatable(EmptyQuery, EmptyQuery)
|
||||||
|
|
||||||
local indices: { { number } }
|
|
||||||
local compatibleArchetypes: { Archetype }
|
|
||||||
local length
|
|
||||||
local components: { number }
|
|
||||||
local queryLength: number
|
|
||||||
local lastArchetype: number
|
local lastArchetype: number
|
||||||
local archetype: Archetype
|
local archetype: Archetype
|
||||||
|
local queryOutput: { any }
|
||||||
|
local queryLength: number
|
||||||
|
local entities: { number }
|
||||||
|
local i: number
|
||||||
|
|
||||||
local queryOutput: { any }
|
local compatible_archetypes: { Archetype }
|
||||||
|
local column_indices: { { number} }
|
||||||
|
local ids: { number }
|
||||||
|
|
||||||
local entities: {}
|
local function world_query_next(): any
|
||||||
local i: number
|
|
||||||
|
|
||||||
local function world_query_next()
|
|
||||||
local entityId = entities[i]
|
local entityId = entities[i]
|
||||||
while entityId == nil do
|
while entityId == nil do
|
||||||
lastArchetype += 1
|
lastArchetype += 1
|
||||||
archetype = compatibleArchetypes[lastArchetype]
|
archetype = compatible_archetypes[lastArchetype]
|
||||||
|
|
||||||
if not archetype then
|
if not archetype then
|
||||||
return
|
return nil
|
||||||
end
|
end
|
||||||
|
entities = archetype.entities
|
||||||
entities = archetype.entities
|
|
||||||
i = #entities
|
i = #entities
|
||||||
entityId = entities[i]
|
entityId = entities[i]
|
||||||
end
|
end
|
||||||
|
|
||||||
local row = i
|
local row = i
|
||||||
i-=1
|
i-=1
|
||||||
|
|
||||||
local columns = archetype.columns
|
local columns = archetype.columns
|
||||||
local tr = indices[lastArchetype]
|
local tr = column_indices[lastArchetype]
|
||||||
|
|
||||||
if queryLength == 1 then
|
if queryLength == 1 then
|
||||||
return entityId, columns[tr[1]][row]
|
return entityId, columns[tr[1]][row]
|
||||||
elseif queryLength == 2 then
|
elseif queryLength == 2 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row]
|
return entityId, columns[tr[1]][row], columns[tr[2]][row]
|
||||||
elseif queryLength == 3 then
|
elseif queryLength == 3 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row]
|
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row]
|
||||||
elseif queryLength == 4 then
|
elseif queryLength == 4 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row]
|
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row]
|
||||||
elseif queryLength == 5 then
|
elseif queryLength == 5 then
|
||||||
return entityId,columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
return entityId,
|
||||||
columns[tr[5]][row]
|
columns[tr[1]][row],
|
||||||
elseif queryLength == 6 then
|
columns[tr[2]][row],
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
columns[tr[3]][row],
|
||||||
columns[tr[5]][row],
|
columns[tr[4]][row],
|
||||||
columns[tr[6]][row]
|
columns[tr[5]][row]
|
||||||
elseif queryLength == 7 then
|
elseif queryLength == 6 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
return entityId,
|
||||||
columns[tr[5]][row],
|
columns[tr[1]][row],
|
||||||
columns[tr[6]][row],
|
columns[tr[2]][row],
|
||||||
columns[tr[7]][row]
|
columns[tr[3]][row],
|
||||||
elseif queryLength == 8 then
|
columns[tr[4]][row],
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
columns[tr[5]][row],
|
||||||
columns[tr[5]][row],
|
columns[tr[6]][row]
|
||||||
columns[tr[6]][row],
|
elseif queryLength == 7 then
|
||||||
columns[tr[7]][row],
|
return entityId,
|
||||||
columns[tr[8]][row]
|
columns[tr[1]][row],
|
||||||
end
|
columns[tr[2]][row],
|
||||||
|
columns[tr[3]][row],
|
||||||
|
columns[tr[4]][row],
|
||||||
|
columns[tr[5]][row],
|
||||||
|
columns[tr[6]][row],
|
||||||
|
columns[tr[7]][row]
|
||||||
|
elseif queryLength == 8 then
|
||||||
|
return entityId,
|
||||||
|
columns[tr[1]][row],
|
||||||
|
columns[tr[2]][row],
|
||||||
|
columns[tr[3]][row],
|
||||||
|
columns[tr[4]][row],
|
||||||
|
columns[tr[5]][row],
|
||||||
|
columns[tr[6]][row],
|
||||||
|
columns[tr[7]][row],
|
||||||
|
columns[tr[8]][row]
|
||||||
|
end
|
||||||
|
|
||||||
for i in components do
|
for j in ids do
|
||||||
queryOutput[i] = columns[tr[i]][row]
|
queryOutput[j] = columns[tr[j]][row]
|
||||||
end
|
end
|
||||||
|
|
||||||
return entityId, unpack(queryOutput, 1, queryLength)
|
return entityId, unpack(queryOutput, 1, queryLength)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_without(self, ...): Query
|
local function world_query_iter()
|
||||||
|
return world_query_next
|
||||||
|
end
|
||||||
|
|
||||||
|
local function world_query_without(self, ...)
|
||||||
local withoutComponents = { ... }
|
local withoutComponents = { ... }
|
||||||
for i = #compatibleArchetypes, 1, -1 do
|
for i = #compatible_archetypes, 1, -1 do
|
||||||
local archetype = compatibleArchetypes[i]
|
local archetype = compatible_archetypes[i]
|
||||||
local records = archetype.records
|
local records = archetype.records
|
||||||
local shouldRemove = false
|
local shouldRemove = false
|
||||||
|
|
||||||
for _, componentId in withoutComponents do
|
for _, componentId in withoutComponents do
|
||||||
if records[componentId] then
|
if records[componentId] then
|
||||||
shouldRemove = true
|
shouldRemove = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if shouldRemove then
|
if shouldRemove then
|
||||||
table.remove(compatibleArchetypes, i)
|
table.remove(compatible_archetypes, i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if #compatibleArchetypes == 0 then
|
|
||||||
return EmptyQuery
|
|
||||||
end
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
|
|
||||||
local function world_query_iter()
|
|
||||||
lastArchetype = 1
|
lastArchetype = 1
|
||||||
archetype = compatibleArchetypes[1]
|
archetype = compatible_archetypes[lastArchetype]
|
||||||
entities = archetype.entities
|
|
||||||
i = #entities
|
|
||||||
|
|
||||||
return world_query_next
|
if not archetype then
|
||||||
|
return EmptyQuery
|
||||||
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_replace_values(row, columns, ...)
|
local function world_query_replace_values(row, columns, ...)
|
||||||
for i, column in columns do
|
for i, column in columns do
|
||||||
column[row] = select(i, ...)
|
column[row] = select(i, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_replace(_, fn: any)
|
local function world_query_replace(_, fn: (...any) -> (...any))
|
||||||
for i, archetype in compatibleArchetypes do
|
for i, archetype in compatible_archetypes do
|
||||||
local tr = indices[i]
|
local tr = column_indices[i]
|
||||||
local columns = archetype.columns
|
local columns = archetype.columns
|
||||||
|
|
||||||
for row in archetype.entities do
|
for row in archetype.entities do
|
||||||
|
@ -855,80 +891,88 @@ do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function world_query(world: World, ...: number): Query
|
|
||||||
-- breaking?
|
|
||||||
if (...) == nil then
|
|
||||||
error("Missing components")
|
|
||||||
end
|
|
||||||
|
|
||||||
indices = {}
|
function world_query(world: World, ...: any): Query
|
||||||
compatibleArchetypes = {}
|
-- breaking?
|
||||||
length = 0
|
if (...) == nil then
|
||||||
components = { ... }
|
error("Missing components")
|
||||||
|
end
|
||||||
|
|
||||||
local archetypes: { Archetype } = world.archetypes :: any
|
local indices = {}
|
||||||
local firstArchetypeMap: ArchetypeMap
|
local compatibleArchetypes = {}
|
||||||
local componentIndex = world.componentIndex
|
local length = 0
|
||||||
|
|
||||||
for _, componentId in components do
|
local components = { ... } :: any
|
||||||
local map: ArchetypeMap = componentIndex[componentId] :: any
|
local archetypes = world.archetypes
|
||||||
if not map then
|
|
||||||
return EmptyQuery
|
|
||||||
end
|
|
||||||
|
|
||||||
if (firstArchetypeMap :: any) == nil or firstArchetypeMap.size > map.size then
|
local firstArchetypeMap: ArchetypeMap
|
||||||
firstArchetypeMap = map
|
local componentIndex = world.componentIndex
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for id in firstArchetypeMap.cache do
|
for _, componentId in components do
|
||||||
local compatibleArchetype = archetypes[id]
|
local map = componentIndex[componentId]
|
||||||
local archetypeRecords = compatibleArchetype.records
|
if not map then
|
||||||
|
return EmptyQuery
|
||||||
|
end
|
||||||
|
|
||||||
local records: { number } = {}
|
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
|
||||||
local skip = false
|
firstArchetypeMap = map
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
for i, componentId in components do
|
for id in firstArchetypeMap.cache do
|
||||||
local index = archetypeRecords[componentId]
|
local compatibleArchetype = archetypes[id]
|
||||||
if not index then
|
local archetypeRecords = compatibleArchetype.records
|
||||||
skip = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
-- index should be index.offset
|
|
||||||
records[i] = index
|
|
||||||
end
|
|
||||||
|
|
||||||
if skip then
|
local records = {}
|
||||||
continue
|
local skip = false
|
||||||
end
|
|
||||||
|
|
||||||
length += 1
|
for i, componentId in components do
|
||||||
compatibleArchetypes[length] = compatibleArchetype
|
local index = archetypeRecords[componentId]
|
||||||
indices[length] = records
|
if not index then
|
||||||
end
|
skip = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
-- index should be index.offset
|
||||||
|
records[i] = index
|
||||||
|
end
|
||||||
|
|
||||||
lastArchetype = 1
|
if skip then
|
||||||
archetype = compatibleArchetypes[lastArchetype]
|
continue
|
||||||
|
end
|
||||||
|
|
||||||
if not archetype then
|
length += 1
|
||||||
return EmptyQuery
|
compatibleArchetypes[length] = compatibleArchetype
|
||||||
end
|
indices[length] = records
|
||||||
|
end
|
||||||
|
|
||||||
queryOutput = {}
|
compatible_archetypes = compatibleArchetypes
|
||||||
queryLength = #components
|
column_indices = indices
|
||||||
|
ids = components
|
||||||
|
|
||||||
entities = archetype.entities
|
lastArchetype = 1
|
||||||
i = #entities
|
archetype = compatible_archetypes[lastArchetype]
|
||||||
|
|
||||||
local it = {
|
if not archetype then
|
||||||
__iter = world_query_iter,
|
return EmptyQuery
|
||||||
next = world_query_next,
|
end
|
||||||
without = world_query_without,
|
|
||||||
replace = world_query_replace
|
|
||||||
}
|
|
||||||
|
|
||||||
return setmetatable(it, it) :: any
|
queryOutput = {}
|
||||||
end
|
queryLength = #ids
|
||||||
|
|
||||||
|
entities = archetype.entities
|
||||||
|
i = #entities
|
||||||
|
|
||||||
|
local it = {
|
||||||
|
__iter = world_query_iter,
|
||||||
|
next = world_query_next,
|
||||||
|
without = world_query_without,
|
||||||
|
replace = world_query_replace
|
||||||
|
} :: any
|
||||||
|
|
||||||
|
setmetatable(it, it)
|
||||||
|
|
||||||
|
return it
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53)
|
type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53)
|
||||||
|
@ -1077,6 +1121,7 @@ World.component = world_component
|
||||||
World.add = world_add
|
World.add = world_add
|
||||||
World.set = world_set
|
World.set = world_set
|
||||||
World.get = world_get
|
World.get = world_get
|
||||||
|
World.has = world_has
|
||||||
World.target = world_target
|
World.target = world_target
|
||||||
World.parent = world_parent
|
World.parent = world_parent
|
||||||
|
|
||||||
|
|
|
@ -60,10 +60,9 @@ TEST("world", function()
|
||||||
world:clear(e)
|
world:clear(e)
|
||||||
CHECK(world:get(e, A) == nil)
|
CHECK(world:get(e, A) == nil)
|
||||||
CHECK(world:get(e, B) == nil)
|
CHECK(world:get(e, B) == nil)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE("iterator should not drain the query")
|
do CASE("should drain query while iterating")
|
||||||
local world = jecs.World.new() :: World
|
local world = jecs.World.new() :: World
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
@ -85,7 +84,8 @@ TEST("world", function()
|
||||||
for _ in q do
|
for _ in q do
|
||||||
j+=1
|
j+=1
|
||||||
end
|
end
|
||||||
CHECK(i == j)
|
CHECK(i == 2)
|
||||||
|
CHECK(j == 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE("should be able to get next results")
|
do CASE("should be able to get next results")
|
||||||
|
|
Loading…
Reference in a new issue