mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 09:30:03 +00:00
Compare commits
No commits in common. "f8b3772bcef55f712ec5fbb84a4eebff8b4bbf90" and "ed3b2ae35d7cb89eb48a761fac1324e8988157b6" have entirely different histories.
f8b3772bce
...
ed3b2ae35d
2 changed files with 233 additions and 234 deletions
409
jecs.luau
409
jecs.luau
|
@ -89,8 +89,8 @@ local EcsOnDeleteTarget = HI_COMPONENT_ID + 8
|
|||
local EcsDelete = HI_COMPONENT_ID + 9
|
||||
local EcsRemove = HI_COMPONENT_ID + 10
|
||||
local EcsName = HI_COMPONENT_ID + 11
|
||||
local EcsOnArchetypeCreate = HI_COMPONENT_ID + 12
|
||||
local EcsOnArchetypeDelete = HI_COMPONENT_ID + 13
|
||||
local EcsArchetypeCreate = HI_COMPONENT_ID + 12
|
||||
local EcsArchetypeDelete = HI_COMPONENT_ID + 13
|
||||
local EcsRest = HI_COMPONENT_ID + 14
|
||||
|
||||
local ECS_PAIR_FLAG = 0x8
|
||||
|
@ -611,7 +611,6 @@ local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?)
|
|||
local idr_t = id_record_ensure(world, t)
|
||||
archetype_append_to_records(idr_t, archetype_id, records, t, i)
|
||||
end
|
||||
|
||||
if bit32.band(idr.flags, ECS_ID_IS_TAG) == 0 then
|
||||
columns[i] = {}
|
||||
else
|
||||
|
@ -620,7 +619,7 @@ local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?)
|
|||
end
|
||||
|
||||
for _, id in id_types do
|
||||
local observer_list = find_observers(world, EcsOnArchetypeCreate, id)
|
||||
local observer_list = find_observers(world, EcsArchetypeCreate, id)
|
||||
if not observer_list then
|
||||
continue
|
||||
end
|
||||
|
@ -1053,7 +1052,7 @@ local function archetype_destroy(world: World, archetype: Archetype)
|
|||
local records = archetype.records
|
||||
|
||||
for id in records do
|
||||
local observer_list = find_observers(world, EcsOnArchetypeDelete, id)
|
||||
local observer_list = find_observers(world, EcsArchetypeDelete, id)
|
||||
if not observer_list then
|
||||
continue
|
||||
end
|
||||
|
@ -1458,6 +1457,7 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
|
|||
return entityId, a[row], b[row], c[row], d[row], e[row], f[row], g[row], h[row]
|
||||
end
|
||||
|
||||
local records = archetype.records
|
||||
for j, id in ids do
|
||||
queryOutput[j] = columns[records[id].column][row]
|
||||
end
|
||||
|
@ -1553,10 +1553,10 @@ local function query_cached(query: QueryInner)
|
|||
-- because the event will be emitted for all components of that Archetype.
|
||||
local first = query.ids[1]
|
||||
local observerable = world.observerable
|
||||
local on_create_action = observerable[EcsOnArchetypeCreate]
|
||||
local on_create_action = observerable[EcsArchetypeCreate]
|
||||
if not on_create_action then
|
||||
on_create_action = {}
|
||||
observerable[EcsOnArchetypeCreate] = on_create_action
|
||||
observerable[EcsArchetypeCreate] = on_create_action
|
||||
end
|
||||
local query_cache_on_create = on_create_action[first]
|
||||
if not query_cache_on_create then
|
||||
|
@ -1564,10 +1564,10 @@ local function query_cached(query: QueryInner)
|
|||
on_create_action[first] = query_cache_on_create
|
||||
end
|
||||
|
||||
local on_delete_action = observerable[EcsOnArchetypeDelete]
|
||||
local on_delete_action = observerable[EcsArchetypeDelete]
|
||||
if not on_delete_action then
|
||||
on_delete_action = {}
|
||||
observerable[EcsOnArchetypeDelete] = on_delete_action
|
||||
observerable[EcsArchetypeDelete] = on_delete_action
|
||||
end
|
||||
local query_cache_on_delete = on_delete_action[first]
|
||||
if not query_cache_on_delete then
|
||||
|
@ -1609,11 +1609,181 @@ local function query_cached(query: QueryInner)
|
|||
|
||||
local world_query_iter_next
|
||||
local columns: { Column }
|
||||
local entities: { number }
|
||||
local entities: { i53 }
|
||||
local i: number
|
||||
local archetype: Archetype
|
||||
local records: { ArchetypeRecord }
|
||||
|
||||
if not B then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row]
|
||||
end
|
||||
elseif not C then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
local records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row], b[row]
|
||||
end
|
||||
elseif not D then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
local records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row], b[row], c[row]
|
||||
end
|
||||
elseif not E then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
local records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row], b[row], c[row], d[row]
|
||||
end
|
||||
else
|
||||
local queryOutput = {}
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
local records = archetype.records
|
||||
|
||||
if 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
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
if not F then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row]
|
||||
elseif not G then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row], f[row]
|
||||
elseif not H then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row], f[row], g[row]
|
||||
elseif not I then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row], f[row], g[row], h[row]
|
||||
end
|
||||
|
||||
local records = archetype.records
|
||||
for j, id in ids do
|
||||
queryOutput[j] = columns[records[id].column][row]
|
||||
end
|
||||
|
||||
return entityId, unpack(queryOutput)
|
||||
end
|
||||
end
|
||||
|
||||
local function cached_query_iter()
|
||||
lastArchetype = 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
|
@ -1673,175 +1843,6 @@ local function query_cached(query: QueryInner)
|
|||
return world_query_iter_next
|
||||
end
|
||||
|
||||
if not B then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row]
|
||||
end
|
||||
elseif not C then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row], b[row]
|
||||
end
|
||||
elseif not D then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row], b[row], c[row]
|
||||
end
|
||||
elseif not E then
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
records = archetype.records
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
return entityId, a[row], b[row], c[row], d[row]
|
||||
end
|
||||
else
|
||||
local queryOutput = {}
|
||||
function world_query_iter_next(): any
|
||||
local entityId = entities[i]
|
||||
while entityId == nil do
|
||||
lastArchetype += 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
if not archetype then
|
||||
return nil
|
||||
end
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
entityId = entities[i]
|
||||
columns = archetype.columns
|
||||
records = archetype.records
|
||||
|
||||
if 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
|
||||
end
|
||||
|
||||
local row = i
|
||||
i -= 1
|
||||
|
||||
if not F then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row]
|
||||
elseif not G then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row], f[row]
|
||||
elseif not H then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row], f[row], g[row]
|
||||
elseif not I then
|
||||
return entityId, a[row], b[row], c[row], d[row], e[row], f[row], g[row], h[row]
|
||||
end
|
||||
|
||||
for j, id in ids do
|
||||
queryOutput[j] = columns[records[id].column][row]
|
||||
end
|
||||
|
||||
return entityId, unpack(queryOutput)
|
||||
end
|
||||
end
|
||||
|
||||
local cached_query = query :: any
|
||||
cached_query.archetypes = query_archetypes
|
||||
cached_query.__iter = cached_query_iter
|
||||
|
@ -2141,26 +2142,16 @@ function World.new()
|
|||
return self
|
||||
end
|
||||
|
||||
type Id<T = unknown> =
|
||||
| (number & { __jecs_pair_value: T })
|
||||
| (number & { __T: T })
|
||||
export type Id<T = unknown> = Entity<T>
|
||||
|
||||
export type Pair<P = Entity, O = Entity> = number & {
|
||||
__jecs_pair_value: ecs_id_t<ecs_pair_t<P, O>>
|
||||
}
|
||||
|
||||
type function ecs_id_t(entity)
|
||||
local ty = entity:components()[2]
|
||||
local __T = ty:readproperty(types.singleton("__T"))
|
||||
if not __T then
|
||||
return ty:readproperty(types.singleton("__jecs_pair_value"))
|
||||
end
|
||||
return __T
|
||||
type function ecs_entity_t(entity)
|
||||
return entity:components()[2]:readproperty(types.singleton("__T"))
|
||||
end
|
||||
|
||||
type function ecs_pair_t(first, second)
|
||||
local ty = first:components()[2]
|
||||
if ty:readproperty(types.singleton("__T")):is("nil") then
|
||||
type function Pair(first, second)
|
||||
local thing = first:components()[2]
|
||||
|
||||
if thing:readproperty(types.singleton("__T")):is("nil") then
|
||||
return second
|
||||
else
|
||||
return first
|
||||
|
@ -2169,7 +2160,7 @@ end
|
|||
|
||||
type Item<T...> = (self: Query<T...>) -> (Entity, T...)
|
||||
|
||||
export type Entity<T = nil> = number & { __T: T }
|
||||
export type Entity<T = unknown> = number & { __T: T }
|
||||
|
||||
type Iter<T...> = (query: Query<T...>) -> () -> (Entity, T...)
|
||||
|
||||
|
@ -2197,6 +2188,14 @@ type Observer = {
|
|||
query: QueryInner,
|
||||
}
|
||||
|
||||
type function ecs_partial_t(ty)
|
||||
local output = types.newtable()
|
||||
for k, v in ty:properties() do
|
||||
output:setproperty(k, types.unionof(v.write, types.singleton(nil)))
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
export type World = {
|
||||
archetypeIndex: { [string]: Archetype },
|
||||
archetypes: Archetypes,
|
||||
|
@ -2251,14 +2250,14 @@ export type World = {
|
|||
children: (self: World, id: Id) -> () -> Entity,
|
||||
|
||||
--- Searches the world for entities that match a given query
|
||||
query: (<A>(World, A) -> Query<ecs_id_t<A>>)
|
||||
& (<A, B>(World, A, B) -> Query<ecs_id_t<A>, ecs_id_t<B>>)
|
||||
& (<A, B, C>(World, A, B, C) -> Query<ecs_id_t<A>, ecs_id_t<B>, ecs_id_t<C>>)
|
||||
& (<A, B, C, D>(World, A, B, C, D) -> Query<ecs_id_t<A>, ecs_id_t<B>, ecs_id_t<C>, ecs_id_t<D>>)
|
||||
& (<A, B, C, D, E>(World, A, B, C, D, E) -> Query<ecs_id_t<A>, ecs_id_t<B>, ecs_id_t<C>, ecs_id_t<D>, ecs_id_t<E>>)
|
||||
& (<A, B, C, D, E, F>(World, A, B, C, D, E, F) -> Query<ecs_id_t<A>, ecs_id_t<B>, ecs_id_t<C>, ecs_id_t<D>, ecs_id_t<E>, ecs_id_t<F>>)
|
||||
& (<A, B, C, D, E, F, G>(World, A, B, C, D, E, F, G) -> Query<ecs_id_t<A>, ecs_id_t<B>, ecs_id_t<C>, ecs_id_t<D>, ecs_id_t<E>, ecs_id_t<F>, ecs_id_t<G>>)
|
||||
& (<A, B, C, D, E, F, G, H>(World, A, B, C, D, E, F, G, H) -> Query<ecs_id_t<A>, ecs_id_t<B>, ecs_id_t<C>, ecs_id_t<D>, ecs_id_t<E>, ecs_id_t<F>, ecs_id_t<G>, ecs_id_t<H>>)
|
||||
query: (<A>(World, A) -> Query<ecs_entity_t<A>>)
|
||||
& (<A, B>(World, A, B) -> Query<ecs_entity_t<A>, ecs_entity_t<B>>)
|
||||
& (<A, B, C>(World, A, B, C) -> Query<ecs_entity_t<A>, ecs_entity_t<B>, ecs_entity_t<C>>)
|
||||
& (<A, B, C, D>(World, A, B, C, D) -> Query<ecs_entity_t<A>, ecs_entity_t<B>, ecs_entity_t<C>, ecs_entity_t<D>>)
|
||||
& (<A, B, C, D, E>(World, A, B, C, D, E) -> Query<ecs_entity_t<A>, ecs_entity_t<B>, ecs_entity_t<C>, ecs_entity_t<D>, ecs_entity_t<E>>)
|
||||
& (<A, B, C, D, E, F>(World, A, B, C, D, E, F) -> Query<ecs_entity_t<A>, ecs_entity_t<B>, ecs_entity_t<C>, ecs_entity_t<D>, ecs_entity_t<E>, ecs_entity_t<F>>)
|
||||
& (<A, B, C, D, E, F, G>(World, A, B, C, D, E, F, G) -> Query<ecs_entity_t<A>, ecs_entity_t<B>, ecs_entity_t<C>, ecs_entity_t<D>, ecs_entity_t<E>, ecs_entity_t<F>, ecs_entity_t<G>>)
|
||||
& (<A, B, C, D, E, F, G, H>(World, A, B, C, D, E, F, G, H) -> Query<ecs_entity_t<A>, ecs_entity_t<B>, ecs_entity_t<C>, ecs_entity_t<D>, ecs_entity_t<E>, ecs_entity_t<F>, ecs_entity_t<G>, ecs_entity_t<H>>)
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ukendio/jecs"
|
||||
version = "0.5.1"
|
||||
version = "0.5.0"
|
||||
registry = "https://github.com/UpliftGames/wally-index"
|
||||
realm = "shared"
|
||||
license = "MIT"
|
||||
|
|
Loading…
Reference in a new issue