import { createMembershipSet, membershipAdd } from '../../../mx/tools/MembershipSet'; import type { BattleSession } from 'db://assets/mx/module/battle'; import type { Player } from '../../game/Player'; import { entityHasActivePositiveBuff, forEachGridIndexForEntity, } from '../combat/TowerPositiveBuffState'; import type { MapManager } from '../map/legacy/MapManager'; import type { TextGrid } from './TextGrid'; /** 地图字块正面 buff 特效(怪_攻击buff):按实体同步到全部占格 */ export class CocosTowerPositiveBuffPresenter { private _activeGridIndices = createMembershipSet(); /** 复用 MembershipSet,sync 末尾与 _activeGridIndices 交换,避免每帧分配 */ private _nextActiveScratch = createMembershipSet(); sync( session: BattleSession, map: MapManager, player: Player | null, mapGrids: ReadonlyMap, ): void { const nextActive = this._nextActiveScratch; nextActive.clear(); if (player) { for (const entityId of player.deployedUnits) { if (!entityHasActivePositiveBuff(session, entityId)) continue; forEachGridIndexForEntity(entityId, session, map, player, (gridIndex) => { membershipAdd(nextActive, gridIndex); }); } } // 按当前 mapGrids 清扫:拆组合后字块可能换格复用,旧 gridIndex 清不到节点上的特效 for (const [gridIndex, grid] of mapGrids) { if (!nextActive.has(gridIndex)) { grid?.clearPositiveBuffFx?.(); } } for (const gridIndex of nextActive.keys()) { mapGrids.get(gridIndex)?.showPositiveBuffFx?.(); } this._nextActiveScratch = this._activeGridIndices; this._activeGridIndices = nextActive; } clearAll(mapGrids: ReadonlyMap): void { for (const gridIndex of this._activeGridIndices.keys()) { mapGrids.get(gridIndex)?.clearPositiveBuffFx?.(); } this._activeGridIndices.clear(); } unbind(mapGrids?: ReadonlyMap | null): void { if (mapGrids) this.clearAll(mapGrids); this._activeGridIndices.clear(); this._nextActiveScratch.clear(); } }