refactor: combat system

This commit is contained in:
AncTe committed 2026-10-03 16:04:20 +08:00
1 parent d306d11bce
commit e25ef3fa93
9 files changed
+1319 -167

No files matched your search

+79 -59
View File
@@ -2,6 +2,7 @@ import { IRange, ITileLocator, logger } from '@motajs/common';
import {
IAuraConverter,
IAuraView,
IComputingEnemyView,
IDamageSystem,
IEnemyAuraView,
IEnemyCommonQueryEffect,
@@ -23,13 +24,15 @@ import {
} from '@user/data-base';
import { EnemyView } from './enemy';
import { ILocationIndexer, MapLocIndexer } from '@user/data-common';
import { isNil } from 'lodash-es';
export class EnemyContext<TEnemy, THero> implements IEnemyContext<
TEnemy,
THero
> {
/** 坐标索引 -> 怪物视图 */
private readonly enemyViewMap: Map<number, EnemyView<TEnemy>> = new Map();
private readonly enemyViewMap: Map<number, IComputingEnemyView<TEnemy>> =
new Map();
/** 坐标索引 -> 计算前怪物对象 */
private readonly enemyMap: Map<number, IEnemy<TEnemy>> = new Map();
/** 怪物视图 -> 坐标索引 */
@@ -40,7 +43,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
/** 计算后怪物对象 -> 怪物视图 */
private readonly computedToView: Map<
IReadonlyEnemy<TEnemy>,
EnemyView<TEnemy>
IComputingEnemyView<TEnemy>
> = new Map();
/** 当前已注册的光环转换器 */
@@ -203,29 +206,45 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
return this.bindedHero;
}
attachDamageSystem(system: IDamageSystem<TEnemy, THero> | null): void {
this.damageSystem = system;
if (system) {
system.bindHeroStatus(this.bindedHero);
}
}
getDamageSystem(): IDamageSystem<TEnemy, THero> | null {
return this.damageSystem;
}
attachMapDamage(damage: IMapDamage<TEnemy, THero> | null): void {
this.mapDamage = damage;
if (damage) {
damage.refreshAll();
}
}
getMapDamage(): IMapDamage<TEnemy, THero> | null {
return this.mapDamage;
}
addAura(aura: IAuraView<TEnemy>): void {
this.globalAuraList.add(aura);
this.needUpdate = true;
}
deleteAura(aura: IAuraView<TEnemy>): void {
this.globalAuraList.delete(aura);
this.needUpdate = true;
}
//#endregion
/**
* 创建可修改信息对象
* @param enemy 怪物对象
* @param locator 怪物位置
*/
private createHandler(
enemy: IEnemy<TEnemy>,
locator: ITileLocator
): IEnemyHandler<TEnemy, THero> {
return {
enemy,
context: this,
locator,
hero: this.bindedHero!,
state: this.state
};
}
//#region 怪物操作
getEnemyLocator(enemy: IEnemy<TEnemy>): Readonly<ITileLocator> | null {
const index = this.locatorEnemyMap.get(enemy);
if (index === undefined) return null;
if (isNil(index)) return null;
return this.indexer.locator(index);
}
@@ -233,7 +252,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
view: IEnemyView<TEnemy>
): Readonly<ITileLocator> | null {
const index = this.locatorViewMap.get(view);
if (index === undefined) return null;
if (isNil(index)) return null;
return this.indexer.locator(index);
}
@@ -315,17 +334,15 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
private *internalScanRange<T>(
range: IRange<T>,
param: T
): Iterable<[ITileLocator, EnemyView<TEnemy>]> {
): Iterable<[ITileLocator, IComputingEnemyView<TEnemy>]> {
range.bindHost(this);
const keys = new Set(this.enemyViewMap.keys());
const matched = range.autoDetect(keys, param);
const viewMap = this.enemyViewMap;
for (const index of matched) {
const view = viewMap.get(index);
if (view) {
const locator = this.indexer.locator(index);
yield [locator, view];
}
const view = viewMap.get(index)!;
const locator = this.indexer.locator(index);
yield [locator, view];
}
}
@@ -343,36 +360,26 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
}
}
addAura(aura: IAuraView<TEnemy>): void {
this.globalAuraList.add(aura);
this.needUpdate = true;
}
//#endregion
deleteAura(aura: IAuraView<TEnemy>): void {
this.globalAuraList.delete(aura);
this.needUpdate = true;
}
//#region 上下文构建
attachMapDamage(damage: IMapDamage<TEnemy, THero> | null): void {
this.mapDamage = damage;
if (damage) {
damage.refreshAll();
}
}
getMapDamage(): IMapDamage<TEnemy, THero> | null {
return this.mapDamage;
}
attachDamageSystem(system: IDamageSystem<TEnemy, THero> | null): void {
this.damageSystem = system;
if (system) {
system.bindHeroStatus(this.bindedHero);
}
}
getDamageSystem(): IDamageSystem<TEnemy, THero> | null {
return this.damageSystem;
/**
* 创建可修改信息对象
* @param enemy 怪物对象
* @param locator 怪物位置
*/
private createHandler(
enemy: IEnemy<TEnemy>,
locator: ITileLocator
): IEnemyHandler<TEnemy, THero> {
return {
enemy,
context: this,
locator,
hero: this.bindedHero!,
state: this.state
};
}
/**
@@ -389,6 +396,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
for (const converter of this.auraConverter) {
if (!this.converterStatus.get(converter)) continue;
if (converter.shouldConvert(special, handler)) {
// 一个特殊属性只能对应一个转换器
if (matched) {
logger.warn(97, special.code.toString());
return null;
@@ -566,10 +574,12 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
* 构建所有由特殊属性衍生出的光环与特殊查询结果
*/
private buildupSpecials(): void {
// 首先把全局光环(无来源光环)加到排序列表中
for (const aura of this.globalAuraList) {
this.insertIntoSortedAura(aura);
}
// 进行第一轮的特殊属性转换,生成可能的光环视图
for (const [index, view] of this.enemyViewMap) {
const enemy = view.getComputingEnemy();
const locator = this.indexer.locator(index);
@@ -583,27 +593,31 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
}
}
// 其实大多数情况下并不存在光环,直接返回
if (this.sortedAura.size === 0) return;
const processedPriorities = new Set<number>();
// 由于期间可能会产生新优先级的光环,所以要用 while (true) 而不是直接遍历
// 直接使用 forEach 的话,会无法遍历到临时新增的那些优先级
while (true) {
let maxPriority: number | null = null;
let maxPriority: number | undefined = undefined;
for (const priority of this.sortedAura.keys()) {
if (!processedPriorities.has(priority)) {
if (maxPriority === null || priority > maxPriority) {
if (isNil(maxPriority) || priority > maxPriority) {
maxPriority = priority;
}
}
}
for (const priority of this.specialQueryEffects.keys()) {
if (!processedPriorities.has(priority)) {
if (maxPriority === null || priority > maxPriority) {
if (isNil(maxPriority) || priority > maxPriority) {
maxPriority = priority;
}
}
}
if (maxPriority === null) break;
if (isNil(maxPriority)) break;
processedPriorities.add(maxPriority);
const auras = this.sortedAura.get(maxPriority);
@@ -723,6 +737,10 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
}
}
//#endregion
//#region 局部刷新
markDirty(view: IEnemyView<TEnemy>): void {
if (!this.locatorViewMap.has(view)) return;
this.dirtyEnemy.add(view);
@@ -898,6 +916,8 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
}
}
//#endregion
clear(): void {
this.enemyViewMap.clear();
this.enemyMap.clear();
+50 -48
View File
@@ -19,6 +19,8 @@ import {
IStateBase
} from '@user/data-base';
// TODO: 与不在地图上的怪物战斗
interface ICriticalSearchResult {
/** 此临界点的属性值 */
readonly value: number;
@@ -131,6 +133,54 @@ export class DamageContext<TEnemy, THero> implements IDamageContext<
};
}
/**
* 计算下一个临界点
* @param handler 信息对象,其中的 `hero` 成员与 `hero` 参数同引用
* @param hero 可修改勇士属性对象,与 `handler` 中的 `hero` 成员同引用
* @param attribute 勇士属性名
* @param currentValue 当前勇士属性值
* @param upperLimit 二分上界
* @param referenceDamage 参考伤害值
* @param maxIterations 最大迭代数量
*/
private findNextCritical(
handler: IReadonlyEnemyHandler<TEnemy, THero>,
hero: IHeroAttribute<THero>,
attribute: CriticalableHeroStatus<THero>,
currentValue: number,
upperLimit: number,
referenceDamage: number,
maxIterations: number
): ICriticalSearchResult | null {
let left = currentValue;
let right = upperLimit;
hero.set(attribute, right as THero[typeof attribute]);
let targetInfo = this.calculator!.calculate(handler);
if (targetInfo.damage >= referenceDamage) return null;
let iter = 0;
while (iter++ < maxIterations) {
const middle = Math.floor((left + right) / 2);
hero.set(attribute, middle as THero[typeof attribute]);
const middleInfo = this.calculator!.calculate(handler);
if (middleInfo.damage < referenceDamage) {
right = middle;
targetInfo = middleInfo;
} else {
left = middle;
}
if (right - left <= 1) break;
}
return {
value: right,
info: targetInfo
};
}
*calculateCritical(
view: IEnemyView<TEnemy>,
attribute: CriticalableHeroStatus<THero>,
@@ -192,54 +242,6 @@ export class DamageContext<TEnemy, THero> implements IDamageContext<
baseInfo = next.info;
}
}
/**
* 计算下一个临界点
* @param handler 信息对象,其中的 `hero` 成员与 `hero` 参数同引用
* @param hero 可修改勇士属性对象,与 `handler` 中的 `hero` 成员同引用
* @param attribute 勇士属性名
* @param currentValue 当前勇士属性值
* @param upperLimit 二分上界
* @param referenceDamage 参考伤害值
* @param maxIterations 最大迭代数量
*/
private findNextCritical(
handler: IReadonlyEnemyHandler<TEnemy, THero>,
hero: IHeroAttribute<THero>,
attribute: CriticalableHeroStatus<THero>,
currentValue: number,
upperLimit: number,
referenceDamage: number,
maxIterations: number
): ICriticalSearchResult | null {
let left = currentValue;
let right = upperLimit;
hero.set(attribute, right as THero[typeof attribute]);
let targetInfo = this.calculator!.calculate(handler);
if (targetInfo.damage >= referenceDamage) return null;
let iter = 0;
while (iter++ < maxIterations) {
const middle = Math.floor((left + right) / 2);
hero.set(attribute, middle as THero[typeof attribute]);
const middleInfo = this.calculator!.calculate(handler);
if (middleInfo.damage < referenceDamage) {
right = middle;
targetInfo = middleInfo;
} else {
left = middle;
}
if (right - left <= 1) break;
}
return {
value: right,
info: targetInfo
};
}
}
export class DamageSystem<TEnemy, THero>
@@ -1,7 +1,7 @@
import { IEnemy, IReadonlyEnemy } from '@user/data-base';
import { IEnemyView, IEnemyContext } from './types';
import { IEnemyContext, IComputingEnemyView } from './types';
export class EnemyView<TAttr> implements IEnemyView<TAttr> {
export class EnemyView<TAttr> implements IComputingEnemyView<TAttr> {
/** 计算后怪物 */
private readonly computedEnemy: IEnemy<TAttr>;
@@ -25,9 +25,6 @@ export class EnemyView<TAttr> implements IEnemyView<TAttr> {
return this.computedEnemy;
}
/**
* 获取计算中怪物对象,这个接口不对外暴露,仅在系统内部的 EnemyContext 中使用。
*/
getComputingEnemy(): IEnemy<TAttr> {
return this.computedEnemy;
}
@@ -45,17 +45,18 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
private readonly sourcelessDamage: Map<number, IPointInfo> = new Map();
/** 有来源地图伤害,坐标 -> 点伤害信息 */
private readonly sourcedDamage: Map<number, IPointInfo> = new Map();
/** 地图伤害视图 -> 其信息对象 */
/** 地图伤害视图 -> 地图伤害视图对应的信息对象 */
private readonly viewStore: Map<IMapDamageView<any>, IViewStore<TEnemy>> =
new Map();
/** 地图伤害信息 -> 其信息对象 */
/** 地图伤害数据 -> 地图伤害数据对应的信息对象 */
private readonly damageStore: Map<IMapDamageInfo, IDamageStore<TEnemy>> =
new Map();
/** 怪物视图 -> 其影响对象 */
/** 怪物视图 -> 其产生的地图伤害视图 */
private readonly enemyStore: Map<
IEnemyView<TEnemy>,
Set<IMapDamageView<any>>
> = new Map();
/** 需要延迟刷新的坐标索引 */
private readonly dirtyIndexes: Set<number> = new Set();
/** 合并后伤害缓存,索引 -> 合并结果 */
@@ -96,6 +97,16 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
};
}
/**
* 创建空地图伤害对象
*/
private createEmptyDamage(): IPointInfo {
return {
affectedBy: new Set(),
damages: new Set()
};
}
useReducer(reducer: IMapDamageReducer): void {
this.reducer = reducer;
this.reducedCache.clear();
@@ -103,10 +114,9 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
addMapDamage(locator: ITileLocator, info: IMapDamageInfo): void {
const index = this.indexer.locatorToIndex(locator);
const store = this.sourcelessDamage.getOrInsertComputed(index, () => ({
affectedBy: new Set(),
damages: new Set()
}));
const store = this.sourcelessDamage.getOrInsertComputed(index, () =>
this.createEmptyDamage()
);
store.damages.add(info);
this.markDirtyIndex(index);
}
@@ -150,27 +160,36 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
this.refreshEnemyAndClearCache(view, locator);
}
deleteEnemy(view: IEnemyView<TEnemy>): void {
const store = this.enemyStore.get(view);
if (!store) return;
const collection = new Set<number>();
for (const viewItem of store) {
const affecting = this.viewStore.get(viewItem);
if (!affecting) continue;
affecting.damages.forEach((dam, index) => {
this.damageStore.delete(dam);
collection.add(index);
/**
* 移除指定怪物所产生的地图伤害
* @param view 怪物视图
*/
private removeEnemyAffecting(view: IEnemyView<TEnemy>) {
const views = this.enemyStore.get(view);
if (!views) return;
// 收集被移除贡献的坐标索引,遍历结束后统一标脏以清掉其旧合并缓存
const removed = new Set<number>();
views.forEach(viewItem => {
const store = this.viewStore.get(viewItem);
if (!store) return;
store.damages.forEach((dam, index) => {
removed.add(index);
const point = this.sourcedDamage.get(index);
if (!point) return;
point.affectedBy.delete(viewItem);
point.damages.delete(dam);
this.damageStore.delete(dam);
});
this.viewStore.delete(viewItem);
}
this.enemyStore.delete(view);
collection.forEach(v => {
this.markDirtyIndex(v);
});
removed.forEach(index => {
this.markDirtyIndex(index);
});
}
deleteEnemy(view: IEnemyView<TEnemy>): void {
this.removeEnemyAffecting(view);
this.enemyStore.delete(view);
}
getReducedDamage(locator: ITileLocator): Readonly<IMapDamageInfo> | null {
@@ -240,34 +259,6 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
this.reducedCache.clear();
}
/**
* 移除指定怪物所产生的地图伤害
* @param view 怪物视图
*/
private removeEnemyAffecting(view: IEnemyView<TEnemy>) {
const views = this.enemyStore.get(view);
if (!views) return;
// 收集被移除贡献的坐标索引,遍历结束后统一标脏以清掉其旧合并缓存
const removed = new Set<number>();
views.forEach(viewItem => {
const store = this.viewStore.get(viewItem);
if (!store) return;
store.damages.forEach((dam, index) => {
removed.add(index);
const point = this.sourcedDamage.get(index);
if (!point) return;
point.affectedBy.delete(viewItem);
point.damages.delete(dam);
this.damageStore.delete(dam);
});
this.viewStore.delete(viewItem);
});
this.enemyStore.delete(view);
removed.forEach(index => {
this.markDirtyIndex(index);
});
}
/**
* 登记有来源伤害的反向索引,供删除与局部刷新时按来源清理
* @param point 坐标点伤害信息
@@ -322,10 +313,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
const loc = this.indexer.locator(index);
const point = this.sourcedDamage.getOrInsertComputed(
index,
() => ({
affectedBy: new Set(),
damages: new Set()
})
() => this.createEmptyDamage()
);
const damage = viewItem.getDamageWithoutCheck(loc);
if (damage) {
@@ -71,6 +71,13 @@ export interface IEnemyView<TEnemy> {
markDirty(): void;
}
export interface IComputingEnemyView<TEnemy> extends IEnemyView<TEnemy> {
/**
* 获取正在计算过程中的怪物对象,此方法理应只用于内部上下文的属性计算,外部不应调用
*/
getComputingEnemy(): IEnemy<TEnemy>;
}
//#endregion
//#region 光环与查询