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 } }
|
||||
|
||||
local world_query: (World, ...i53) -> Query
|
||||
do
|
||||
|
||||
local noop: Item = function()
|
||||
return nil :: any
|
||||
end
|
||||
|
@ -724,6 +721,9 @@ do
|
|||
local Arm = function(self: Query, ...)
|
||||
return self
|
||||
end
|
||||
local world_query
|
||||
do
|
||||
|
||||
local EmptyQuery: Query = {
|
||||
__iter = function(): Item
|
||||
return noop
|
||||
|
@ -737,91 +737,76 @@ do
|
|||
|
||||
setmetatable(EmptyQuery, EmptyQuery)
|
||||
|
||||
local lastArchetype: number
|
||||
local archetype: Archetype
|
||||
local queryOutput: { any }
|
||||
local entities: { number }
|
||||
local i: number
|
||||
local function world_query_replace_values(row, columns, ...)
|
||||
for i, column in columns do
|
||||
column[row] = select(i, ...)
|
||||
end
|
||||
end
|
||||
|
||||
local compatible_archetypes: { Archetype }
|
||||
local ids: { number }
|
||||
local columns
|
||||
function world_query(world, ...)
|
||||
-- breaking
|
||||
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 init
|
||||
local drain
|
||||
local archetypes = world.archetypes
|
||||
|
||||
local function query_init(query)
|
||||
if init and drain then
|
||||
return
|
||||
local idr: ArchetypeMap
|
||||
local componentIndex = world.componentIndex
|
||||
|
||||
for _, id in ids do
|
||||
local map = componentIndex[id]
|
||||
if not map then
|
||||
return EmptyQuery
|
||||
end
|
||||
|
||||
init = true
|
||||
lastArchetype = 1
|
||||
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]
|
||||
if idr == nil or map.size < idr.size then
|
||||
idr = map
|
||||
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 function world_query_iter_create()
|
||||
if not B then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
|
@ -1005,9 +990,79 @@ do
|
|||
return entityId, unpack(queryOutput)
|
||||
end
|
||||
end
|
||||
|
||||
local init = false
|
||||
local drain = false
|
||||
|
||||
local function query_init(query)
|
||||
if init and drain then
|
||||
return
|
||||
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 = { ... }
|
||||
for i = #compatible_archetypes, 1, -1 do
|
||||
local archetype = compatible_archetypes[i]
|
||||
|
@ -1027,16 +1082,15 @@ do
|
|||
compatible_archetypes[i] = compatible_archetypes[last]
|
||||
end
|
||||
compatible_archetypes[last] = nil
|
||||
length -= 1
|
||||
end
|
||||
end
|
||||
|
||||
return self
|
||||
if length == 0 then
|
||||
return EmptyQuery
|
||||
end
|
||||
|
||||
local function world_query_replace_values(row, columns, ...)
|
||||
for i, column in columns do
|
||||
column[row] = select(i, ...)
|
||||
end
|
||||
return query
|
||||
end
|
||||
|
||||
local function world_query_replace(query, fn: (...any) -> (...any))
|
||||
|
@ -1083,14 +1137,14 @@ do
|
|||
end
|
||||
|
||||
local function world_query_with(query, ...)
|
||||
local ids = { ... }
|
||||
local with = { ... }
|
||||
for i = #compatible_archetypes, 1, -1 do
|
||||
local archetype = compatible_archetypes[i]
|
||||
local records = archetype.records
|
||||
local tr = archetype.records
|
||||
local shouldRemove = false
|
||||
|
||||
for _, id in ids do
|
||||
if not records[id] then
|
||||
for _, id in with do
|
||||
if not tr[id] then
|
||||
shouldRemove = true
|
||||
break
|
||||
end
|
||||
|
@ -1102,11 +1156,12 @@ do
|
|||
compatible_archetypes[i] = compatible_archetypes[last]
|
||||
end
|
||||
compatible_archetypes[last] = nil
|
||||
length -= 1
|
||||
end
|
||||
end
|
||||
|
||||
query_init(query)
|
||||
|
||||
if length == 0 then
|
||||
return EmptyQuery
|
||||
end
|
||||
return query
|
||||
end
|
||||
|
||||
|
@ -1119,20 +1174,22 @@ do
|
|||
|
||||
local function world_query_drain(query)
|
||||
drain = true
|
||||
query_init(query)
|
||||
if query_init(query) then
|
||||
return query
|
||||
end
|
||||
return EmptyQuery
|
||||
end
|
||||
|
||||
local function world_query_iter(query)
|
||||
query_init(query)
|
||||
return world_query_iter_next
|
||||
end
|
||||
|
||||
local function world_query_next()
|
||||
local function world_query_next(world)
|
||||
if not drain then
|
||||
error("Did you forget to call query:drain()?")
|
||||
end
|
||||
return world_query_iter_next()
|
||||
return world_query_iter_next(world)
|
||||
end
|
||||
|
||||
local it = {
|
||||
|
@ -1147,70 +1204,13 @@ do
|
|||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53)
|
||||
-- __nominal_type_dont_use could not be any or T as it causes a type error
|
||||
-- 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 QueryShim<T...> = typeof(setmetatable({
|
||||
|
|
Loading…
Reference in a new issue