feat: 怪物及地图伤害系统

This commit is contained in:
AncTe committed 2026-04-14 18:10:21 +08:00
1 parent fe67939a05
commit 057d5ab813
28 files changed
+3638 -264

No files matched your search

@@ -0,0 +1,720 @@
import { IRange, logger } from '@motajs/common';
import {
IAuraConverter,
IAuraView,
IEnemy,
IEnemyAuraView,
IEnemyCommonQueryEffect,
IEnemyContext,
IEnemyFinalEffect,
IEnemySpecialModifier,
IEnemySpecialQueryEffect,
IEnemyView,
IMapDamage,
IReadonlyEnemy,
ISpecial
} from './types';
import { EnemyView } from './enemy';
import { MapLocIndexer } from './utils';
import { ITileLocator } from '@user/types';
export class EnemyContext implements IEnemyContext {
private readonly enemyViewMap: Map<number, EnemyView> = new Map();
private readonly enemyMap: Map<number, IEnemy> = new Map();
private readonly locatorViewMap: Map<IEnemyView, number> = new Map();
private readonly locatorEnemyMap: Map<IEnemy, number> = new Map();
private readonly computedToView: Map<IReadonlyEnemy, EnemyView> = new Map();
private readonly auraConverter: Set<IAuraConverter> = new Set();
private readonly converterStatus: Map<IAuraConverter, boolean> = new Map();
private readonly convertedAura: Map<ISpecial<any>, IAuraView> = new Map();
private readonly commonQueryMap: Map<number, IEnemyCommonQueryEffect[]> =
new Map();
private readonly specialQueryEffects: Map<
number,
IEnemySpecialQueryEffect[]
> = new Map();
private readonly finalEffects: IEnemyFinalEffect[] = [];
private readonly globalAuraList: Set<IAuraView> = new Set();
private readonly sortedAura: Map<number, Set<IAuraView>> = new Map();
private readonly needTotallyRefresh: Set<IEnemyView> = new Set();
private readonly requestedCommonContext: Set<IEnemyView> = new Set();
private readonly dirtyEnemy: Set<IEnemyView> = new Set();
private mapDamage: IMapDamage | null = null;
readonly indexer: MapLocIndexer = new MapLocIndexer();
private needUpdate: boolean = true;
built: boolean = false;
width: number = 0;
height: number = 0;
resize(width: number, height: number): void {
this.clear();
this.width = width;
this.height = height;
this.indexer.setWidth(width);
}
registerAuraConverter(converter: IAuraConverter): void {
this.auraConverter.add(converter);
this.converterStatus.set(converter, true);
}
unregisterAuraConverter(converter: IAuraConverter): void {
this.auraConverter.delete(converter);
this.converterStatus.delete(converter);
}
setAuraConverterEnabled(converter: IAuraConverter, enabled: boolean): void {
if (!this.auraConverter.has(converter)) return;
this.converterStatus.set(converter, enabled);
}
registerCommonQueryEffect(
code: number,
effect: IEnemyCommonQueryEffect
): void {
const array = this.commonQueryMap.getOrInsert(code, []);
array.push(effect);
array.sort((a, b) => b.priority - a.priority);
}
unregisterCommonQueryEffect(
code: number,
effect: IEnemyCommonQueryEffect
): void {
const array = this.commonQueryMap.get(code);
if (!array) return;
const index = array.indexOf(effect);
if (index === -1) return;
array.splice(index, 1);
}
registerSpecialQueryEffect(effect: IEnemySpecialQueryEffect): void {
const list = this.specialQueryEffects.getOrInsert(effect.priority, []);
list.push(effect);
}
unregisterSpecialQueryEffect(effect: IEnemySpecialQueryEffect): void {
const list = this.specialQueryEffects.get(effect.priority);
if (!list) return;
const index = list.indexOf(effect);
if (index !== -1) {
list.splice(index, 1);
}
if (list.length === 0) {
this.specialQueryEffects.delete(effect.priority);
}
}
registerFinalEffect(effect: IEnemyFinalEffect): void {
this.finalEffects.push(effect);
this.finalEffects.sort((a, b) => b.priority - a.priority);
}
unregisterFinalEffect(effect: IEnemyFinalEffect): void {
const index = this.finalEffects.indexOf(effect);
if (index !== -1) {
this.finalEffects.splice(index, 1);
}
}
getEnemyLocator(enemy: IEnemy): Readonly<ITileLocator> | null {
const index = this.locatorEnemyMap.get(enemy);
if (index === undefined) return null;
return this.indexer.indexToLocator(index);
}
getEnemyLocatorByView(view: IEnemyView): Readonly<ITileLocator> | null {
const index = this.locatorViewMap.get(view);
if (index === undefined) return null;
return this.indexer.indexToLocator(index);
}
getEnemyByLocator(locator: ITileLocator): IEnemyView | null {
const index = this.indexer.locToIndex(locator.x, locator.y);
return this.enemyViewMap.get(index) ?? null;
}
getEnemyByLoc(x: number, y: number): IEnemyView | null {
const index = this.indexer.locToIndex(x, y);
return this.enemyViewMap.get(index) ?? null;
}
getViewByComputed(enemy: IReadonlyEnemy): IEnemyView | null {
return this.computedToView.get(enemy) ?? null;
}
private deleteEnemyAt(index: number) {
const view = this.enemyViewMap.get(index);
const enemy = this.enemyMap.get(index);
if (!view || !enemy) return;
this.needUpdate = true;
if (this.mapDamage) {
this.mapDamage.deleteEnemy(view);
}
this.needTotallyRefresh.delete(view);
this.dirtyEnemy.delete(view);
this.requestedCommonContext.delete(view);
this.computedToView.delete(view.getComputingEnemy());
this.enemyViewMap.delete(index);
this.enemyMap.delete(index);
this.locatorViewMap.delete(view);
this.locatorEnemyMap.delete(enemy);
}
setEnemyAt(locator: ITileLocator, enemy: IEnemy): void {
const index = this.indexer.locToIndex(locator.x, locator.y);
this.deleteEnemyAt(index);
const view = new EnemyView(enemy, this);
this.enemyMap.set(index, enemy);
this.enemyViewMap.set(index, view);
this.locatorEnemyMap.set(enemy, index);
this.locatorViewMap.set(view, index);
this.computedToView.set(view.getComputingEnemy(), view);
this.needUpdate = true;
}
deleteEnemy(locator: ITileLocator): void {
const index = this.indexer.locToIndex(locator.x, locator.y);
this.deleteEnemyAt(index);
}
private *internalScanRange<T>(
range: IRange<T>,
param: T
): Iterable<EnemyView> {
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) {
yield view;
}
}
}
scanRange<T>(range: IRange<T>, param: T): Iterable<IEnemyView> {
return this.internalScanRange(range, param);
}
*iterateEnemy(): Iterable<[ITileLocator, IEnemyView]> {
for (const [index, view] of this.enemyViewMap) {
const locator = this.indexer.indexToLocator(index);
yield [locator, view];
}
}
addAura(aura: IAuraView): void {
this.globalAuraList.add(aura);
this.needUpdate = true;
}
deleteAura(aura: IAuraView): void {
this.globalAuraList.delete(aura);
this.needUpdate = true;
}
attachMapDamage(damage: IMapDamage | null): void {
this.mapDamage = damage;
if (damage) {
damage.refreshAll();
}
}
getMapDamage(): IMapDamage | null {
return this.mapDamage;
}
private convertSpecial(
special: ISpecial<any>,
enemy: IReadonlyEnemy,
locator: ITileLocator
): IEnemyAuraView<any, any> | null {
let matched: IAuraConverter | null = null;
for (const converter of this.auraConverter) {
if (!this.converterStatus.get(converter)) continue;
if (converter.shouldConvert(special, enemy, locator)) {
if (matched) {
logger.warn(97, special.code.toString());
return null;
}
matched = converter;
}
}
if (!matched) return null;
return matched.convert(special, enemy, locator);
}
private insertIntoSortedAura(aura: IAuraView): void {
const set = this.sortedAura.getOrInsertComputed(
aura.priority,
() => new Set()
);
set.add(aura);
}
private removeFromSortedAura(aura: IAuraView): void {
const set = this.sortedAura.get(aura.priority);
if (set) {
set.delete(aura);
if (set.size === 0) {
this.sortedAura.delete(aura.priority);
}
}
}
private processSpecialModifier(
modifier: IEnemySpecialModifier,
enemy: IEnemy,
locator: ITileLocator,
currentPriority: number
): Set<IAuraView> {
const toAdd = modifier.add(enemy, locator);
const toDelete = modifier.delete(enemy, locator);
const affectedAuras = new Set<IAuraView>();
if (toAdd.length > 0 && toDelete.length > 0) {
logger.warn(100);
return affectedAuras;
}
for (const adding of toAdd) {
const aura = this.convertSpecial(adding, enemy, locator);
if (aura) {
if (import.meta.env.DEV && aura.priority > currentPriority) {
logger.warn(
99,
aura.priority.toString(),
currentPriority.toString()
);
continue;
}
this.convertedAura.set(adding, aura);
this.insertIntoSortedAura(aura);
affectedAuras.add(aura);
}
enemy.addSpecial(adding);
}
for (const deleting of toDelete) {
enemy.deleteSpecial(deleting);
const aura = this.convertedAura.get(deleting);
if (aura) {
if (import.meta.env.DEV && aura.priority >= currentPriority) {
logger.warn(
98,
aura.priority.toString(),
currentPriority.toString()
);
continue;
}
this.removeFromSortedAura(aura);
this.convertedAura.delete(deleting);
affectedAuras.add(aura);
}
}
for (const special of enemy.iterateSpecials()) {
const success = modifier.modify(enemy, special, locator);
if (!success) continue;
const aura = this.convertedAura.get(special);
if (!aura) continue;
affectedAuras.add(aura);
if (import.meta.env.DEV && aura.priority >= currentPriority) {
logger.warn(
98,
aura.priority.toString(),
currentPriority.toString()
);
}
}
return affectedAuras;
}
private processSpecialQuery(
effect: IEnemySpecialQueryEffect,
currentPriority: number
): void {
const modifier = effect.for(this);
for (const [index, view] of this.enemyViewMap) {
const locator = this.indexer.indexToLocator(index);
const enemy = view.getComputingEnemy();
if (!modifier.shouldQuery(enemy, locator)) continue;
const affectedAuras = this.processSpecialModifier(
modifier,
enemy,
locator,
currentPriority
);
if (affectedAuras.size > 0) {
this.needTotallyRefresh.add(view);
} else {
this.requestedCommonContext.add(view);
}
}
}
private processAuraSpecial(aura: IAuraView, currentPriority: number): void {
const param = aura.getRangeParam();
for (const enemyView of this.internalScanRange(aura.range, param)) {
const locator = this.getEnemyLocatorByView(enemyView);
if (!locator) continue;
const enemy = enemyView.getComputingEnemy();
const base = enemyView.getBaseEnemy();
const modifier = aura.applySpecial(enemy, base, locator);
if (!modifier) continue;
this.processSpecialModifier(
modifier,
enemy,
locator,
currentPriority
);
this.needTotallyRefresh.add(enemyView);
}
}
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.indexToLocator(index);
for (const special of enemy.specials) {
const aura = this.convertSpecial(special, enemy, locator);
if (!aura) continue;
this.convertedAura.set(special, aura);
this.insertIntoSortedAura(aura);
}
}
const processedPriorities = new Set<number>();
while (true) {
let maxPriority: number | null = null;
for (const priority of this.sortedAura.keys()) {
if (!processedPriorities.has(priority)) {
if (maxPriority === null || priority > maxPriority) {
maxPriority = priority;
}
}
}
for (const priority of this.specialQueryEffects.keys()) {
if (!processedPriorities.has(priority)) {
if (maxPriority === null || priority > maxPriority) {
maxPriority = priority;
}
}
}
if (maxPriority === null) break;
processedPriorities.add(maxPriority);
const auras = this.sortedAura.get(maxPriority);
if (auras) {
for (const aura of auras) {
if (aura.couldApplySpecial) {
this.processAuraSpecial(aura, maxPriority);
}
}
}
const effects = this.specialQueryEffects.get(maxPriority);
if (effects) {
for (const effect of effects) {
this.processSpecialQuery(effect, maxPriority);
}
}
}
}
private buildupBase(): void {
const priorities = [...this.sortedAura.keys()].sort((a, b) => b - a);
for (const p of priorities) {
const auras = this.sortedAura.get(p);
if (!auras) continue;
for (const aura of auras) {
const param = aura.getRangeParam();
for (const view of this.internalScanRange(aura.range, param)) {
const enemy = view.getComputingEnemy();
const base = view.getBaseEnemy();
const locator = this.getEnemyLocatorByView(view)!;
aura.apply(enemy, base, locator);
}
}
}
}
private buildupQuery(): void {
for (const [index, view] of this.enemyViewMap) {
const enemy = view.getComputingEnemy();
const locator = this.indexer.indexToLocator(index);
let queried = false;
const query = () => {
queried = true;
return this;
};
for (const special of enemy.specials) {
const effects = this.commonQueryMap.get(special.code);
if (!effects) continue;
for (const effect of effects) {
effect.apply(enemy, special, query, locator);
}
}
if (queried) {
this.requestedCommonContext.add(view);
}
}
}
private buildupFinal(): void {
for (const [index, view] of this.enemyViewMap) {
const enemy = view.getComputingEnemy();
const locator = this.indexer.indexToLocator(index);
for (const effect of this.finalEffects) {
effect.apply(enemy, locator);
}
}
}
buildup(): void {
if (!this.needUpdate) return;
this.needUpdate = false;
this.sortedAura.clear();
this.convertedAura.clear();
this.dirtyEnemy.clear();
this.needTotallyRefresh.clear();
this.requestedCommonContext.clear();
const hasAura = this.auraConverter.size > 0;
const hasSpecialQuery = this.specialQueryEffects.size > 0;
if (hasAura || hasSpecialQuery) {
this.buildupSpecials();
this.buildupBase();
}
if (this.commonQueryMap.size > 0) {
this.buildupQuery();
}
if (this.finalEffects.length > 0) {
this.buildupFinal();
}
if (this.mapDamage) {
this.mapDamage.refreshAll();
}
}
markDirty(view: IEnemyView): void {
if (!this.locatorViewMap.has(view)) return;
this.dirtyEnemy.add(view);
}
private refreshSpecialModifier(
modifier: IEnemySpecialModifier,
enemy: IEnemy,
locator: ITileLocator
): void {
const toAdd = modifier.add(enemy, locator);
const toDelete = modifier.delete(enemy, locator);
if (toAdd.length > 0 && toDelete.length > 0) {
logger.warn(100);
return;
}
for (const adding of toAdd) {
enemy.addSpecial(adding);
if (import.meta.env.DEV) {
const aura = this.convertSpecial(adding, enemy, locator);
if (aura) {
logger.warn(101, adding.code.toString());
}
}
}
for (const deleting of toDelete) {
enemy.deleteSpecial(deleting);
if (import.meta.env.DEV) {
const aura = this.convertSpecial(deleting, enemy, locator);
if (aura) {
logger.warn(101, deleting.code.toString());
}
}
}
for (const special of enemy.iterateSpecials()) {
const success = modifier.modify(enemy, special, locator);
if (import.meta.env.DEV && success) {
const aura = this.convertedAura.get(special);
if (aura) {
logger.warn(101, special.code.toString());
}
}
}
}
private refreshEnemy(view: EnemyView): void {
const locator = this.getEnemyLocatorByView(view);
if (!locator) return;
view.reset();
const enemy = view.getComputingEnemy();
const base = view.getBaseEnemy();
const specialPriorities = new Set<number>();
for (const priority of this.sortedAura.keys()) {
specialPriorities.add(priority);
}
for (const priority of this.specialQueryEffects.keys()) {
specialPriorities.add(priority);
}
const orderedSpecialPriorities = [...specialPriorities].sort(
(a, b) => b - a
);
for (const priority of orderedSpecialPriorities) {
const auras = this.sortedAura.get(priority);
if (auras) {
for (const aura of auras) {
if (!aura.couldApplySpecial) continue;
const param = aura.getRangeParam();
aura.range.bindHost(this);
const inRange = aura.range.inRange(
locator.x,
locator.y,
param
);
if (!inRange) continue;
const modifier = aura.applySpecial(enemy, base, locator);
if (!modifier) continue;
this.refreshSpecialModifier(modifier, enemy, locator);
}
}
const effects = this.specialQueryEffects.get(priority);
if (effects) {
for (const effect of effects) {
const modifier = effect.for(this);
if (!modifier.shouldQuery(enemy, locator)) continue;
this.refreshSpecialModifier(modifier, enemy, locator);
}
}
}
const basePriorities = [...this.sortedAura.keys()].sort(
(a, b) => b - a
);
for (const priority of basePriorities) {
const auras = this.sortedAura.get(priority);
if (!auras) continue;
for (const aura of auras) {
const param = aura.getRangeParam();
aura.range.bindHost(this);
if (!aura.range.inRange(locator.x, locator.y, param)) {
continue;
}
aura.apply(enemy, base, locator);
}
}
this.requestedCommonContext.delete(view);
let queried = false;
const query = () => {
queried = true;
return this;
};
for (const special of enemy.specials) {
const effects = this.commonQueryMap.get(special.code);
if (!effects) continue;
for (const effect of effects) {
effect.apply(enemy, special, query, locator);
}
}
if (queried) {
this.requestedCommonContext.add(view);
}
for (const effect of this.finalEffects) {
effect.apply(enemy, locator);
}
this.dirtyEnemy.delete(view);
if (this.mapDamage) {
this.mapDamage.markEnemyDirty(view);
}
}
requestRefresh(view: IEnemyView): void {
if (!this.dirtyEnemy.has(view)) return;
if (this.needTotallyRefresh.has(view)) {
this.needUpdate = true;
}
if (this.needUpdate) {
this.buildup();
return;
}
this.refreshEnemy(view as EnemyView);
for (const requestedView of this.requestedCommonContext) {
if (requestedView === view) continue;
this.refreshEnemy(requestedView as EnemyView);
}
}
clear(): void {
this.enemyViewMap.clear();
this.enemyMap.clear();
this.locatorViewMap.clear();
this.locatorEnemyMap.clear();
this.computedToView.clear();
this.globalAuraList.clear();
this.sortedAura.clear();
this.needTotallyRefresh.clear();
this.requestedCommonContext.clear();
this.dirtyEnemy.clear();
if (this.mapDamage) {
this.mapDamage.refreshAll();
}
}
destroy(): void {
this.clear();
this.attachMapDamage(null);
this.auraConverter.clear();
this.commonQueryMap.clear();
this.specialQueryEffects.clear();
this.finalEffects.length = 0;
}
}
+134
View File
@@ -0,0 +1,134 @@
import { logger } from '@motajs/common';
import {
IEnemy,
IEnemyAttributes,
IEnemyContext,
IReadonlyEnemy,
ISpecial,
IEnemyView
} from './types';
export class Enemy implements IEnemy {
readonly specials: Set<ISpecial<any>> = new Set();
/** code -> ISpecial 映射,用于快速查找 */
private readonly specialMap: Map<number, ISpecial<any>> = new Map();
constructor(
readonly id: string,
readonly code: number,
readonly attributes: IEnemyAttributes
) {}
getSpecial<T>(code: number): ISpecial<T> | null {
return (this.specialMap.get(code) as ISpecial<T>) ?? null;
}
hasSpecial(code: number): boolean {
return this.specialMap.has(code);
}
addSpecial(special: ISpecial<any>): void {
if (this.specialMap.has(special.code)) {
logger.warn(96, this.id, special.code.toString());
return;
}
this.specials.add(special);
this.specialMap.set(special.code, special);
}
deleteSpecial(special: number | ISpecial<any>): void {
const code = typeof special === 'number' ? special : special.code;
const existing = this.specialMap.get(code);
if (!existing) return;
this.specials.delete(existing);
this.specialMap.delete(code);
}
iterateSpecials(): Iterable<ISpecial<any>> {
return this.specials;
}
setAttribute<K extends keyof IEnemyAttributes>(
key: K,
value: IEnemyAttributes[K]
): void {
this.attributes[key] = value;
}
getAttribute<K extends keyof IEnemyAttributes>(
key: K
): IEnemyAttributes[K] {
return this.attributes[key];
}
clone(): IEnemy {
const cloned = new Enemy(
this.id,
this.code,
structuredClone(this.attributes)
);
for (const special of this.specials) {
cloned.addSpecial(special.clone());
}
return cloned;
}
copy(enemy: IReadonlyEnemy): void {
ATTRIBUTE_KEYS.forEach(key => {
this.setAttribute(key, structuredClone(enemy.getAttribute(key)));
});
this.specials.clear();
this.specialMap.clear();
for (const special of enemy.iterateSpecials()) {
this.addSpecial(special.clone());
}
}
}
export class EnemyView implements IEnemyView {
private computedEnemy: IEnemy;
constructor(
readonly baseEnemy: IEnemy,
readonly context: IEnemyContext
) {
this.computedEnemy = baseEnemy.clone();
}
reset(): void {
this.computedEnemy.copy(this.baseEnemy);
}
getBaseEnemy(): IReadonlyEnemy {
return this.baseEnemy;
}
getComputedEnemy(): IReadonlyEnemy {
this.context.requestRefresh(this);
return this.computedEnemy;
}
/**
* 获取计算中怪物对象,这个接口不对外暴露,仅在系统内部的 EnemyContext 中使用。
*/
getComputingEnemy(): IEnemy {
return this.computedEnemy;
}
getModifiableEnemy(): IEnemy {
return this.baseEnemy;
}
markDirty(): void {
this.context.markDirty(this);
}
}
export const ATTRIBUTE_KEYS: (keyof IEnemyAttributes)[] = [
'hp',
'atk',
'def',
'money',
'exp',
'point'
];
@@ -0,0 +1,7 @@
export * from './enemy';
export * from './context';
export * from './mapDamage';
export * from './manager';
export * from './special';
export * from './types';
export * from './utils';
@@ -0,0 +1,146 @@
import { logger } from '@motajs/common';
import { Enemy as EnemyImpl } from './enemy';
import {
IEnemy,
IEnemyAttributes,
IEnemyManager,
ISpecial,
SpecialCreation
} from './types';
export class EnemyManager implements IEnemyManager {
/** 特殊属性注册表,code -> 创建函数 */
private readonly specialRegistry: Map<number, SpecialCreation<any>> =
new Map();
/** 自定义怪物属性注册表,name -> 默认值 */
private readonly attributeRegistry: Map<string, any> = new Map();
/** 怪物模板表,code -> IEnemy */
private readonly prefabByCode: Map<number, IEnemy> = new Map();
/** 怪物模板表,id -> IEnemy */
private readonly prefabById: Map<string, IEnemy> = new Map();
/** 旧样板怪物 id 到 code 的映射,用于 fromLegacyEnemy 快速查找已有模板 */
private readonly legacyIdToCode: Map<string, number> = new Map();
registerSpecial(
code: number,
cons: (enemy: IEnemy) => ISpecial<any>
): void {
this.specialRegistry.set(code, cons);
}
registerAttribute(name: string, defaultValue: any): void {
if (
typeof defaultValue === 'function' ||
typeof defaultValue === 'symbol' ||
typeof defaultValue === 'bigint' ||
typeof defaultValue === 'undefined'
) {
logger.error(53);
return;
}
this.attributeRegistry.set(name, defaultValue);
}
fromLegacyEnemy(enemy: Enemy): IEnemy {
// 如果该旧样板怪物已经通过 addPrefabFromLegacy 注册为模板,直接克隆模板
const existingCode = this.legacyIdToCode.get(enemy.id);
if (existingCode) {
const prefab = this.prefabByCode.get(existingCode);
if (prefab) {
return prefab.clone();
}
}
return this.convertLegacyEnemy(0, enemy);
}
/**
* 真正执行旧样板怪物到新怪物对象的转换
* @param code 怪物图块数字
* @param enemy 旧样板怪物对象
*/
private convertLegacyEnemy(code: number, enemy: Enemy): IEnemy {
const attrs: IEnemyAttributes = {
hp: enemy.hp,
atk: enemy.atk,
def: enemy.def,
money: enemy.money,
exp: enemy.exp,
point: enemy.point
};
const result = new EnemyImpl(enemy.id, code, structuredClone(attrs));
// 转换特殊属性
if (enemy.special) {
for (const specialCode of enemy.special) {
const creator = this.specialRegistry.get(specialCode);
if (!creator) continue;
const special = creator(result);
special.fromLegacyEnemy(enemy);
result.addSpecial(special);
}
}
return result;
}
createEnemy(code: number): IEnemy | null {
const prefab = this.prefabByCode.get(code);
if (!prefab) return null;
return prefab.clone();
}
createEnemyById(id: string): IEnemy | null {
const prefab = this.prefabById.get(id);
if (!prefab) return null;
return prefab.clone();
}
addPrefab(enemy: IEnemy): void {
if (
this.prefabByCode.has(enemy.code) ||
this.prefabById.has(enemy.id)
) {
return;
}
const cloned = enemy.clone();
this.prefabByCode.set(enemy.code, cloned);
this.prefabById.set(enemy.id, cloned);
}
addPrefabFromLegacy(code: number, enemy: Enemy): void {
if (this.prefabByCode.has(code) || this.prefabById.has(enemy.id)) {
return;
}
const prefab = this.convertLegacyEnemy(code, enemy);
this.prefabByCode.set(code, prefab);
this.prefabById.set(prefab.id, prefab);
this.legacyIdToCode.set(enemy.id, code);
}
getPrefab(code: number): IEnemy | null {
return this.prefabByCode.get(code) ?? null;
}
getPrefabById(id: string): IEnemy | null {
return this.prefabById.get(id) ?? null;
}
deletePrefab(code: number | string): void {
const prefab =
typeof code === 'number'
? this.prefabByCode.get(code)
: this.prefabById.get(code);
if (!prefab) return;
this.prefabByCode.delete(prefab.code);
this.prefabById.delete(prefab.id);
}
changePrefab(code: number | string, enemy: IEnemy): void {
// 先删除旧的模板(如果存在)
this.deletePrefab(code);
// 再添加新的模板
this.prefabByCode.set(enemy.code, enemy);
this.prefabById.set(enemy.id, enemy);
}
}
@@ -0,0 +1,341 @@
import { logger } from '@motajs/common';
import { ITileLocator } from '@user/types';
import {
IEnemyContext,
IEnemyView,
IMapDamage,
IMapDamageConverter,
IMapDamageInfo,
IMapDamageReducer,
IMapDamageView,
IMapLocIndexer
} from './types';
interface IPointInfo {
/** 该点所有的地图伤害 */
readonly damages: Set<Readonly<IMapDamageInfo>>;
/** 所有影响该点的地图伤害视图 */
readonly affectedBy: Set<IMapDamageView<any>>;
}
interface IViewStore {
/** 该地图伤害视图所影响的伤害信息 */
readonly damages: Map<number, Readonly<IMapDamageInfo>>;
/** 当前视图所属的怪物视图 */
readonly enemy: IEnemyView;
}
interface IDamageStore {
/** 该地图伤害信息的地图伤害视图来源 */
readonly sourceView: IMapDamageView<any>;
/** 地图伤害信息的来源怪物 */
readonly sourceEnemy: IEnemyView;
/** 该地图伤害信息所处的索引 */
readonly index: number;
}
export class MapDamage implements IMapDamage {
/** 当前使用的地图伤害转换器 */
private converter: IMapDamageConverter | null = null;
/** 当前使用的地图伤害合并器 */
private reducer: IMapDamageReducer | null = null;
/** 无来源地图伤害,坐标 -> 点伤害信息 */
private readonly sourcelessDamage: Map<number, IPointInfo> = new Map();
/** 有来源地图伤害,坐标 -> 点伤害信息 */
private readonly sourcedDamage: Map<number, IPointInfo> = new Map();
/** 地图伤害视图 -> 其信息对象 */
private readonly viewStore: Map<IMapDamageView, IViewStore> = new Map();
/** 地图伤害信息 -> 其信息对象 */
private readonly damageStore: Map<IMapDamageInfo, IDamageStore> = new Map();
/** 怪物视图 -> 其影响对象 */
private readonly enemyStore: Map<IEnemyView, Set<IMapDamageView>> =
new Map();
/** 需要延迟刷新的坐标索引 */
private readonly dirtyIndexes: Set<number> = new Set();
/** 合并后伤害缓存,索引 -> 合并结果 */
private readonly reducedCache: Map<number, IMapDamageInfo> = new Map();
constructor(
readonly context: IEnemyContext,
readonly indexer: IMapLocIndexer
) {}
useConverter(converter: IMapDamageConverter): void {
this.converter = converter;
this.refreshAll();
}
useReducer(reducer: IMapDamageReducer): void {
this.reducer = reducer;
this.reducedCache.clear();
}
addMapDamage(locator: ITileLocator, info: IMapDamageInfo): void {
const index = this.indexer.locaterToIndex(locator);
const store = this.sourcelessDamage.getOrInsertComputed(index, () => ({
affectedBy: new Set(),
damages: new Set()
}));
store.damages.add(info);
this.markDirtyIndex(index);
}
deleteMapDamage(locator: ITileLocator, info: IMapDamageInfo): void {
const index = this.indexer.locaterToIndex(locator);
const current = this.sourcelessDamage.get(index);
if (!current) return;
current.damages.delete(info);
this.markDirtyIndex(index);
}
/**
* 将指定索引标记为脏
* @param index 坐标索引
*/
private markDirtyIndex(index: number) {
this.dirtyIndexes.add(index);
this.reducedCache.delete(index);
}
markDirty(locator: ITileLocator): void {
this.markDirtyIndex(this.indexer.locaterToIndex(locator));
}
markEnemyDirty(view: IEnemyView): void {
const store = this.enemyStore.get(view);
const locator = this.context.getEnemyLocatorByView(view);
if (!store) {
if (!locator) {
logger.warn(104);
} else {
this.refreshAll();
}
return;
}
if (!locator) return;
this.refreshEnemyAndClearCache(view, locator);
}
deleteEnemy(view: IEnemyView): 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);
});
this.viewStore.delete(viewItem);
}
this.enemyStore.delete(view);
collection.forEach(v => {
this.markDirtyIndex(v);
});
}
getReducedDamage(locator: ITileLocator): Readonly<IMapDamageInfo> | null {
if (!this.reducer) {
logger.warn(103);
return null;
}
const index = this.indexer.locaterToIndex(locator);
if (this.dirtyIndexes.has(index)) {
this.refreshIndex(index);
}
const cache = this.reducedCache.get(index);
if (cache) return cache;
const separated = this.getSeparatedDamageByIndex(index);
if (separated.size === 0) return null;
const reduced = this.reducer.reduce(separated, locator);
this.reducedCache.set(index, reduced);
return reduced;
}
/**
* 根据索引获取指定位置的合并前伤害
* @param index 坐标索引
*/
private getSeparatedDamageByIndex(
index: number
): Set<Readonly<IMapDamageInfo>> {
const sourceless = this.sourcelessDamage.get(index);
const sourced = this.sourcedDamage.get(index);
if (sourceless) {
if (sourced) {
return sourced.damages.union(sourceless.damages);
} else {
return sourceless.damages;
}
} else if (sourced) {
return sourced.damages;
} else {
return new Set();
}
}
getSeparatedDamage(
locator: ITileLocator
): Iterable<Readonly<IMapDamageInfo>> {
const index = this.indexer.locaterToIndex(locator);
if (this.dirtyIndexes.has(index)) {
this.refreshIndex(index);
}
return this.getSeparatedDamageByIndex(index);
}
/**
* 清空所有有来源伤害的内部状态
*/
private clearSourceState(): void {
this.sourcedDamage.clear();
this.damageStore.clear();
this.viewStore.clear();
this.enemyStore.clear();
this.dirtyIndexes.clear();
this.reducedCache.clear();
}
/**
* 移除指定怪物所产生的地图伤害
* @param view 怪物视图
*/
private removeEnemyAffecting(view: IEnemyView) {
const views = this.enemyStore.get(view);
if (!views) return;
views.forEach(viewItem => {
const store = this.viewStore.get(viewItem);
if (!store) return;
store.damages.forEach((dam, 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);
}
/**
* 刷新指定位置的怪物地图伤害,并执行刷新缓存的操作
*/
private refreshEnemyAndClearCache(view: IEnemyView, locator: ITileLocator) {
this.removeEnemyAffecting(view);
const enemy = view.getComputedEnemy();
const views = this.converter!.convert(enemy, locator, this.context);
const set = new Set(views);
if (set.size === 0) return;
this.enemyStore.set(view, set);
const collection = new Set<number>();
set.forEach(viewItem => {
const range = viewItem.getRange();
const param = viewItem.getRangeParam();
range.bindHost(this.context);
for (const index of range.iterateLoc(param)) {
const loc = this.indexer.indexToLocator(index);
const point = this.sourcedDamage.getOrInsertComputed(
index,
() => ({
affectedBy: new Set(),
damages: new Set()
})
);
const damage = viewItem.getDamageWithoutCheck(loc);
if (damage) {
point.affectedBy.add(viewItem);
point.damages.add(damage);
collection.add(index);
}
}
});
collection.forEach(v => {
this.dirtyIndexes.delete(v);
this.reducedCache.delete(v);
});
}
/**
* 刷新指定位置的怪物地图伤害
*/
private refreshEnemy(view: IEnemyView, locator: ITileLocator) {
this.removeEnemyAffecting(view);
const enemy = view.getComputedEnemy();
const views = this.converter!.convert(enemy, locator, this.context);
const set = new Set(views);
if (set.size === 0) return;
this.enemyStore.set(view, set);
set.forEach(viewItem => {
const range = viewItem.getRange();
const param = viewItem.getRangeParam();
range.bindHost(this.context);
for (const index of range.iterateLoc(param)) {
const loc = this.indexer.indexToLocator(index);
const point = this.sourcedDamage.getOrInsertComputed(
index,
() => ({
affectedBy: new Set(),
damages: new Set()
})
);
const damage = viewItem.getDamageWithoutCheck(loc);
if (damage) {
point.affectedBy.add(viewItem);
point.damages.add(damage);
}
}
});
}
refreshAll(): void {
if (!this.converter) {
logger.warn(102);
return;
}
if (!this.reducer) {
logger.warn(103);
return;
}
this.clearSourceState();
this.reducedCache.clear();
for (const [locator, view] of this.context.iterateEnemy()) {
this.refreshEnemy(view, locator);
}
}
/**
* 重新计算指定点位的有来源伤害缓存
*/
private refreshIndex(index: number): void {
this.dirtyIndexes.delete(index);
this.reducedCache.delete(index);
const locator = this.indexer.indexToLocator(index);
const point = this.sourcedDamage.get(index);
if (!point) return;
for (const damage of point.damages) {
const store = this.damageStore.get(damage);
if (!store) continue;
const viewStore = this.viewStore.get(store.sourceView);
if (!viewStore) continue;
viewStore.damages.delete(index);
this.damageStore.delete(damage);
}
point.damages.clear();
point.affectedBy.forEach(view => {
const damage = view.getDamageWithoutCheck(locator);
if (damage) point.damages.add(damage);
});
}
}
@@ -0,0 +1,95 @@
import { ISpecial, SpecialCreation } from './types';
export interface ICommonSerializableSpecialConfig<T> {
/** 获取特殊属性的名称 */
getSpecialName: (special: ISpecial<T>) => string;
/** 获取特殊属性的描述 */
getDescription: (special: ISpecial<T>) => string;
/** 从旧样板怪物对象获取此特殊属性对应的属性值 */
fromLegacyEnemy: (enemy: Enemy) => T;
}
export class CommonSerializableSpecial<T> implements ISpecial<T> {
constructor(
readonly code: number,
public value: T,
readonly config: ICommonSerializableSpecialConfig<T>
) {}
setValue(value: T): void {
this.value = value;
}
getValue(): T {
return this.value;
}
getSpecialName(): string {
return this.config.getSpecialName(this);
}
getDescription(): string {
return this.config.getDescription(this);
}
fromLegacyEnemy(enemy: Enemy): void {
this.value = this.config.fromLegacyEnemy(enemy);
}
clone(): ISpecial<T> {
return new CommonSerializableSpecial(
this.code,
structuredClone(this.value),
this.config
);
}
}
export class NonePropertySpecial implements ISpecial<void> {
value: void = undefined;
constructor(
readonly code: number,
readonly config: ICommonSerializableSpecialConfig<void>
) {}
setValue(_value: void): void {
// unneeded
}
getValue(): void {
return void 0;
}
getSpecialName(): string {
return this.config.getSpecialName(this);
}
getDescription(): string {
return this.config.getDescription(this);
}
fromLegacyEnemy(_enemy: Enemy): void {
// unneeded
}
clone(): ISpecial<void> {
return new NonePropertySpecial(this.code, this.config);
}
}
export function defineCommonSerializableSpecial<T>(
code: number,
value: T,
config: ICommonSerializableSpecialConfig<T>
): SpecialCreation<T> {
return () =>
new CommonSerializableSpecial(code, structuredClone(value), config);
}
export function defineNonePropertySpecial(
code: number,
config: ICommonSerializableSpecialConfig<void>
): SpecialCreation<void> {
return () => new NonePropertySpecial(code, config);
}
+739
View File
@@ -0,0 +1,739 @@
import { IRange } from '@motajs/common';
import { ITileLocator } from '@user/types';
export interface IEnemyAttributes {
/** 怪物生命值 */
hp: number;
/** 怪物攻击力 */
atk: number;
/** 怪物防御力 */
def: number;
/** 怪物金币 */
money: number;
/** 怪物经验值 */
exp: number;
/** 怪物加点量 */
point: number;
}
export interface ISpecial<T = void> {
/** 特殊属性代码 */
readonly code: number;
/** 特殊属性需要的数值 */
readonly value: T;
/**
* 设置特殊属性数值
* @param value 特殊属性数值
*/
setValue(value: T): void;
/**
* 获取特殊属性数值
*/
getValue(): T;
/**
* 获取此特殊属性的名称
*/
getSpecialName(): string;
/**
* 获取此特殊属性的描述
*/
getDescription(): string;
/**
* 从旧样板的怪物对象中导入此特殊属性
* @param enemy 旧样板怪物对象
*/
fromLegacyEnemy(enemy: Enemy): void;
/**
* 深拷贝此特殊属性
*/
clone(): ISpecial<T>;
}
export interface IReadonlyEnemy {
/** 怪物标识符 */
readonly id: string;
/** 怪物在地图上的标识数字 */
readonly code: number;
/**
* 根据特殊属性代码获取对应的对象
* @param code 特殊属性代码
*/
getSpecial<T>(code: number): ISpecial<T> | null;
/**
* 判断怪物是否拥有指定属性
* @param code 特殊属性代码
*/
hasSpecial(code: number): boolean;
/**
* 迭代此怪物所包含的所有特殊属性
*/
iterateSpecials(): Iterable<ISpecial<any>>;
/**
* 获取怪物属性值
* @param key 属性名称
*/
getAttribute<K extends keyof IEnemyAttributes>(key: K): IEnemyAttributes[K];
/**
* 深拷贝此怪物对象
*/
clone(): IReadonlyEnemy;
}
export interface IEnemy extends IReadonlyEnemy {
/** 怪物标识符 */
readonly id: string;
/** 怪物在地图上的标识数字 */
readonly code: number;
/** 怪物属性值 */
readonly attributes: Readonly<IEnemyAttributes>;
/** 怪物拥有的特殊属性列表 */
readonly specials: Set<ISpecial<any>>;
/**
* 添加特殊属性
* @param special 特殊属性对象
*/
addSpecial(special: ISpecial<any>): void;
/**
* 删除指定的特殊属性
* @param special 特殊属性代码或对象
*/
deleteSpecial(special: number | ISpecial<any>): void;
/**
* 设置怪物属性值
* @param key 属性名称
* @param value 新的属性值
*/
setAttribute<K extends keyof IEnemyAttributes>(
key: K,
value: IEnemyAttributes[K]
): void;
/**
* 深拷贝此怪物对象
*/
clone(): IEnemy;
/**
* 从一个怪物对象中将属性复制到当前对象
* @param enemy 怪物对象
*/
copy(enemy: IReadonlyEnemy): void;
}
export type SpecialCreation<T> = (enemy: IEnemy) => ISpecial<T>;
export interface IEnemyManager {
/**
* 注册一个特殊属性
* @param code 特殊属性代码
* @param cons 特殊属性创建函数
*/
registerSpecial(code: number, cons: SpecialCreation<any>): void;
/**
* 注册一个怪物属性
* @param name 属性名称
* @param defaultValue 属性默认值
*/
registerAttribute(name: string, defaultValue: any): void;
/**
* 根据旧样板怪物对象生成一个新的怪物对象
* @param enemy 旧样板怪物对象
*/
fromLegacyEnemy(enemy: Enemy): IEnemy;
/**
* 创建怪物对象,如果对应数字的怪物不存在则会返回 `null`
* @param code 怪物图块数字
*/
createEnemy(code: number): IEnemy | null;
/**
* 根据怪物的 `id` 创建怪物对象,如果对应的怪物不存在则会返回 `null`
* @param id 怪物 `id`
*/
createEnemyById(id: string): IEnemy | null;
/**
* 添加怪物模板,如果 `id` 或 `code` 与已有的冲突,则不会做任何操作,
* 如果需要修改怪物模板,请使用 {@link changePrefab}
* @param enemy 怪物对象
*/
addPrefab(enemy: IEnemy): void;
/**
* 从旧样板的怪物对象中添加怪物模板
* @param code 怪物对象对应的图块数字
* @param enemy 旧样板怪物对象
*/
addPrefabFromLegacy(code: number, enemy: Enemy): void;
/**
* 获取指定怪物的模板
* @param code 怪物图块数字
*/
getPrefab(code: number): IEnemy | null;
/**
* 根据怪物的 `id` 获取对应的怪物模板
* @param id 怪物 `id`
*/
getPrefabById(id: string): IEnemy | null;
/**
* 删除指定的怪物模板
* @param code 怪物的图块数字或 `id`
*/
deletePrefab(code: number | string): void;
/**
* 修改一个已有的怪物模板,如果不存在则会新增
* @param code 怪物的图块数字或 `id`
* @param enemy 新的怪物模板
*/
changePrefab(code: number | string, enemy: IEnemy): void;
}
//#region 辅助接口
export interface IMapLocHelper {
/**
* 坐标 -> 索引
* @param x 横坐标
* @param y 纵坐标
*/
locToIndex(x: number, y: number): number;
/**
* 定位符 -> 索引
* @param locator 定位符
*/
locaterToIndex(locator: ITileLocator): number;
/**
* 索引 -> 定位符
* @param index 索引
*/
indexToLocator(index: number): ITileLocator;
}
export interface IMapLocIndexer extends IMapLocHelper {
/**
* 设置地图宽度
* @param width 地图宽度
*/
setWidth(width: number): void;
}
//#endregion
//#region 怪物对象
export interface IEnemyView {
/** 怪物视图所属的上下文 */
readonly context: IEnemyContext;
/**
* 重置此怪物视图的状态,将计算后怪物对象恢复至初始状态
*/
reset(): void;
/**
* 获取基本怪物对象
*/
getBaseEnemy(): IReadonlyEnemy;
/**
* 获取计算后的怪物对象,返回的怪物对象同引用
*/
getComputedEnemy(): IReadonlyEnemy;
/**
* 获取可修改的怪物对象。如果修改此方法获取的怪物对象,那么怪物的真实信息是不会刷新的,
* 需要手动调用 markDirty 方法来刷新。
*/
getModifiableEnemy(): IEnemy;
/**
* 将此怪物标记为脏,需要更新
*/
markDirty(): void;
}
//#endregion
//#region 光环与查询
export interface IEnemySpecialModifier {
/**
* 获取要添加到指定怪物身上的特殊属性
* @param enemy 怪物对象
* @param locator 怪物定位符
*/
add(enemy: IReadonlyEnemy, locator: ITileLocator): ISpecial<any>[];
/**
* 获取制定怪物身上要删除的特殊属性
* @param enemy 怪物对象
* @param locator 怪物定位符
*/
delete(enemy: IReadonlyEnemy, locator: ITileLocator): ISpecial<any>[];
/**
* 修改一个怪物的特殊属性,如果真正进行了修改则返回 true,否则返回 false
* @param enemy 怪物对象
* @param special 要修改的怪物特殊属性
* @param locator 怪物定位符
*/
modify(
enemy: IReadonlyEnemy,
special: ISpecial<any>,
locator: ITileLocator
): boolean;
}
export interface IAuraView<T = any> {
/** 此光环视图的优先级 */
readonly priority: number;
/** 此光环视图的影响范围 */
readonly range: IRange<T>;
/** 这个光环视图是否有可能修改怪物的基本属性 */
readonly couldApplyBase: boolean;
/** 这个光环视图是否有可能修改怪物的特殊属性 */
readonly couldApplySpecial: boolean;
/**
* 获取范围扫描参数
*/
getRangeParam(): T;
/**
* 对指定怪物对象施加修饰器
* @param enemy 怪物对象
* @param locator 怪物定位符
*/
apply(
enemy: IEnemy,
baseEnemy: IReadonlyEnemy,
locator: ITileLocator
): void;
/**
* 对指定怪物对象添加特殊属性修饰器
* @param enemy 怪物对象
* @param locator 怪物定位符
*/
applySpecial(
enemy: IReadonlyEnemy,
baseEnemy: IReadonlyEnemy,
locator: ITileLocator
): IEnemySpecialModifier | null;
}
export interface IEnemyAuraView<T, S> extends IAuraView<T> {
/** 此光环视图所属的怪物 */
readonly enemy: IReadonlyEnemy;
/** 此光环视图所属的特殊属性 */
readonly special: ISpecial<S>;
/** 此光环视图所属怪物的定位符 */
readonly locator: ITileLocator;
}
export interface IAuraConverter {
/**
* 判断一个特殊属性是否应该被当前光环转换器执行转换
*/
shouldConvert(
special: ISpecial<any>,
enemy: IReadonlyEnemy,
locator: ITileLocator
): boolean;
/**
* 将一个特殊属性转换为光环视图
*/
convert(
special: ISpecial<any>,
enemy: IReadonlyEnemy,
locator: ITileLocator
): IEnemyAuraView<any, any>;
}
export interface IEnemySpecialQueryModifier extends IEnemySpecialModifier {
/**
* 判断一个怪物是否应该查询外部状态
*/
shouldQuery(enemy: IReadonlyEnemy, locator: ITileLocator): boolean;
}
export interface IEnemySpecialQueryEffect {
/** 效果优先级,与光环属性共用 */
readonly priority: number;
/**
* 根据传入的怪物上下文,获取对应的怪物特殊属性修饰器
*/
for(ctx: IEnemyContext): IEnemySpecialQueryModifier;
}
export interface IEnemyCommonQueryEffect {
/** 优先级,越高的越先执行 */
readonly priority: number;
/**
* 对怪物的某个特殊属性施加常规查询效果
*/
apply(
enemy: IEnemy,
special: ISpecial<any>,
query: () => IEnemyContext,
locator: ITileLocator
): void;
}
export interface IEnemyFinalEffect {
/** 效果优先级,越高会越先被执行 */
readonly priority: number;
/**
* 向怪物施加最终修饰效果
*/
apply(enemy: IEnemy, locator: ITileLocator): void;
}
//#endregion
//#region 地图伤害
export interface IMapDamageInfoExtra {
/** 捕捉怪物信息 */
catch: Set<ITileLocator>;
/** 阻击怪物信息 */
repulse: Set<ITileLocator>;
}
export interface IMapDamageInfo {
/** 伤害值 */
damage: number;
/** 伤害类型 */
type: number;
/** 地图伤害额外信息 */
extra: IMapDamageInfoExtra;
}
export interface IMapDamageView<T = any> {
/** 获取地图伤害影响范围 */
getRange(): IRange<T>;
/** 获取范围参数 */
getRangeParam(): T;
/**
* 获取指定位置的地图伤害,会对坐标进行判断
* @param locator 伤害位置
*/
getDamageAt(locator: ITileLocator): Readonly<IMapDamageInfo> | null;
/**
* 获取指定位置的地图伤害,但是不会对坐标进行判断
* @param locator 伤害位置
*/
getDamageWithoutCheck(
locator: ITileLocator
): Readonly<IMapDamageInfo> | null;
}
export interface IMapDamageConverter {
/** 转换地图伤害视图 */
convert(
enemy: IReadonlyEnemy,
locator: ITileLocator,
context: IEnemyContext
): IMapDamageView<any>[];
}
export interface IMapDamageReducer {
/** 对伤害信息进行合并 */
reduce(
info: Iterable<Readonly<IMapDamageInfo>>,
locator: ITileLocator
): Readonly<IMapDamageInfo>;
}
export interface IMapDamage {
/** 当前绑定的怪物上下文 */
readonly context: IEnemyContext;
/**
* 设置地图伤害转换器,并基于当前上下文重建所有地图伤害视图
* @param converter 地图伤害转换器
*/
useConverter(converter: IMapDamageConverter): void;
/**
* 设置地图伤害合并器
* @param reducer 地图伤害合并器
*/
useReducer(reducer: IMapDamageReducer): void;
/**
* 在指定位置添加一条无来源地图伤害
* @param locator 地图定位符
* @param info 地图伤害信息
*/
addMapDamage(locator: ITileLocator, info: IMapDamageInfo): void;
/**
* 在指定位置删除一条无来源地图伤害
* @param locator 地图定位符
* @param info 地图伤害信息
*/
deleteMapDamage(locator: ITileLocator, info: IMapDamageInfo): void;
/**
* 将指定位置标记为脏,后续访问时会重新计算该点的有来源伤害
* @param locator 地图定位符
*/
markDirty(locator: ITileLocator): void;
/**
* 将指定怪物对应的地图伤害标记为脏并刷新
* @param view 怪物视图
*/
markEnemyDirty(view: IEnemyView): void;
/**
* 基于当前上下文重新刷新全部有来源地图伤害
*/
refreshAll(): void;
/**
* 删除指定怪物带来的全部地图伤害来源
* @param view 怪物视图
*/
deleteEnemy(view: IEnemyView): void;
/**
* 获取指定位置合并后的地图伤害
* @param locator 地图定位符
*/
getReducedDamage(locator: ITileLocator): Readonly<IMapDamageInfo> | null;
/**
* 获取指定位置未合并的地图伤害列表
* @param locator 地图定位符
*/
getSeparatedDamage(
locator: ITileLocator
): Iterable<Readonly<IMapDamageInfo>>;
}
//#endregion
//#region 上下文
export interface IEnemyContext {
/** 怪物上下文宽度 */
readonly width: number;
/** 怪物上下文高度 */
readonly height: number;
/**
* 调整上下文尺寸,并清空当前上下文中的所有怪物与状态
* @param width 地图宽度
* @param height 地图高度
*/
resize(width: number, height: number): void;
/**
* 注册一个光环转换器
* @param converter 光环转换器
*/
registerAuraConverter(converter: IAuraConverter): void;
/**
* 注销一个光环转换器
* @param converter 光环转换器
*/
unregisterAuraConverter(converter: IAuraConverter): void;
/**
* 设置光环转换器的启用状态
* @param converter 光环转换器
* @param enabled 是否启用
*/
setAuraConverterEnabled(converter: IAuraConverter, enabled: boolean): void;
/**
* 注册一个特殊属性查询效果
* @param effect 特殊属性查询效果
*/
registerSpecialQueryEffect(effect: IEnemySpecialQueryEffect): void;
/**
* 注销一个特殊属性查询效果
* @param effect 特殊属性查询效果
*/
unregisterSpecialQueryEffect(effect: IEnemySpecialQueryEffect): void;
/**
* 为指定特殊属性代码注册常规查询效果
* @param code 特殊属性代码
* @param effect 常规查询效果
*/
registerCommonQueryEffect(
code: number,
effect: IEnemyCommonQueryEffect
): void;
/**
* 注销指定特殊属性代码上的常规查询效果
* @param code 特殊属性代码
* @param effect 常规查询效果
*/
unregisterCommonQueryEffect(
code: number,
effect: IEnemyCommonQueryEffect
): void;
/**
* 注册一个最终效果
* @param effect 最终效果
*/
registerFinalEffect(effect: IEnemyFinalEffect): void;
/**
* 注销一个最终效果
* @param effect 最终效果
*/
unregisterFinalEffect(effect: IEnemyFinalEffect): void;
/**
* 获取指定怪物对象当前所在位置
* @param enemy 怪物对象
*/
getEnemyLocator(enemy: IEnemy): Readonly<ITileLocator> | null;
/**
* 获取指定怪物视图当前所在位置
* @param view 怪物视图
*/
getEnemyLocatorByView(view: IEnemyView): Readonly<ITileLocator> | null;
/**
* 根据定位符获取怪物视图
* @param locator 地图定位符
*/
getEnemyByLocator(locator: ITileLocator): IEnemyView | null;
/**
* 根据坐标获取怪物视图
* @param x 横坐标
* @param y 纵坐标
*/
getEnemyByLoc(x: number, y: number): IEnemyView | null;
/**
* 根据计算后怪物对象反查怪物视图
* @param enemy 计算后怪物对象
*/
getViewByComputed(enemy: IReadonlyEnemy): IEnemyView | null;
/**
* 在指定位置放置一个怪物对象
* @param locator 地图定位符
* @param enemy 怪物对象
*/
setEnemyAt(locator: ITileLocator, enemy: IEnemy): void;
/**
* 删除指定位置的怪物
* @param locator 地图定位符
*/
deleteEnemy(locator: ITileLocator): void;
/**
* 扫描指定范围内的怪物视图
* @param range 范围对象
* @param param 范围参数
*/
scanRange<T>(range: IRange<T>, param: T): Iterable<IEnemyView>;
/**
* 迭代上下文中的全部怪物
*/
iterateEnemy(): Iterable<[ITileLocator, IEnemyView]>;
/**
* 添加一个全局光环视图
* @param aura 光环视图
*/
addAura(aura: IAuraView): void;
/**
* 删除一个全局光环视图
* @param aura 光环视图
*/
deleteAura(aura: IAuraView): void;
/**
* 绑定地图伤害管理器
* @param damage 地图伤害管理器
*/
attachMapDamage(damage: IMapDamage | null): void;
/**
* 获取当前绑定的地图伤害管理器
*/
getMapDamage(): IMapDamage | null;
/**
* 重建当前上下文中的全部怪物计算结果
*
* 1. 对所有光环及特殊查询进行构建操作,这一步中会决定每个怪物所拥有的特殊属性,后续不会变动
* 2. 执行所有的普通光环效果,修改怪物的基础属性
* 3. 执行常规查询效果,允许查询上下文状态并修改怪物自身的基础属性
* 4. 执行最终效果,不允许查询上下文状态,仅允许修改怪物自身的基础属性
*/
buildup(): void;
/**
* 将指定怪物视图标记为脏
* @param view 怪物视图
*/
markDirty(view: IEnemyView): void;
/**
* 申请刷新指定怪物视图
* @param view 怪物视图
*/
requestRefresh(view: IEnemyView): void;
/**
* 清空当前上下文中的所有对象与运行状态
*/
clear(): void;
/**
* 销毁当前上下文
*/
destroy(): void;
}
//#endregion
@@ -0,0 +1,25 @@
import { ITileLocator } from '@user/types';
import { IMapLocIndexer } from './types';
export class MapLocIndexer implements IMapLocIndexer {
private width: number = 0;
setWidth(width: number): void {
this.width = width;
}
locToIndex(x: number, y: number): number {
return y * this.width + x;
}
locaterToIndex(locator: ITileLocator): number {
return locator.y * this.width + locator.x;
}
indexToLocator(index: number): ITileLocator {
return {
x: index % this.width,
y: Math.floor(index / this.width)
};
}
}
+1
View File
@@ -1,3 +1,4 @@
export * from './enemy';
export * from './load';
export * from './game';