mirror of
https://github.com/Ukendio/jecs.git
synced 2025-08-04 19:29:18 +00:00
Compare commits
6 commits
3d366d26ec
...
c2b4398a4f
Author | SHA1 | Date | |
---|---|---|---|
|
c2b4398a4f | ||
|
bb03e88d3d | ||
|
5dba6e7bac | ||
|
0541f80b5d | ||
|
fd685537ea | ||
|
96bed9bd7e |
4 changed files with 38 additions and 41 deletions
|
@ -2,15 +2,20 @@ local jecs = require("@jecs")
|
||||||
|
|
||||||
type Observer = {
|
type Observer = {
|
||||||
callback: (jecs.Entity) -> (),
|
callback: (jecs.Entity) -> (),
|
||||||
query: jecs.Query<...jecs.Id>,
|
query: jecs.Query<...any>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
|
added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||||
removed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id) -> ()) -> () -> (),
|
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
|
||||||
changed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
|
changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||||
observer: (PatchedWorld, Observer) -> (),
|
observer: (PatchedWorld, Observer) -> (),
|
||||||
monitor: (PatchedWorld, Observer) -> (),
|
monitor: (PatchedWorld, Monitor) -> (),
|
||||||
}
|
}
|
||||||
|
|
||||||
local function observers_new(world, description)
|
local function observers_new(world, description)
|
||||||
|
@ -45,7 +50,6 @@ 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 = {}
|
||||||
|
@ -163,16 +167,16 @@ local function observers_add(world: jecs.World): PatchedWorld
|
||||||
listener(entity, id, value)
|
listener(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local idr = world.component_index[component]
|
local existing_hook = world:get(component, jecs.OnAdd)
|
||||||
if idr then
|
if existing_hook then
|
||||||
local idr_hook_existing = idr.hooks.on_add
|
table.insert(listeners, existing_hook)
|
||||||
if idr_hook_existing then
|
local idr = world.component_index[component]
|
||||||
table.insert(listeners, idr_hook_existing)
|
if idr then
|
||||||
|
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()
|
||||||
|
@ -197,16 +201,15 @@ local function observers_add(world: jecs.World): PatchedWorld
|
||||||
listener(entity, id, value)
|
listener(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local idr = world.component_index[component]
|
local existing_hook = world:get(component, jecs.OnChange)
|
||||||
if idr then
|
if existing_hook then
|
||||||
local idr_hook_existing = idr.hooks.on_change
|
table.insert(listeners, existing_hook)
|
||||||
if idr_hook_existing then
|
local idr = world.component_index[component]
|
||||||
table.insert(listeners, idr_hook_existing)
|
if idr then
|
||||||
|
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()
|
||||||
|
@ -231,16 +234,16 @@ local function observers_add(world: jecs.World): PatchedWorld
|
||||||
listener(entity, id, value)
|
listener(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local idr = world.component_index[component]
|
local existing_hook = world:get(component, jecs.OnRemove)
|
||||||
if idr then
|
if existing_hook then
|
||||||
local idr_hook_existing = idr.hooks.on_remove
|
table.insert(listeners, existing_hook)
|
||||||
if idr_hook_existing then
|
local idr = world.component_index[component]
|
||||||
table.insert(listeners, idr_hook_existing)
|
if idr then
|
||||||
|
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()
|
||||||
|
|
|
@ -25,10 +25,6 @@ export default defineConfig({
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"/learn/": [
|
"/learn/": [
|
||||||
{
|
|
||||||
text: "Overview",
|
|
||||||
items: [{ text: "Overview", link: "/learn/overview" }],
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
text: "API Reference",
|
text: "API Reference",
|
||||||
items: [
|
items: [
|
||||||
|
|
|
@ -89,6 +89,7 @@ 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
|
||||||
|
|
||||||
|
@ -98,14 +99,13 @@ 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
|
||||||
|
|
||||||
local e = world.entity()
|
const e = world.entity()
|
||||||
print(e)
|
print(e)
|
||||||
// Output:
|
// Output:
|
||||||
// 1000
|
// 1000
|
||||||
```
|
```
|
||||||
```
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
### Hooks
|
### Hooks
|
||||||
|
|
|
@ -32,8 +32,6 @@
|
||||||
- [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)!
|
||||||
|
@ -43,10 +41,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/addons/entity_visualiser.luau)
|
#### [jecs_entity_visualiser](https://github.com/Ukendio/jecs/blob/main/tools/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/addons/lifetime_tracker.luau)
|
#### [jecs_lifetime_tracker](https://github.com/Ukendio/jecs/blob/main/tools/lifetime_tracker.luau)
|
||||||
A tool for inspecting entity lifetimes
|
A tool for inspecting entity lifetimes
|
||||||
|
|
||||||
### Helpers
|
### Helpers
|
||||||
|
|
Loading…
Reference in a new issue