123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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<any, Map<any, Unit>>;
- /**实体单元列表 */
- private units: Map<any, Unit>;
- /**类型实体单元列表 */
- private unitTypes: Map<_EUnitType, Map<any, Unit>>;
- constructor () {
- super();
- Tool.log("生成实体单元管理器");
- this.units = new Map<any, Unit>();
- this.unitTypes = new Map<_EUnitType, Map<any, Unit>>();
-
- 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<any, Unit>, 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<any, Unit> = this.unitTypes.get(createUnit.type) || new Map<any, Unit>();
- 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<any, Unit> = this.updateComponents.get(unit.createUnit.type) || new Map<any, Unit>();
- 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<any, Unit> = new Map<any, Unit>();
- 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();
- }
- }
|