mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Fix docs titles
This commit is contained in:
parent
9af08c0553
commit
1067a17101
5 changed files with 360 additions and 354 deletions
|
@ -9,6 +9,9 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
|
|||
[semver]: https://semver.org/spec/v2.0.0.html
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.2.10] - 2024-09-07
|
||||
|
||||
- `[world]`:
|
||||
- Improved performance for hooks
|
||||
- Changed `world:set` to be idempotent when setting tags
|
||||
|
@ -29,6 +32,7 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
|
|||
- Changed `world:contains()` to return a `boolean` instead of an entity which may or may not exist
|
||||
- Fixed `world:has()` to take the correct parameters
|
||||
|
||||
|
||||
## [0.2.2] - 2024-07-07
|
||||
|
||||
### Added
|
||||
|
|
100
docs/api/jecs.md
100
docs/api/jecs.md
|
@ -1,50 +1,50 @@
|
|||
# Jecs
|
||||
|
||||
Jecs. Just an Entity Component System.
|
||||
|
||||
## Properties
|
||||
|
||||
### World
|
||||
```luau
|
||||
jecs.World: World
|
||||
```
|
||||
A world is a container of all ECS data. Games can have multiple worlds but component IDs may conflict between worlds. Ensure to register the same component IDs in the same order for each world.
|
||||
|
||||
### Wildcard
|
||||
```luau
|
||||
jecs.Wildcard: Entity
|
||||
```
|
||||
Builtin component type. This ID is used for wildcard queries.
|
||||
|
||||
### Component
|
||||
```luau
|
||||
jecs.Component: Entity
|
||||
```
|
||||
Builtin component type. Every ID created with [world:component()](world.md#component()) has this type added to it. This is meant for querying every component ID.
|
||||
|
||||
### ChildOf
|
||||
```luau
|
||||
jecs.ChildOf: Entity
|
||||
```
|
||||
Builtin component type. This ID is for creating parent-child hierarchies.
|
||||
|
||||
### Rest
|
||||
```luau
|
||||
jecs.Rest: Entity
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
### pair()
|
||||
```luau
|
||||
function jecs.pair(
|
||||
first: Entity, -- The first element of the pair, referred to as the relationship of the relationship pair.
|
||||
object: Entity, -- The second element of the pair, referred to as the target of the relationship pair.
|
||||
): number -- Returns the Id with those two elements
|
||||
|
||||
```
|
||||
::: info
|
||||
|
||||
Note that while relationship pairs can be used as components, meaning you can add data with it as an ID, however they cannot be used as entities. Meaning you cannot add components to a pair as the source of a binding.
|
||||
|
||||
:::
|
||||
# Jecs
|
||||
|
||||
Jecs. Just an Entity Component System.
|
||||
|
||||
# Properties
|
||||
|
||||
## World
|
||||
```luau
|
||||
jecs.World: World
|
||||
```
|
||||
A world is a container of all ECS data. Games can have multiple worlds but component IDs may conflict between worlds. Ensure to register the same component IDs in the same order for each world.
|
||||
|
||||
## Wildcard
|
||||
```luau
|
||||
jecs.Wildcard: Entity
|
||||
```
|
||||
Builtin component type. This ID is used for wildcard queries.
|
||||
|
||||
## Component
|
||||
```luau
|
||||
jecs.Component: Entity
|
||||
```
|
||||
Builtin component type. Every ID created with [world:component()](world.md#component()) has this type added to it. This is meant for querying every component ID.
|
||||
|
||||
## ChildOf
|
||||
```luau
|
||||
jecs.ChildOf: Entity
|
||||
```
|
||||
Builtin component type. This ID is for creating parent-child hierarchies.
|
||||
|
||||
## Rest
|
||||
```luau
|
||||
jecs.Rest: Entity
|
||||
```
|
||||
|
||||
# Functions
|
||||
|
||||
## pair()
|
||||
```luau
|
||||
function jecs.pair(
|
||||
first: Entity, -- The first element of the pair, referred to as the relationship of the relationship pair.
|
||||
object: Entity, -- The second element of the pair, referred to as the target of the relationship pair.
|
||||
): number -- Returns the Id with those two elements
|
||||
|
||||
```
|
||||
::: info
|
||||
|
||||
Note that while relationship pairs can be used as components, meaning you can add data with it as an ID, however they cannot be used as entities. Meaning you cannot add components to a pair as the source of a binding.
|
||||
|
||||
:::
|
||||
|
|
|
@ -1,131 +1,131 @@
|
|||
# Query
|
||||
|
||||
A World contains entities which have components. The World is queryable and can be used to get entities with a specific set of components.
|
||||
|
||||
## Functions
|
||||
|
||||
### drain()
|
||||
```luau
|
||||
function query:drain(): Query
|
||||
```
|
||||
This function will impede it from being reset when the query is being iterated.
|
||||
|
||||
### next()
|
||||
```luau
|
||||
function query:next(): Query
|
||||
```
|
||||
Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
|
||||
|
||||
### with()
|
||||
```luau
|
||||
function query:with(
|
||||
...: Entity -- The IDs to query with
|
||||
): Query
|
||||
```
|
||||
Adds IDs to query with, but will not use their data. This is useful for Tags or generally just data you do not care for.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for id, position in world:query(Position):with(Velocity) do
|
||||
-- Do something
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
for (const [id, position] of world.query(Position).with(Velocity)) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
:::info
|
||||
Put the IDs inside of `world:query()` instead if you need the data.
|
||||
:::
|
||||
|
||||
### without()
|
||||
|
||||
```luau
|
||||
function query:without(
|
||||
...: Entity -- The IDs to filter against.
|
||||
): Query -- Returns the Query
|
||||
```
|
||||
Removes entities with the provided IDs from the query.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for _ in world:query(Position):without(Velocity) do
|
||||
-- Do something
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
for (const _ of world.query(Position).without(Velocity)) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
### replace()
|
||||
|
||||
```luau
|
||||
function query:replace(
|
||||
fn: (entity: Entity, ...: T...) -> U... -- ): () -- The callback that will transform the entities' data
|
||||
```
|
||||
This function takes a callback which is given the current queried data of each matching entity. The values returned by the callback will be set as the new data for each given ID on the entity.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
world:query(Position, Velocity):replace(function(e, position, velocity)
|
||||
return position + velocity, velocity * 0.9
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
world
|
||||
.query(Position, Velocity)
|
||||
.replace((e, position, velocity) =>
|
||||
$tuple(position.add(velocity), velocity.mul(0.9)),
|
||||
);
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
|
||||
### archetypes()
|
||||
```luau
|
||||
function query.archetypes(): { Archetype }
|
||||
```
|
||||
Returns the matching archetypes of the query.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for i, archetype in world:query(Position, Velocity).archetypes() do
|
||||
local columns = archetype.columns
|
||||
local field = archetype.records
|
||||
|
||||
local P = field[Position]
|
||||
local V = field[Velocity]
|
||||
|
||||
for row, entity in archetype.entities do
|
||||
local position = columns[P][row]
|
||||
local velocity = columns[V][row]
|
||||
-- Do something
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
:::info
|
||||
This function is meant for internal usage. Use this if you want to maximize performance by inlining the iterator.
|
||||
:::
|
||||
# Query
|
||||
|
||||
A World contains entities which have components. The World is queryable and can be used to get entities with a specific set of components.
|
||||
|
||||
# Functions
|
||||
|
||||
## drain()
|
||||
```luau
|
||||
function query:drain(): Query
|
||||
```
|
||||
This function will impede it from being reset when the query is being iterated.
|
||||
|
||||
## next()
|
||||
```luau
|
||||
function query:next(): Query
|
||||
```
|
||||
Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
|
||||
|
||||
## with()
|
||||
```luau
|
||||
function query:with(
|
||||
...: Entity -- The IDs to query with
|
||||
): Query
|
||||
```
|
||||
Adds IDs to query with, but will not use their data. This is useful for Tags or generally just data you do not care for.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for id, position in world:query(Position):with(Velocity) do
|
||||
-- Do something
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
for (const [id, position] of world.query(Position).with(Velocity)) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
:::info
|
||||
Put the IDs inside of `world:query()` instead if you need the data.
|
||||
:::
|
||||
|
||||
## without()
|
||||
|
||||
```luau
|
||||
function query:without(
|
||||
...: Entity -- The IDs to filter against.
|
||||
): Query -- Returns the Query
|
||||
```
|
||||
Removes entities with the provided IDs from the query.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for _ in world:query(Position):without(Velocity) do
|
||||
-- Do something
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
for (const _ of world.query(Position).without(Velocity)) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## replace()
|
||||
|
||||
```luau
|
||||
function query:replace(
|
||||
fn: (entity: Entity, ...: T...) -> U... -- ): () -- The callback that will transform the entities' data
|
||||
```
|
||||
This function takes a callback which is given the current queried data of each matching entity. The values returned by the callback will be set as the new data for each given ID on the entity.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
world:query(Position, Velocity):replace(function(e, position, velocity)
|
||||
return position + velocity, velocity * 0.9
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
world
|
||||
.query(Position, Velocity)
|
||||
.replace((e, position, velocity) =>
|
||||
$tuple(position.add(velocity), velocity.mul(0.9)),
|
||||
);
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
|
||||
## archetypes()
|
||||
```luau
|
||||
function query.archetypes(): { Archetype }
|
||||
```
|
||||
Returns the matching archetypes of the query.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for i, archetype in world:query(Position, Velocity).archetypes() do
|
||||
local columns = archetype.columns
|
||||
local field = archetype.records
|
||||
|
||||
local P = field[Position]
|
||||
local V = field[Velocity]
|
||||
|
||||
for row, entity in archetype.entities do
|
||||
local position = columns[P][row]
|
||||
local velocity = columns[V][row]
|
||||
-- Do something
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
:::info
|
||||
This function is meant for internal usage. Use this if you want to maximize performance by inlining the iterator.
|
||||
:::
|
||||
|
|
|
@ -1,173 +1,173 @@
|
|||
# World
|
||||
|
||||
A World contains entities which have components. The World is queryable and can be used to get entities with a specific set of components.
|
||||
|
||||
## Functions
|
||||
|
||||
### new()
|
||||
```luau
|
||||
function World.new(): World
|
||||
```
|
||||
Creates a new world.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local world = jecs.World.new()
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
import { World } from "@rbxts/jecs";
|
||||
|
||||
const world = new World();
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## entity()
|
||||
```luau
|
||||
function World:entity(): Entity -- The new entit.
|
||||
```
|
||||
Creates a new entity.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local entity = world:entity()
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
const entity = world.entity();
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
### component()
|
||||
```luau
|
||||
function World:component<T>(): Entity<T> -- The new componen.
|
||||
```
|
||||
Creates a new component.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local Health = world:component() :: jecs.Entity<number>
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
const Health = world.component<number>();
|
||||
```
|
||||
:::
|
||||
|
||||
::: info
|
||||
You should use this when creating components.
|
||||
|
||||
For example, a Health type should be created using this.
|
||||
:::
|
||||
|
||||
### get()
|
||||
```luau
|
||||
function World:get<T>(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T> -- The component ID to fetch
|
||||
): T
|
||||
```
|
||||
Returns the data for the component data the corresponding entity, nil if entity does not have the ID or was a tag.
|
||||
|
||||
### has()
|
||||
```luau
|
||||
function World:has(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T> -- The component ID to check
|
||||
): boolean
|
||||
```
|
||||
Returns whether the entity has the ID.
|
||||
|
||||
:::
|
||||
|
||||
### add()
|
||||
```luau
|
||||
function World:add(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T> -- The component ID to add
|
||||
): ()
|
||||
```
|
||||
Adds a component ID to the entity.
|
||||
|
||||
This operation adds a single (component) id to an entity.
|
||||
|
||||
::: info
|
||||
This function is idempotent, meaning if the entity already has the id, this operation will have no side effects.
|
||||
:::
|
||||
|
||||
|
||||
### set()
|
||||
```luau
|
||||
function World:set(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T>, -- The component ID to set
|
||||
data: T -- The data of the component's type
|
||||
): ()
|
||||
```
|
||||
Adds or changes the entity's component.
|
||||
|
||||
### query()
|
||||
```luau
|
||||
function World:query(
|
||||
...: Entity -- The IDs to query with
|
||||
): Query
|
||||
```
|
||||
Creates a [`query`](query) with the given IDs. Entities that satisfies the conditions of the query will be returned and their corresponding data.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for id, position, velocity in world:query(Position, Velocity) do
|
||||
-- Do something
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
for (const [id, position, velocity] of world.query(Position, Velocity) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
:::info
|
||||
Queries are uncached by default, this is generally very cheap unless you have high fragmentation from e.g. relationships.
|
||||
|
||||
:::
|
||||
### target()
|
||||
```luau
|
||||
function World:target(
|
||||
entity: Entity, -- The entity
|
||||
relation: Entity -- The relationship between the entity and the target
|
||||
): Entity? -- Returns the parent of the child
|
||||
```
|
||||
|
||||
Get the target of a relationship.
|
||||
|
||||
This will return a target (second element of a pair) of the entity for the specified relationship.
|
||||
|
||||
If there is no pair with specified relationship, it will return nil.
|
||||
|
||||
### parent()
|
||||
```luau
|
||||
function World:parent(
|
||||
child: Entity -- The child ID to find the parent of
|
||||
): Entity? -- Returns the parent of the child
|
||||
```
|
||||
|
||||
Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil.
|
||||
|
||||
This operation is the same as calling:
|
||||
|
||||
```luau
|
||||
world:target(entity, jecs.ChildOf)
|
||||
```
|
||||
# World
|
||||
|
||||
A World contains entities which have components. The World is queryable and can be used to get entities with a specific set of components.
|
||||
|
||||
# Functions
|
||||
|
||||
## new()
|
||||
```luau
|
||||
function World.new(): World
|
||||
```
|
||||
Creates a new world.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local world = jecs.World.new()
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
import { World } from "@rbxts/jecs";
|
||||
|
||||
const world = new World();
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## entity()
|
||||
```luau
|
||||
function World:entity(): Entity -- The new entit.
|
||||
```
|
||||
Creates a new entity.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local entity = world:entity()
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
const entity = world.entity();
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## component()
|
||||
```luau
|
||||
function World:component<T>(): Entity<T> -- The new componen.
|
||||
```
|
||||
Creates a new component.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local Health = world:component() :: jecs.Entity<number>
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
const Health = world.component<number>();
|
||||
```
|
||||
:::
|
||||
|
||||
::: info
|
||||
You should use this when creating components.
|
||||
|
||||
For example, a Health type should be created using this.
|
||||
:::
|
||||
|
||||
## get()
|
||||
```luau
|
||||
function World:get<T>(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T> -- The component ID to fetch
|
||||
): T
|
||||
```
|
||||
Returns the data for the component data the corresponding entity, nil if entity does not have the ID or was a tag.
|
||||
|
||||
## has()
|
||||
```luau
|
||||
function World:has(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T> -- The component ID to check
|
||||
): boolean
|
||||
```
|
||||
Returns whether the entity has the ID.
|
||||
|
||||
:::
|
||||
|
||||
## add()
|
||||
```luau
|
||||
function World:add(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T> -- The component ID to add
|
||||
): ()
|
||||
```
|
||||
Adds a component ID to the entity.
|
||||
|
||||
This operation adds a single (component) id to an entity.
|
||||
|
||||
::: info
|
||||
This function is idempotent, meaning if the entity already has the id, this operation will have no side effects.
|
||||
:::
|
||||
|
||||
|
||||
## set()
|
||||
```luau
|
||||
function World:set(
|
||||
entity: Entity, -- The entity
|
||||
id: Entity<T>, -- The component ID to set
|
||||
data: T -- The data of the component's type
|
||||
): ()
|
||||
```
|
||||
Adds or changes the entity's component.
|
||||
|
||||
## query()
|
||||
```luau
|
||||
function World:query(
|
||||
...: Entity -- The IDs to query with
|
||||
): Query
|
||||
```
|
||||
Creates a [`query`](query) with the given IDs. Entities that satisfies the conditions of the query will be returned and their corresponding data.
|
||||
|
||||
Example:
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for id, position, velocity in world:query(Position, Velocity) do
|
||||
-- Do something
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
for (const [id, position, velocity] of world.query(Position, Velocity) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
:::info
|
||||
Queries are uncached by default, this is generally very cheap unless you have high fragmentation from e.g. relationships.
|
||||
|
||||
:::
|
||||
## target()
|
||||
```luau
|
||||
function World:target(
|
||||
entity: Entity, -- The entity
|
||||
relation: Entity -- The relationship between the entity and the target
|
||||
): Entity? -- Returns the parent of the child
|
||||
```
|
||||
|
||||
Get the target of a relationship.
|
||||
|
||||
This will return a target (second element of a pair) of the entity for the specified relationship.
|
||||
|
||||
If there is no pair with specified relationship, it will return nil.
|
||||
|
||||
## parent()
|
||||
```luau
|
||||
function World:parent(
|
||||
child: Entity -- The child ID to find the parent of
|
||||
): Entity? -- Returns the parent of the child
|
||||
```
|
||||
|
||||
Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil.
|
||||
|
||||
This operation is the same as calling:
|
||||
|
||||
```luau
|
||||
world:target(entity, jecs.ChildOf)
|
||||
```
|
||||
|
|
|
@ -1520,8 +1520,10 @@ export type World = {
|
|||
& (<A, B, C>(self: World, id: Entity, Id<A>, Id<B>, Id<C>) -> (A?, B?, C?))
|
||||
& <A, B, C, D>(self: World, id: Entity, Id<A>, Id<B>, Id<C>, Id<D>) -> (A?, B?, C?, D?),
|
||||
|
||||
--- Returns whether the entity has the ID.
|
||||
has: (self: World, entity: Entity, ...Id) -> boolean,
|
||||
|
||||
--- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil.
|
||||
parent: (self: World, entity: Entity) -> Entity,
|
||||
|
||||
--- Checks if the world contains the given entity
|
||||
|
|
Loading…
Reference in a new issue