@latticexyz/recs 2.0.12-main-9be2bb86 → 2.0.12-main-e43c0938
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -13
- package/CHANGELOG.md +0 -1248
- package/src/Component.spec.ts +0 -275
- package/src/Component.ts +0 -531
- package/src/Entity.spec.ts +0 -45
- package/src/Entity.ts +0 -46
- package/src/Indexer.spec.ts +0 -288
- package/src/Indexer.ts +0 -71
- package/src/Performance.spec.ts +0 -153
- package/src/Query.spec.ts +0 -811
- package/src/Query.ts +0 -554
- package/src/System.spec.ts +0 -139
- package/src/System.ts +0 -150
- package/src/World.spec.ts +0 -79
- package/src/World.ts +0 -102
- package/src/constants.ts +0 -54
- package/src/deprecated/constants.ts +0 -9
- package/src/deprecated/createActionSystem.spec.ts +0 -501
- package/src/deprecated/createActionSystem.ts +0 -236
- package/src/deprecated/defineActionComponent.ts +0 -18
- package/src/deprecated/index.ts +0 -2
- package/src/deprecated/types.ts +0 -45
- package/src/deprecated/waitForActionCompletion.ts +0 -15
- package/src/deprecated/waitForComponentValueIn.ts +0 -38
- package/src/index.ts +0 -9
- package/src/types.ts +0 -260
- package/src/utils.ts +0 -68
package/src/Component.spec.ts
DELETED
@@ -1,275 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
defineComponent,
|
3
|
-
setComponent,
|
4
|
-
removeComponent,
|
5
|
-
getComponentValue,
|
6
|
-
hasComponent,
|
7
|
-
withValue,
|
8
|
-
componentValueEquals,
|
9
|
-
getEntitiesWithValue,
|
10
|
-
overridableComponent,
|
11
|
-
} from "./Component";
|
12
|
-
import { Type } from "./constants";
|
13
|
-
import { createEntity, getEntitySymbol } from "./Entity";
|
14
|
-
import { AnyComponent, Entity, World } from "./types";
|
15
|
-
import { createWorld } from "./World";
|
16
|
-
|
17
|
-
describe("Component", () => {
|
18
|
-
let world: World;
|
19
|
-
|
20
|
-
beforeEach(() => {
|
21
|
-
world = createWorld();
|
22
|
-
});
|
23
|
-
|
24
|
-
it("emit changes to its stream", () => {
|
25
|
-
const entity = createEntity(world);
|
26
|
-
const component = defineComponent(world, { x: Type.Number, y: Type.Number });
|
27
|
-
|
28
|
-
const mock = jest.fn();
|
29
|
-
component.update$.subscribe((update) => {
|
30
|
-
mock(update);
|
31
|
-
});
|
32
|
-
|
33
|
-
setComponent(component, entity, { x: 1, y: 2 });
|
34
|
-
setComponent(component, entity, { x: 7, y: 2 });
|
35
|
-
setComponent(component, entity, { x: 7, y: 2 });
|
36
|
-
removeComponent(component, entity);
|
37
|
-
|
38
|
-
expect(mock).toHaveBeenNthCalledWith(1, { entity, value: [{ x: 1, y: 2 }, undefined], component });
|
39
|
-
expect(mock).toHaveBeenNthCalledWith(2, {
|
40
|
-
entity,
|
41
|
-
component,
|
42
|
-
value: [
|
43
|
-
{ x: 7, y: 2 },
|
44
|
-
{ x: 1, y: 2 },
|
45
|
-
],
|
46
|
-
});
|
47
|
-
expect(mock).toHaveBeenNthCalledWith(3, {
|
48
|
-
entity,
|
49
|
-
component,
|
50
|
-
value: [
|
51
|
-
{ x: 7, y: 2 },
|
52
|
-
{ x: 7, y: 2 },
|
53
|
-
],
|
54
|
-
});
|
55
|
-
expect(mock).toHaveBeenNthCalledWith(4, { entity, component, value: [undefined, { x: 7, y: 2 }] });
|
56
|
-
});
|
57
|
-
|
58
|
-
describe("defineComponent", () => {
|
59
|
-
it("should register the component in the world", () => {
|
60
|
-
expect(world.components.length).toBe(0);
|
61
|
-
defineComponent(world, { value: Type.Boolean });
|
62
|
-
expect(world.components.length).toBe(1);
|
63
|
-
});
|
64
|
-
});
|
65
|
-
|
66
|
-
describe("setComponent", () => {
|
67
|
-
let component: AnyComponent;
|
68
|
-
let entity: Entity;
|
69
|
-
let value: number;
|
70
|
-
|
71
|
-
beforeEach(() => {
|
72
|
-
component = defineComponent(world, { value: Type.Number });
|
73
|
-
entity = createEntity(world);
|
74
|
-
value = 1;
|
75
|
-
setComponent(component, entity, { value });
|
76
|
-
});
|
77
|
-
|
78
|
-
it("should store the component value", () => {
|
79
|
-
expect(component.values.value.get(getEntitySymbol(entity))).toBe(value);
|
80
|
-
});
|
81
|
-
|
82
|
-
it("should store the entity", () => {
|
83
|
-
expect(hasComponent(component, entity)).toBe(true);
|
84
|
-
});
|
85
|
-
|
86
|
-
it.todo("should store the value array");
|
87
|
-
});
|
88
|
-
|
89
|
-
describe("removeComponent", () => {
|
90
|
-
let component: AnyComponent;
|
91
|
-
let entity: Entity;
|
92
|
-
let value: number;
|
93
|
-
|
94
|
-
beforeEach(() => {
|
95
|
-
component = defineComponent(world, { value: Type.Number });
|
96
|
-
entity = createEntity(world);
|
97
|
-
value = 1;
|
98
|
-
setComponent(component, entity, { value });
|
99
|
-
removeComponent(component, entity);
|
100
|
-
});
|
101
|
-
|
102
|
-
it("should remove the component value", () => {
|
103
|
-
expect(component.values.value.get(getEntitySymbol(entity))).toBe(undefined);
|
104
|
-
});
|
105
|
-
|
106
|
-
it("should remove the entity", () => {
|
107
|
-
expect(hasComponent(component, entity)).toBe(false);
|
108
|
-
});
|
109
|
-
|
110
|
-
// it("shouldremove the component from the entity's component set", () => {
|
111
|
-
// expect(world.entities.get(entity)?.has(component)).toBe(false);
|
112
|
-
// });
|
113
|
-
});
|
114
|
-
|
115
|
-
describe("hasComponent", () => {
|
116
|
-
it("should return true if the entity has the component", () => {
|
117
|
-
const component = defineComponent(world, { x: Type.Number, y: Type.Number });
|
118
|
-
const entity = createEntity(world);
|
119
|
-
const value = { x: 1, y: 2 };
|
120
|
-
setComponent(component, entity, value);
|
121
|
-
|
122
|
-
expect(hasComponent(component, entity)).toEqual(true);
|
123
|
-
});
|
124
|
-
});
|
125
|
-
|
126
|
-
describe("getComponentValue", () => {
|
127
|
-
it("should return the correct component value", () => {
|
128
|
-
const component = defineComponent(world, { x: Type.Number, y: Type.Number });
|
129
|
-
const entity = createEntity(world);
|
130
|
-
const value = { x: 1, y: 2 };
|
131
|
-
setComponent(component, entity, value);
|
132
|
-
|
133
|
-
const receivedValue = getComponentValue(component, entity);
|
134
|
-
expect(receivedValue).toEqual(value);
|
135
|
-
});
|
136
|
-
});
|
137
|
-
|
138
|
-
describe("getComponentValueStrict", () => {
|
139
|
-
it.todo("should return the correct component value");
|
140
|
-
it.todo("should error if the component value does not exist");
|
141
|
-
});
|
142
|
-
|
143
|
-
describe("componentValueEquals", () => {
|
144
|
-
const value1 = { x: 1, y: 2, z: "x" };
|
145
|
-
const value2 = { x: 1, y: 2, z: "x" };
|
146
|
-
const value3 = { x: "1", y: 2, z: "x" };
|
147
|
-
|
148
|
-
expect(componentValueEquals(value1, value2)).toBe(true);
|
149
|
-
expect(componentValueEquals(value2, value3)).toBe(false);
|
150
|
-
});
|
151
|
-
|
152
|
-
describe("withValue", () => {
|
153
|
-
it("should return a ComponentWithValue", () => {
|
154
|
-
const component = defineComponent(world, { x: Type.Number, y: Type.Number });
|
155
|
-
const value = { x: 1, y: 2 };
|
156
|
-
const componentWithValue = withValue(component, value);
|
157
|
-
expect(componentWithValue).toEqual([component, value]);
|
158
|
-
});
|
159
|
-
});
|
160
|
-
|
161
|
-
describe("getEntitiesWithValue", () => {
|
162
|
-
it("Should return all and only entities with this value", () => {
|
163
|
-
const Position = defineComponent(world, { x: Type.Number, y: Type.Number });
|
164
|
-
const entity1 = createEntity(world, [withValue(Position, { x: 1, y: 2 })]);
|
165
|
-
createEntity(world, [withValue(Position, { x: 2, y: 1 })]);
|
166
|
-
createEntity(world);
|
167
|
-
const entity4 = createEntity(world, [withValue(Position, { x: 1, y: 2 })]);
|
168
|
-
|
169
|
-
expect(getEntitiesWithValue(Position, { x: 1, y: 2 })).toEqual(new Set([entity1, entity4]));
|
170
|
-
});
|
171
|
-
});
|
172
|
-
|
173
|
-
describe("overridableComponent", () => {
|
174
|
-
it("should return a overridable component", () => {
|
175
|
-
const Position = defineComponent(world, { x: Type.Number, y: Type.Number });
|
176
|
-
const OverridablePosition = overridableComponent(Position);
|
177
|
-
expect("addOverride" in OverridablePosition).toBe(true);
|
178
|
-
expect("addOverride" in OverridablePosition).toBe(true);
|
179
|
-
});
|
180
|
-
|
181
|
-
it("should mirror all values of the source component", () => {
|
182
|
-
const Position = defineComponent(world, { x: Type.Number, y: Type.Number });
|
183
|
-
const entity1 = createEntity(world);
|
184
|
-
setComponent(Position, entity1, { x: 1, y: 2 });
|
185
|
-
|
186
|
-
const OverridablePosition = overridableComponent(Position);
|
187
|
-
expect(getComponentValue(OverridablePosition, entity1)).toEqual({ x: 1, y: 2 });
|
188
|
-
});
|
189
|
-
|
190
|
-
it("the overridable component should be updated if the original component is updated", () => {
|
191
|
-
const Position = defineComponent(world, { x: Type.Number, y: Type.Number });
|
192
|
-
const entity1 = createEntity(world);
|
193
|
-
setComponent(Position, entity1, { x: 1, y: 2 });
|
194
|
-
|
195
|
-
const OverridableComponent = overridableComponent(Position);
|
196
|
-
|
197
|
-
setComponent(Position, entity1, { x: 2, y: 2 });
|
198
|
-
expect(getComponentValue(OverridableComponent, entity1)).toEqual({ x: 2, y: 2 });
|
199
|
-
|
200
|
-
const entity2 = createEntity(world, [withValue(Position, { x: 3, y: 3 })]);
|
201
|
-
expect(getComponentValue(OverridableComponent, entity2)).toEqual({ x: 3, y: 3 });
|
202
|
-
});
|
203
|
-
|
204
|
-
it("should return the updated component value if there is a relevant update for the given entity", () => {
|
205
|
-
const Position = defineComponent(world, { x: Type.Number, y: Type.Number });
|
206
|
-
const entity1 = createEntity(world);
|
207
|
-
const entity2 = createEntity(world);
|
208
|
-
setComponent(Position, entity1, { x: 1, y: 2 });
|
209
|
-
setComponent(Position, entity2, { x: 5, y: 6 });
|
210
|
-
|
211
|
-
const OverridableComponent = overridableComponent(Position);
|
212
|
-
OverridableComponent.addOverride("firstOverride", { entity: entity1, value: { x: 2, y: 3 } });
|
213
|
-
expect(getComponentValue(OverridableComponent, entity1)).toEqual({ x: 2, y: 3 });
|
214
|
-
expect(getComponentValue(OverridableComponent, entity2)).toEqual({ x: 5, y: 6 });
|
215
|
-
|
216
|
-
OverridableComponent.addOverride("secondOverride", { entity: entity1, value: { x: 3, y: 3 } });
|
217
|
-
expect(getComponentValue(OverridableComponent, entity1)).toEqual({ x: 3, y: 3 });
|
218
|
-
|
219
|
-
OverridableComponent.removeOverride("secondOverride");
|
220
|
-
expect(getComponentValue(OverridableComponent, entity1)).toEqual({ x: 2, y: 3 });
|
221
|
-
|
222
|
-
setComponent(Position, entity1, { x: 10, y: 20 });
|
223
|
-
expect(getComponentValue(OverridableComponent, entity1)).toEqual({ x: 2, y: 3 });
|
224
|
-
|
225
|
-
OverridableComponent.removeOverride("firstOverride");
|
226
|
-
expect(getComponentValue(OverridableComponent, entity1)).toEqual({ x: 10, y: 20 });
|
227
|
-
});
|
228
|
-
|
229
|
-
it("adding an override should trigger reactions depending on the getComponentValue of the overriden component", () => {
|
230
|
-
const Position = defineComponent(world, { x: Type.Number, y: Type.Number });
|
231
|
-
const entity1 = createEntity(world);
|
232
|
-
setComponent(Position, entity1, { x: 1, y: 2 });
|
233
|
-
|
234
|
-
const OverridablePosition = overridableComponent(Position);
|
235
|
-
|
236
|
-
const spy = jest.fn();
|
237
|
-
OverridablePosition.update$.subscribe(spy);
|
238
|
-
|
239
|
-
expect(spy).toHaveBeenCalledTimes(0);
|
240
|
-
|
241
|
-
OverridablePosition.addOverride("firstOverride", { entity: entity1, value: { x: 3, y: 3 } });
|
242
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
243
|
-
expect(spy).toHaveBeenLastCalledWith({
|
244
|
-
entity: entity1,
|
245
|
-
component: OverridablePosition,
|
246
|
-
value: [
|
247
|
-
{ x: 3, y: 3 },
|
248
|
-
{ x: 1, y: 2 },
|
249
|
-
],
|
250
|
-
});
|
251
|
-
|
252
|
-
OverridablePosition.removeOverride("firstOverride");
|
253
|
-
expect(spy).toHaveBeenCalledTimes(2);
|
254
|
-
expect(spy).toHaveBeenLastCalledWith({
|
255
|
-
entity: entity1,
|
256
|
-
component: OverridablePosition,
|
257
|
-
value: [
|
258
|
-
{ x: 1, y: 2 },
|
259
|
-
{ x: 3, y: 3 },
|
260
|
-
],
|
261
|
-
});
|
262
|
-
|
263
|
-
OverridablePosition.addOverride("secondOverride", {
|
264
|
-
entity: "42" as Entity,
|
265
|
-
value: { x: 2, y: 3 },
|
266
|
-
});
|
267
|
-
expect(spy).toHaveBeenLastCalledWith({
|
268
|
-
entity: "42",
|
269
|
-
component: OverridablePosition,
|
270
|
-
value: [{ x: 2, y: 3 }, undefined],
|
271
|
-
});
|
272
|
-
expect(spy).toHaveBeenCalledTimes(3);
|
273
|
-
});
|
274
|
-
});
|
275
|
-
});
|