mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Update documentation
Some checks are pending
Some checks are pending
This commit is contained in:
parent
753adf884d
commit
52e03683db
2 changed files with 610 additions and 490 deletions
|
@ -4,11 +4,20 @@ A World contains entities which have components. The World is queryable and can
|
|||
|
||||
# Methods
|
||||
|
||||
## iter
|
||||
|
||||
Returns an iterator that can be used to iterate over the query.
|
||||
|
||||
```luau
|
||||
function Query:iter(): () -> (Entity, ...)
|
||||
```
|
||||
|
||||
## with
|
||||
|
||||
Adds components (IDs) to query with, but will not use their data. This is useful for Tags or generally just data you do not care for.
|
||||
|
||||
```luau
|
||||
function query:with(
|
||||
function Query:with(
|
||||
...: Entity -- The IDs to query with
|
||||
): Query
|
||||
```
|
||||
|
@ -24,7 +33,7 @@ end
|
|||
|
||||
```ts [typescript]
|
||||
for (const [id, position] of world.query(Position).with(Velocity)) {
|
||||
// Do something
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -35,10 +44,11 @@ Put the IDs inside of `world:query()` instead if you need the data.
|
|||
:::
|
||||
|
||||
## without
|
||||
|
||||
Removes entities with the provided components from the query.
|
||||
|
||||
```luau
|
||||
function query:without(
|
||||
function Query:without(
|
||||
...: Entity -- The IDs to filter against.
|
||||
): Query -- Returns the Query
|
||||
```
|
||||
|
@ -46,6 +56,7 @@ function query:without(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
for entity, position in world:query(Position):without(Velocity) do
|
||||
-- Do something
|
||||
|
@ -54,15 +65,18 @@ end
|
|||
|
||||
```ts [typescript]
|
||||
for (const [entity, position] of world.query(Position).without(Velocity)) {
|
||||
// Do something
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## archetypes
|
||||
|
||||
Returns the matching archetypes of the query.
|
||||
|
||||
```luau
|
||||
function query:archetypes(): { Archetype }
|
||||
function Query:archetypes(): { Archetype }
|
||||
```
|
||||
|
||||
Example:
|
||||
|
@ -86,3 +100,11 @@ end
|
|||
:::info
|
||||
This function is meant for people who want to really customize their query behaviour at the archetype-level
|
||||
:::
|
||||
|
||||
## cached
|
||||
|
||||
Returns a cached version of the query. This is useful if you want to iterate over the same query multiple times.
|
||||
|
||||
```luau
|
||||
function Query:cached(): Query -- Returns the cached Query
|
||||
```
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
# 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 and to perform different kinds of operations on them.
|
||||
|
||||
# Functions
|
||||
|
||||
## new
|
||||
|
||||
`World` utilizes a class, meaning JECS allows you to create multiple worlds.
|
||||
|
||||
```luau
|
||||
function World.new(): World
|
||||
```
|
||||
|
@ -11,6 +15,7 @@ function World.new(): World
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local world = jecs.World.new()
|
||||
local myOtherWorld = jecs.World.new()
|
||||
|
@ -22,11 +27,15 @@ import { World } from "@rbxts/jecs";
|
|||
const world = new World();
|
||||
const myOtherWorld = new World();
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
# Methods
|
||||
|
||||
## entity
|
||||
|
||||
Creates a new entity.
|
||||
|
||||
```luau
|
||||
function World:entity(): Entity
|
||||
```
|
||||
|
@ -34,6 +43,7 @@ function World:entity(): Entity
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local entity = world:entity()
|
||||
```
|
||||
|
@ -41,12 +51,15 @@ local entity = world:entity()
|
|||
```ts [typescript]
|
||||
const entity = world.entity();
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## component
|
||||
|
||||
Creates a new component. Do note components are entities as well, meaning JECS allows you to add other components onto them.
|
||||
|
||||
These are meant to be added onto other entities through `add` and `set`
|
||||
|
||||
```luau
|
||||
function World:component<T>(): Entity<T> -- The new componen.
|
||||
```
|
||||
|
@ -54,6 +67,7 @@ function World:component<T>(): Entity<T> -- The new componen.
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local Health = world:component() :: jecs.Entity<number> -- Typecasting this will allow us to know what kind of data the component holds!
|
||||
```
|
||||
|
@ -61,10 +75,13 @@ local Health = world:component() :: jecs.Entity<number> -- Typecasting this will
|
|||
```ts [typescript]
|
||||
const Health = world.component<number>();
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## get
|
||||
|
||||
Returns the data present in the component that was set in the entity. Will return nil if the component was a tag or is not present.
|
||||
|
||||
```luau
|
||||
function World:get<T>(
|
||||
entity: Entity, -- The entity
|
||||
|
@ -75,6 +92,7 @@ function World:get<T>(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local Health = world:component() :: jecs.Entity<number>
|
||||
|
||||
|
@ -93,15 +111,18 @@ const Health = world.component<number>();
|
|||
const Entity = world.entity();
|
||||
world.set(Entity, Health, 100);
|
||||
|
||||
print(world.get(Entity, Health))
|
||||
print(world.get(Entity, Health));
|
||||
|
||||
// Outputs:
|
||||
// 100
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## has
|
||||
|
||||
Returns whether an entity has a component (ID). Useful for checking if an entity has a tag or if you don't care of the data that is inside the component.
|
||||
|
||||
```luau
|
||||
function World:has(
|
||||
entity: Entity, -- The entity
|
||||
|
@ -112,6 +133,7 @@ function World:has(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local IsMoving = world:component()
|
||||
local Ragdolled = world:entity() -- This is a tag, meaning it won't contain data
|
||||
|
@ -155,9 +177,11 @@ print(world.has(Entity, Ragdolled));
|
|||
// nil
|
||||
// true
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## add
|
||||
|
||||
Adds a component (ID) to the entity. Useful for adding a tag to an entity, as this adds the component to the entity without any additional values inside
|
||||
|
||||
```luau
|
||||
|
@ -172,7 +196,9 @@ This function is idempotent, meaning if the entity already has the id, this oper
|
|||
:::
|
||||
|
||||
## set
|
||||
|
||||
Adds or changes data in the entity's component.
|
||||
|
||||
```luau
|
||||
function World:set(
|
||||
entity: Entity, -- The entity
|
||||
|
@ -184,6 +210,7 @@ function World:set(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local Health = world:component() :: jecs.Entity<number>
|
||||
|
||||
|
@ -206,19 +233,22 @@ const Health = world.component<number>();
|
|||
const Entity = world.entity();
|
||||
world.set(Entity, Health, 100);
|
||||
|
||||
print(world.get(Entity, Health))
|
||||
print(world.get(Entity, Health));
|
||||
|
||||
world.set(Entity, Health, 50);
|
||||
print(world.get(Entity, Health))
|
||||
print(world.get(Entity, Health));
|
||||
|
||||
// Outputs:
|
||||
// 100
|
||||
// 50
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## query
|
||||
|
||||
Creates a [`query`](query) with the given components (IDs). Entities that satisfies the conditions of the query will be returned and their corresponding data.
|
||||
|
||||
```luau
|
||||
function World:query(
|
||||
...: Entity -- The components to query with
|
||||
|
@ -228,6 +258,7 @@ function World:query(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
-- Entity could also be a component if a component also meets the requirements, since they are also entities which you can add more components onto
|
||||
for entity, position, velocity in world:query(Position, Velocity) do
|
||||
|
@ -242,16 +273,20 @@ for (const [entity, 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
|
||||
|
||||
Get the target of a relationship.
|
||||
This will return a target (second element of a pair) of the entity for the specified relationship. The index allows for iterating through the targets, if a single entity has multiple targets for the same relationship.
|
||||
If the index is larger than the total number of instances the entity has for the relationship or if there is no pair with the specified relationship on the entity, the operation will return nil.
|
||||
|
||||
```luau
|
||||
function World:target(
|
||||
entity: Entity, -- The entity
|
||||
|
@ -261,7 +296,9 @@ function World:target(
|
|||
```
|
||||
|
||||
## parent
|
||||
|
||||
Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil.
|
||||
|
||||
```luau
|
||||
function World:parent(
|
||||
child: Entity -- The child ID to find the parent of
|
||||
|
@ -275,7 +312,9 @@ world:target(entity, jecs.ChildOf, 0)
|
|||
```
|
||||
|
||||
## contains
|
||||
|
||||
Checks if an entity or component (id) exists in the world.
|
||||
|
||||
```luau
|
||||
function World:contains(
|
||||
entity: Entity,
|
||||
|
@ -285,6 +324,7 @@ function World:contains(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local entity = world:entity()
|
||||
print(world:contains(entity))
|
||||
|
@ -308,10 +348,13 @@ print(world.contains(2));
|
|||
// true
|
||||
// false
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## remove
|
||||
|
||||
Removes a component (ID) from an entity
|
||||
|
||||
```luau
|
||||
function World:remove(
|
||||
entity: Entity,
|
||||
|
@ -322,6 +365,7 @@ function World:remove(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local IsMoving = world:component()
|
||||
|
||||
|
@ -353,10 +397,13 @@ print(world.has(entity, IsMoving));
|
|||
// true
|
||||
// false
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## delete
|
||||
|
||||
Deletes an entity and all of its related components and relationships.
|
||||
|
||||
```luau
|
||||
function World:delete(
|
||||
entity: Entity
|
||||
|
@ -366,6 +413,7 @@ function World:delete(
|
|||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local entity = world:entity()
|
||||
print(world:has(entity))
|
||||
|
@ -391,12 +439,62 @@ print(world.has(entity));
|
|||
// true
|
||||
// false
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## clear
|
||||
|
||||
Clears all of the components and relationships of the entity without deleting it.
|
||||
|
||||
```luau
|
||||
function World:clear(
|
||||
entity: Entity
|
||||
): void
|
||||
```
|
||||
|
||||
## each
|
||||
|
||||
Iterate over all entities with the specified component.
|
||||
Useful when you only need the entity for a specific ID and you want to avoid creating a query.
|
||||
|
||||
```luau
|
||||
function World:each(
|
||||
id: Entity -- The component ID
|
||||
): () -> Entity
|
||||
```
|
||||
|
||||
Example:
|
||||
|
||||
::: code-group
|
||||
|
||||
```luau [luau]
|
||||
local id = world:entity()
|
||||
for entity in world:each(id) do
|
||||
-- Do something
|
||||
end
|
||||
```
|
||||
|
||||
```ts [typescript]
|
||||
const id = world.entity();
|
||||
for (const entity of world.each(id)) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## children
|
||||
|
||||
Iterate entities in root of parent
|
||||
|
||||
```luau
|
||||
function World:children(
|
||||
parent: Entity -- The parent entity
|
||||
): () -> Entity
|
||||
```
|
||||
|
||||
This is the same as calling:
|
||||
|
||||
```luau
|
||||
world:each(pair(ChildOf, parent))
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue