From 059a3f9bed70faa8798d6074cf8bbc1ae524a9a5 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Fri, 26 Jul 2024 04:36:30 +0200 Subject: [PATCH] Fix world:remove --- docs/api.md | 59 ----------------- docs/api/query.md | 132 ++++++++++++++++++++++++++++++++++++++ docs/api/world.md | 70 ++++++++++++++++++++ docs/concepts/entities.md | 2 - src/init.luau | 1 + test/tests.luau | 7 ++ 6 files changed, 210 insertions(+), 61 deletions(-) delete mode 100644 docs/api.md create mode 100644 docs/api/query.md create mode 100644 docs/api/world.md diff --git a/docs/api.md b/docs/api.md deleted file mode 100644 index d11eb98f..00000000 --- a/docs/api.md +++ /dev/null @@ -1,59 +0,0 @@ -# API - -## World - -### 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(); -``` - -::: - -### world:entity() -> `Entity` -Creates a new entity. - -Example: -::: code-group - -```luau [luau] -local entity = world:entity() -``` - -```ts [typescript] -const entity = world.entity(); -``` - -::: - -### world:component() -> `Entity` -Creates a new static component. Keep in mind that components are also entities. - -Example: -::: code-group - -```luau [luau] -local Health = world:component() -``` - -```ts [typescript] -const Health = world.component(); -``` - -::: - -::: info -You should use this when creating static components. - -For example, a generic Health entity should be created using this. -::: \ No newline at end of file diff --git a/docs/api/query.md b/docs/api/query.md new file mode 100644 index 00000000..e7702fa4 --- /dev/null +++ b/docs/api/query.md @@ -0,0 +1,132 @@ +# 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 + +### 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(): Entity -- The new componen. +``` +Creates a new component. + +Example: +::: code-group + +```luau [luau] +local Health = world:component() :: jecs.Entity +``` + +```ts [typescript] +const Health = world.component(); +``` +::: + +::: info +You should use this when creating components. + +For example, a Health type should be created using this. +::: + +### get() +```luau +function World:get( + entity: Entity, -- The entity + ...: Entity -- The types to fetch +): ... -- Returns the component data in the same order they were passed in +``` +Returns the data for each provided type for the corresponding entity. + +::: + +### add() +```luau +function World:add( + entity: Entity, -- The entity + id: Entity -- 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, -- 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 component IDs to query with. Entities that satifies the conditions will be returned +): Query<...Entity> -- Returns the Query which gets the entity and their corresponding data when iterated +``` +Creates a [`query`](query) with the given component IDs. + +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 +} +``` + +::: diff --git a/docs/api/world.md b/docs/api/world.md new file mode 100644 index 00000000..96f7fb30 --- /dev/null +++ b/docs/api/world.md @@ -0,0 +1,70 @@ +# 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() +```lua +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 +``` +Creates a new entity. + +Example: +::: code-group + +```luau [luau] +local entity = world:entity() +``` + +```ts [typescript] +const entity = world.entity(); +``` + +::: + +### component()` +```luau +function World:component(): Entity +``` +Creates a new component. + +Example: +::: code-group + +```luau [luau] +local Health = world:component() :: jecs.Entity +``` + +```ts [typescript] +const Health = world.component(); +``` + +::: + +::: info +You should use this when creating components. + +For example, a Health type should be created using this. +::: diff --git a/docs/concepts/entities.md b/docs/concepts/entities.md index 2e034280..d3f5a12f 100644 --- a/docs/concepts/entities.md +++ b/docs/concepts/entities.md @@ -1,3 +1 @@ -## TODO -This is a TODO stub. \ No newline at end of file diff --git a/src/init.luau b/src/init.luau index c7e5c74d..b2f4b5d2 100644 --- a/src/init.luau +++ b/src/init.luau @@ -512,6 +512,7 @@ end local function archetype_traverse_remove(world: World, componentId: i53, from: Archetype): Archetype + from = from or world.ROOT_ARCHETYPE local edge = edge_ensure(from, componentId) local remove = edge.remove diff --git a/test/tests.luau b/test/tests.luau index 6110be06..f7807d3d 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -25,6 +25,13 @@ local N = 10 type World = jecs.WorldShim TEST("world", function() + do CASE "should not error when removing a component that entity doesn't have" + local world = jecs.World.new() :: World + local A = world:component() + local e = world:entity() + world:remove(e, A) + CHECK(true) + end do CASE("should find every component id") local world = jecs.World.new() :: World local A = world:component()