mirror of
https://github.com/Ukendio/jecs.git
synced 2025-08-04 19:29:18 +00:00
Compare commits
8 commits
e0620b65e4
...
3c9d09d855
Author | SHA1 | Date | |
---|---|---|---|
|
3c9d09d855 | ||
|
6881a27563 | ||
|
4696fc980a | ||
|
4bb7e64edd | ||
|
a19a422c17 | ||
|
98ffef93df | ||
|
45c12a842b | ||
|
fd6883cfed |
7 changed files with 170 additions and 126 deletions
|
@ -1,16 +1,16 @@
|
|||
local jecs = require("@jecs")
|
||||
|
||||
type Observer<T...> = {
|
||||
type Observer = {
|
||||
callback: (jecs.Entity) -> (),
|
||||
query: jecs.Query<T...>,
|
||||
query: jecs.Query<...jecs.Id>,
|
||||
}
|
||||
|
||||
export type PatchedWorld = jecs.World & {
|
||||
added: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: any) -> ()) -> (),
|
||||
removed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
|
||||
changed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
|
||||
observer: (PatchedWorld, Observer<any>) -> (),
|
||||
monitor: (PatchedWorld, Observer<any>) -> (),
|
||||
added: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
|
||||
removed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id) -> ()) -> () -> (),
|
||||
changed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
|
||||
observer: (PatchedWorld, Observer) -> (),
|
||||
monitor: (PatchedWorld, Observer) -> (),
|
||||
}
|
||||
|
||||
local function observers_new(world, description)
|
||||
|
@ -70,6 +70,7 @@ local function join(world, component)
|
|||
sparse_array[entity] = nil
|
||||
dense_array[max_id] = nil
|
||||
values[max_id] = nil
|
||||
max_id -= 1
|
||||
end)
|
||||
|
||||
world:changed(component, function(entity, id, value)
|
||||
|
@ -89,62 +90,6 @@ local function join(world, component)
|
|||
end
|
||||
end
|
||||
|
||||
local function query_changed(world, component)
|
||||
assert(jecs.IS_PAIR(component) == false)
|
||||
local callerid = debug.info(2, "sl")
|
||||
|
||||
local tracker = world.trackers[callerid]
|
||||
if not tracker then
|
||||
local records = {}
|
||||
local connections = {}
|
||||
tracker = {
|
||||
records = records,
|
||||
connections = connections
|
||||
}
|
||||
world.trackers[callerid] = tracker
|
||||
|
||||
table.insert(connections, world:added(component, function(entity, id, v)
|
||||
tracker[entity] = {
|
||||
new = v
|
||||
}
|
||||
end))
|
||||
table.insert(connections, world:changed(component, function(entity, id, v)
|
||||
local record = tracker[entity]
|
||||
record.old = record.new
|
||||
record.new = v
|
||||
end))
|
||||
|
||||
table.insert(connections, world:removed(component, function(entity, id)
|
||||
local record = tracker[entity]
|
||||
record.old = record.new
|
||||
record.new = nil
|
||||
end))
|
||||
end
|
||||
|
||||
local entity = nil
|
||||
local record = nil
|
||||
return function()
|
||||
entity, record = next(tracker, entity)
|
||||
if entity == nil then
|
||||
return
|
||||
end
|
||||
return entity, record
|
||||
end
|
||||
end
|
||||
|
||||
local function spy_on_world_delete(world)
|
||||
local world_delete = world.delete
|
||||
world.delete = function(world, entity)
|
||||
world_delete(world, entity)
|
||||
for _, tracker in world.trackers do
|
||||
tracker.records[entity] = nil
|
||||
for _, connection in tracker.connections do
|
||||
connection()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function monitors_new(world, description)
|
||||
local query = description.query
|
||||
local callback = description.callback
|
||||
|
@ -192,18 +137,23 @@ local function monitors_new(world, description)
|
|||
end
|
||||
end
|
||||
|
||||
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld
|
||||
local function observers_add(world: jecs.World): PatchedWorld
|
||||
type Signal = { [jecs.Entity]: { (...any) -> () } }
|
||||
|
||||
local world_mut = world :: jecs.World & {[string]: any}
|
||||
|
||||
local signals = {
|
||||
added = {} :: Signal,
|
||||
emplaced = {} :: Signal,
|
||||
removed = {} :: Signal
|
||||
}
|
||||
|
||||
world.added = function(_, component, fn)
|
||||
world_mut.added = function<T>(
|
||||
_: jecs.World,
|
||||
component: jecs.Id<T>,
|
||||
fn: (e: jecs.Entity, id: jecs.Id, value: T) -> ()
|
||||
)
|
||||
local listeners = signals.added[component]
|
||||
local component_index = world.component_index :: jecs.ComponentIndex
|
||||
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
|
||||
if not listeners then
|
||||
listeners = {}
|
||||
signals.added[component] = listeners
|
||||
|
@ -213,8 +163,17 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
listener(entity, id, value)
|
||||
end
|
||||
end
|
||||
local idr = world.component_index[component]
|
||||
if idr then
|
||||
local idr_hook_existing = idr.hooks.on_add
|
||||
if idr_hook_existing then
|
||||
table.insert(listeners, idr_hook_existing)
|
||||
end
|
||||
idr.hooks.on_add = on_add :: any
|
||||
else
|
||||
world:set(component, jecs.OnAdd, on_add)
|
||||
end
|
||||
end
|
||||
table.insert(listeners, fn)
|
||||
return function()
|
||||
local n = #listeners
|
||||
|
@ -224,10 +183,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
end
|
||||
end
|
||||
|
||||
world.changed = function(_, component, fn)
|
||||
world_mut.changed = function<T>(
|
||||
_: jecs.World,
|
||||
component: jecs.Id<T>,
|
||||
fn: (e: jecs.Entity, id: jecs.Id, value: T) -> ()
|
||||
)
|
||||
local listeners = signals.emplaced[component]
|
||||
local component_index = world.component_index :: jecs.ComponentIndex
|
||||
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
|
||||
if not listeners then
|
||||
listeners = {}
|
||||
signals.emplaced[component] = listeners
|
||||
|
@ -236,8 +197,17 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
listener(entity, id, value)
|
||||
end
|
||||
end
|
||||
local idr = world.component_index[component]
|
||||
if idr then
|
||||
local idr_hook_existing = idr.hooks.on_change
|
||||
if idr_hook_existing then
|
||||
table.insert(listeners, idr_hook_existing)
|
||||
end
|
||||
idr.hooks.on_change = on_change :: any
|
||||
else
|
||||
world:set(component, jecs.OnChange, on_change)
|
||||
end
|
||||
end
|
||||
table.insert(listeners, fn)
|
||||
return function()
|
||||
local n = #listeners
|
||||
|
@ -247,10 +217,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
end
|
||||
end
|
||||
|
||||
world.removed = function(_, component, fn)
|
||||
world_mut.removed = function<T>(
|
||||
_: jecs.World,
|
||||
component: jecs.Id<T>,
|
||||
fn: (e: jecs.Entity, id: jecs.Id) -> ()
|
||||
)
|
||||
local listeners = signals.removed[component]
|
||||
local component_index = world.component_index :: jecs.ComponentIndex
|
||||
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
|
||||
if not listeners then
|
||||
listeners = {}
|
||||
signals.removed[component] = listeners
|
||||
|
@ -259,8 +231,17 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
listener(entity, id, value)
|
||||
end
|
||||
end
|
||||
local idr = world.component_index[component]
|
||||
if idr then
|
||||
local idr_hook_existing = idr.hooks.on_remove
|
||||
if idr_hook_existing then
|
||||
table.insert(listeners, idr_hook_existing)
|
||||
end
|
||||
idr.hooks.on_remove = on_remove :: any
|
||||
else
|
||||
world:set(component, jecs.OnRemove, on_remove)
|
||||
end
|
||||
end
|
||||
table.insert(listeners, fn)
|
||||
return function()
|
||||
local n = #listeners
|
||||
|
@ -270,15 +251,15 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
|
|||
end
|
||||
end
|
||||
|
||||
world.signals = signals
|
||||
world_mut.signals = signals
|
||||
|
||||
world.observer = observers_new
|
||||
world_mut.observer = observers_new
|
||||
|
||||
world.monitor = monitors_new
|
||||
world_mut.monitor = monitors_new
|
||||
|
||||
world.trackers = {}
|
||||
world_mut.trackers = {}
|
||||
|
||||
return world :: PatchedWorld
|
||||
return world_mut :: PatchedWorld
|
||||
end
|
||||
|
||||
return observers_add
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local collect = require(ReplicatedStorage.collect)
|
||||
local types = require(ReplicatedStorage.types)
|
||||
local ct = require(ReplicatedStorage.components)
|
||||
local collect = require("../../ReplicatedStorage/collect")
|
||||
local types = require("../../ReplicatedStorage/types")
|
||||
local ct = require("../../ReplicatedStorage/components")
|
||||
local Players = game:GetService("Players")
|
||||
|
||||
local player_added = collect(Players.PlayerAdded)
|
||||
|
@ -13,7 +12,8 @@ return function(world: types.World, dt: number)
|
|||
|
||||
for entity, player in world:query(ct.Player):without(ct.Renderable) do
|
||||
local character = player.Character
|
||||
if character and character.Parent ~= nil then
|
||||
if character then
|
||||
if not character.Parent then
|
||||
world:set(entity, ct.Renderable, character)
|
||||
end
|
||||
end
|
||||
|
|
69
jecs.d.ts
vendored
69
jecs.d.ts
vendored
|
@ -108,6 +108,13 @@ export class World {
|
|||
*/
|
||||
constructor();
|
||||
|
||||
/**
|
||||
* Enforces a check for entities to be created within a desired range.
|
||||
* @param range_begin The starting point
|
||||
* @param range_end The end point (optional)
|
||||
*/
|
||||
range(range_begin: number, range_end?: number): void;
|
||||
|
||||
/**
|
||||
* Creates a new entity.
|
||||
* @returns An entity (Tag) with no data.
|
||||
|
@ -130,19 +137,6 @@ export class World {
|
|||
*/
|
||||
target(entity: Entity, relation: Entity, index?: number): Entity | undefined;
|
||||
|
||||
/**
|
||||
* Cleans up the world by removing empty archetypes and rebuilding the archetype collections.
|
||||
* This helps maintain memory efficiency by removing unused archetype definitions.
|
||||
*/
|
||||
cleanup(): void;
|
||||
|
||||
/**
|
||||
* Clears all components and relationships from the given entity, but
|
||||
* does not delete the entity from the world.
|
||||
* @param entity The entity to clear.
|
||||
*/
|
||||
clear(entity: Entity): void;
|
||||
|
||||
/**
|
||||
* Deletes an entity (and its components/relationships) from the world entirely.
|
||||
* @param entity The entity to delete.
|
||||
|
@ -164,6 +158,19 @@ export class World {
|
|||
*/
|
||||
set<E extends Id<unknown>>(entity: Entity, component: E, value: InferComponent<E>): void;
|
||||
|
||||
/**
|
||||
* Cleans up the world by removing empty archetypes and rebuilding the archetype collections.
|
||||
* This helps maintain memory efficiency by removing unused archetype definitions.
|
||||
*/
|
||||
cleanup(): void;
|
||||
|
||||
/**
|
||||
* Clears all components and relationships from the given entity, but
|
||||
* does not delete the entity from the world.
|
||||
* @param entity The entity to clear.
|
||||
*/
|
||||
clear(entity: Entity): void;
|
||||
|
||||
/**
|
||||
* Removes a component from the given entity.
|
||||
* @param entity The target entity.
|
||||
|
@ -191,12 +198,6 @@ export class World {
|
|||
*/
|
||||
has(entity: Entity, ...components: Id[]): boolean;
|
||||
|
||||
/**
|
||||
* Checks if an entity exists in the world.
|
||||
* @param entity The entity to verify.
|
||||
*/
|
||||
contains(entity: Entity): boolean;
|
||||
|
||||
/**
|
||||
* Gets the parent (the target of a `ChildOf` relationship) for an entity,
|
||||
* if such a relationship exists.
|
||||
|
@ -205,11 +206,17 @@ export class World {
|
|||
parent(entity: Entity): Entity | undefined;
|
||||
|
||||
/**
|
||||
* Searches the world for entities that match specified components.
|
||||
* @param components The list of components to query.
|
||||
* @returns A Query object to iterate over results.
|
||||
* Checks if an entity exists in the world.
|
||||
* @param entity The entity to verify.
|
||||
*/
|
||||
query<T extends Id[]>(...components: T): Query<InferComponents<T>>;
|
||||
contains(entity: Entity): boolean;
|
||||
|
||||
/**
|
||||
* Checks if an entity with the given ID is currently alive, ignoring its generation.
|
||||
* @param entity The entity to verify.
|
||||
* @returns boolean true if any entity with the given ID exists (ignoring generation), false otherwise
|
||||
*/
|
||||
exists(entity: Entity): boolean;
|
||||
|
||||
/**
|
||||
* Returns an iterator that yields all entities that have the specified component or relationship.
|
||||
|
@ -225,8 +232,24 @@ export class World {
|
|||
* @returns An iterator function that yields child entities
|
||||
*/
|
||||
children(parent: Entity): IterableFunction<Entity>;
|
||||
|
||||
/**
|
||||
* Searches the world for entities that match specified components.
|
||||
* @param components The list of components to query.
|
||||
* @returns A Query object to iterate over results.
|
||||
*/
|
||||
query<T extends Id[]>(...components: T): Query<InferComponents<T>>;
|
||||
}
|
||||
|
||||
export function component<T>(): Entity<T>;
|
||||
|
||||
export function tag<T>(): Entity<T>;
|
||||
|
||||
// note: original types had id: Entity, id: Id<T>, which does not work with TS.
|
||||
export function meta<T>(e: Entity, id: Id<T>, value?: T): Entity<T>
|
||||
|
||||
export function is_tag<T>(world: World, id: Id<T>): boolean;
|
||||
|
||||
/**
|
||||
* Creates a composite key (pair)
|
||||
* @param pred The first entity (predicate)
|
||||
|
|
10
jecs.luau
10
jecs.luau
|
@ -780,7 +780,11 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
|
|||
|
||||
local any = dense_array[dense]
|
||||
if dense <= alive_count then
|
||||
return any
|
||||
if any ~= entity then
|
||||
error("Entity ID is already in use with a different generation")
|
||||
else
|
||||
return entity
|
||||
end
|
||||
end
|
||||
|
||||
local e_swap = dense_array[dense]
|
||||
|
@ -790,9 +794,9 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
|
|||
r_swap.dense = dense
|
||||
r.dense = alive_count
|
||||
dense_array[dense] = e_swap
|
||||
dense_array[alive_count] = any
|
||||
dense_array[alive_count] = entity
|
||||
|
||||
return any
|
||||
return entity
|
||||
else
|
||||
for i = max_id + 1, index do
|
||||
sparse_array[i] = { dense = i } :: ecs_record_t
|
||||
|
|
|
@ -8,6 +8,23 @@ local observers_add = require("@addons/observers")
|
|||
TEST("addons/observers", function()
|
||||
local world = observers_add(jecs.world())
|
||||
|
||||
do CASE "Should not override hook"
|
||||
local A = world:component()
|
||||
|
||||
local count = 0
|
||||
local function counter()
|
||||
count += 1
|
||||
end
|
||||
|
||||
world:set(A, jecs.OnAdd, counter)
|
||||
world:set(world:entity(), A, true)
|
||||
CHECK(count == 1)
|
||||
world:added(A, counter)
|
||||
world:set(world:entity(), A, true)
|
||||
|
||||
CHECK(count == 3)
|
||||
end
|
||||
|
||||
do CASE "Ensure ordering between signals and observers"
|
||||
local A = world:component()
|
||||
local B = world:component()
|
||||
|
@ -24,17 +41,12 @@ TEST("addons/observers", function()
|
|||
world:added(A, counter)
|
||||
world:added(A, counter)
|
||||
|
||||
world:removed(A, counter)
|
||||
|
||||
local e = world:entity()
|
||||
world:add(e, A)
|
||||
CHECK(count == 2)
|
||||
|
||||
world:add(e, B)
|
||||
CHECK(count == 3)
|
||||
|
||||
world:remove(e, A)
|
||||
CHECK(count == 4)
|
||||
end
|
||||
|
||||
do CASE "Rematch entities in observers"
|
||||
|
|
|
@ -25,6 +25,30 @@ local entity_visualiser = require("@tools/entity_visualiser")
|
|||
local lifetime_tracker_add = require("@tools/lifetime_tracker")
|
||||
local dwi = entity_visualiser.stringify
|
||||
|
||||
TEST("repro", function()
|
||||
local Model = jecs.component()
|
||||
local Relation = jecs.component()
|
||||
local Relation2 = jecs.component()
|
||||
|
||||
local world = jecs.world()
|
||||
|
||||
local e1 = world:entity()
|
||||
world:set(e1, Model, 2)
|
||||
|
||||
local e2 = world:entity()
|
||||
world:set(e2, Model, 2)
|
||||
world:set(e2, jecs.pair(Relation, e1), 5)
|
||||
|
||||
local e3 = world:entity()
|
||||
world:set(e3, Model, 2)
|
||||
world:set(e3, jecs.pair(Relation, e1), 5)
|
||||
|
||||
world:delete(e1)
|
||||
|
||||
for _ in world:query(Model) do end
|
||||
jecs.ECS_META_RESET()
|
||||
end)
|
||||
|
||||
TEST("world:add()", function()
|
||||
do CASE "idempotent"
|
||||
local world = jecs.world()
|
||||
|
@ -648,7 +672,7 @@ TEST("world:range()", function()
|
|||
local e2v1 = world:entity(399)
|
||||
CHECK(world:contains(e2v1))
|
||||
CHECK(ECS_ID(e2v1) == 399)
|
||||
CHECK(ECS_GENERATION(e2v1) == 1)
|
||||
CHECK(ECS_GENERATION(e2v1) == 0)
|
||||
end
|
||||
|
||||
do CASE "over range start"
|
||||
|
@ -661,12 +685,12 @@ TEST("world:range()", function()
|
|||
local e2v1 = world:entity(405)
|
||||
CHECK(world:contains(e2v1))
|
||||
CHECK(ECS_ID(e2v1) == 405)
|
||||
CHECK(ECS_GENERATION(e2v1) == 1)
|
||||
CHECK(ECS_GENERATION(e2v1) == 0)
|
||||
|
||||
do
|
||||
local _e2v1 = world:entity(405)
|
||||
CHECK(_e2v1 == e2v1)
|
||||
end
|
||||
world:delete(e2v1)
|
||||
local e2v2 = world:entity(e2v1)
|
||||
CHECK(ECS_ID(e2v2) == 405)
|
||||
CHECK(ECS_GENERATION(e2v2) == 0)
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ukendio/jecs"
|
||||
version = "0.5.5"
|
||||
version = "0.6.0-rc.1"
|
||||
registry = "https://github.com/UpliftGames/wally-index"
|
||||
realm = "shared"
|
||||
license = "MIT"
|
||||
|
|
Loading…
Reference in a new issue