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