import EventManager from "../../Engine/Event/EventManager"; import Tool from "../../Engine/Tool/Tool"; import { UnitClass } from "../Game/Data/Unit/UnitClass"; import { _EUnitType, _ICreateUnit, _IGetUnit } from "../Game/Data/Unit/UnitConfig"; import GameEventName from "../Event/GameEventName"; import Unit from "./Unit"; import BasicManager from "../../Engine/Basic/BasicManager"; /** * 实体单元管理器 * @param 设计目的 管理游戏内所有实体单元 * @param 可能问题 实体单元创建、销毁、同步错误 */ export default class UnitManager extends BasicManager { protected updateComponents: Map>; /**实体单元列表 */ private units: Map; /**类型实体单元列表 */ private unitTypes: Map<_EUnitType, Map>; constructor () { super(); Tool.log("生成实体单元管理器"); this.units = new Map(); this.unitTypes = new Map<_EUnitType, Map>(); EventManager.onEvent(GameEventName.Time.Time_ConstFrame, this, this._update); EventManager.onEvent(GameEventName.Unit.Unit_CreateUnit, this, this.onCreateUnit); EventManager.onEvent(GameEventName.Unit.Unit_CreateUnitComplete, this, this.onCreateUnitEnd, 1); EventManager.onEvent(GameEventName.Unit.Unit_GetUnit, this, this.getUnitByunitID); EventManager.onEvent(GameEventName.Unit.Unit_RemoveUnit, this, this.onRemoveUnit); EventManager.onEvent(GameEventName.Game_EndGame, this, this.onEndGame); } private onEndGame (): void { this.updateComponents.forEach((values: Map, i: any) => { let lenght = values.size; values.forEach((value: Unit, j: any) => { if (value.createUnit.isDestroy == true) { values.delete(j); } }); if (lenght != values.size) { if (values.size > 0) { this.updateComponents.set(i, values); } else { this.updateComponents.delete(i); } } }); } /** * 创建实体单元 * @param 设计目的 添加实体单元 * @param 可能问题 实体单元创建、同步错误 */ private onCreateUnit (createUnit: _ICreateUnit): void { let list: Map = this.unitTypes.get(createUnit.type) || new Map(); if (list != null && list.size > 0) { let temp: Unit; list.forEach((value: Unit, key: any) => { if (value.fgui.visible == false && value.isEnabled == false && temp == null) { temp = value; temp.isEnabled = true; EventManager.sendEventByTargetID(GameEventName.Unit.Unit_ReuseUnit, temp.id, createUnit); return; } }); if (temp != null) { return; } } let unit: Unit = new UnitClass[createUnit.type](createUnit); this.units.set(unit.id, unit); list.set(unit.id, unit); this.unitTypes.set(unit.type, list); } /**创建实体单元完成 */ private onCreateUnitEnd (unit: Unit): void { if (unit.isUpdate == true) { let updateComponents: Map = this.updateComponents.get(unit.createUnit.type) || new Map(); updateComponents.set(unit.id, unit); this.updateComponents.set(unit.createUnit.type, updateComponents); } } /** * 删除实体单元 * @param 设计目的 删除实体单元 * @param 可能问题 实体单元销毁错误 */ private onRemoveUnit (unitID: any): void { if (unitID == null) { this.units.forEach((value: Unit, key: any) => { if (value.createUnit.isDestroy == true) { this.onRemoveUnit(key); } }); } else { if (this.units.has(unitID) == true) { let unit = this.units.get(unitID); this.units.delete(unitID); this.unitTypes.get(unit.createUnit.type).delete(unitID); this.updateComponents.delete(unitID); let node = unit.fgui; unit._destroy(); node.removeFromParent(); } } } /** * 获得实体单元 * @param 设计目的 获得实体单元 * @param 可能问题 对象已被销毁 */ private getUnitByunitID (getUnit: _IGetUnit): void { if (getUnit.unitID != null) { let unit: Unit = this.units.get(getUnit.unitID); if (unit != null) { getUnit.callback(unit); } } else if (getUnit.teamID != null) { let units: Map = new Map(); this.units.forEach((value: Unit, _key: any) => { if (getUnit.teamID.includes(value.createUnit.teamID) == true) { if (getUnit.type != null) { if (getUnit.type == value.createUnit.type) { units.set(value.id, value); } } else { units.set(value.id, value); } } }); getUnit.callback(units); } else { getUnit.callback(this.units); } } _destroy () { Tool.log("销毁实体单元管理器"); this.units.forEach((value: Unit, key: any) => { value._destroy(); }); this.units = null; this.unitTypes = null; super._destroy(); } }