Skip to content

Commit

Permalink
Specialized method to find entities with a single ID (#165)
Browse files Browse the repository at this point in the history
* Initial commit

* Export query functions to make Michael happy

* Adding trailing commas
  • Loading branch information
Ukendio authored Dec 20, 2024
1 parent d9be40d commit 4841915
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 606 deletions.
51 changes: 48 additions & 3 deletions jecs.luau
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type Archetype = {
records: { ArchetypeRecord },
} & GraphNode

type Record = {
export type Record = {
archetype: Archetype,
row: number,
dense: i24,
Expand Down Expand Up @@ -1548,6 +1548,39 @@ local function world_query(world: World, ...)
return q
end

local function world_each(world: World, id): () -> ()
local idr = world.componentIndex[id]
if not idr then
return NOOP
end

local idr_cache = idr.cache
local archetypes = world.archetypes
local archetype_id = next(idr_cache, nil) :: number
local archetype = archetypes[archetype_id]
if not archetype then
return NOOP
end

local last = 0
return function(): any
last += 1
local entity = archetype.entities[last]
while not entity do
archetype_id = next(idr_cache, archetype_id)
if not archetype_id then
return
end
archetype = archetypes[archetype_id]
end
return entity
end
end

local function world_children(world, parent)
return world_each(world, ECS_PAIR(EcsChildOf, parent))
end

local World = {}
World.__index = World

Expand All @@ -1565,6 +1598,8 @@ World.target = world_target
World.parent = world_parent
World.contains = world_contains
World.cleanup = world_cleanup
World.each = world_each
World.children = world_children

if _G.__JECS_DEBUG then
-- taken from https://github.com/centau/ecr/blob/main/src/ecr.luau
Expand Down Expand Up @@ -1725,7 +1760,7 @@ function World.new()
return self
end

export type Id<T = nil> = Entity<T> | Pair<Entity<T>, Entity<unknown>>
export type Id<T = unknown> = Entity<T> | Pair<Entity<T>, Entity<unknown>>

export type Pair<First, Second> = number & {
__relation: First,
Expand All @@ -1745,7 +1780,7 @@ export type Pair<First, Second> = number & {

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...)

Expand Down Expand Up @@ -1805,6 +1840,10 @@ export type World = {
--- Checks if the world contains the given entity
contains: (self: World, entity: Entity) -> boolean,

each: (self: World, id: Id) -> () -> Entity,

children: (self: World, id: Id) -> () -> Entity,

--- Searches the world for entities that match a given query
query: (<A>(self: World, Id<A>) -> Query<A>)
& (<A, B>(self: World, Id<A>, Id<B>) -> Query<A, B>)
Expand Down Expand Up @@ -1895,4 +1934,10 @@ return {
entity_index_try_get_fast = entity_index_try_get_fast,
entity_index_is_alive = entity_index_is_alive,
entity_index_new_id = entity_index_new_id,

query_iter = query_iter,
query_iter_init = query_iter_init,
query_with = query_with,
query_without = query_without,
query_archetypes = query_archetypes,
}
2 changes: 1 addition & 1 deletion test/testkit.luau
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ local function FINISH(): boolean
return success, table.clear(tests)
end

local function SKIP(name: string)
local function SKIP()
skip = true
end

Expand Down
Loading

0 comments on commit 4841915

Please sign in to comment.