refactor: 数据端用户结构

This commit is contained in:
AncTe committed 2026-10-16 17:29:51 +08:00
1 parent 0a133058ca
commit a0e402728e
41 files changed
+901 -100

No files matched your search

+1 -1
View File
@@ -175,7 +175,7 @@ export class GuardAura implements IEnemyAuraView<
if (locator.x === this.locator.x && locator.y === this.locator.y) {
return;
}
enemy.getAttribute('guard').add(this.locator);
enemy.getAttribute('guard').add(this.sourceView);
}
applySpecial(): IEnemySpecialModifier<IEnemyAttr> | null {
+1 -7
View File
@@ -5,7 +5,6 @@ import {
IReadonlyEnemyHandler
} from '@user/data-system';
import { IEnemyAttr, IHeroAttr } from '@user/data-common';
import { logger } from '@motajs/common';
import { ISpecial9Value } from './special';
export class MainDamageCalculator implements IDamageCalculator<
@@ -83,13 +82,8 @@ export class MainDamageCalculator implements IDamageCalculator<
this.inGuard = true;
// 计算支援怪的伤害,同时把打支援怪花费的回合数加到当前怪物上,因为打支援怪的时候当前怪物也会打你
// 因此回合数需要加上打支援怪的回合数
for (const guard of guards) {
for (const view of guards) {
// 直接把 enemy 传过去,因此支援的 enemy 会吃到其原本所在位置的光环加成
const view = handler.context.getEnemyByLocator(guard);
if (!view) {
logger.warn(137, guard.x.toString(), guard.y.toString());
continue;
}
const extraInfo = this.calculate({
enemy: view.getComputedEnemy(),
context: handler.context,
+222
View File
@@ -0,0 +1,222 @@
import {
IGameMap,
IHeroLocation,
IHeroLocationHooks,
IHeroModifier,
IHeroMover,
PercentageModifier
} from '@user/data-base';
import {
IEnemyAttr,
IHeroAttr,
IObjectMover,
IObjectMoverHooks,
ObjectMoveStep,
ObjectMoveType
} from '@user/data-common';
import { ICoreState } from '@user/data-state';
import {
ICombatFlowHooks,
IEnemyDamageInfo,
IPathfindingResult,
IPathfindingSystem,
IPathfindingSystemHooks
} from '@user/data-system';
import { ICombatWeakHandler } from './types';
import { isNil } from 'lodash-es';
export class EnemyMoveHooks implements IObjectMoverHooks<IHeroLocation> {
constructor(readonly state: ICoreState) {}
/**
* 处理中毒效果
* @param step 移动步对象
*/
private handlePoison(
step: Readonly<ObjectMoveStep>,
mover: IHeroMover<IHeroLocation>
) {
const poison = this.state.flags.getFieldValueDefaults('poison', 0);
// 中毒伤害小于等于 0,直接返回
if (poison <= 0) return;
const config = mover.getConfig();
// 如果忽略地形,那么也不计算中毒伤害
if (config.ignoreTerrain) return;
// 获取勇士的可修改属性对象
const hero = this.state.hero.getModifiableAttribute();
// 判断是否为常规移动步
if (
step.type === ObjectMoveType.Dir ||
step.type === ObjectMoveType.DirFace ||
step.type === ObjectMoveType.Special
) {
// 给勇士加上 -poison 的生命值,就相当于扣 poison 的生命值
hero.add('hp', -poison);
}
}
async onStepEnd(
_code: number,
step: Readonly<ObjectMoveStep>,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 检查状态对象
if (tile.state !== this.state) return;
// 检查移动器是否为勇士移动器
if (mover !== this.state.hero.location.mover) return;
// 处理中毒
this.handlePoison(step, mover as IHeroMover<IHeroLocation>);
}
}
export class EnemyPathfindingHooks implements IPathfindingSystemHooks {
constructor(readonly state: ICoreState) {}
/**
* 处理中毒效果
* @param path 自动寻路路径
*/
private handlePoison(path: IPathfindingResult) {
const poison = this.state.flags.getFieldValueDefaults('poison', 0);
// 中毒伤害小于等于 0,直接返回
if (poison <= 0) return;
const mover = this.state.hero.location.mover;
const config = mover.getConfig();
// 如果忽略地形,那么也不计算中毒伤害
if (config.ignoreTerrain) return;
const hero = this.state.hero.getModifiableAttribute();
// 勇士减少步数乘以单步中毒的伤害。
hero.add('hp', -path.path.length);
}
onAfterTeleportTo(
path: IPathfindingResult,
system: IPathfindingSystem
): void {
// 判断状态对象
if (system.state !== this.state) return;
// 判断当前绑定的是不是勇士移动器
if (system.mover !== this.state.hero.location.mover) return;
// 处理中毒
this.handlePoison(path);
}
}
export class EnemyCombatHooks
implements ICombatFlowHooks<IEnemyAttr, IHeroAttr>, ICombatWeakHandler
{
/** 当前的虚弱值 */
private weakValue: number = 0;
/** 攻击虚弱修饰器 */
private readonly weakAtkModifier: IHeroModifier<number, number, number>;
/** 防御虚弱修饰器 */
private readonly weakDefModifier: IHeroModifier<number, number, number>;
constructor(readonly state: ICoreState) {
const hero = state.hero.getModifiableAttribute();
// 初始化虚弱属性的修饰器,并添加到勇士修饰器中
this.weakAtkModifier = new PercentageModifier(0);
this.weakDefModifier = new PercentageModifier(0);
hero.addModifier('atk', this.weakAtkModifier);
hero.addModifier('def', this.weakDefModifier);
}
setWeakValue(value: number): void {
this.weakValue = value;
// 需要填写负数才能使勇士属性降低
this.weakAtkModifier.setValue(-value);
this.weakDefModifier.setValue(-value);
}
getWeakValue(): number {
return this.weakValue;
}
async onAfterCombat(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>
): Promise<void> {
// 判断状态对象
if (info.handler.state !== this.state) return;
// 判断勇士对象
if (info.handler.hero !== this.state.hero.getAttribute()) return;
// 处理中毒属性
const poison = info.handler.enemy.getSpecial<number>(10);
if (poison) {
// 获取 poison 全局 flag,如果 flag 不存在,则自动插入为 0
const field = this.state.flags.getOrInsert('poison', 0);
const max = Math.max(field.get(), poison.value);
field.set(max);
}
// 处理虚弱属性
const weak = info.handler.enemy.getSpecial<number>(11);
if (weak) {
const max = Math.max(this.weakValue, weak.value);
this.setWeakValue(max);
}
// 处理重生属性
if (info.handler.enemy.hasSpecial(21)) {
const field = this.state.flags.getFieldValueDefaults<
Map<string, [number, number, number][]>
>('rebirth', new Map());
// 如果战斗不发生在任何地图,或者不存在坐标,那么不计入重生
if (isNil(info.handler.context.floor) || !info.handler.locator) {
return;
}
// 获取当前楼层的重生存储,并向其中添加重生标记
const floor = field.getOrInsert(info.handler.context.floor, []);
const { x, y } = info.handler.locator;
floor.push([info.handler.enemy.code, x, y]);
}
}
}
export class EnemyChangeFloorHooks implements IHeroLocationHooks {
constructor(readonly state: ICoreState) {}
/**
* 处理重生属性
* @param floor 目标楼层
*/
private handleRebirth(map: IGameMap) {
// 先获取指定楼层的重生标记
const field = this.state.flags.getFieldValueDefaults<
Map<string, [number, number, number][]>
>('rebirth', new Map());
const list = field.get(map.floorId);
// 如果不存在重生,直接返回
if (!list || list.length === 0) return;
const event = map.eventLayer;
// 如果不存在事件层,直接返回
if (!event) return;
// 把每个重生怪都放到事件层上
for (const [tile, x, y] of list) {
event.setBlock(tile, x, y);
}
// 清空重生怪列表
list.length = 0;
}
onSetFloor(map: IGameMap | null): void {
if (!map) return;
// 判断状态对象
if (map.state !== this.state) return;
// 处理重生
this.handleRebirth(map);
}
}
+23 -1
View File
@@ -10,13 +10,23 @@ import { MainEnemyFinalEffect } from './final';
import { MainDamageCalculator } from './calculator';
import { MainEnemyComparer } from './comparer';
import { MainMapDamageConverter, MainMapDamageReducer } from './mapDamage';
import { IEnemyInitializationReturns } from './types';
import {
EnemyChangeFloorHooks,
EnemyCombatHooks,
EnemyMoveHooks,
EnemyPathfindingHooks
} from './hook';
import { MainCombatScript } from './script';
/**
* 初始化怪物相关内容
* @param state 数据端主对象
* @param config 数据端配置
*/
export function initializeEnemy(state: ICoreState) {
export function initializeEnemy(
state: ICoreState
): IEnemyInitializationReturns {
// 怪物管理器初始化
const comparer = new MainEnemyComparer();
state.enemyManager.attachEnemyComparer(comparer);
@@ -45,4 +55,16 @@ export function initializeEnemy(state: ICoreState) {
// 注册怪物最终效果
ctx.registerFinalEffect(new MainEnemyFinalEffect());
// 添加默认战前战后脚本
state.combat.addCombatScript(new MainCombatScript());
// 注册相关的钩子
state.hero.location.mover.addHook(new EnemyMoveHooks(state));
state.pathfinding.addHook(new EnemyPathfindingHooks(state));
const combatHook = new EnemyCombatHooks(state);
state.combat.addHook(combatHook);
state.hero.location.addHook(new EnemyChangeFloorHooks(state));
return { weak: combatHook };
}
+60
View File
@@ -0,0 +1,60 @@
import { IEnemyAttr, IHeroAttr } from '@user/data-common';
import {
ICombatFlowHandler,
ICombatScript,
IEnemyDamageInfo
} from '@user/data-system';
export class MainCombatScript implements ICombatScript<IEnemyAttr, IHeroAttr> {
priority: number = 0;
async before(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>,
handler: ICombatFlowHandler<IEnemyAttr, IHeroAttr>
): Promise<boolean> {
const hero = handler.hero;
// 如果伤害大于勇士血量,直接不允许战斗
if (!handler.force && hero.getFinalAttribute('hp') < info.damage) {
return false;
}
return true;
}
async after(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>,
handler: ICombatFlowHandler<IEnemyAttr, IHeroAttr>
): Promise<void> {
const hero = handler.hero;
const readonlyEnemy = info.handler.enemy;
// 计算金币和经验,支援怪也算上
const baseExp = readonlyEnemy.getAttribute('exp');
const baseMoney = readonlyEnemy.getAttribute('money');
const [guardExp, guardMoney] = [
...readonlyEnemy.getAttribute('guard')
].reduce(
(prev, curr) => {
const attr = curr.getComputedEnemy();
const exp = attr.getAttribute('exp');
const money = attr.getAttribute('money');
return [prev[0] + exp, prev[1] + money];
},
[0, 0]
);
const totalExp = baseExp + guardExp;
const totalMoney = baseMoney + guardMoney;
// 给勇士添加经验和金币
hero.add('exp', totalExp);
hero.add('money', totalMoney);
// 进行全局提示
handler.state.emitTip(
`打败${readonlyEnemy.getAttribute('name')},金币+${totalMoney},经验+${totalExp}。`,
readonlyEnemy.code
);
}
}
+5 -16
View File
@@ -77,10 +77,10 @@ export interface ISpecial26Value {
* 2. 坚固、模仿等仅影响自身基础属性的属性:./final.ts, MainEnemyFinalEffect.apply 方法
* 3. 地图伤害:./mapDamage.ts,具体写法参考文档
* 4. 光环属性:./aura.ts 具体写法参考文档
* 5. 中毒的每步效果:在插件系统中,目前暂未实装
* 6. 中毒的瞬移效果:在插件系统中,目前暂未实装
* 7. 衰弱效果:在插件系统中,目前尚未实装
* 8. 重生属性:在插件系统中,目前尚未实装
* 5. 中毒的每步效果:./hook.ts EnemyMoveHooks.handlePoison 方法
* 6. 中毒的瞬移效果:./hook.ts EnemyPathfindingHooks.handlePoison 方法
* 7. 衰弱效果:./hook.ts EnemyCombatHooks.onAfterCombat 方法
* 8. 重生属性:./hook.ts EnemyCombatHooks.onAfterCombat 和 EnemyChangeFloorHooks.onSetFloor 方法
*/
export function createSpecials(
state: ICoreState
@@ -233,18 +233,7 @@ export function createSpecials(
name: '衰弱',
desc: special => {
const weak = special.value;
const suffix = ',多次触发时取最高值。';
if (weak < 1) {
return (
`战斗后,角色陷入衰弱状态,攻防暂时下降${Math.floor(weak * 100)}%` +
suffix
);
} else {
return (
`战斗后,角色陷入衰弱状态,攻防暂时下降${weak}点` +
suffix
);
}
return `战斗后,角色陷入衰弱状态,攻防暂时下降${Math.floor(weak * 100)}%,多次触发时取最高值。`;
},
color: '#F0BBCC'
})
+18
View File
@@ -10,3 +10,21 @@ export const enum MapDamageType {
/** 夹击伤害 */
Between
}
export interface IEnemyInitializationReturns {
/** 虚弱处理对象 */
readonly weak: ICombatWeakHandler;
}
export interface ICombatWeakHandler {
/**
* 设置当前的虚弱值
* @param value 虚弱值
*/
setWeakValue(value: number): void;
/**
* 获取当前的虚弱值
*/
getWeakValue(): number;
}
+24
View File
@@ -0,0 +1,24 @@
import { IEnemyAttr, IHeroAttr } from '@user/data-common';
import { ICoreState } from '@user/data-state';
import { ICombatFlowHooks, IEnemyDamageInfo } from '@user/data-system';
export class MainCombatFlowHooks implements ICombatFlowHooks<
IEnemyAttr,
IHeroAttr
> {
constructor(readonly state: ICoreState) {}
async onBeforeCombat(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>
): Promise<void> {
// 删除下面这一行开始编写你自己的战前钩子
void info;
}
async onAfterCombat(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>
): Promise<void> {
// 删除下面这一行开始编写你自己的战后钩子
void info;
}
}
+30
View File
@@ -0,0 +1,30 @@
import { ICoreState } from '@user/data-state';
import {
IBlockEventParam,
IGameEventExecutorHooks,
IGameEventInvocation
} from '@user/data-system';
export class MainGameEventExecutorHooks implements IGameEventExecutorHooks {
constructor(readonly state: ICoreState) {}
onBeforeExecuteEvent(
events: IGameEventInvocation[],
param: IBlockEventParam
): void {
// 删除下面这两行注释开始编写你自己的事件执行前钩子
void events;
void param;
}
onAfterExecuteEvent(
events: IGameEventInvocation[],
param: IBlockEventParam,
returns: unknown
): void {
// 删除下面这三行注释开始编写你自己的事件执行后钩子
void events;
void param;
void returns;
}
}
+141
View File
@@ -0,0 +1,141 @@
import {
IGameMap,
IHeroAttributeHooks,
IHeroChangeFloorInfo,
IHeroLocation,
IHeroLocationHooks,
IHeroStateHooks
} from '@user/data-base';
import {
FaceDirection,
IHeroAttr,
IObjectMover,
IObjectMoverHooks,
ObjectMoveStep
} from '@user/data-common';
import { ICoreState } from '@user/data-state';
export class MainHeroAttributeHooks implements IHeroAttributeHooks<IHeroAttr> {
constructor(readonly state: ICoreState) {}
onUpdateAttribute<K extends keyof IHeroAttr>(
name: K,
value: IHeroAttr[K]
): void {
// 删除下面这两行开始编写你自己的勇士属性更新钩子
void name;
void value;
}
}
export class MainHeroLocationHooks implements IHeroLocationHooks {
constructor(readonly state: ICoreState) {}
onSetFloor(map: IGameMap | null): void {
// 删除下面这两行开始编写你自己的切换楼层钩子
void map;
}
onSetPos(x: number, y: number): void {
// 删除下面这两行开始编写你自己的勇士位置设置钩子
void x;
void y;
}
}
export class MainHeroMoverHooks implements IObjectMoverHooks<IHeroLocation> {
constructor(readonly state: ICoreState) {}
async onMoveStart(
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这两行注释开始编写你自己的勇士移动前钩子
void tile;
void mover;
}
async onStepStart(
code: number,
step: Readonly<ObjectMoveStep>,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这四行注释开始编写你自己的勇士每步开始时钩子
void code;
void step;
void tile;
void mover;
}
async onStepEnd(
code: number,
step: Readonly<ObjectMoveStep>,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这四行注释开始编写你自己的勇士每步结束后钩子
void code;
void step;
void tile;
void mover;
}
async onMoveEnd(
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这两行注释开始编写你自己的勇士移动前钩子
void tile;
void mover;
}
onSetFaceDir(
dir: FaceDirection,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): void {
// 删除下面这三行注释开始编写你自己的设置面朝朝向的钩子
void dir;
void tile;
void mover;
}
onSetMoveDir(
dir: FaceDirection,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): void {
// 删除下面这三行注释开始编写你自己的设置移动朝向的钩子
void dir;
void tile;
void mover;
}
onSetPos(
x: number,
y: number,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): void {
// 删除下面这三行注释开始编写你自己的设置位置的钩子
void x;
void y;
void tile;
void mover;
}
}
export class MainHeroStateHooks implements IHeroStateHooks {
constructor(readonly state: ICoreState) {}
async onBeforeChangeFloor(info: IHeroChangeFloorInfo): Promise<void> {
// 删除下面这一行注释开始编写你自己的楼层转换前钩子
void info;
}
async onAfterChangeFloor(info: IHeroChangeFloorInfo): Promise<void> {
// 删除下面这一行注释开始编写你自己的楼层转换后钩子
void info;
}
}
+6
View File
@@ -0,0 +1,6 @@
export * from './combat';
export * from './event';
export * from './hero';
export * from './initialize';
export * from './loader';
export * from './path';
+26
View File
@@ -0,0 +1,26 @@
import { ICoreState } from '@user/data-state';
import { MainCombatFlowHooks } from './combat';
import { MainGameEventExecutorHooks } from './event';
import {
MainHeroAttributeHooks,
MainHeroLocationHooks,
MainHeroMoverHooks,
MainHeroStateHooks
} from './hero';
import { MainMotaDataLoaderHooks } from './loader';
import { MainPathfindingSystemHooks } from './path';
/**
* 初始化数据端主要钩子
* @param state 数据端主对象
*/
export function initializeHooks(state: ICoreState) {
state.combat.addHook(new MainCombatFlowHooks(state));
state.eventSystem.executor.addHook(new MainGameEventExecutorHooks(state));
state.hero.addHook(new MainHeroStateHooks(state));
state.hero.attribute.addHook(new MainHeroAttributeHooks(state));
state.hero.location.addHook(new MainHeroLocationHooks(state));
state.hero.location.mover.addHook(new MainHeroMoverHooks(state));
state.loader.addHook(new MainMotaDataLoaderHooks(state));
state.pathfinding.addHook(new MainPathfindingSystemHooks(state));
}
+19
View File
@@ -0,0 +1,19 @@
import {
ICoreState,
IMotaDataLoader,
IMotaDataLoaderHooks
} from '@user/data-state';
export class MainMotaDataLoaderHooks implements IMotaDataLoaderHooks {
constructor(readonly state: ICoreState) {}
async onCoreConfigLoaded(loader: IMotaDataLoader): Promise<void> {
// 删除下面这两行注释开始编写你自己的核心配置加载后钩子
void loader;
}
async onExtraConfigLoaded(loader: IMotaDataLoader): Promise<void> {
// 删除下面这两行注释开始编写你自己的额外配置加载后钩子
void loader;
}
}
+28
View File
@@ -0,0 +1,28 @@
import { ICoreState } from '@user/data-state';
import {
IPathfindingResult,
IPathfindingSystem,
IPathfindingSystemHooks
} from '@user/data-system';
export class MainPathfindingSystemHooks implements IPathfindingSystemHooks {
constructor(readonly state: ICoreState) {}
onBeforeTeleportTo(
path: IPathfindingResult,
system: IPathfindingSystem
): void {
// 删除下面这两行注释开始编写你自己的自动寻路瞬移前钩子,注意非勇士的瞬移也会触发
void path;
void system;
}
onAfterTeleportTo(
path: IPathfindingResult,
system: IPathfindingSystem
): void {
// 删除下面这两行注释开始编写你自己的自动寻路瞬移后钩子,注意非勇士的瞬移也会触发
void path;
void system;
}
}
+11 -8
View File
@@ -5,21 +5,21 @@ import {
FaceGroup,
IFacedTileLocator
} from '@user/data-common';
import {
ICoreState,
ICoreStateCoreConfig,
IDataPluginDefine
} from '@user/data-state';
import { initializeEnemy } from './enemy';
import { ICoreState, ICoreStateCoreConfig } from '@user/data-state';
import { ICombatWeakHandler, initializeEnemy } from './enemy';
import { initializeReplay } from './replay';
import { logger } from '@motajs/common';
import { DefaultHeroMoveTopImpl, DefaultPassPredicateImpl } from './hero';
import { DefaultDataLoaderHook } from './loader';
import { IDataMainPlugin } from './types';
import { initializeHooks } from './hook';
export class MainDataPlugin implements IDataPluginDefine {
export class MainDataPlugin implements IDataMainPlugin {
readonly id: string = 'main-data-plugin';
readonly dependencies: string[] = [];
weak!: ICombatWeakHandler;
constructor(readonly heroFace: FaceGroup) {}
awake(state: ICoreState): void {
@@ -46,8 +46,11 @@ export class MainDataPlugin implements IDataPluginDefine {
state.loader.addHook(new DefaultDataLoaderHook());
// 其他复杂初始化
initializeEnemy(state);
const enemyReturns = initializeEnemy(state);
initializeReplay(state);
initializeHooks(state);
this.weak = enemyReturns.weak;
// 存档初始化
state.addSaveableContent('@system/hero', state.hero);
+2 -2
View File
@@ -1,5 +1,5 @@
import { IDataPluginDefine } from '@user/data-state';
import { ShopPlugin } from './shop';
import { DataShopPlugin } from './shop';
/** 数据端插件列表 */
export const dataPlugins: IDataPluginDefine[] = [new ShopPlugin()];
export const dataPlugins: IDataPluginDefine[] = [new DataShopPlugin()];
+5 -1
View File
@@ -1 +1,5 @@
export * from './shop';
export * from './manager';
export * from './plugin';
export * from './state';
export * from './system';
export * from './types';
@@ -4,8 +4,8 @@ import { ShopManager } from './manager';
import { ShopState } from './state';
import { ShopSystem } from './system';
export class ShopPlugin implements IShopPlugin {
id: string = 'shop-plugin';
export class DataShopPlugin implements IShopPlugin {
id: string = 'data-shop-plugin';
dependencies: string[] = [];
shop!: IShopSystem;
+7
View File
@@ -0,0 +1,7 @@
import { IDataPluginDefine } from '@user/data-state';
import { ICombatWeakHandler } from './enemy';
export interface IDataMainPlugin extends IDataPluginDefine {
/** 虚弱处理对象 */
readonly weak: ICombatWeakHandler;
}