MCPcopy Create free account
hub / github.com/esengine/esengine / EntityRef

Function EntityRef

packages/core/src/ECS/Decorators/EntityRefDecorator.ts:57–119  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55 * ```
56 */
57export function EntityRef(): PropertyDecorator {
58 return function (target: any, propertyKey: string | symbol) {
59 const constructor = target.constructor;
60
61 let metadata: EntityRefMetadata = constructor[ENTITY_REF_METADATA];
62 if (!metadata) {
63 metadata = {
64 properties: new Set()
65 };
66 constructor[ENTITY_REF_METADATA] = metadata;
67 }
68
69 const propKeyString = typeof propertyKey === 'symbol' ? propertyKey.toString() : propertyKey;
70 metadata.properties.add(propKeyString);
71
72 Object.defineProperty(target, propertyKey, {
73 get: function (this: Component) {
74 const valueMap = getValueMap(this);
75 return valueMap.get(propKeyString) || null;
76 },
77 set: function (this: Component, newValue: Entity | null) {
78 const valueMap = getValueMap(this);
79 const oldValue = valueMap.get(propKeyString) || null;
80
81 if (oldValue === newValue) {
82 return;
83 }
84
85 const scene = this.entityId !== null ? getSceneByEntityId(this.entityId) : null;
86
87 if (!scene || !scene.referenceTracker) {
88 valueMap.set(propKeyString, newValue);
89 return;
90 }
91
92 const tracker = scene.referenceTracker;
93
94 if (oldValue) {
95 tracker.unregisterReference(oldValue, this, propKeyString);
96 }
97
98 if (newValue) {
99 if (newValue.scene !== scene) {
100 logger.error(`Cannot reference Entity from different Scene. Entity: ${newValue.name}, Scene: ${newValue.scene?.name || 'null'}`);
101 return;
102 }
103
104 if (newValue.isDestroyed) {
105 logger.warn(`Cannot reference destroyed Entity: ${newValue.name}`);
106 valueMap.set(propKeyString, null);
107 return;
108 }
109
110 tracker.registerReference(newValue, this, propKeyString);
111 }
112
113 valueMap.set(propKeyString, newValue);
114 },

Callers

nothing calls this directly

Calls 10

getSceneByEntityIdFunction · 0.90
getValueMapFunction · 0.85
unregisterReferenceMethod · 0.80
registerReferenceMethod · 0.80
getMethod · 0.65
setMethod · 0.65
toStringMethod · 0.45
addMethod · 0.45
errorMethod · 0.45
warnMethod · 0.45

Tested by

no test coverage detected