mirror of
https://github.com/motajs/template.git
synced 2026-11-16 17:04:31 +08:00
refactor: 跟随者与勇士移动
This commit is contained in:
1 parent
02bf39a62d
commit
fadcc98864
20 files changed
+1011
-310
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { IFacedTileLocator, logger } from '@motajs/common';
|
||||
import {
|
||||
FaceDirection,
|
||||
IDataCommon,
|
||||
IFaceHandler,
|
||||
SaveCompression
|
||||
} from '@user/data-common';
|
||||
import { HeroLocation } from './location';
|
||||
import { HeroRendering } from './rendering';
|
||||
import {
|
||||
IHeroFollower,
|
||||
IHeroFollowerSave,
|
||||
IHeroFollowersController,
|
||||
IHeroLocation,
|
||||
IHeroRendering
|
||||
} from './types';
|
||||
import { isNil } from 'lodash-es';
|
||||
|
||||
export class HeroFollower implements IHeroFollower {
|
||||
readonly num: number;
|
||||
readonly state: IDataCommon;
|
||||
readonly rendering: IHeroRendering;
|
||||
readonly location: IHeroLocation;
|
||||
|
||||
/** 所属的跟随者控制器,用于获取相邻跟随者 */
|
||||
private readonly controller: IHeroFollowersController;
|
||||
|
||||
constructor(
|
||||
num: number | string,
|
||||
loc: IFacedTileLocator,
|
||||
faceHandler: IFaceHandler<FaceDirection>,
|
||||
controller: IHeroFollowersController
|
||||
) {
|
||||
const state = controller.state;
|
||||
if (typeof num === 'string') {
|
||||
const n = state.tileStore.idToNumber(num);
|
||||
if (isNil(n)) {
|
||||
logger.warn(142);
|
||||
this.num = 0;
|
||||
} else {
|
||||
this.num = n;
|
||||
}
|
||||
} else {
|
||||
this.num = num;
|
||||
}
|
||||
this.state = state;
|
||||
this.rendering = new HeroRendering(state);
|
||||
this.location = new HeroLocation(state, loc, faceHandler);
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
next(): IHeroFollower | null {
|
||||
const all = this.controller.getAllFollowers();
|
||||
const myIndex = all.indexOf(this);
|
||||
if (myIndex === -1) return null;
|
||||
return this.controller.getFollower(myIndex + 1);
|
||||
}
|
||||
|
||||
last(): IHeroFollower | null {
|
||||
const all = this.controller.getAllFollowers();
|
||||
const myIndex = all.indexOf(this);
|
||||
if (myIndex === -1) return null;
|
||||
return this.controller.getFollower(myIndex - 1);
|
||||
}
|
||||
|
||||
saveState(compression: SaveCompression): IHeroFollowerSave {
|
||||
return {
|
||||
rendering: this.rendering.saveState(compression),
|
||||
location: this.location.saveState(compression)
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IHeroFollowerSave, compression: SaveCompression): void {
|
||||
this.rendering.loadState(state.rendering, compression);
|
||||
this.location.loadState(state.location, compression);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IFacedTileLocator,
|
||||
IHookController
|
||||
} from '@motajs/common';
|
||||
import { FaceDirection, IDataCommon, IFaceHandler } from '@user/data-common';
|
||||
import { HeroFollower } from './follower';
|
||||
import {
|
||||
IHeroFollowersController,
|
||||
IHeroFollowersControllerHooks,
|
||||
IHeroFollower,
|
||||
IHeroLocation
|
||||
} from './types';
|
||||
|
||||
export class HeroFollowersController
|
||||
extends Hookable<IHeroFollowersControllerHooks>
|
||||
implements IHeroFollowersController
|
||||
{
|
||||
readonly state: IDataCommon;
|
||||
/** 跟随者列表 */
|
||||
private readonly followers: HeroFollower[] = [];
|
||||
/** 勇士位置对象,用于获取聚集目标位置 */
|
||||
private readonly heroLocation: IHeroLocation;
|
||||
/** 朝向处理器,传递给新创建的跟随者 */
|
||||
private readonly faceHandler: IFaceHandler<FaceDirection>;
|
||||
|
||||
constructor(
|
||||
state: IDataCommon,
|
||||
heroLocation: IHeroLocation,
|
||||
faceHandler: IFaceHandler<FaceDirection>
|
||||
) {
|
||||
super();
|
||||
this.state = state;
|
||||
this.heroLocation = heroLocation;
|
||||
this.faceHandler = faceHandler;
|
||||
}
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IHeroFollowersControllerHooks>
|
||||
): IHookController<IHeroFollowersControllerHooks> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
addFollower(num: number | string): IHeroFollower {
|
||||
const loc: IFacedTileLocator = {
|
||||
x: this.heroLocation.x,
|
||||
y: this.heroLocation.y,
|
||||
direction: this.heroLocation.mover.faceDirection
|
||||
};
|
||||
const follower = new HeroFollower(num, loc, this.faceHandler, this);
|
||||
this.followers.push(follower);
|
||||
const index = this.followers.length - 1;
|
||||
this.forEachHook(hook => {
|
||||
hook.onAddFollower?.(follower, index);
|
||||
});
|
||||
return follower;
|
||||
}
|
||||
|
||||
getFollower(index: number): IHeroFollower | null {
|
||||
return this.followers[index] ?? null;
|
||||
}
|
||||
|
||||
*getFollowersById(
|
||||
num: number | string
|
||||
): IterableIterator<[number, IHeroFollower]> {
|
||||
const store = this.state.tileStore;
|
||||
const target = typeof num === 'string' ? store.idToNumber(num)! : num;
|
||||
for (let i = 0; i < this.followers.length; i++) {
|
||||
if (this.followers[i].num === target) {
|
||||
yield [i, this.followers[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getAllFollowers(): IHeroFollower[] {
|
||||
return this.followers.slice();
|
||||
}
|
||||
|
||||
async removeFollower(index: number): Promise<void> {
|
||||
const removed = this.followers.splice(index, 1);
|
||||
if (removed.length > 0) {
|
||||
this.forEachHook(hook => {
|
||||
hook.onRemoveFollower?.(removed[0], index);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async removeAllFollowers(): Promise<void> {
|
||||
const snapshot = this.followers.slice();
|
||||
this.followers.length = 0;
|
||||
this.forEachHook(hook => {
|
||||
snapshot.forEach((v, i) => {
|
||||
hook.onRemoveFollower?.(v, i);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async gatherFollowers(): Promise<void> {
|
||||
const { x, y, mover } = this.heroLocation;
|
||||
|
||||
for (let i = 0; i < this.followers.length; i++) {
|
||||
const follower = this.followers[i];
|
||||
follower.location.mover.clear();
|
||||
|
||||
let skip = false;
|
||||
for (let j = i - 1; j >= 0; j--) {
|
||||
const dir = this.followers[j].location.mover.moveDirection;
|
||||
if (dir === FaceDirection.Unknown) {
|
||||
follower.location.mover.clear();
|
||||
follower.location.mover.tp(x, y);
|
||||
follower.location.mover.face(mover.faceDirection);
|
||||
skip = true;
|
||||
break;
|
||||
} else {
|
||||
follower.location.mover.step(dir);
|
||||
}
|
||||
}
|
||||
|
||||
if (!skip) {
|
||||
follower.location.mover.step(mover.moveDirection);
|
||||
}
|
||||
}
|
||||
|
||||
const controllers = this.followers.map(
|
||||
v => v.location.mover.start()?.onEnd
|
||||
);
|
||||
await Promise.all(controllers);
|
||||
this.forEachHook(hook => {
|
||||
hook.onGatherFollowers?.(false);
|
||||
});
|
||||
}
|
||||
|
||||
gatherFollowersSync(): void {
|
||||
const targetX = this.heroLocation.x;
|
||||
const targetY = this.heroLocation.y;
|
||||
const targetDir = this.heroLocation.mover.faceDirection;
|
||||
for (const follower of this.followers) {
|
||||
follower.location.setPos(targetX, targetY);
|
||||
follower.location.mover.face(targetDir).start();
|
||||
}
|
||||
this.forEachHook(hook => {
|
||||
hook.onGatherFollowers?.(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
export * from './attribute';
|
||||
export * from './follower';
|
||||
export * from './followersController';
|
||||
export * from './location';
|
||||
export * from './rendering';
|
||||
export * from './mover';
|
||||
export * from './state';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,48 @@
|
||||
import { IFacedTileLocator } from '@motajs/common';
|
||||
import { FaceDirection, IDataCommon, IFaceHandler } from '@user/data-common';
|
||||
import { HeroMover } from './mover';
|
||||
import { IHeroLocation, IHeroLocationSave, IHeroMover } from './types';
|
||||
|
||||
export class HeroLocation implements IHeroLocation {
|
||||
x: number;
|
||||
y: number;
|
||||
|
||||
readonly state: IDataCommon;
|
||||
readonly mover: IHeroMover<this>;
|
||||
|
||||
constructor(
|
||||
state: IDataCommon,
|
||||
loc: IFacedTileLocator,
|
||||
faceHandler: IFaceHandler<FaceDirection>
|
||||
) {
|
||||
this.state = state;
|
||||
this.x = loc.x;
|
||||
this.y = loc.y;
|
||||
const mover = new HeroMover(this, faceHandler);
|
||||
mover.faceDirection = loc.direction;
|
||||
this.mover = mover;
|
||||
}
|
||||
|
||||
setPos(x: number, y: number): void {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
getCurrentFaceDirection(): FaceDirection {
|
||||
return this.mover.faceDirection;
|
||||
}
|
||||
|
||||
saveState(): IHeroLocationSave {
|
||||
return {
|
||||
x: this.x,
|
||||
y: this.y,
|
||||
direction: this.mover.faceDirection
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IHeroLocationSave): void {
|
||||
this.x = state.x;
|
||||
this.y = state.y;
|
||||
this.mover.face(state.direction).start();
|
||||
}
|
||||
}
|
||||
@@ -1,142 +1,182 @@
|
||||
import { Hookable, HookController, IHookController } from '@motajs/common';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { getFaceMovement, nextFaceDirection } from '@user/data-common';
|
||||
import { ITileLocator } from '@motajs/common';
|
||||
import {
|
||||
IHeroFollower,
|
||||
IHeroMoveController,
|
||||
IHeroMoveControllerHooks
|
||||
FaceDirection,
|
||||
getFaceMovement,
|
||||
IFaceHandler,
|
||||
IMoverController,
|
||||
ObjectMoveStep,
|
||||
ObjectMover,
|
||||
ObjectMoveStepType
|
||||
} from '@user/data-common';
|
||||
import {
|
||||
HeroMoveCode,
|
||||
IHeroLocation,
|
||||
IHeroMover,
|
||||
IHeroMoverConfig
|
||||
} from './types';
|
||||
import { FaceDirection } from '@user/data-common';
|
||||
|
||||
const DEFAULT_HERO_IMAGE: ImageIds = 'hero.png';
|
||||
|
||||
export class HeroMoveController
|
||||
extends Hookable<IHeroMoveControllerHooks>
|
||||
implements IHeroMoveController
|
||||
export class HeroMover<T extends IHeroLocation>
|
||||
extends ObjectMover<T>
|
||||
implements IHeroMover<T>
|
||||
{
|
||||
x: number = 0;
|
||||
y: number = 0;
|
||||
direction: FaceDirection = FaceDirection.Down;
|
||||
image: ImageIds = DEFAULT_HERO_IMAGE;
|
||||
readonly tile: T;
|
||||
|
||||
/** 当前勇士是否正在移动 */
|
||||
moving: boolean = false;
|
||||
alpha: number = 1;
|
||||
/** 本次移动是否不记录进路线系统 */
|
||||
private noRoute: boolean = false;
|
||||
/** 本次移动是否忽略地形碰撞检测 */
|
||||
private ignoreTerrain: boolean = false;
|
||||
/** 本次移动是否在特定时机触发自动存档 */
|
||||
private autoSave: boolean = false;
|
||||
|
||||
readonly followers: IHeroFollower[] = [];
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IHeroMoveControllerHooks>
|
||||
): IHookController<IHeroMoveControllerHooks> {
|
||||
return new HookController(this, hook);
|
||||
constructor(tile: T, faceHandler: IFaceHandler<FaceDirection>) {
|
||||
super(faceHandler);
|
||||
this.tile = tile;
|
||||
}
|
||||
|
||||
setPosition(x: number, y: number): void {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.forEachHook(hook => {
|
||||
hook.onSetPosition?.(x, y);
|
||||
});
|
||||
config(config: Readonly<IHeroMoverConfig>): this {
|
||||
if (config.noRoute !== undefined) {
|
||||
this.noRoute = config.noRoute;
|
||||
}
|
||||
if (config.ignoreTerrain !== undefined) {
|
||||
this.ignoreTerrain = config.ignoreTerrain;
|
||||
}
|
||||
if (config.autoSave !== undefined) {
|
||||
this.autoSave = config.autoSave;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
turn(direction?: FaceDirection): void {
|
||||
const next = isNil(direction)
|
||||
? nextFaceDirection(this.direction)
|
||||
: direction;
|
||||
this.direction = next;
|
||||
this.forEachHook(hook => {
|
||||
hook.onTurnHero?.(next);
|
||||
});
|
||||
getConfig(): Readonly<IHeroMoverConfig> {
|
||||
return {
|
||||
noRoute: this.noRoute,
|
||||
ignoreTerrain: this.ignoreTerrain,
|
||||
autoSave: this.autoSave
|
||||
};
|
||||
}
|
||||
|
||||
startMove(): void {
|
||||
this.moving = true;
|
||||
this.forEachHook(hook => {
|
||||
hook.onStartMove?.();
|
||||
});
|
||||
protected async onMoveStart(
|
||||
_tile: IHeroLocation,
|
||||
_controller: Readonly<IMoverController>
|
||||
): Promise<void> {
|
||||
// TODO: 通知渲染端开始移动,同步平滑视角
|
||||
}
|
||||
|
||||
async move(dir: FaceDirection, time: number = 100): Promise<void> {
|
||||
await Promise.all(
|
||||
this.forEachHook(hook => {
|
||||
return hook.onMoveHero?.(dir, time);
|
||||
})
|
||||
);
|
||||
const { x, y } = getFaceMovement(dir);
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
protected async onMoveEnd(
|
||||
_tile: IHeroLocation,
|
||||
_controller: Readonly<IMoverController>
|
||||
): Promise<void> {
|
||||
// TODO: 通知渲染端移动结束
|
||||
// TODO: 清除自动寻路状态
|
||||
}
|
||||
|
||||
async endMove(): Promise<void> {
|
||||
if (!this.moving) return;
|
||||
await Promise.all(
|
||||
this.forEachHook(hook => {
|
||||
return hook.onEndMove?.();
|
||||
})
|
||||
);
|
||||
this.moving = false;
|
||||
protected async onStepStart(
|
||||
step: ObjectMoveStep,
|
||||
tile: IHeroLocation,
|
||||
controller: Readonly<IMoverController>
|
||||
): Promise<number> {
|
||||
const type = step.type;
|
||||
|
||||
if (
|
||||
type !== ObjectMoveStepType.Dir &&
|
||||
type !== ObjectMoveStepType.DirFace &&
|
||||
type !== ObjectMoveStepType.Special
|
||||
) {
|
||||
return HeroMoveCode.Step;
|
||||
}
|
||||
|
||||
if (!this.ignoreTerrain) {
|
||||
const result = this.checkTerrain(tile, controller);
|
||||
if (result !== HeroMoveCode.Step) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.autoSave) {
|
||||
// TODO: 检查当前步是否需要触发自动存档(如进入/离开地图伤害区域)
|
||||
}
|
||||
|
||||
return HeroMoveCode.Step;
|
||||
}
|
||||
|
||||
async jumpHero(
|
||||
protected async onStepEnd(
|
||||
code: number,
|
||||
step: ObjectMoveStep,
|
||||
tile: IHeroLocation,
|
||||
controller: Readonly<IMoverController>
|
||||
): Promise<ITileLocator> {
|
||||
const type = step.type;
|
||||
|
||||
const isMovementStep =
|
||||
type === ObjectMoveStepType.Dir ||
|
||||
type === ObjectMoveStepType.DirFace ||
|
||||
type === ObjectMoveStepType.Special;
|
||||
|
||||
if (
|
||||
!isMovementStep &&
|
||||
type !== ObjectMoveStepType.Teleport &&
|
||||
type !== ObjectMoveStepType.Jump
|
||||
) {
|
||||
return { x: tile.x, y: tile.y };
|
||||
}
|
||||
|
||||
if (code === HeroMoveCode.CannotMove || code === HeroMoveCode.Hit) {
|
||||
void controller.stop();
|
||||
// TODO: 处理撞击图块逻辑
|
||||
return { x: tile.x, y: tile.y };
|
||||
}
|
||||
|
||||
if (code === HeroMoveCode.Stop) {
|
||||
void controller.stop();
|
||||
// TODO: 清除自动寻路
|
||||
return { x: tile.x, y: tile.y };
|
||||
}
|
||||
|
||||
if (type === ObjectMoveStepType.Teleport) {
|
||||
return this.calcPos(tile, step.x, step.y, step.rel);
|
||||
}
|
||||
|
||||
if (type === ObjectMoveStepType.Jump) {
|
||||
return this.calcPos(tile, step.x, step.y, step.rel);
|
||||
}
|
||||
|
||||
const offset = getFaceMovement(this.moveDirection);
|
||||
const nx = tile.x + offset.x;
|
||||
const ny = tile.y + offset.y;
|
||||
|
||||
// TODO: 路线记录
|
||||
// TODO: 中毒伤害处理
|
||||
// TODO: 步数累计
|
||||
|
||||
return { x: nx, y: ny };
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算传送 / 跳跃的目标坐标
|
||||
* @param tile 当前所在图块
|
||||
* @param x 目标横坐标或偏移量
|
||||
* @param y 目标纵坐标或偏移量
|
||||
* @param rel 是否使用相对模式
|
||||
*/
|
||||
private calcPos(
|
||||
tile: IHeroLocation,
|
||||
x: number,
|
||||
y: number,
|
||||
time: number = 500,
|
||||
waitFollower: boolean = false
|
||||
): Promise<void> {
|
||||
await Promise.all(
|
||||
this.forEachHook(hook => {
|
||||
return hook.onJumpHero?.(x, y, time, waitFollower);
|
||||
})
|
||||
);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
rel: boolean
|
||||
): ITileLocator {
|
||||
if (rel) {
|
||||
return { x: tile.x + x, y: tile.y + y };
|
||||
}
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
setImage(image: ImageIds): void {
|
||||
this.image = image;
|
||||
this.forEachHook(hook => {
|
||||
hook.onSetImage?.(image);
|
||||
});
|
||||
}
|
||||
|
||||
setAlpha(alpha: number): void {
|
||||
this.alpha = alpha;
|
||||
this.forEachHook(hook => {
|
||||
hook.onSetAlpha?.(alpha);
|
||||
});
|
||||
}
|
||||
|
||||
setFollowerAlpha(identifier: string, alpha: number): void {
|
||||
const follower = this.followers.find(v => v.identifier === identifier);
|
||||
if (!follower) return;
|
||||
follower.alpha = alpha;
|
||||
this.forEachHook(hook => {
|
||||
hook.onSetFollowerAlpha?.(identifier, alpha);
|
||||
});
|
||||
}
|
||||
|
||||
addFollower(follower: number, identifier: string): void {
|
||||
this.followers.push({ num: follower, identifier, alpha: 1 });
|
||||
this.forEachHook(hook => {
|
||||
hook.onAddFollower?.(follower, identifier);
|
||||
});
|
||||
}
|
||||
|
||||
removeFollower(identifier: string, animate: boolean = false): void {
|
||||
const index = this.followers.findIndex(
|
||||
v => v.identifier === identifier
|
||||
);
|
||||
if (index === -1) return;
|
||||
this.followers.splice(index, 1);
|
||||
this.forEachHook(hook => {
|
||||
hook.onRemoveFollower?.(identifier, animate);
|
||||
});
|
||||
}
|
||||
|
||||
removeAllFollowers(): void {
|
||||
this.followers.length = 0;
|
||||
this.forEachHook(hook => {
|
||||
hook.onRemoveAllFollowers?.();
|
||||
});
|
||||
/**
|
||||
* 检测当前移动方向前方一格的地形是否可通行
|
||||
*/
|
||||
private checkTerrain(
|
||||
_tile: IHeroLocation,
|
||||
_controller: Readonly<IMoverController>
|
||||
): HeroMoveCode {
|
||||
// TODO: 需要地形检测接口(替代 core.noPass / core.canMoveHero)
|
||||
return HeroMoveCode.Step;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Hookable, HookController, IHookController } from '@motajs/common';
|
||||
import { IDataCommon } from '@user/data-common';
|
||||
import {
|
||||
IHeroRendering,
|
||||
IHeroRenderingHooks,
|
||||
IHeroRenderingSave
|
||||
} from './types';
|
||||
|
||||
export class HeroRendering
|
||||
extends Hookable<IHeroRenderingHooks>
|
||||
implements IHeroRendering
|
||||
{
|
||||
alpha: number = 1;
|
||||
readonly state: IDataCommon;
|
||||
|
||||
constructor(state: IDataCommon) {
|
||||
super();
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IHeroRenderingHooks>
|
||||
): IHookController<IHeroRenderingHooks> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
setAlpha(alpha: number): void {
|
||||
this.alpha = alpha;
|
||||
this.forEachHook(hook => {
|
||||
hook.onSetAlpha?.(alpha);
|
||||
});
|
||||
}
|
||||
|
||||
saveState(): IHeroRenderingSave {
|
||||
return { alpha: this.alpha };
|
||||
}
|
||||
|
||||
loadState(state: IHeroRenderingSave): void {
|
||||
this.setAlpha(state.alpha);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
IReadonlyHeroAttribute
|
||||
} from './types';
|
||||
import { SaveCompression } from '@user/data-common';
|
||||
import { logger } from '@motajs/common';
|
||||
import { IFacedTileLocator, logger } from '@motajs/common';
|
||||
|
||||
export class HeroState<THero> implements IHeroState<THero> {
|
||||
/** 修饰器工厂函数注册表 */
|
||||
@@ -32,6 +32,10 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
return this.mover;
|
||||
}
|
||||
|
||||
getLocation(): IFacedTileLocator {
|
||||
return this.mover;
|
||||
}
|
||||
|
||||
getModifiableAttribute(): IHeroAttribute<THero> {
|
||||
return this.attribute;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { IFacedTileLocator, IHookBase, IHookable } from '@motajs/common';
|
||||
import { FaceDirection, ISaveableContent } from '@user/data-common';
|
||||
import {
|
||||
IDataCommonExtended,
|
||||
IObjectMovable,
|
||||
IObjectMover,
|
||||
ISaveableContent
|
||||
} from '@user/data-common';
|
||||
|
||||
//#region 勇士属性
|
||||
|
||||
@@ -171,206 +176,189 @@ export interface IHeroAttribute<THero> extends IReadonlyHeroAttribute<THero> {
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 勇士位置
|
||||
|
||||
export interface IHeroLocationSave extends Readonly<IFacedTileLocator> {}
|
||||
|
||||
export interface IHeroLocation
|
||||
extends
|
||||
ISaveableContent<IHeroLocationSave>,
|
||||
IObjectMovable,
|
||||
IDataCommonExtended {
|
||||
/** 勇士的移动对象 */
|
||||
readonly mover: IObjectMover<this>;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 勇士移动
|
||||
|
||||
export const enum HeroAnimateDirection {
|
||||
/** 正向播放动画 */
|
||||
Forward,
|
||||
/** 反向播放动画 */
|
||||
Backward
|
||||
export const enum HeroMoveCode {
|
||||
/** 正常移动 */
|
||||
Step,
|
||||
/** 移动被停止 */
|
||||
Stop,
|
||||
/** 不能移动,并撞击目标格触发器 */
|
||||
Hit,
|
||||
/** 不能移动,不触发触发器 */
|
||||
CannotMove
|
||||
}
|
||||
|
||||
export interface IHeroFollower {
|
||||
/** 跟随者的图块数字 */
|
||||
readonly num: number;
|
||||
/** 跟随者的标识符 */
|
||||
readonly identifier: string;
|
||||
/** 跟随者的不透明度 */
|
||||
alpha: number;
|
||||
export interface IHeroMoverConfig {
|
||||
/** 本次移动是否不记录进路线系统 */
|
||||
noRoute?: boolean;
|
||||
/** 本次移动是否忽略地形碰撞检测 */
|
||||
ignoreTerrain?: boolean;
|
||||
/** 本次移动是否在特定时机触发自动存档 */
|
||||
autoSave?: boolean;
|
||||
}
|
||||
|
||||
export interface IHeroMoveControllerHooks extends IHookBase {
|
||||
export interface IHeroMover<T extends IHeroLocation> extends IObjectMover<T> {
|
||||
/**
|
||||
* 当设置勇士的坐标时触发
|
||||
* @param x 勇士横坐标
|
||||
* @param y 勇士纵坐标
|
||||
* 配置本次移动的行为模式
|
||||
* @param config 配置对象,未传入的字段保持当前值
|
||||
*/
|
||||
onSetPosition(x: number, y: number): void;
|
||||
config(config: Readonly<IHeroMoverConfig>): this;
|
||||
|
||||
/**
|
||||
* 当设置勇士朝向时触发
|
||||
* @param direction 勇士朝向
|
||||
* 获取当前移动配置的只读快照
|
||||
*/
|
||||
onTurnHero(direction: FaceDirection): void;
|
||||
getConfig(): Readonly<IHeroMoverConfig>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当勇士开始移动时触发
|
||||
*/
|
||||
onStartMove(): void;
|
||||
//#endregion
|
||||
|
||||
/**
|
||||
* 当移动勇士时触发
|
||||
* @param direction 移动方向
|
||||
* @param time 移动动画时长
|
||||
*/
|
||||
onMoveHero(direction: FaceDirection, time: number): Promise<void>;
|
||||
//#region 勇士渲染
|
||||
|
||||
/**
|
||||
* 当停止移动时触发
|
||||
*/
|
||||
onEndMove(): Promise<void>;
|
||||
export interface IHeroRenderingSave {
|
||||
/** 勇士的不透明度 */
|
||||
readonly alpha: number;
|
||||
}
|
||||
|
||||
export interface IHeroRenderingHooks extends IHookBase {
|
||||
/**
|
||||
* 当勇士跳跃时触发
|
||||
* @param x 目标点横坐标
|
||||
* @param y 目标点纵坐标
|
||||
* @param time 跳跃动画时长
|
||||
* @param waitFollower 是否等待跟随者跳跃完毕
|
||||
*/
|
||||
onJumpHero(
|
||||
x: number,
|
||||
y: number,
|
||||
time: number,
|
||||
waitFollower: boolean
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当设置勇士图片时触发
|
||||
* @param image 勇士图片 id
|
||||
*/
|
||||
onSetImage(image: ImageIds): void;
|
||||
|
||||
/**
|
||||
* 当设置勇士不透明度时执行
|
||||
* 当勇士的不透明度被修改时触发
|
||||
* @param alpha 不透明度
|
||||
*/
|
||||
onSetAlpha(alpha: number): void;
|
||||
|
||||
/**
|
||||
* 添加跟随者时触发
|
||||
* @param follower 跟随者的图块数字
|
||||
* @param identifier 跟随者的标识符
|
||||
*/
|
||||
onAddFollower(follower: number, identifier: string): void;
|
||||
|
||||
/**
|
||||
* 当移除跟随者时触发
|
||||
* @param identifier 跟随者的标识符
|
||||
* @param animate 填 `true` 的话,如果删除了中间的跟随者,后续跟随者会使用移动动画移动到下一格,否则瞬移至下一格
|
||||
*/
|
||||
onRemoveFollower(identifier: string, animate: boolean): void;
|
||||
|
||||
/**
|
||||
* 当移除所有跟随者时触发
|
||||
*/
|
||||
onRemoveAllFollowers(): void;
|
||||
|
||||
/**
|
||||
* 设置跟随者的不透明度
|
||||
* @param identifier 跟随者标识符
|
||||
* @param alpha 跟随者不透明度
|
||||
*/
|
||||
onSetFollowerAlpha(identifier: string, alpha: number): void;
|
||||
onSetAlpha?(alpha: number): void;
|
||||
}
|
||||
|
||||
export interface IHeroMoveController extends IHookable<IHeroMoveControllerHooks> {
|
||||
/** 勇士横坐标 */
|
||||
readonly x: number;
|
||||
/** 勇士纵坐标 */
|
||||
readonly y: number;
|
||||
/** 勇士朝向 */
|
||||
readonly direction: FaceDirection;
|
||||
/** 勇士图片 */
|
||||
readonly image?: ImageIds;
|
||||
/** 跟随者列表 */
|
||||
readonly followers: readonly IHeroFollower[];
|
||||
/** 勇士当前的不透明度 */
|
||||
export interface IHeroRendering
|
||||
extends
|
||||
ISaveableContent<IHeroRenderingSave>,
|
||||
IHookable<IHeroRenderingHooks>,
|
||||
IDataCommonExtended {
|
||||
/** 勇士的当前不透明度 */
|
||||
readonly alpha: number;
|
||||
|
||||
/**
|
||||
* 设置勇士位置
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
setPosition(x: number, y: number): void;
|
||||
|
||||
/**
|
||||
* 设置勇士朝向
|
||||
* @param direction 勇士朝向,不填表示顺时针旋转
|
||||
*/
|
||||
turn(direction?: FaceDirection): void;
|
||||
|
||||
/**
|
||||
* 开始勇士移动,在移动前必须先调用此方法将勇士切换为移动状态
|
||||
*/
|
||||
startMove(): void;
|
||||
|
||||
/**
|
||||
* 移动勇士。能否移动的逻辑暂时不在这里,目前作为过渡作用,仅服务于渲染
|
||||
* @param dir 移动方向
|
||||
* @param time 移动动画时长,默认 100ms
|
||||
* @returns 移动的 `Promise`,当相关的移动动画结束后兑现
|
||||
*/
|
||||
move(dir: FaceDirection, time?: number): Promise<void>;
|
||||
|
||||
/**
|
||||
* 结束勇士移动
|
||||
* @returns 当移动动画结束后兑现的 `Promise`
|
||||
*/
|
||||
endMove(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 跳跃勇士至目标点
|
||||
* @param x 目标点横坐标
|
||||
* @param y 目标点纵坐标
|
||||
* @param time 跳跃动画时长,默认 500ms
|
||||
* @param waitFollower 是否等待跟随者跳跃完毕,默认不等待
|
||||
* @returns 跳跃的 `Promise`,当相关的移动动画结束后兑现
|
||||
*/
|
||||
jumpHero(
|
||||
x: number,
|
||||
y: number,
|
||||
time?: number,
|
||||
waitFollower?: boolean
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 设置勇士图片
|
||||
* @param image 图片 id
|
||||
*/
|
||||
setImage(image: ImageIds): void;
|
||||
|
||||
/**
|
||||
* 设置勇士的不透明度
|
||||
* @param alpha 不透明度
|
||||
*/
|
||||
setAlpha(alpha: number): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 勇士跟随者
|
||||
|
||||
export interface IHeroFollowerSave {
|
||||
/** 跟随者渲染对象保存 */
|
||||
readonly rendering: IHeroRenderingSave;
|
||||
/** 跟随者位置保存 */
|
||||
readonly location: IHeroLocationSave;
|
||||
}
|
||||
|
||||
export interface IHeroFollower
|
||||
extends ISaveableContent<IHeroFollowerSave>, IDataCommonExtended {
|
||||
/** 跟随者的图块数字 */
|
||||
readonly num: number;
|
||||
/** 跟随者的渲染信息对象 */
|
||||
readonly rendering: IHeroRendering;
|
||||
/** 跟随者的位置对象 */
|
||||
readonly location: IHeroLocation;
|
||||
|
||||
/**
|
||||
* 添加一个跟随者
|
||||
* @param follower 跟随者的图块数字
|
||||
* @param identifier 跟随者的标识符,可以用来移除
|
||||
* 获取下一个跟随者
|
||||
* @returns null 表示当前为最后一个
|
||||
*/
|
||||
addFollower(follower: number, identifier: string): void;
|
||||
next(): IHeroFollower | null;
|
||||
|
||||
/**
|
||||
* 移除指定的跟随者
|
||||
* @param identifier 跟随者的标识符
|
||||
* @param animate 填 `true` 的话,如果删除了中间的跟随者,后续跟随者会使用移动动画移动到下一格,否则瞬移至下一格
|
||||
* 获取上一个跟随者
|
||||
* @returns null 表示当前为第一个
|
||||
*/
|
||||
removeFollower(identifier: string, animate?: boolean): void;
|
||||
last(): IHeroFollower | null;
|
||||
}
|
||||
|
||||
export interface IHeroFollowersControllerHooks extends IHookBase {
|
||||
/**
|
||||
* 当添加跟随者时触发
|
||||
* @param follower 跟随者对象
|
||||
* @param index 添加的跟随者的索引
|
||||
*/
|
||||
onAddFollower?(follower: IHeroFollower, index: number): void;
|
||||
|
||||
/**
|
||||
* 移除所有跟随者
|
||||
* 当移除跟随者时触发
|
||||
* @param follower 跟随者对象
|
||||
* @param index 要移除的跟随者索引
|
||||
*/
|
||||
removeAllFollowers(): void;
|
||||
onRemoveFollower?(follower: IHeroFollower, index: number): void;
|
||||
|
||||
/**
|
||||
* 设置指定跟随者的不透明度
|
||||
* @param identifier 跟随者标识符
|
||||
* @param alpha 跟随者不透明度
|
||||
* 当聚集跟随者时触发
|
||||
* @param sync 是否为同步调用(即是否通过 `gatherFollowersSync` 触发)
|
||||
*/
|
||||
setFollowerAlpha(identifier: string, alpha: number): void;
|
||||
onGatherFollowers?(sync: boolean): void;
|
||||
}
|
||||
|
||||
export interface IHeroFollowersController
|
||||
extends IHookable<IHeroFollowersControllerHooks>, IDataCommonExtended {
|
||||
/**
|
||||
* 添加跟随者
|
||||
* @param num 跟随者的图块数字或图块 id
|
||||
*/
|
||||
addFollower(num: number | string): IHeroFollower;
|
||||
|
||||
/**
|
||||
* 根据跟随者的索引数字获取跟随者对象
|
||||
* @param index 跟随者的索引数字
|
||||
*/
|
||||
getFollower(index: number): IHeroFollower | null;
|
||||
|
||||
/**
|
||||
* 根据跟随者的图块数字或图块 id 获取所有符合的跟随者
|
||||
* @param num 跟随者的图块数字或图块 id
|
||||
* @returns 一个输出 [跟随者索引, 跟随者对象] 的迭代器
|
||||
*/
|
||||
getFollowersById(num: number | string): Iterable<[number, IHeroFollower]>;
|
||||
|
||||
/**
|
||||
* 获取勇士的所有跟随者
|
||||
*/
|
||||
getAllFollowers(): IHeroFollower[];
|
||||
|
||||
/**
|
||||
* 移除指定跟随者
|
||||
* @param index 跟随者索引
|
||||
*/
|
||||
removeFollower(index: number): Promise<void>;
|
||||
|
||||
/**
|
||||
* 移除所有的跟随者
|
||||
*/
|
||||
removeAllFollowers(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 将所有跟随者聚集到勇士位置,并调整朝向为与勇士相同
|
||||
*/
|
||||
gatherFollowers(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 立刻将所有跟随者聚集到勇士位置,并调整朝向为与勇士相同,不会播放移动动画
|
||||
*/
|
||||
gatherFollowersSync(): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -381,9 +369,9 @@ export interface IHeroStateSave<THero> {
|
||||
/** 勇士属性状态 */
|
||||
readonly attribute: THero;
|
||||
/** 勇士当前位置 */
|
||||
readonly locator: IFacedTileLocator;
|
||||
readonly locator: IHeroLocationSave;
|
||||
/** 勇士当前的跟随者 */
|
||||
readonly followers: readonly Readonly<IHeroFollower>[];
|
||||
readonly followers: readonly IHeroFollowerSave[];
|
||||
/** 勇士属性修饰器状态 */
|
||||
readonly modifiers: readonly IModifierStateSave[];
|
||||
}
|
||||
@@ -392,20 +380,18 @@ export interface IHeroState<THero> extends ISaveableContent<
|
||||
IHeroStateSave<THero>
|
||||
> {
|
||||
/** 勇士移动对象 */
|
||||
readonly mover: IHeroMoveController;
|
||||
readonly location: IHeroLocation;
|
||||
/** 勇士属性对象 */
|
||||
readonly attribute: IReadonlyHeroAttribute<THero>;
|
||||
/** 勇士跟随者对象 */
|
||||
readonly followers: IHeroFollowersController;
|
||||
/** 勇士的渲染对象,包含一些必要渲染信息,存在于数据端,并非渲染端 */
|
||||
readonly rendering: IHeroRendering;
|
||||
|
||||
/**
|
||||
* 绑定勇士移动对象
|
||||
* @param mover 勇士移动对象
|
||||
* 获取勇士当前的位置
|
||||
*/
|
||||
attachMover(mover: IHeroMoveController): void;
|
||||
|
||||
/**
|
||||
* 获取勇士移动对象
|
||||
*/
|
||||
getHeroMover(): IHeroMoveController;
|
||||
getLocation(): IFacedTileLocator;
|
||||
|
||||
/**
|
||||
* 绑定勇士属性对象
|
||||
|
||||
@@ -72,6 +72,19 @@ export class DynamicTileMover extends ObjectMover<IDynamicTile> {
|
||||
locator.y += y;
|
||||
break;
|
||||
}
|
||||
case ObjectMoveStepType.Teleport:
|
||||
case ObjectMoveStepType.Jump: {
|
||||
const { x, y, rel } = step;
|
||||
tile.setFaceDirection(this.faceDirection);
|
||||
if (rel) {
|
||||
locator.x += x;
|
||||
locator.y += y;
|
||||
} else {
|
||||
locator.x = x;
|
||||
locator.y = y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Promise.resolve(locator);
|
||||
}
|
||||
|
||||
@@ -2,3 +2,5 @@ import { FaceGroup } from '@user/data-common';
|
||||
|
||||
/** 动态图块所使用的默认移动组,不知道干什么的就别动 */
|
||||
export const DYNAMIC_MOVER_FACE = FaceGroup.Dir8;
|
||||
/** 勇士移动所使用的默认移动组,不知道干什么的就别动 */
|
||||
export const HERO_MOVER_FACE = FaceGroup.Dir8;
|
||||
Reference in new issue
Block a user