-
Notifications
You must be signed in to change notification settings - Fork 4
/
Entity.ts
222 lines (196 loc) · 6.79 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
import {Component} from './Component';
import {IRnObject, RnObject} from './RnObject';
import {ComponentTID, EntityUID} from '../../types/CommonTypes';
import {Is} from '../misc/Is';
import {BlendShapeComponent} from '../components/BlendShape/BlendShapeComponent';
import {WellKnownComponentTIDs} from '../components/WellKnownComponentTIDs';
import {CameraControllerComponent} from '../components/CameraController/CameraControllerComponent';
import {LightComponent} from '../components/Light/LightComponent';
import {MeshComponent} from '../components/Mesh/MeshComponent';
import {MeshRendererComponent} from '../components/MeshRenderer/MeshRendererComponent';
import {PhysicsComponent} from '../components/Physics/PhysicsComponent';
import {SceneGraphComponent} from '../components/SceneGraph/SceneGraphComponent';
import {SkeletalComponent} from '../components/Skeletal/SkeletalComponent';
import {TransformComponent} from '../components/Transform/TransformComponent';
import {AnimationComponent} from '../components/Animation/AnimationComponent';
import {CameraComponent} from '../components/Camera/CameraComponent';
/**
* The Interface for an Entity.
*/
export interface IEntity extends IRnObject {
entityUID: EntityUID;
_isAlive: boolean;
getComponent(componentType: typeof Component): Component | undefined;
getComponentByComponentTID(componentTID: ComponentTID): Component | undefined;
_setComponent(componentType: typeof Component, com: Component): void;
hasComponent(componentType: typeof Component): boolean;
_removeComponent(componentTID: ComponentTID): void;
tryToGetBlendShape(): BlendShapeComponent | undefined;
tryToGetCamera(): CameraComponent | undefined;
tryToGetCameraController(): CameraControllerComponent | undefined;
tryToGetLight(): LightComponent | undefined;
tryToGetMesh(): MeshComponent | undefined;
tryToGetMeshRenderer(): MeshRendererComponent | undefined;
tryToGetPhysics(): PhysicsComponent | undefined;
tryToGetSceneGraph(): SceneGraphComponent | undefined;
tryToGetSkeletal(): SkeletalComponent | undefined;
tryToGetTransform(): TransformComponent | undefined;
tryToGetAnimation(): AnimationComponent | undefined;
destroy(): void;
}
/**
* The class that represents an entity.
*
* @remarks
* The Rhodonite Entity Class which are an entities that exists in space.
* Entities can acquire various functions by having components on themselves.
*/
export class Entity extends RnObject implements IEntity {
/** The Unique ID of Entity */
private readonly __entity_uid: number;
/** The Map of components. All components must be managed in this map */
protected __components: Map<ComponentTID, Component>; // index is ComponentTID
/** Invalid Entity UID constant value */
static readonly invalidEntityUID = -1;
/** No use yet */
_isAlive: boolean;
/**
* The constructor of the Entity class.
*
* @remarks
* When creating an Entity, use the createEntity method of the EntityRepository class
* instead of directly calling this constructor.
*
* @param entityUID - The unique ID of the entity
* @param isAlive - Whether this entity alive or not
* @param entityComponent - The instance of EntityComponent (Dependency Injection)
*/
constructor(
entityUID: EntityUID,
isAlive: boolean,
components?: Map<ComponentTID, Component>
) {
super();
this.__entity_uid = entityUID;
this._isAlive = isAlive;
this.__components = Is.exist(components) ? components : new Map();
}
/**
* Get Unique ID of the entity.
*/
get entityUID() {
return this.__entity_uid;
}
/**
* Sets a component to this entity.
* @param component The component to set.
*
* @internal
*/
_setComponent(componentType: typeof Component, component: Component): void {
this.__components.set(componentType.componentTID, component);
}
/**
* return whether this entity has the component or not
* @param componentType - The component to check
* @returns
*/
hasComponent(componentType: typeof Component): boolean {
return this.__components.has(componentType.componentTID);
}
/**
* Get the component of the specified type that the entity has
* @param componentType
*/
getComponent(componentType: typeof Component): Component | undefined {
return this.__components.get(componentType.componentTID);
}
/**
* Gets the component corresponding to the ComponentTID.
* @param componentTID - The ComponentTID to get the component.
*/
getComponentByComponentTID(
componentTID: ComponentTID
): Component | undefined {
return this.__components.get(componentTID);
}
/**
* @param componentTID
*
* @internal
*/
_removeComponent(componentTID: ComponentTID) {
this.__components.delete(componentTID);
}
///
/// tryToGet methods
///
/**
* try to get an Animation Component if the entity has it.
* @returns AnimationComponent | undefined
*/
tryToGetAnimation() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.AnimationComponentTID
) as AnimationComponent | undefined;
}
tryToGetBlendShape() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.BlendShapeComponentTID
) as BlendShapeComponent | undefined;
}
tryToGetCamera() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.CameraComponentTID
) as CameraComponent | undefined;
}
tryToGetCameraController() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.CameraControllerComponentTID
) as CameraControllerComponent | undefined;
}
tryToGetLight() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.LightComponentTID
) as LightComponent | undefined;
}
tryToGetMesh() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.MeshComponentTID
) as MeshComponent | undefined;
}
tryToGetMeshRenderer() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.MeshRendererComponentTID
) as MeshRendererComponent | undefined;
}
tryToGetPhysics() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.PhysicsComponentTID
) as PhysicsComponent | undefined;
}
tryToGetSceneGraph() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.SceneGraphComponentTID
) as SceneGraphComponent | undefined;
}
tryToGetSkeletal() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.SkeletalComponentTID
) as SkeletalComponent | undefined;
}
tryToGetTransform() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.TransformComponentTID
) as TransformComponent | undefined;
}
/**
* Mark the entity as destroyed
*/
destroy() {
this.__components.forEach(component => {
component._destroy();
});
this._isAlive = false;
}
}