mirror of
https://github.com/motajs/template.git
synced 2026-11-28 08:54:30 +08:00
refactor: 数据端用户结构
This commit is contained in:
1 parent
0a133058ca
commit
a0e402728e
41 files changed
+901
-100
No files matched your search
@@ -84,10 +84,12 @@ export class CombatFlow<TEnemy, THero>
|
||||
*/
|
||||
private createHandler(
|
||||
enemy: IReadonlyEnemy<TEnemy>,
|
||||
locator: ITileLocator | null
|
||||
locator: ITileLocator | null,
|
||||
force: boolean
|
||||
): ICombatFlowHandler<TEnemy, THero> {
|
||||
return {
|
||||
onMap: locator !== null,
|
||||
force,
|
||||
hero: this.hero!,
|
||||
enemy,
|
||||
context: this.context!,
|
||||
@@ -205,7 +207,8 @@ export class CombatFlow<TEnemy, THero>
|
||||
|
||||
@shouldReplay('Battle with enemy should be replayed.')
|
||||
battle(
|
||||
enemy: IEnemyView<TEnemy>
|
||||
enemy: IEnemyView<TEnemy>,
|
||||
force: boolean = false
|
||||
): Promise<IEnemyDamageInfo<TEnemy, THero> | null> {
|
||||
if (!this.context) {
|
||||
logger.warn(139, 'an enemy context object');
|
||||
@@ -219,7 +222,7 @@ export class CombatFlow<TEnemy, THero>
|
||||
const computed = enemy.getComputedEnemy();
|
||||
const origin = enemy.getModifiableEnemy();
|
||||
const locator = this.tryGetEnemyLocator(enemy, computed, origin);
|
||||
const handler = this.createHandler(origin, locator);
|
||||
const handler = this.createHandler(origin, locator, force);
|
||||
const eHandler = this.createEnemyHandler(computed, locator);
|
||||
|
||||
return this.combatFlow(handler, eHandler);
|
||||
@@ -227,7 +230,8 @@ export class CombatFlow<TEnemy, THero>
|
||||
|
||||
@shouldReplay('Battle with enemy should be replayed.')
|
||||
battleComputed(
|
||||
enemy: IReadonlyEnemy<TEnemy>
|
||||
enemy: IReadonlyEnemy<TEnemy>,
|
||||
force: boolean = false
|
||||
): Promise<IEnemyDamageInfo<TEnemy, THero> | null> {
|
||||
if (!this.context) {
|
||||
logger.warn(139, 'an enemy context object');
|
||||
@@ -243,7 +247,7 @@ export class CombatFlow<TEnemy, THero>
|
||||
if (view) return this.battle(view);
|
||||
else {
|
||||
// 否则走单独的流程
|
||||
const handler = this.createHandler(enemy, null);
|
||||
const handler = this.createHandler(enemy, null, force);
|
||||
const eHandler = this.createEnemyHandler(enemy, null);
|
||||
return this.combatFlow(handler, eHandler);
|
||||
}
|
||||
|
||||
@@ -102,9 +102,14 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
|
||||
built: boolean = false;
|
||||
width: number = 0;
|
||||
height: number = 0;
|
||||
floor: string | undefined;
|
||||
|
||||
constructor(readonly state: IStateBase) {}
|
||||
|
||||
setFloor(floor: string | undefined): void {
|
||||
this.floor = floor;
|
||||
}
|
||||
|
||||
resize(width: number, height: number): void {
|
||||
this.clear();
|
||||
this.width = width;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './combat';
|
||||
export * from './context';
|
||||
export * from './damage';
|
||||
export * from './enemy';
|
||||
|
||||
@@ -537,6 +537,8 @@ export interface IReadonlyEnemyContext<
|
||||
readonly width: number;
|
||||
/** 怪物上下文高度 */
|
||||
readonly height: number;
|
||||
/** 怪物上下文对应的楼层 */
|
||||
readonly floor: string | undefined;
|
||||
/** 此上下文使用的索引对象 */
|
||||
readonly indexer: ILocationHelper;
|
||||
|
||||
@@ -612,9 +614,16 @@ export interface IEnemyContext<TEnemy, THero> extends IReadonlyEnemyContext<
|
||||
readonly width: number;
|
||||
/** 怪物上下文高度 */
|
||||
readonly height: number;
|
||||
|
||||
/** 此上下文使用的索引对象 */
|
||||
readonly indexer: ILocationHelper;
|
||||
|
||||
/**
|
||||
* 设置怪物上下文对应的楼层
|
||||
* @param floor 上下文对应的楼层
|
||||
*/
|
||||
setFloor(floor: string | undefined): void;
|
||||
|
||||
/**
|
||||
* 调整上下文尺寸,并清空当前上下文中的所有怪物与状态
|
||||
* @param width 地图宽度
|
||||
@@ -775,6 +784,8 @@ export interface IEnemyContext<TEnemy, THero> extends IReadonlyEnemyContext<
|
||||
export interface ICombatFlowHandler<TEnemy, THero> extends IDataBaseExtended {
|
||||
/** 战斗的怪物是否在地图上 */
|
||||
readonly onMap: boolean;
|
||||
/** 是否为强制战斗 */
|
||||
readonly force: boolean;
|
||||
/** 可修改勇士对象 */
|
||||
readonly hero: IHeroAttribute<THero>;
|
||||
/** 可修改怪物对象 */
|
||||
@@ -863,9 +874,11 @@ export interface ICombatFlow<TEnemy, THero>
|
||||
/**
|
||||
* 与指定怪物战斗
|
||||
* @param enemy 怪物视图对象
|
||||
* @param force 是否为强制战斗,默认为 `false`。强制战斗下,不再有战败保护
|
||||
*/
|
||||
battle(
|
||||
enemy: IEnemyView<TEnemy>
|
||||
enemy: IEnemyView<TEnemy>,
|
||||
force?: boolean
|
||||
): Promise<IEnemyDamageInfo<TEnemy, THero> | null>;
|
||||
|
||||
/**
|
||||
@@ -879,8 +892,12 @@ export interface ICombatFlow<TEnemy, THero>
|
||||
/**
|
||||
* 添加战前战后脚本
|
||||
* @param script 战前战后脚本
|
||||
* @param force 是否为强制战斗,默认为 `false`。强制战斗下,不再有战败保护
|
||||
*/
|
||||
addCombatScript(script: ICombatScript<TEnemy, THero>): void;
|
||||
addCombatScript(
|
||||
script: ICombatScript<TEnemy, THero>,
|
||||
force?: boolean
|
||||
): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -1,4 +1,10 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IHookBase,
|
||||
IHookController,
|
||||
logger
|
||||
} from '@motajs/common';
|
||||
import { IGameEventStore, shouldReplay } from '@user/data-common';
|
||||
import { AnonTokyoInterpreter } from '@motajs/anon-tokyo';
|
||||
import {
|
||||
@@ -7,17 +13,29 @@ import {
|
||||
IGameEventExecutor,
|
||||
IBlockEventEnv,
|
||||
IBlockEventParam,
|
||||
IGameEventInvocation
|
||||
IGameEventInvocation,
|
||||
IGameEventExecutorHooks
|
||||
} from './types';
|
||||
|
||||
export class EventExecutor implements IGameEventExecutor {
|
||||
export class EventExecutor
|
||||
extends Hookable<IGameEventExecutorHooks>
|
||||
implements IGameEventExecutor
|
||||
{
|
||||
mode: EventExecuteMode = EventExecuteMode.Normal;
|
||||
reduce: EventReduceMode = EventReduceMode.NoReduce;
|
||||
|
||||
constructor(
|
||||
readonly interpreter: AnonTokyoInterpreter,
|
||||
private readonly storeRef: () => IGameEventStore | null
|
||||
) {}
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IHookBase>
|
||||
): IHookController<IHookBase> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
setMode(mode: EventExecuteMode): void {
|
||||
this.mode = mode;
|
||||
@@ -57,6 +75,7 @@ export class EventExecutor implements IGameEventExecutor {
|
||||
events: IGameEventInvocation[],
|
||||
param: IBlockEventParam
|
||||
): Promise<R> {
|
||||
this.forEachHook(hook => hook.onBeforeExecuteEvent?.(events, param));
|
||||
// 事件执行
|
||||
const results: any[] = [];
|
||||
for (const invocation of events) {
|
||||
@@ -86,6 +105,10 @@ export class EventExecutor implements IGameEventExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
return this.reduceResults(this.reduce, results);
|
||||
const reduced = this.reduceResults(this.reduce, results);
|
||||
this.forEachHook(hook =>
|
||||
hook.onAfterExecuteEvent?.(events, param, reduced)
|
||||
);
|
||||
return reduced;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ITileLocator } from '@motajs/common';
|
||||
import { IHookable, IHookBase, ITileLocator } from '@motajs/common';
|
||||
import {
|
||||
IDataBaseExtended,
|
||||
IGameMap,
|
||||
@@ -105,7 +105,31 @@ export interface IGameEventInit {
|
||||
addBuiltinFunction(name: string, func: GameEventBuiltinFunction): void;
|
||||
}
|
||||
|
||||
export interface IGameEventExecutor {
|
||||
export interface IGameEventExecutorHooks extends IHookBase {
|
||||
/**
|
||||
* 当事件执行前触发
|
||||
* @param events 要执行的事件列表,可以直接修改此列表
|
||||
* @param param 要传递给事件的参数,可以直接修改此参数
|
||||
*/
|
||||
onBeforeExecuteEvent(
|
||||
events: IGameEventInvocation[],
|
||||
param: IBlockEventParam
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 当一个事件执行完毕后触发
|
||||
* @param events 执行的事件列表
|
||||
* @param param 事件的执行参数
|
||||
* @param returns 事件的执行返回值
|
||||
*/
|
||||
onAfterExecuteEvent(
|
||||
events: IGameEventInvocation[],
|
||||
param: IBlockEventParam,
|
||||
returns: unknown
|
||||
): void;
|
||||
}
|
||||
|
||||
export interface IGameEventExecutor extends IHookable<IGameEventExecutorHooks> {
|
||||
/** 当前的执行器执行模式 */
|
||||
readonly mode: EventExecuteMode;
|
||||
/** 当前的执行器返回值折叠方式 */
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { ITileLocator, logger } from '@motajs/common';
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IHookController,
|
||||
ITileLocator,
|
||||
logger
|
||||
} from '@motajs/common';
|
||||
import { IObjectMovable, IObjectMover, shouldReplay } from '@user/data-common';
|
||||
import { IStateBase } from '@user/data-base';
|
||||
import { PathFinder } from './finder';
|
||||
@@ -7,25 +13,36 @@ import {
|
||||
IPathfindingController,
|
||||
IPathfindingResult,
|
||||
IPathfindingSystem,
|
||||
IPathfindingSystemHooks,
|
||||
PathFallbackPolicy,
|
||||
PathfindingStatus
|
||||
} from './types';
|
||||
|
||||
export class PathfindingSystem implements IPathfindingSystem {
|
||||
export class PathfindingSystem
|
||||
extends Hookable<IPathfindingSystemHooks>
|
||||
implements IPathfindingSystem
|
||||
{
|
||||
/** 寻路求解器 */
|
||||
readonly finder: IPathFinder;
|
||||
|
||||
/** 当前绑定的移动器 */
|
||||
private mover: IObjectMover<IObjectMovable> | null = null;
|
||||
mover: IObjectMover<IObjectMovable> | null = null;
|
||||
|
||||
/** 注入的瞬移回退策略,未注入时必定回退为逐步寻路 */
|
||||
private policy: PathFallbackPolicy | null = null;
|
||||
/** 最近一次寻路移动的控制器包装 */
|
||||
private current: IPathfindingController | null = null;
|
||||
|
||||
constructor(readonly state: IStateBase) {
|
||||
super();
|
||||
this.finder = new PathFinder(state);
|
||||
}
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IPathfindingSystemHooks>
|
||||
): IHookController<IPathfindingSystemHooks> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
useMover(mover: IObjectMover<IObjectMovable> | null): void {
|
||||
this.mover = mover;
|
||||
}
|
||||
@@ -66,6 +83,7 @@ export class PathfindingSystem implements IPathfindingSystem {
|
||||
if (teleport) {
|
||||
const last = array.at(-1)!;
|
||||
mover.tp(last.to.x, last.to.y);
|
||||
this.forEachHook(hook => hook.onBeforeTeleportTo?.(path, this));
|
||||
} else {
|
||||
for (const step of array) {
|
||||
mover.step(step.dir);
|
||||
@@ -76,6 +94,11 @@ export class PathfindingSystem implements IPathfindingSystem {
|
||||
if (!controller) return null;
|
||||
const result: IPathfindingController = { controller, path };
|
||||
this.current = result;
|
||||
|
||||
controller.onEnd.then(() => {
|
||||
this.forEachHook(hook => hook.onAfterTeleportTo?.(path, this));
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { ITileLocator, IFaceHandler } from '@motajs/common';
|
||||
import {
|
||||
ITileLocator,
|
||||
IFaceHandler,
|
||||
IHookBase,
|
||||
IHookable
|
||||
} from '@motajs/common';
|
||||
import {
|
||||
IDataBaseExtended,
|
||||
ILayerLocation,
|
||||
@@ -156,9 +161,32 @@ export interface IPathFinder extends IDataBaseExtended {
|
||||
*/
|
||||
export type PathFallbackPolicy = (path: readonly IPathfindingStep[]) => boolean;
|
||||
|
||||
export interface IPathfindingSystem extends IDataBaseExtended {
|
||||
export interface IPathfindingSystemHooks extends IHookBase {
|
||||
/**
|
||||
* 在瞬移自动寻路前触发
|
||||
* @param path 瞬移的寻路路径
|
||||
*/
|
||||
onBeforeTeleportTo?(
|
||||
path: IPathfindingResult,
|
||||
system: IPathfindingSystem
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 在自动寻路之后触发
|
||||
* @param path 瞬移的寻路路径
|
||||
*/
|
||||
onAfterTeleportTo?(
|
||||
path: IPathfindingResult,
|
||||
system: IPathfindingSystem
|
||||
): void;
|
||||
}
|
||||
|
||||
export interface IPathfindingSystem
|
||||
extends IDataBaseExtended, IHookable<IPathfindingSystemHooks> {
|
||||
/** 寻路求解器 */
|
||||
readonly finder: IPathFinder;
|
||||
/** 当前正在使用的移动寻路器 */
|
||||
readonly mover: IObjectMover<IObjectMovable> | null;
|
||||
|
||||
/**
|
||||
* 绑定寻路移动器,可绑定勇士或任意 `IObjectMover`,如动态图块移动器
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { IStateBase } from '@user/data-base';
|
||||
import { IEnemyContext } from './combat';
|
||||
import { ICombatFlow, IEnemyContext } from './combat';
|
||||
import { IEnemyAttr, IHeroAttr } from '@user/data-common';
|
||||
import { IGameEventSystem } from './event';
|
||||
import { IPathfindingSystem } from './path';
|
||||
@@ -7,6 +7,8 @@ import { IPathfindingSystem } from './path';
|
||||
export interface IStateSystem extends IStateBase {
|
||||
/** 怪物上下文 */
|
||||
readonly enemyContext: IEnemyContext<IEnemyAttr, IHeroAttr>;
|
||||
/** 怪物战斗流程对象 */
|
||||
readonly combat: ICombatFlow<IEnemyAttr, IHeroAttr>;
|
||||
/** 游戏事件系统 */
|
||||
readonly eventSystem: IGameEventSystem;
|
||||
/** 已绑定勇士移动器的寻路系统 */
|
||||
|
||||
Reference in new issue
Block a user