Skip to content

Commit 631ae15

Browse files
committed
🔖 version bump
1 parent 69e02b3 commit 631ae15

File tree

5 files changed

+443
-39
lines changed

5 files changed

+443
-39
lines changed

packages/publish/README.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ hero.has(Targeting(goblin)) // True
207207
208208
#### Querying relations
209209
210-
Relations can be queried with specific targets, wildcard targets using `*` and even inverted wildcard searches with `Wildcard` to get all entities with a relation targeting another entity.
210+
Relations can be queried with specific targets and wildcard targets using `*`.
211211
212212
```js
213213
const gold = world.spawn()
@@ -217,11 +217,9 @@ const inventory = world.spawn(Contains(gold), Contains(silver))
217217
const targets = inventory.targetsFor(Contains) // Returns [gold, silver]
218218

219219
const chest = world.spawn(Contains(gold))
220-
const dwarf = world.spawn(Desires(gold))
221220

222-
const constainsSilver = world.query(Contains(silver)) // Returns [inventory]
221+
const containsSilver = world.query(Contains(silver)) // Returns [inventory]
223222
const containsAnything = world.query(Contains('*')) // Returns [inventory, chest]
224-
const relatesToGold = world.query(Wildcard(gold)) // Returns [inventory, chest, dwarf]
225223
```
226224
227225
#### Removing relations
@@ -253,6 +251,41 @@ player.has(apple) // false
253251
player.has(banana) // false
254252
```
255253
254+
#### Tracking relation changes
255+
256+
Relations work with tracking modifiers to detect when entities gain, lose, or update relations. Changes can only be tracked on relations that have a store.
257+
258+
> 👉 **Note**<br>
259+
> You can currently only track changes to all relations of a given type, such as `ChildOf`, but not specific relation pairs, such as `ChildOf(parent)`.
260+
261+
```js
262+
import { createAdded, createRemoved, createChanged } from 'koota'
263+
264+
const Added = createAdded()
265+
const Removed = createRemoved()
266+
const Changed = createChanged()
267+
268+
const ChildOf = relation({ store: { priority: 0 } })
269+
270+
// Track when any entity adds the ChildOf relation
271+
const newChildren = world.query(Added(ChildOf))
272+
273+
// Track when any entity removes the ChildOf relation
274+
const orphaned = world.query(Removed(ChildOf))
275+
276+
// Track when relation data changes for any target
277+
const updated = world.query(Changed(ChildOf))
278+
```
279+
280+
Combine with relation filters to track changes for specific targets.
281+
282+
```js
283+
const parent = world.spawn()
284+
285+
// Track changes only for entities related to parent
286+
const changedChildren = world.query(Changed(ChildOf), ChildOf(parent))
287+
```
288+
256289
### Query modifiers
257290
258291
Modifiers are used to filter query results enabling powerful patterns. All modifiers can be mixed together.
@@ -279,46 +312,55 @@ const movingOrVisible = world.query(Or(Velocity, Renderable))
279312
280313
#### Added
281314
282-
The `Added` modifier tracks all entities that have added the specified traits since the last time the query was run. A new instance of the modifier must be created for tracking to be unique.
315+
The `Added` modifier tracks all entities that have added the specified traits or relations since the last time the query was run. A new instance of the modifier must be created for tracking to be unique.
283316
284317
```js
285318
import { createAdded } from 'koota'
286319

287320
const Added = createAdded()
288321

289-
// This query will return entities that have just added the Position trait
322+
// Track entities that added the Position trait
290323
const newPositions = world.query(Added(Position))
291324

325+
// Track entities that added a ChildOf relation
326+
const newChildren = world.query(Added(ChildOf))
327+
292328
// After running the query, the Added modifier is reset
293329
```
294330
295331
#### Removed
296332
297-
The `Removed` modifier tracks all entities that have removed the specified traits since the last time the query was run. This includes entities that have been destroyed. A new instance of the modifier must be created for tracking to be unique.
333+
The `Removed` modifier tracks all entities that have removed the specified traits or relations since the last time the query was run. This includes entities that have been destroyed. A new instance of the modifier must be created for tracking to be unique.
298334
299335
```js
300336
import { createRemoved } from 'koota'
301337

302338
const Removed = createRemoved()
303339

304-
// This query will return entities that have just removed the Velocity trait
340+
// Track entities that removed the Velocity trait
305341
const stoppedEntities = world.query(Removed(Velocity))
306342

343+
// Track entities that removed a ChildOf relation
344+
const orphaned = world.query(Removed(ChildOf))
345+
307346
// After running the query, the Removed modifier is reset
308347
```
309348
310349
#### Changed
311350
312-
The `Changed` modifier tracks all entities that have had the specified traits values change since the last time the query was run. A new instance of the modifier must be created for tracking to be unique.
351+
The `Changed` modifier tracks all entities that have had the specified traits or relation stores change since the last time the query was run. A new instance of the modifier must be created for tracking to be unique.
313352
314353
```js
315354
import { createChanged } from 'koota'
316355

317356
const Changed = createChanged()
318357

319-
// This query will return entities whose Position has changed
358+
// Track entities whose Position has changed
320359
const movedEntities = world.query(Changed(Position))
321360

361+
// Track entities whose ChildOf relation data has changed
362+
const updatedChildren = world.query(Changed(ChildOf))
363+
322364
// After running the query, the Changed modifier is reset
323365
```
324366

packages/publish/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "koota",
3-
"version": "0.5.3",
3+
"version": "0.6.0",
44
"description": "🌎 Performant real-time state management for React and TypeScript",
55
"license": "ISC",
66
"type": "module",

packages/publish/tests/core/entity.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeEach, describe, expect, expectTypeOf, it } from 'vitest';
2-
import { createWorld, type Entity, getStore, trait, unpackEntity } from '../../dist';
2+
import { $internal, createWorld, type Entity, getStore, trait, unpackEntity } from '../../dist';
33

44
const Foo = trait();
55
const Bar = trait({ value: 0 });
@@ -200,4 +200,16 @@ describe('Entity', () => {
200200
const entity2 = world.spawn();
201201
expect(entity2.generation()).toBe(1);
202202
});
203+
204+
it('can check if entity exists in world', () => {
205+
const entity = world.spawn();
206+
expect(world.has(entity)).toBe(true);
207+
208+
entity.destroy();
209+
expect(world.has(entity)).toBe(false);
210+
211+
// Should work with world entities as well
212+
const worldEntity = world[$internal].worldEntity;
213+
expect(world.has(worldEntity)).toBe(true);
214+
});
203215
});

0 commit comments

Comments
 (0)