mirror of
https://github.com/Ukendio/jecs.git
synced 2025-08-04 11:19:17 +00:00
Merge branch 'main' of https://github.com/Ukendio/jecs into example
This commit is contained in:
commit
3ba6a2fb49
1 changed files with 401 additions and 401 deletions
300
src/init.luau
300
src/init.luau
|
@ -714,9 +714,6 @@ export type Query = typeof({
|
||||||
|
|
||||||
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
||||||
|
|
||||||
local world_query: (World, ...i53) -> Query
|
|
||||||
do
|
|
||||||
|
|
||||||
local noop: Item = function()
|
local noop: Item = function()
|
||||||
return nil :: any
|
return nil :: any
|
||||||
end
|
end
|
||||||
|
@ -724,6 +721,9 @@ do
|
||||||
local Arm = function(self: Query, ...)
|
local Arm = function(self: Query, ...)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
local world_query
|
||||||
|
do
|
||||||
|
|
||||||
local EmptyQuery: Query = {
|
local EmptyQuery: Query = {
|
||||||
__iter = function(): Item
|
__iter = function(): Item
|
||||||
return noop
|
return noop
|
||||||
|
@ -737,91 +737,76 @@ do
|
||||||
|
|
||||||
setmetatable(EmptyQuery, EmptyQuery)
|
setmetatable(EmptyQuery, EmptyQuery)
|
||||||
|
|
||||||
local lastArchetype: number
|
local function world_query_replace_values(row, columns, ...)
|
||||||
local archetype: Archetype
|
for i, column in columns do
|
||||||
local queryOutput: { any }
|
column[row] = select(i, ...)
|
||||||
local entities: { number }
|
end
|
||||||
local i: number
|
end
|
||||||
|
|
||||||
local compatible_archetypes: { Archetype }
|
function world_query(world, ...)
|
||||||
local ids: { number }
|
-- breaking
|
||||||
local columns
|
if (...) == nil then
|
||||||
|
error("Missing components")
|
||||||
|
end
|
||||||
|
|
||||||
local A, B, C, D, E, F, G, H, I
|
local compatible_archetypes = {}
|
||||||
|
local length = 0
|
||||||
|
|
||||||
|
local ids = { ... }
|
||||||
|
local A, B, C, D, E, F, G, H, I = ...
|
||||||
local a, b, c, d, e, f, g, h
|
local a, b, c, d, e, f, g, h
|
||||||
|
|
||||||
local init
|
local archetypes = world.archetypes
|
||||||
local drain
|
|
||||||
|
|
||||||
local function query_init(query)
|
local idr: ArchetypeMap
|
||||||
if init and drain then
|
local componentIndex = world.componentIndex
|
||||||
return
|
|
||||||
|
for _, id in ids do
|
||||||
|
local map = componentIndex[id]
|
||||||
|
if not map then
|
||||||
|
return EmptyQuery
|
||||||
end
|
end
|
||||||
|
|
||||||
init = true
|
if idr == nil or map.size < idr.size then
|
||||||
lastArchetype = 1
|
idr = map
|
||||||
archetype = compatible_archetypes[lastArchetype]
|
|
||||||
|
|
||||||
if not archetype then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
queryOutput = {}
|
|
||||||
|
|
||||||
entities = archetype.entities
|
|
||||||
i = #entities
|
|
||||||
columns = archetype.columns
|
|
||||||
|
|
||||||
local records = archetype.records
|
|
||||||
if not B then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
elseif not C then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
b = columns[records[B].column]
|
|
||||||
elseif not D then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
b = columns[records[B].column]
|
|
||||||
c = columns[records[C].column]
|
|
||||||
elseif not E then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
b = columns[records[B].column]
|
|
||||||
c = columns[records[C].column]
|
|
||||||
d = columns[records[D].column]
|
|
||||||
elseif not F then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
b = columns[records[B].column]
|
|
||||||
c = columns[records[C].column]
|
|
||||||
d = columns[records[D].column]
|
|
||||||
e = columns[records[E].column]
|
|
||||||
elseif not G then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
b = columns[records[B].column]
|
|
||||||
c = columns[records[C].column]
|
|
||||||
d = columns[records[D].column]
|
|
||||||
e = columns[records[E].column]
|
|
||||||
f = columns[records[F].column]
|
|
||||||
elseif not H then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
b = columns[records[B].column]
|
|
||||||
c = columns[records[C].column]
|
|
||||||
d = columns[records[D].column]
|
|
||||||
e = columns[records[E].column]
|
|
||||||
f = columns[records[F].column]
|
|
||||||
g = columns[records[G].column]
|
|
||||||
elseif H then
|
|
||||||
a = columns[records[A].column]
|
|
||||||
b = columns[records[B].column]
|
|
||||||
c = columns[records[D].column]
|
|
||||||
e = columns[records[E].column]
|
|
||||||
f = columns[records[F].column]
|
|
||||||
g = columns[records[G].column]
|
|
||||||
h = columns[records[H].column]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
for archetype_id in idr.cache do
|
||||||
|
local compatibleArchetype = archetypes[archetype_id]
|
||||||
|
local tr = compatibleArchetype.records
|
||||||
|
|
||||||
|
local skip = false
|
||||||
|
|
||||||
|
for i, id in ids do
|
||||||
|
local index = tr[id]
|
||||||
|
if not index then
|
||||||
|
skip = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if skip then
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
|
||||||
|
length += 1
|
||||||
|
compatible_archetypes[length] = compatibleArchetype
|
||||||
|
end
|
||||||
|
|
||||||
|
if length == 0 then
|
||||||
|
return EmptyQuery
|
||||||
|
end
|
||||||
|
|
||||||
|
local lastArchetype = 1
|
||||||
|
local archetype
|
||||||
|
local columns
|
||||||
|
local entities
|
||||||
|
local i
|
||||||
|
local queryOutput
|
||||||
|
|
||||||
local world_query_iter_next
|
local world_query_iter_next
|
||||||
|
|
||||||
local function world_query_iter_create()
|
|
||||||
if not B then
|
if not B then
|
||||||
function world_query_iter_next(): any
|
function world_query_iter_next(): any
|
||||||
local entityId = entities[i]
|
local entityId = entities[i]
|
||||||
|
@ -1005,9 +990,79 @@ do
|
||||||
return entityId, unpack(queryOutput)
|
return entityId, unpack(queryOutput)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local init = false
|
||||||
|
local drain = false
|
||||||
|
|
||||||
|
local function query_init(query)
|
||||||
|
if init and drain then
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_without(self, ...)
|
init = true
|
||||||
|
lastArchetype = 1
|
||||||
|
archetype = compatible_archetypes[lastArchetype]
|
||||||
|
|
||||||
|
if not archetype then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
queryOutput = {}
|
||||||
|
|
||||||
|
entities = archetype.entities
|
||||||
|
i = #entities
|
||||||
|
columns = archetype.columns
|
||||||
|
|
||||||
|
local records = archetype.records
|
||||||
|
if not B then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
elseif not C then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
b = columns[records[B].column]
|
||||||
|
elseif not D then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
b = columns[records[B].column]
|
||||||
|
c = columns[records[C].column]
|
||||||
|
elseif not E then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
b = columns[records[B].column]
|
||||||
|
c = columns[records[C].column]
|
||||||
|
d = columns[records[D].column]
|
||||||
|
elseif not F then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
b = columns[records[B].column]
|
||||||
|
c = columns[records[C].column]
|
||||||
|
d = columns[records[D].column]
|
||||||
|
e = columns[records[E].column]
|
||||||
|
elseif not G then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
b = columns[records[B].column]
|
||||||
|
c = columns[records[C].column]
|
||||||
|
d = columns[records[D].column]
|
||||||
|
e = columns[records[E].column]
|
||||||
|
f = columns[records[F].column]
|
||||||
|
elseif not H then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
b = columns[records[B].column]
|
||||||
|
c = columns[records[C].column]
|
||||||
|
d = columns[records[D].column]
|
||||||
|
e = columns[records[E].column]
|
||||||
|
f = columns[records[F].column]
|
||||||
|
g = columns[records[G].column]
|
||||||
|
elseif not I then
|
||||||
|
a = columns[records[A].column]
|
||||||
|
b = columns[records[B].column]
|
||||||
|
c = columns[records[C].column]
|
||||||
|
d = columns[records[D].column]
|
||||||
|
e = columns[records[E].column]
|
||||||
|
f = columns[records[F].column]
|
||||||
|
g = columns[records[G].column]
|
||||||
|
h = columns[records[H].column]
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local function world_query_without(query, ...)
|
||||||
local withoutComponents = { ... }
|
local withoutComponents = { ... }
|
||||||
for i = #compatible_archetypes, 1, -1 do
|
for i = #compatible_archetypes, 1, -1 do
|
||||||
local archetype = compatible_archetypes[i]
|
local archetype = compatible_archetypes[i]
|
||||||
|
@ -1027,16 +1082,15 @@ do
|
||||||
compatible_archetypes[i] = compatible_archetypes[last]
|
compatible_archetypes[i] = compatible_archetypes[last]
|
||||||
end
|
end
|
||||||
compatible_archetypes[last] = nil
|
compatible_archetypes[last] = nil
|
||||||
|
length -= 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return self
|
if length == 0 then
|
||||||
|
return EmptyQuery
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_replace_values(row, columns, ...)
|
return query
|
||||||
for i, column in columns do
|
|
||||||
column[row] = select(i, ...)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_replace(query, fn: (...any) -> (...any))
|
local function world_query_replace(query, fn: (...any) -> (...any))
|
||||||
|
@ -1083,14 +1137,14 @@ do
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_with(query, ...)
|
local function world_query_with(query, ...)
|
||||||
local ids = { ... }
|
local with = { ... }
|
||||||
for i = #compatible_archetypes, 1, -1 do
|
for i = #compatible_archetypes, 1, -1 do
|
||||||
local archetype = compatible_archetypes[i]
|
local archetype = compatible_archetypes[i]
|
||||||
local records = archetype.records
|
local tr = archetype.records
|
||||||
local shouldRemove = false
|
local shouldRemove = false
|
||||||
|
|
||||||
for _, id in ids do
|
for _, id in with do
|
||||||
if not records[id] then
|
if not tr[id] then
|
||||||
shouldRemove = true
|
shouldRemove = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -1102,11 +1156,12 @@ do
|
||||||
compatible_archetypes[i] = compatible_archetypes[last]
|
compatible_archetypes[i] = compatible_archetypes[last]
|
||||||
end
|
end
|
||||||
compatible_archetypes[last] = nil
|
compatible_archetypes[last] = nil
|
||||||
|
length -= 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if length == 0 then
|
||||||
query_init(query)
|
return EmptyQuery
|
||||||
|
end
|
||||||
return query
|
return query
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1119,20 +1174,22 @@ do
|
||||||
|
|
||||||
local function world_query_drain(query)
|
local function world_query_drain(query)
|
||||||
drain = true
|
drain = true
|
||||||
query_init(query)
|
if query_init(query) then
|
||||||
return query
|
return query
|
||||||
end
|
end
|
||||||
|
return EmptyQuery
|
||||||
|
end
|
||||||
|
|
||||||
local function world_query_iter(query)
|
local function world_query_iter(query)
|
||||||
query_init(query)
|
query_init(query)
|
||||||
return world_query_iter_next
|
return world_query_iter_next
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_next()
|
local function world_query_next(world)
|
||||||
if not drain then
|
if not drain then
|
||||||
error("Did you forget to call query:drain()?")
|
error("Did you forget to call query:drain()?")
|
||||||
end
|
end
|
||||||
return world_query_iter_next()
|
return world_query_iter_next(world)
|
||||||
end
|
end
|
||||||
|
|
||||||
local it = {
|
local it = {
|
||||||
|
@ -1147,70 +1204,13 @@ do
|
||||||
|
|
||||||
setmetatable(it, it)
|
setmetatable(it, it)
|
||||||
|
|
||||||
function world_query(world: World, ...: any): Query
|
|
||||||
-- breaking
|
|
||||||
if (...) == nil then
|
|
||||||
error("Missing components")
|
|
||||||
end
|
|
||||||
|
|
||||||
compatible_archetypes = {}
|
|
||||||
local length = 0
|
|
||||||
|
|
||||||
local components = { ... } :: any
|
|
||||||
A, B, C, D, E, F, G, H, I = ...
|
|
||||||
|
|
||||||
local archetypes = world.archetypes
|
|
||||||
|
|
||||||
local firstArchetypeMap: ArchetypeMap
|
|
||||||
local componentIndex = world.componentIndex
|
|
||||||
|
|
||||||
for _, componentId in components do
|
|
||||||
local map = componentIndex[componentId]
|
|
||||||
if not map then
|
|
||||||
return EmptyQuery
|
|
||||||
end
|
|
||||||
|
|
||||||
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
|
|
||||||
firstArchetypeMap = map
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for id in firstArchetypeMap.cache do
|
|
||||||
local compatibleArchetype = archetypes[id]
|
|
||||||
local archetypeRecords = compatibleArchetype.records
|
|
||||||
|
|
||||||
local skip = false
|
|
||||||
|
|
||||||
for i, componentId in components do
|
|
||||||
local index = archetypeRecords[componentId]
|
|
||||||
if not index then
|
|
||||||
skip = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if skip then
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
|
|
||||||
length += 1
|
|
||||||
compatible_archetypes[length] = compatibleArchetype
|
|
||||||
end
|
|
||||||
|
|
||||||
drain = false
|
|
||||||
init = false
|
|
||||||
ids = components
|
|
||||||
|
|
||||||
world_query_iter_create()
|
|
||||||
|
|
||||||
return it
|
return it
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53)
|
|
||||||
-- __nominal_type_dont_use could not be any or T as it causes a type error
|
-- __nominal_type_dont_use could not be any or T as it causes a type error
|
||||||
-- or produces a union
|
-- or produces a union
|
||||||
export type Entity<T = any> = number & { __nominal_type_dont_use: T }
|
export type Entity<T = any> = number & { __DO_NOT_USE_OR_YOU_WILL_BE_FIRED: T }
|
||||||
export type Pair = number
|
export type Pair = number
|
||||||
|
|
||||||
export type QueryShim<T...> = typeof(setmetatable({
|
export type QueryShim<T...> = typeof(setmetatable({
|
||||||
|
|
Loading…
Reference in a new issue