mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 09:30:03 +00:00
Compare commits
No commits in common. "eaafd272802de1d8b59a908b11a49eb76e2989c9" and "7c025a37827cd8a24306ae0d62d15ed38f969d17" have entirely different histories.
eaafd27280
...
7c025a3782
3 changed files with 96 additions and 57 deletions
20
.github/workflows/styling.yaml
vendored
Normal file
20
.github/workflows/styling.yaml
vendored
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
name: Styling
|
||||||
|
|
||||||
|
on: [push, pull_request, workflow_dispatch]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
run:
|
||||||
|
name: Run Stylua
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout Project
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Run Stylua
|
||||||
|
uses: JohnnyMorganz/stylua-action@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
version: latest # NOTE: we recommend pinning to a specific version in case of formatting changes
|
||||||
|
# CLI arguments
|
||||||
|
args: --check jecs.luau
|
112
jecs.luau
112
jecs.luau
|
@ -257,7 +257,7 @@ local function archetype_move(entity_index: EntityIndex, to: Archetype, dst_row:
|
||||||
local src_entities = from.entities
|
local src_entities = from.entities
|
||||||
|
|
||||||
local last = #src_entities
|
local last = #src_entities
|
||||||
local id_types = from.types
|
local types = from.types
|
||||||
local records = to.records
|
local records = to.records
|
||||||
|
|
||||||
for i, column in src_columns do
|
for i, column in src_columns do
|
||||||
|
@ -266,13 +266,12 @@ local function archetype_move(entity_index: EntityIndex, to: Archetype, dst_row:
|
||||||
end
|
end
|
||||||
-- Retrieves the new column index from the source archetype's record from each component
|
-- Retrieves the new column index from the source archetype's record from each component
|
||||||
-- We have to do this because the columns are tightly packed and indexes may not correspond to each other.
|
-- We have to do this because the columns are tightly packed and indexes may not correspond to each other.
|
||||||
local tr = records[id_types[i]]
|
local tr = records[types[i]]
|
||||||
|
|
||||||
-- Sometimes target column may not exist, e.g. when you remove a component.
|
-- Sometimes target column may not exist, e.g. when you remove a component.
|
||||||
if tr then
|
if tr then
|
||||||
dst_columns[tr.column][dst_row] = column[src_row]
|
dst_columns[tr.column][dst_row] = column[src_row]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If the entity is the last row in the archetype then swapping it would be meaningless.
|
-- If the entity is the last row in the archetype then swapping it would be meaningless.
|
||||||
if src_row ~= last then
|
if src_row ~= last then
|
||||||
-- Swap rempves columns to ensure there are no holes in the archetype.
|
-- Swap rempves columns to ensure there are no holes in the archetype.
|
||||||
|
@ -476,8 +475,7 @@ local function id_record_ensure(world: World, id: number): IdRecord
|
||||||
if not idr then
|
if not idr then
|
||||||
local flags = ECS_ID_MASK
|
local flags = ECS_ID_MASK
|
||||||
local relation = id
|
local relation = id
|
||||||
local is_pair = ECS_IS_PAIR(id)
|
if ECS_IS_PAIR(id) then
|
||||||
if is_pair then
|
|
||||||
relation = ecs_pair_first(world, id)
|
relation = ecs_pair_first(world, id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -494,10 +492,6 @@ local function id_record_ensure(world: World, id: number): IdRecord
|
||||||
|
|
||||||
local is_tag = not world_has_one_inline(world, relation, EcsComponent)
|
local is_tag = not world_has_one_inline(world, relation, EcsComponent)
|
||||||
|
|
||||||
if is_tag and is_pair then
|
|
||||||
is_tag = not world_has_one_inline(world, ecs_pair_second(world, id), EcsComponent)
|
|
||||||
end
|
|
||||||
|
|
||||||
flags = bit32.bor(
|
flags = bit32.bor(
|
||||||
flags,
|
flags,
|
||||||
if on_add then ECS_ID_HAS_ON_ADD else 0,
|
if on_add then ECS_ID_HAS_ON_ADD else 0,
|
||||||
|
@ -541,15 +535,15 @@ local function archetype_append_to_records(
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?): Archetype
|
local function archetype_create(world: World, types: { i24 }, ty, prev: i53?): Archetype
|
||||||
local archetype_id = (world.nextArchetypeId :: number) + 1
|
local archetype_id = (world.nextArchetypeId :: number) + 1
|
||||||
world.nextArchetypeId = archetype_id
|
world.nextArchetypeId = archetype_id
|
||||||
|
|
||||||
local length = #id_types
|
local length = #types
|
||||||
local columns = (table.create(length) :: any) :: { Column }
|
local columns = (table.create(length) :: any) :: { Column }
|
||||||
|
|
||||||
local records: { ArchetypeRecord } = {}
|
local records: { ArchetypeRecord } = {}
|
||||||
for i, componentId in id_types do
|
for i, componentId in types do
|
||||||
local idr = id_record_ensure(world, componentId)
|
local idr = id_record_ensure(world, componentId)
|
||||||
archetype_append_to_records(idr, archetype_id, records, componentId, i)
|
archetype_append_to_records(idr, archetype_id, records, componentId, i)
|
||||||
|
|
||||||
|
@ -578,7 +572,7 @@ local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?)
|
||||||
id = archetype_id,
|
id = archetype_id,
|
||||||
records = records,
|
records = records,
|
||||||
type = ty,
|
type = ty,
|
||||||
types = id_types,
|
types = types,
|
||||||
|
|
||||||
add = {},
|
add = {},
|
||||||
remove = {},
|
remove = {},
|
||||||
|
@ -599,22 +593,22 @@ local function world_parent(world: World, entity: i53)
|
||||||
return world_target(world, entity, EcsChildOf, 0)
|
return world_target(world, entity, EcsChildOf, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function archetype_ensure(world: World, id_types): Archetype
|
local function archetype_ensure(world: World, types): Archetype
|
||||||
if #id_types < 1 then
|
if #types < 1 then
|
||||||
return world.ROOT_ARCHETYPE
|
return world.ROOT_ARCHETYPE
|
||||||
end
|
end
|
||||||
|
|
||||||
local ty = hash(id_types)
|
local ty = hash(types)
|
||||||
local archetype = world.archetypeIndex[ty]
|
local archetype = world.archetypeIndex[ty]
|
||||||
if archetype then
|
if archetype then
|
||||||
return archetype
|
return archetype
|
||||||
end
|
end
|
||||||
|
|
||||||
return archetype_create(world, id_types, ty)
|
return archetype_create(world, types, ty)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function find_insert(id_types: { i53 }, toAdd: i53): number
|
local function find_insert(types: { i53 }, toAdd: i53): number
|
||||||
for i, id in id_types do
|
for i, id in types do
|
||||||
if id == toAdd then
|
if id == toAdd then
|
||||||
return -1
|
return -1
|
||||||
end
|
end
|
||||||
|
@ -622,7 +616,7 @@ local function find_insert(id_types: { i53 }, toAdd: i53): number
|
||||||
return i
|
return i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return #id_types + 1
|
return #types + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local function find_archetype_with(world: World, node: Archetype, id: i53): Archetype
|
local function find_archetype_with(world: World, node: Archetype, id: i53): Archetype
|
||||||
|
@ -892,7 +886,7 @@ end
|
||||||
local function archetype_delete(world: World, archetype: Archetype, row: number, destruct: boolean?)
|
local function archetype_delete(world: World, archetype: Archetype, row: number, destruct: boolean?)
|
||||||
local entityIndex = world.entity_index
|
local entityIndex = world.entity_index
|
||||||
local columns = archetype.columns
|
local columns = archetype.columns
|
||||||
local id_types = archetype.types
|
local types = archetype.types
|
||||||
local entities = archetype.entities
|
local entities = archetype.entities
|
||||||
local column_count = #entities
|
local column_count = #entities
|
||||||
local last = #entities
|
local last = #entities
|
||||||
|
@ -911,7 +905,7 @@ local function archetype_delete(world: World, archetype: Archetype, row: number,
|
||||||
|
|
||||||
-- TODO: if last == 0 then deactivate table
|
-- TODO: if last == 0 then deactivate table
|
||||||
|
|
||||||
for _, id in id_types do
|
for _, id in types do
|
||||||
local on_remove: (entity: i53) -> () = world_get_one_inline(world, id, EcsOnRemove)
|
local on_remove: (entity: i53) -> () = world_get_one_inline(world, id, EcsOnRemove)
|
||||||
if on_remove then
|
if on_remove then
|
||||||
on_remove(delete)
|
on_remove(delete)
|
||||||
|
@ -919,9 +913,9 @@ local function archetype_delete(world: World, archetype: Archetype, row: number,
|
||||||
end
|
end
|
||||||
|
|
||||||
if row == last then
|
if row == last then
|
||||||
archetype_fast_delete_last(columns, column_count, id_types, delete)
|
archetype_fast_delete_last(columns, column_count, types, delete)
|
||||||
else
|
else
|
||||||
archetype_fast_delete(columns, column_count, row, id_types, delete)
|
archetype_fast_delete(columns, column_count, row, types, delete)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1775,21 +1769,23 @@ function World.new()
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
export type Id<T = unknown> = Entity<T>
|
export type Id<T = unknown> = Entity<T> | Pair<Entity<T>, Entity<unknown>>
|
||||||
|
|
||||||
type function ecs_entity_t(entity)
|
export type Pair<First, Second> = number & {
|
||||||
return entity:components()[2]:readproperty(types.singleton("__T"))
|
__relation: First,
|
||||||
end
|
}
|
||||||
|
|
||||||
export type function Pair(first, second)
|
-- type function _Pair(first, second)
|
||||||
local thing = first:components()[2]
|
-- local thing = first:components()[2]
|
||||||
|
|
||||||
if thing:readproperty(types.singleton("__T")):is("nil") then
|
-- if thing:readproperty(types.singleton("__T")):is("nil") then
|
||||||
return second
|
-- return second
|
||||||
else
|
-- else
|
||||||
return first
|
-- return first
|
||||||
end
|
-- end
|
||||||
end
|
-- end
|
||||||
|
|
||||||
|
-- type TestPair = _Pair<Entity<number>, Entity<Vector3>>
|
||||||
|
|
||||||
type Item<T...> = (self: Query<T...>) -> (Entity, T...)
|
type Item<T...> = (self: Query<T...>) -> (Entity, T...)
|
||||||
|
|
||||||
|
@ -1858,14 +1854,42 @@ export type World = {
|
||||||
children: (self: World, id: Id) -> () -> Entity,
|
children: (self: World, id: Id) -> () -> Entity,
|
||||||
|
|
||||||
--- Searches the world for entities that match a given query
|
--- Searches the world for entities that match a given query
|
||||||
query: (<A>(World, A) -> Query<ecs_entity_t<A>>)
|
query: (<A>(self: World, Id<A>) -> Query<A>)
|
||||||
& (<A, B>(World, A, B) -> Query<ecs_entity_t<A>, ecs_entity_t<B>>)
|
& (<A, B>(self: World, Id<A>, Id<B>) -> Query<A, B>)
|
||||||
& (<A, B, C>(World, A, B, C) -> Query<ecs_entity_t<A>, ecs_entity_t<B>, ecs_entity_t<C>>)
|
& (<A, B, C>(self: World, Id<A>, Id<B>, Id<C>) -> Query<A, B, 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>(self: World, Id<A>, Id<B>, Id<C>, Id<D>) -> Query<A, B, C, 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>(self: World, Id<A>, Id<B>, Id<C>, Id<D>, Id<E>) -> Query<A, B, C, D, 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>(
|
||||||
& (<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>>)
|
self: World,
|
||||||
& (<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>>)
|
Id<A>,
|
||||||
|
Id<B>,
|
||||||
|
Id<C>,
|
||||||
|
Id<D>,
|
||||||
|
Id<E>,
|
||||||
|
Id<F>
|
||||||
|
) -> Query<A, B, C, D, E, F>)
|
||||||
|
& (<A, B, C, D, E, F, G>(
|
||||||
|
self: World,
|
||||||
|
Id<A>,
|
||||||
|
Id<B>,
|
||||||
|
Id<C>,
|
||||||
|
Id<D>,
|
||||||
|
Id<E>,
|
||||||
|
Id<F>,
|
||||||
|
Id<G>
|
||||||
|
) -> Query<A, B, C, D, E, F, G>)
|
||||||
|
& (<A, B, C, D, E, F, G, H>(
|
||||||
|
self: World,
|
||||||
|
Id<A>,
|
||||||
|
Id<B>,
|
||||||
|
Id<C>,
|
||||||
|
Id<D>,
|
||||||
|
Id<E>,
|
||||||
|
Id<F>,
|
||||||
|
Id<G>,
|
||||||
|
Id<H>,
|
||||||
|
...Id<any>
|
||||||
|
) -> Query<A, B, C, D, E, F, G, H>),
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -165,7 +165,8 @@ TEST("world:entity()", function()
|
||||||
CHECK(ECS_GENERATION(e) == 1) -- 1
|
CHECK(ECS_GENERATION(e) == 1) -- 1
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE("pairs")
|
do
|
||||||
|
CASE("pairs")
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local _e = world:entity()
|
local _e = world:entity()
|
||||||
local e2 = world:entity()
|
local e2 = world:entity()
|
||||||
|
@ -174,17 +175,11 @@ TEST("world:entity()", function()
|
||||||
-- Incomplete pair, must have a bit flag that notes it is a pair
|
-- Incomplete pair, must have a bit flag that notes it is a pair
|
||||||
CHECK(IS_PAIR(world:entity()) == false)
|
CHECK(IS_PAIR(world:entity()) == false)
|
||||||
|
|
||||||
local p = pair(e2, e3)
|
local pair = pair(e2, e3)
|
||||||
CHECK(IS_PAIR(p) == true)
|
CHECK(IS_PAIR(pair) == true)
|
||||||
|
|
||||||
CHECK(ecs_pair_first(world, p) == e2)
|
CHECK(ecs_pair_first(world, pair) == e2)
|
||||||
CHECK(ecs_pair_second(world, p) == e3)
|
CHECK(ecs_pair_second(world, pair) == e3)
|
||||||
|
|
||||||
world:delete(e2)
|
|
||||||
local e2v2 = world:entity()
|
|
||||||
CHECK(IS_PAIR(e2v2) == false)
|
|
||||||
|
|
||||||
CHECK(IS_PAIR(pair(e2v2, e3)) == true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Recycling"
|
do CASE "Recycling"
|
||||||
|
@ -286,7 +281,7 @@ TEST("world:set()", function()
|
||||||
|
|
||||||
CHECK(world:get(e, pair(C1, C2)))
|
CHECK(world:get(e, pair(C1, C2)))
|
||||||
CHECK(world:get(e, pair(C1, T1)))
|
CHECK(world:get(e, pair(C1, T1)))
|
||||||
CHECK(world:get(e, pair(T1, C1)))
|
CHECK(not world:get(e, pair(T1, C1)))
|
||||||
CHECK(not world:get(e, pair(T1, T2)))
|
CHECK(not world:get(e, pair(T1, T2)))
|
||||||
|
|
||||||
local e2 = world:entity()
|
local e2 = world:entity()
|
||||||
|
@ -404,7 +399,7 @@ TEST("world:query()", function()
|
||||||
for id, a, b, c, d in world:query(pair(C1, C2), pair(C1, T1), pair(T1, C1), pair(T1, T2)) do
|
for id, a, b, c, d in world:query(pair(C1, C2), pair(C1, T1), pair(T1, C1), pair(T1, T2)) do
|
||||||
CHECK(a == true)
|
CHECK(a == true)
|
||||||
CHECK(b == true)
|
CHECK(b == true)
|
||||||
CHECK(c == true)
|
CHECK(c == nil)
|
||||||
CHECK(d == nil)
|
CHECK(d == nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue