Compare commits

..

1 commit

Author SHA1 Message Date
Clown
3d366d26ec
Merge 96bed9bd7e into 8194b7db24 2025-05-18 15:05:01 +01:00
4 changed files with 41 additions and 38 deletions

View file

@ -2,20 +2,15 @@ local jecs = require("@jecs")
type Observer = { type Observer = {
callback: (jecs.Entity) -> (), callback: (jecs.Entity) -> (),
query: jecs.Query<...any>, query: jecs.Query<...jecs.Id>,
}
type Monitor = {
callback: (jecs.Entity, jecs.Entity) -> (),
queyr: jecs.Query<any>
} }
export type PatchedWorld = jecs.World & { export type PatchedWorld = jecs.World & {
added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (), added: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (), removed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id) -> ()) -> () -> (),
changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (), changed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
observer: (PatchedWorld, Observer) -> (), observer: (PatchedWorld, Observer) -> (),
monitor: (PatchedWorld, Monitor) -> (), monitor: (PatchedWorld, Observer) -> (),
} }
local function observers_new(world, description) local function observers_new(world, description)
@ -50,6 +45,7 @@ local function observers_new(world, description)
end end
end end
local function join(world, component) local function join(world, component)
local sparse_array = {} local sparse_array = {}
local dense_array = {} local dense_array = {}
@ -167,16 +163,16 @@ local function observers_add(world: jecs.World): PatchedWorld
listener(entity, id, value) listener(entity, id, value)
end end
end end
local existing_hook = world:get(component, jecs.OnAdd) local idr = world.component_index[component]
if existing_hook then if idr then
table.insert(listeners, existing_hook) local idr_hook_existing = idr.hooks.on_add
local idr = world.component_index[component] if idr_hook_existing then
if idr then table.insert(listeners, idr_hook_existing)
idr.hooks.on_add = on_add
end end
idr.hooks.on_add = on_add :: any
else
world:set(component, jecs.OnAdd, on_add)
end end
world:set(component, jecs.OnAdd, on_add)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
return function() return function()
@ -201,15 +197,16 @@ local function observers_add(world: jecs.World): PatchedWorld
listener(entity, id, value) listener(entity, id, value)
end end
end end
local existing_hook = world:get(component, jecs.OnChange) local idr = world.component_index[component]
if existing_hook then if idr then
table.insert(listeners, existing_hook) local idr_hook_existing = idr.hooks.on_change
local idr = world.component_index[component] if idr_hook_existing then
if idr then table.insert(listeners, idr_hook_existing)
idr.hooks.on_change = on_change
end end
idr.hooks.on_change = on_change :: any
else
world:set(component, jecs.OnChange, on_change)
end end
world:set(component, jecs.OnChange, on_change)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
return function() return function()
@ -234,16 +231,16 @@ local function observers_add(world: jecs.World): PatchedWorld
listener(entity, id, value) listener(entity, id, value)
end end
end end
local existing_hook = world:get(component, jecs.OnRemove) local idr = world.component_index[component]
if existing_hook then if idr then
table.insert(listeners, existing_hook) local idr_hook_existing = idr.hooks.on_remove
local idr = world.component_index[component] if idr_hook_existing then
if idr then table.insert(listeners, idr_hook_existing)
idr.hooks.on_remove = on_remove
end end
idr.hooks.on_remove = on_remove :: any
else
world:set(component, jecs.OnRemove, on_remove)
end end
world:set(component, jecs.OnRemove, on_remove)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
return function() return function()

View file

@ -25,6 +25,10 @@ export default defineConfig({
}, },
], ],
"/learn/": [ "/learn/": [
{
text: "Overview",
items: [{ text: "Overview", link: "/learn/overview" }],
},
{ {
text: "API Reference", text: "API Reference",
items: [ items: [

View file

@ -89,7 +89,6 @@ print(`${world.get(Position, jecs.Name)} is a Component: ${world.has(Position, j
### Entity ranges ### Entity ranges
Jecs reserves entity ids under a threshold (HI_COMPONENT_ID, default is 256) for components. That means that regular entities will start after this number. This number can be further specified via the `range` member function. Jecs reserves entity ids under a threshold (HI_COMPONENT_ID, default is 256) for components. That means that regular entities will start after this number. This number can be further specified via the `range` member function.
::: code-group
```luau [luau] ```luau [luau]
world:range(1000, 5000) -- Defines the lower and upper bounds of the entity range respectively world:range(1000, 5000) -- Defines the lower and upper bounds of the entity range respectively
@ -99,13 +98,14 @@ print(e)
-- 1000 -- 1000
``` ```
```typescript [typescript] ```typescript [typescript]
world.range(1000, 5000) // Defines the lower and upper bounds of the entity range respectively world.range(1000, 5000) -- Defines the lower and upper bounds of the entity range respectively
const e = world.entity() local e = world.entity()
print(e) print(e)
// Output: // Output:
// 1000 // 1000
``` ```
```
::: :::
### Hooks ### Hooks

View file

@ -32,6 +32,8 @@
- [Awesome Entity Component System (link collection related to ECS) - Jeongseok Lee](https://github.com/jslee02/awesome-entity-component-system) - [Awesome Entity Component System (link collection related to ECS) - Jeongseok Lee](https://github.com/jslee02/awesome-entity-component-system)
- [Hibitset - DOCS.RS](https://docs.rs/hibitset/0.6.3/hibitset/) - [Hibitset - DOCS.RS](https://docs.rs/hibitset/0.6.3/hibitset/)
------------------
## Addons ## Addons
A collection of third-party jecs addons made by the community. If you would like to share what you're working on, [submit a pull request](/learn/contributing/pull-requests#addons)! A collection of third-party jecs addons made by the community. If you would like to share what you're working on, [submit a pull request](/learn/contributing/pull-requests#addons)!
@ -41,10 +43,10 @@ A collection of third-party jecs addons made by the community. If you would like
#### [jabby](https://github.com/alicesaidhi/jabby) #### [jabby](https://github.com/alicesaidhi/jabby)
A jecs debugger with a string-based query language and entity editing capabilities. A jecs debugger with a string-based query language and entity editing capabilities.
#### [jecs_entity_visualiser](https://github.com/Ukendio/jecs/blob/main/tools/entity_visualiser.luau) #### [jecs_entity_visualiser](https://github.com/Ukendio/jecs/blob/main/addons/entity_visualiser.luau)
A simple entity and component visualiser in the output A simple entity and component visualiser in the output
#### [jecs_lifetime_tracker](https://github.com/Ukendio/jecs/blob/main/tools/lifetime_tracker.luau) #### [jecs_lifetime_tracker](https://github.com/Ukendio/jecs/blob/main/addons/lifetime_tracker.luau)
A tool for inspecting entity lifetimes A tool for inspecting entity lifetimes
### Helpers ### Helpers