mirror of
https://github.com/motajs/template.git
synced 2026-11-20 03:14:30 +08:00
refactor: 数据端用户结构
This commit is contained in:
1 parent
0a133058ca
commit
a0e402728e
41 files changed
+901
-100
No files matched your search
@@ -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`,如动态图块移动器
|
||||
|
||||
Reference in new issue
Block a user