UnitManager.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import EventManager from "../../Engine/Event/EventManager";
  2. import Tool from "../../Engine/Tool/Tool";
  3. import { UnitClass } from "../Game/Data/Unit/UnitClass";
  4. import { _EUnitType, _ICreateUnit, _IGetUnit } from "../Game/Data/Unit/UnitConfig";
  5. import GameEventName from "../Event/GameEventName";
  6. import Unit from "./Unit";
  7. import BasicManager from "../../Engine/Basic/BasicManager";
  8. /**
  9. * 实体单元管理器
  10. * @param 设计目的 管理游戏内所有实体单元
  11. * @param 可能问题 实体单元创建、销毁、同步错误
  12. */
  13. export default class UnitManager extends BasicManager {
  14. protected updateComponents: Map<any, Map<any, Unit>>;
  15. /**实体单元列表 */
  16. private units: Map<any, Unit>;
  17. /**类型实体单元列表 */
  18. private unitTypes: Map<_EUnitType, Map<any, Unit>>;
  19. constructor () {
  20. super();
  21. Tool.log("生成实体单元管理器");
  22. this.units = new Map<any, Unit>();
  23. this.unitTypes = new Map<_EUnitType, Map<any, Unit>>();
  24. EventManager.onEvent(GameEventName.Time.Time_ConstFrame, this, this._update);
  25. EventManager.onEvent(GameEventName.Unit.Unit_CreateUnit, this, this.onCreateUnit);
  26. EventManager.onEvent(GameEventName.Unit.Unit_CreateUnitComplete, this, this.onCreateUnitEnd, 1);
  27. EventManager.onEvent(GameEventName.Unit.Unit_GetUnit, this, this.getUnitByunitID);
  28. EventManager.onEvent(GameEventName.Unit.Unit_RemoveUnit, this, this.onRemoveUnit);
  29. EventManager.onEvent(GameEventName.Game_EndGame, this, this.onEndGame);
  30. }
  31. private onEndGame (): void {
  32. this.updateComponents.forEach((values: Map<any, Unit>, i: any) => {
  33. let lenght = values.size;
  34. values.forEach((value: Unit, j: any) => {
  35. if (value.createUnit.isDestroy == true) {
  36. values.delete(j);
  37. }
  38. });
  39. if (lenght != values.size) {
  40. if (values.size > 0) {
  41. this.updateComponents.set(i, values);
  42. }
  43. else {
  44. this.updateComponents.delete(i);
  45. }
  46. }
  47. });
  48. }
  49. /**
  50. * 创建实体单元
  51. * @param 设计目的 添加实体单元
  52. * @param 可能问题 实体单元创建、同步错误
  53. */
  54. private onCreateUnit (createUnit: _ICreateUnit): void {
  55. let list: Map<any, Unit> = this.unitTypes.get(createUnit.type) || new Map<any, Unit>();
  56. if (list != null && list.size > 0) {
  57. let temp: Unit;
  58. list.forEach((value: Unit, key: any) => {
  59. if (value.fgui.visible == false && value.isEnabled == false && temp == null) {
  60. temp = value;
  61. temp.isEnabled = true;
  62. EventManager.sendEventByTargetID(GameEventName.Unit.Unit_ReuseUnit, temp.id, createUnit);
  63. return;
  64. }
  65. });
  66. if (temp != null) {
  67. return;
  68. }
  69. }
  70. let unit: Unit = new UnitClass[createUnit.type](createUnit);
  71. this.units.set(unit.id, unit);
  72. list.set(unit.id, unit);
  73. this.unitTypes.set(unit.type, list);
  74. }
  75. /**创建实体单元完成 */
  76. private onCreateUnitEnd (unit: Unit): void {
  77. if (unit.isUpdate == true) {
  78. let updateComponents: Map<any, Unit> = this.updateComponents.get(unit.createUnit.type) || new Map<any, Unit>();
  79. updateComponents.set(unit.id, unit);
  80. this.updateComponents.set(unit.createUnit.type, updateComponents);
  81. }
  82. }
  83. /**
  84. * 删除实体单元
  85. * @param 设计目的 删除实体单元
  86. * @param 可能问题 实体单元销毁错误
  87. */
  88. private onRemoveUnit (unitID: any): void {
  89. if (unitID == null) {
  90. this.units.forEach((value: Unit, key: any) => {
  91. if (value.createUnit.isDestroy == true) {
  92. this.onRemoveUnit(key);
  93. }
  94. });
  95. }
  96. else {
  97. if (this.units.has(unitID) == true) {
  98. let unit = this.units.get(unitID);
  99. this.units.delete(unitID);
  100. this.unitTypes.get(unit.createUnit.type).delete(unitID);
  101. this.updateComponents.delete(unitID);
  102. let node = unit.fgui;
  103. unit._destroy();
  104. node.removeFromParent();
  105. }
  106. }
  107. }
  108. /**
  109. * 获得实体单元
  110. * @param 设计目的 获得实体单元
  111. * @param 可能问题 对象已被销毁
  112. */
  113. private getUnitByunitID (getUnit: _IGetUnit): void {
  114. if (getUnit.unitID != null) {
  115. let unit: Unit = this.units.get(getUnit.unitID);
  116. if (unit != null) {
  117. getUnit.callback(unit);
  118. }
  119. }
  120. else if (getUnit.teamID != null) {
  121. let units: Map<any, Unit> = new Map<any, Unit>();
  122. this.units.forEach((value: Unit, _key: any) => {
  123. if (getUnit.teamID.includes(value.createUnit.teamID) == true) {
  124. if (getUnit.type != null) {
  125. if (getUnit.type == value.createUnit.type) {
  126. units.set(value.id, value);
  127. }
  128. }
  129. else {
  130. units.set(value.id, value);
  131. }
  132. }
  133. });
  134. getUnit.callback(units);
  135. }
  136. else {
  137. getUnit.callback(this.units);
  138. }
  139. }
  140. _destroy () {
  141. Tool.log("销毁实体单元管理器");
  142. this.units.forEach((value: Unit, key: any) => {
  143. value._destroy();
  144. });
  145. this.units = null;
  146. this.unitTypes = null;
  147. super._destroy();
  148. }
  149. }