- 1 :
import { Vector3 } from '../../math/Vector3.js';
- 2 :
- 3 :
/**
- 4 :
* Class for representing the memory information about a single game entity.
- 5 :
*
- 6 :
* @author {@link https://github.com/Mugen87|Mugen87}
- 7 :
*/
- 8 :
class MemoryRecord {
- 9 :
- 10 :
/**
- 11 :
* Constructs a new memory record.
- 12 :
*
- 13 :
* @param {GameEntity} entity - The game entity that is represented by this memory record.
- 14 :
*/
- 15 :
constructor( entity = null ) {
- 16 :
- 17 :
/**
- 18 :
* The game entity that is represented by this memory record.
- 19 :
* @type {?GameEntity}
- 20 :
* @default null
- 21 :
*/
- 22 :
this.entity = entity;
- 23 :
- 24 :
/**
- 25 :
* Records the time the entity became visible. Useful in combination with a reaction time
- 26 :
* in order to prevent immediate actions.
- 27 :
* @type {Number}
- 28 :
* @default - Infinity
- 29 :
*/
- 30 :
this.timeBecameVisible = - Infinity;
- 31 :
- 32 :
/**
- 33 :
* Records the time the entity was last sensed (e.g. seen or heard). Used to determine
- 34 :
* if a game entity can "remember" this record or not.
- 35 :
* @type {Number}
- 36 :
* @default - Infinity
- 37 :
*/
- 38 :
this.timeLastSensed = - Infinity;
- 39 :
- 40 :
/**
- 41 :
* Marks the position where the opponent was last sensed.
- 42 :
* @type {Vector3}
- 43 :
*/
- 44 :
this.lastSensedPosition = new Vector3();
- 45 :
- 46 :
/**
- 47 :
* Whether this game entity is visible or not.
- 48 :
* @type {Boolean}
- 49 :
* @default false
- 50 :
*/
- 51 :
this.visible = false;
- 52 :
- 53 :
}
- 54 :
- 55 :
/**
- 56 :
* Transforms this instance into a JSON object.
- 57 :
*
- 58 :
* @return {Object} The JSON object.
- 59 :
*/
- 60 :
toJSON() {
- 61 :
- 62 :
return {
- 63 :
type: this.constructor.name,
- 64 :
entity: this.entity.uuid,
- 65 :
timeBecameVisible: this.timeBecameVisible.toString(),
- 66 :
timeLastSensed: this.timeLastSensed.toString(),
- 67 :
lastSensedPosition: this.lastSensedPosition.toArray( new Array() ),
- 68 :
visible: this.visible
- 69 :
};
- 70 :
- 71 :
}
- 72 :
- 73 :
/**
- 74 :
* Restores this instance from the given JSON object.
- 75 :
*
- 76 :
* @param {Object} json - The JSON object.
- 77 :
* @return {MemoryRecord} A reference to this memory record.
- 78 :
*/
- 79 :
fromJSON( json ) {
- 80 :
- 81 :
this.entity = json.entity; // uuid
- 82 :
this.timeBecameVisible = parseFloat( json.timeBecameVisible );
- 83 :
this.timeLastSensed = parseFloat( json.timeLastSensed );
- 84 :
this.lastSensedPosition.fromArray( json.lastSensedPosition );
- 85 :
this.visible = json.visible;
- 86 :
- 87 :
return this;
- 88 :
- 89 :
}
- 90 :
- 91 :
/**
- 92 :
* Restores UUIDs with references to GameEntity objects.
- 93 :
*
- 94 :
* @param {Map<String,GameEntity>} entities - Maps game entities to UUIDs.
- 95 :
* @return {MemoryRecord} A reference to this memory record.
- 96 :
*/
- 97 :
resolveReferences( entities ) {
- 98 :
- 99 :
this.entity = entities.get( this.entity ) || null;
- 100 :
- 101 :
return this;
- 102 :
- 103 :
}
- 104 :
- 105 :
}
- 106 :
- 107 :
export { MemoryRecord };