-
Notifications
You must be signed in to change notification settings - Fork 2
/
Entity.ts
283 lines (228 loc) · 6.2 KB
/
Entity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { v4 as uuidv4 } from "uuid";
import World, { ClassConstructor } from "./World";
import ComponentCollection from "./ComponentCollection";
import { Tag } from "./Tag";
import DevEntity from "./DevEntity";
import SimpleFSM from "./SimpleFSM";
export type EntityId = string;
export type EntityState =
| "creating"
| "created"
| "destroying"
| "destroyed"
| "error";
export interface EntityCompEventArgs<CT> {
world: World<CT>;
component: CT;
}
export default class Entity<CT> {
private _id: string;
private _world: World<CT>;
private _error: Error | null;
private _state: SimpleFSM<EntityState, EntityState>;
get id(): string {
return this._id;
}
get world(): World<CT> {
return this._world;
}
get state(): EntityState {
return this._state.current;
}
constructor(world: World<CT>) {
this._id = uuidv4();
this._world = world;
this._error = null;
const fsmTransition = (
ns: EntityState
): EntityState => {
if (ns === "error" || this._error) return "error";
return ns;
};
this._state = new SimpleFSM<EntityState, EntityState>("creating", {
creating: fsmTransition,
created: fsmTransition,
destroying: fsmTransition,
destroyed: () => "destroyed",
error: () => "error",
});
/*
Registering with the World.
*/
this._world.registerEntity(this);
if (this._world.systems.compNamesBySystemName.size === 0) {
this._state.next('created');
}
}
/* LifeCycle methods, meant to be overridden */
onCreate(world: World<CT>): void {
// abstract
}
onDestroy(world: World<CT>): void {
// abstract
}
onComponentAdd(args: EntityCompEventArgs<CT>): void {
// abstract
}
onTrackedComponentUpdate(args: EntityCompEventArgs<CT>): void {
// abstract
}
onComponentRemove(args: EntityCompEventArgs<CT>): void {
// abstract
}
/**
* Add a component to an Entity, doh.
*/
add<T extends CT>(component: T): this {
this._world.add(this._id, component);
return this;
}
/**
* Add a tag to a component
*/
addTag(tag: Tag): this {
const entitySet = this._world.entitiesByTags.has(tag)
? this._world.entitiesByTags.get(tag)
: new Set<EntityId>();
if (entitySet) {
entitySet.add(this._id);
this._world.entitiesByTags.set(tag, entitySet);
}
return this;
}
/**
* Determines if an entity has a component related to it.
*/
has<T extends CT>(cType: ClassConstructor<T>): boolean {
const cc =
this._world.componentCollections.get(this._id) ||
new ComponentCollection<CT>();
return cc.has(cType);
}
/**
* Check to see if an entity tagged with a given tag.
*/
hasTag(tag: Tag): boolean {
if (this._world.entitiesByTags.has(tag)) {
const entitySet = this._world.entitiesByTags.get(tag);
if (entitySet) {
return entitySet.has(this._id);
}
}
return false;
}
/**
* Get a component that belongs to an entity.
*/
get<T extends CT>(cl: ClassConstructor<T>): T {
const cc =
this._world.componentCollections.get(this._id) ||
new ComponentCollection<CT>();
const component = cc.get<T>(cl);
return component;
}
/**
* Get all components that have been added to an entity, via a ComponentCollection
*/
getAll(): ComponentCollection<CT> {
return (
this._world.componentCollections.get(this._id) ||
new ComponentCollection<CT>()
);
}
/**
* Remove a component from an entity.
* @param cType A component class, eg MyComponent
*/
remove(cType: ClassConstructor<CT>): this {
this._world.remove(this._id, cType);
return this;
}
/**
* Remove a tag from an entity
*/
removeTag(tag: Tag): this {
if (this._world.entitiesByTags.has(tag)) {
const entitySet = this._world.entitiesByTags.get(tag);
if (entitySet) {
entitySet.delete(this._id);
if (entitySet.size === 0) {
this._world.entitiesByTags.delete(tag);
}
}
}
return this;
}
/** Clears all components from an Entity */
clear(): this {
this._world.clearEntityComponents(this._id);
return this;
}
/**
* Remove all tags on an entity
*/
clearTags(): this {
for (const [tag, entitySet] of this._world.entitiesByTags.entries()) {
entitySet.delete(this._id);
if (entitySet.size === 0) {
this._world.entitiesByTags.delete(tag);
}
}
return this;
}
/**
* Sets the state of the entity to 'created'. that's it.
*/
finishCreation(): void {
this._state.next('created');
}
/**
* Destroy an entity. Actual destruction is deferred until after the next pass of systems.
* This gives the systems a chance to do any cleanup that might be needed.
*/
destroy(): void {
// If no systems are added, the destroy immediately.
if (this._world.systems.compNamesBySystemName.size === 0) {
this.destroyImmediately();
return;
}
// Mark as "destroying" so that systems can act on it before actually being destroyed.
this._state.next('destroying');
}
destroyImmediately(): void {
// Right now calling before the actual destorying of the entity.
// Might want to change this to post destruction in the future, who knows.
this.onDestroy(this._world);
// Actually destroy entity.
this._world.destroyEntity(this._id); // should return an error??
this._state.next('destroyed');
}
/**
* Get all components that have been added to an entity, via a ComponentCollection.
* Does the same thing as entityInstance.getAll().
*/
get components(): ComponentCollection<CT> {
return (
this._world.componentCollections.get(this._id) ||
new ComponentCollection<CT>()
);
}
/**
* Retrieves all the tags that have been added to this entity.
*/
get tags(): Set<Tag> {
const tags = new Set<Tag>();
for (const [tag, entitySet] of this._world.entitiesByTags.entries()) {
if (entitySet.has(this._id)) {
tags.add(tag);
}
}
return tags;
}
/**
* Convert Entity to a DevEntity. Very helpful in for debugging.
*/
toDevEntity(): DevEntity<CT> {
return new DevEntity<CT>(this, this._world);
}
}