You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
AdLike2048/assets/script/battle/combat/BattleBuffMoveSpeedHost.ts

121 lines
4.4 KiB

import type { EntityId } from 'db://assets/mx/module/ecs';
import {
AttributeComp,
BattleEventType,
BattleTeamId,
MovementComp,
TeamComp,
type BattleEvent,
} from 'db://assets/mx/module/battle';
import { BattleBuffCarryStore } from '../../manager/battleBuff/BattleBuffCarryStore';
import { resolveCarriedEnemyMarchSlowWan } from '../../manager/battleBuff/BattleBuffPassiveSkillUtil';
import type { ProjectBattleHandle } from '../runtime/BattleRuntime';
import { AI_PLAYER_ID } from '../ai/runtime/AiCampArea';
/** 被动行军速度 modifier 分组 */
const MOVE_SPEED_MOD_GROUP = 'battle_buff_enemy_march_slow';
/** 单玩家:减速攻向其基地的敌军 */
type PlayerApproachSlow = {
/** 防守方 playerId(基地被攻方) */
defenderPlayerId: number;
/** 减速万分比 */
slowWan: number;
};
/**
* 被动 functiontype=13 滞行牌:减速攻向携带方基地的敌方怪物。
*/
export class BattleBuffMoveSpeedHost {
private _battle: ProjectBattleHandle | null = null;
private readonly _slows: PlayerApproachSlow[] = [];
private _onUnitSpawnedBound = false;
/** 绑定本局并监听出怪 */
bind(battle: ProjectBattleHandle): void {
this.unbind();
this._battle = battle;
this._slows.length = 0;
this._pushSlow(battle.localPlayerId);
this._pushSlow(AI_PLAYER_ID);
if (this._slows.length <= 0) return;
battle.session.events.on(BattleEventType.UnitSpawned, this._onUnitSpawned);
this._onUnitSpawnedBound = true;
battle.session.world.forEachAlive((entityId) => {
this._tryApply(entityId);
});
}
/** 解除绑定 */
unbind(): void {
const battle = this._battle;
if (battle && this._onUnitSpawnedBound) {
battle.session.events.off(BattleEventType.UnitSpawned, this._onUnitSpawned);
}
this._onUnitSpawnedBound = false;
this._battle = null;
this._slows.length = 0;
}
/**
* 携带方滞行牌 → 减速攻向该方基地的敌军。
* @param carrierPlayerId 携带方
*/
private _pushSlow(carrierPlayerId: number): void {
const adjust = resolveCarriedEnemyMarchSlowWan(BattleBuffCarryStore.getPassives(carrierPlayerId));
if (!adjust.active || adjust.slowWan <= 0) return;
for (let i = 0; i < this._slows.length; i++) {
if (this._slows[i].defenderPlayerId !== carrierPlayerId) continue;
this._slows[i].slowWan += adjust.slowWan;
return;
}
this._slows.push({ defenderPlayerId: carrierPlayerId, slowWan: adjust.slowWan });
}
private _onUnitSpawned = (event: BattleEvent): void => {
if (event.type !== BattleEventType.UnitSpawned) return;
this._tryApply(event.entity);
};
private _tryApply(entityId: EntityId): void {
const battle = this._battle;
if (!battle || this._slows.length <= 0) return;
const team = battle.session.world.getComponent(entityId, TeamComp);
if (!team || !BattleTeamId.isEnemyTeam(team.team)) return;
const defenderId = Math.max(0, Math.floor(Number(team.ownerId) || 0));
for (let i = 0; i < this._slows.length; i++) {
if (this._slows[i].defenderPlayerId !== defenderId) continue;
this._applyMarchSlow(battle, entityId, this._slows[i].slowWan);
return;
}
}
private _applyMarchSlow(battle: ProjectBattleHandle, entityId: EntityId, slowWan: number): void {
const clamped = Math.min(10000, Math.max(0, slowWan));
const mul = 1 - clamped / 10000;
const world = battle.session.world;
if (!world.getComponent(entityId, AttributeComp)) {
world.addComponent(entityId, new AttributeComp());
}
battle.session.attributes.addModifier(entityId, {
attr: 'move_speed_mul',
op: 'Mul',
value: mul,
source: `${MOVE_SPEED_MOD_GROUP}_${clamped}`,
group: MOVE_SPEED_MOD_GROUP,
});
const move = world.getComponent(entityId, MovementComp);
if (move) {
move.speed = Math.max(0, (move.baseSpeed || move.speed) * mul);
}
}
}
let _host: BattleBuffMoveSpeedHost | null = null;
/** 全局敌军来攻移速被动 Host(单局复用) */
export function getBattleBuffMoveSpeedHost(): BattleBuffMoveSpeedHost {
if (!_host) _host = new BattleBuffMoveSpeedHost();
return _host;
}