mirror of
https://github.com/motajs/template.git
synced 2026-11-25 23:44:37 +08:00
feat: 动态图层
This commit is contained in:
1 parent
4c48b63e7e
commit
fbdd609001
19 files changed
+2063
-15
No files matched your search
@@ -1,3 +1,4 @@
|
||||
export * from './face';
|
||||
export * from './mover';
|
||||
export * from './types';
|
||||
export * from './utils';
|
||||
@@ -0,0 +1,579 @@
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IHookable,
|
||||
IHookBase,
|
||||
IHookController,
|
||||
ITileLocator
|
||||
} from '@motajs/common';
|
||||
import { FaceDirection } from './types';
|
||||
|
||||
//#region 对象移动
|
||||
|
||||
export const enum ObjectMoveStepType {
|
||||
/** 绝对方向步,同步更新移动方向与朝向 */
|
||||
Dir,
|
||||
/** 绝对方向步,显式指定朝向 */
|
||||
DirFace,
|
||||
/** 速度步 */
|
||||
Speed,
|
||||
/** 纯转向步 */
|
||||
Face,
|
||||
/** 特殊步,如前进或后退 */
|
||||
Special,
|
||||
/** 动画方向步 */
|
||||
AnimDir
|
||||
}
|
||||
|
||||
export const enum ObjectSpecialStep {
|
||||
/** 前进 */
|
||||
Forward,
|
||||
/** 后退 */
|
||||
Backward
|
||||
}
|
||||
|
||||
export const enum ObjectAnimDirection {
|
||||
/** 正向播放动画 */
|
||||
Forward,
|
||||
/** 反向播放动画 */
|
||||
Backward
|
||||
}
|
||||
|
||||
export interface IObjectMovable {
|
||||
/** 当前横坐标 */
|
||||
readonly x: number;
|
||||
/** 当前纵坐标 */
|
||||
readonly y: number;
|
||||
|
||||
/**
|
||||
* 设置对象位置
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
setPos(x: number, y: number): void;
|
||||
|
||||
/**
|
||||
* 获取当前朝向
|
||||
*/
|
||||
getCurrentFaceDirection(): FaceDirection;
|
||||
}
|
||||
|
||||
export interface IObjectMoveStepDir {
|
||||
/** 步骤类型 */
|
||||
type: ObjectMoveStepType.Dir;
|
||||
/** 本步移动方向 */
|
||||
move: FaceDirection;
|
||||
}
|
||||
|
||||
export interface IObjectMoveStepDirFace {
|
||||
/** 步骤类型 */
|
||||
type: ObjectMoveStepType.DirFace;
|
||||
/** 本步移动方向 */
|
||||
move: FaceDirection;
|
||||
/** 本步显式朝向 */
|
||||
face: FaceDirection;
|
||||
}
|
||||
|
||||
export interface IObjectMoveStepSpeed {
|
||||
/** 步骤类型 */
|
||||
type: ObjectMoveStepType.Speed;
|
||||
/** 后续移动的每格耗时,单位为 ms */
|
||||
value: number;
|
||||
}
|
||||
|
||||
export interface IObjectMoveStepFace {
|
||||
/** 步骤类型 */
|
||||
type: ObjectMoveStepType.Face;
|
||||
/** 要设置的朝向 */
|
||||
value: FaceDirection;
|
||||
}
|
||||
|
||||
export interface IObjectMoveStepSpecial {
|
||||
/** 步骤类型 */
|
||||
type: ObjectMoveStepType.Special;
|
||||
/** 特殊步方向 */
|
||||
direction: ObjectSpecialStep;
|
||||
}
|
||||
|
||||
export interface IObjectMoveAnimDir {
|
||||
/** 步骤类型 */
|
||||
type: ObjectMoveStepType.AnimDir;
|
||||
/** 动画播放方向 */
|
||||
dir: ObjectAnimDirection;
|
||||
}
|
||||
|
||||
export type ObjectMoveStep =
|
||||
| IObjectMoveStepDir
|
||||
| IObjectMoveStepDirFace
|
||||
| IObjectMoveStepSpeed
|
||||
| IObjectMoveStepFace
|
||||
| IObjectMoveStepSpecial
|
||||
| IObjectMoveAnimDir;
|
||||
|
||||
export interface IMoverController {
|
||||
/** 本次移动是否已经全部完成 */
|
||||
done: boolean;
|
||||
/** 当本次移动结束时兑现 */
|
||||
onEnd: Promise<void>;
|
||||
|
||||
/**
|
||||
* 向当前移动队列末尾追加步骤
|
||||
* @param steps 要追加的步骤列表
|
||||
*/
|
||||
push(...steps: ObjectMoveStep[]): void;
|
||||
|
||||
/**
|
||||
* 在当前步移动之后立刻插入指定步,顺序为参数传入的顺序
|
||||
* @param steps 要插入的步骤列表
|
||||
*/
|
||||
insert(...steps: ObjectMoveStep[]): void;
|
||||
|
||||
/**
|
||||
* 停止当前移动,在当前步骤完成后兑现
|
||||
*/
|
||||
stop(): Promise<void>;
|
||||
}
|
||||
|
||||
export interface IObjectMoverHooks<T extends IObjectMovable> extends IHookBase {
|
||||
/**
|
||||
* 当移动开始时触发
|
||||
* @param tile 移动器绑定的图块
|
||||
* @param mover 当前移动器
|
||||
*/
|
||||
onMoveStart?(tile: T, mover: IObjectMover<T>): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当移动结束时触发
|
||||
* @param tile 移动器绑定的图块
|
||||
* @param mover 当前移动器
|
||||
*/
|
||||
onMoveEnd?(tile: T, mover: IObjectMover<T>): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当单步移动开始前触发,此时对象坐标仍为移动前坐标
|
||||
* @param code 当前步移动代码
|
||||
* @param step 当前步骤
|
||||
* @param tile 移动器绑定的图块
|
||||
* @param mover 当前移动器
|
||||
*/
|
||||
onStepStart?(
|
||||
code: number,
|
||||
step: ObjectMoveStep,
|
||||
tile: T,
|
||||
mover: IObjectMover<T>
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当单步移动结束后触发,此时对象坐标已更新为移动后坐标
|
||||
* @param code 当前步移动代码
|
||||
* @param step 当前步骤
|
||||
* @param tile 移动器绑定的图块
|
||||
* @param mover 当前移动器
|
||||
*/
|
||||
onStepEnd?(
|
||||
code: number,
|
||||
step: ObjectMoveStep,
|
||||
tile: T,
|
||||
mover: IObjectMover<T>
|
||||
): Promise<void>;
|
||||
}
|
||||
|
||||
export interface IObjectMover<T extends IObjectMovable> extends IHookable<
|
||||
IObjectMoverHooks<T>
|
||||
> {
|
||||
/** 当前是否正在移动 */
|
||||
readonly moving: boolean;
|
||||
/** 当前绑定的对象 */
|
||||
readonly tile: T;
|
||||
/** 当前朝向 */
|
||||
readonly faceDirection: FaceDirection;
|
||||
/** 当前移动方向 */
|
||||
readonly moveDirection: FaceDirection;
|
||||
/** 当前动画播放方向 */
|
||||
readonly currAnimDir: ObjectAnimDirection;
|
||||
/** 当前移动速度,单位毫秒 */
|
||||
readonly currentSpeed: number;
|
||||
|
||||
/**
|
||||
* 追加若干个绝对方向步,并同步更新移动方向与朝向
|
||||
* @param dir 移动方向
|
||||
* @param count 追加次数,默认 1
|
||||
*/
|
||||
step(dir: FaceDirection, count?: number): this;
|
||||
|
||||
/**
|
||||
* 追加若干个绝对方向步,移动方向与朝向分别指定
|
||||
* @param move 移动方向
|
||||
* @param face 朝向方向
|
||||
* @param count 追加次数,默认 1
|
||||
*/
|
||||
stepFace(move: FaceDirection, face: FaceDirection, count?: number): this;
|
||||
|
||||
/**
|
||||
* 追加若干个前进步,沿当前移动方向移动
|
||||
* @param count 追加次数,默认 1
|
||||
*/
|
||||
forward(count?: number): this;
|
||||
|
||||
/**
|
||||
* 追加若干个后退步,沿当前移动方向的反方向移动
|
||||
* @param count 追加次数,默认 1
|
||||
*/
|
||||
backward(count?: number): this;
|
||||
|
||||
/**
|
||||
* 设置后续移动速度
|
||||
* @param value 每格耗时,单位为 ms
|
||||
*/
|
||||
speed(value: number): this;
|
||||
|
||||
/**
|
||||
* 追加一个纯转向步骤
|
||||
* @param dir 目标朝向
|
||||
*/
|
||||
face(dir: FaceDirection): this;
|
||||
|
||||
/**
|
||||
* 设置后续步骤的动画播放方向
|
||||
* @param dir 动画播放方向
|
||||
*/
|
||||
animDir(dir: ObjectAnimDirection): this;
|
||||
|
||||
/**
|
||||
* 清空尚未执行的步骤队列
|
||||
*/
|
||||
clear(): this;
|
||||
|
||||
/**
|
||||
* 开始执行当前计划队列
|
||||
* @returns 若当前已在移动,则返回 `null`
|
||||
*/
|
||||
start(): Readonly<IMoverController> | null;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 移动基类
|
||||
|
||||
export abstract class ObjectMover<T extends IObjectMovable>
|
||||
extends Hookable<IObjectMoverHooks<T>>
|
||||
implements IObjectMover<T>
|
||||
{
|
||||
abstract readonly tile: T;
|
||||
|
||||
/** 尚未开始执行的移动步骤队列 */
|
||||
protected readonly moveQueue: Readonly<ObjectMoveStep>[] = [];
|
||||
|
||||
/** 当前是否正在移动 */
|
||||
moving: boolean = false;
|
||||
/** 当前朝向 */
|
||||
faceDirection: FaceDirection = FaceDirection.Unknown;
|
||||
/** 当前移动方向 */
|
||||
moveDirection: FaceDirection = FaceDirection.Unknown;
|
||||
/** 当前动画播放方向 */
|
||||
currAnimDir: ObjectAnimDirection = ObjectAnimDirection.Forward;
|
||||
/** 当前移动速度,单位毫秒 */
|
||||
currentSpeed: number = 100;
|
||||
|
||||
/** 是否调用了 `IMoverController.stop` 接口 */
|
||||
private shouldStop: boolean = false;
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IObjectMoverHooks<T>>
|
||||
): IHookController<IObjectMoverHooks<T>> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当移动开始时执行
|
||||
* @param tile 移动图块
|
||||
* @param controller 移动控制器
|
||||
*/
|
||||
protected abstract onMoveStart(
|
||||
tile: T,
|
||||
controller: Readonly<IMoverController>
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当移动结束时触发
|
||||
* @param tile 移动图块
|
||||
* @param controller 移动控制器
|
||||
*/
|
||||
protected abstract onMoveEnd(
|
||||
tile: T,
|
||||
controller: Readonly<IMoverController>
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当单步移动开始时触发,返回移动代码,此移动代码将会传递至 {@link onStepEnd}
|
||||
* @param step 当前移动步对象
|
||||
* @param tile 移动图块
|
||||
* @param controller 移动控制器
|
||||
*/
|
||||
protected abstract onStepStart(
|
||||
step: ObjectMoveStep,
|
||||
tile: T,
|
||||
controller: Readonly<IMoverController>
|
||||
): Promise<number>;
|
||||
|
||||
/**
|
||||
* 当单步移动结束时触发,返回坐标对象代表这一步移动结果
|
||||
* @param code 移动代码,由 {@link onStepStart} 返回值传递而来
|
||||
* @param step 当前移动步对象
|
||||
* @param tile 移动图块
|
||||
* @param controller 移动控制器
|
||||
*/
|
||||
protected abstract onStepEnd(
|
||||
code: number,
|
||||
step: ObjectMoveStep,
|
||||
tile: T,
|
||||
controller: Readonly<IMoverController>
|
||||
): Promise<ITileLocator>;
|
||||
|
||||
/**
|
||||
* 向计划队列末尾追加一个步骤
|
||||
* @param step 要追加的步骤
|
||||
*/
|
||||
protected pushStep(step: Readonly<ObjectMoveStep>): void {
|
||||
this.moveQueue.push(step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前应当作为相对移动基准的方向
|
||||
*/
|
||||
private getCurrentDirection(): FaceDirection {
|
||||
if (this.moveDirection !== FaceDirection.Unknown) {
|
||||
return this.moveDirection;
|
||||
} else {
|
||||
return this.faceDirection;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: 需要做一个朝向系统以解决朝向难以处理的问题
|
||||
/**
|
||||
* 获取指定方向的反方向
|
||||
* @param dir 当前方向
|
||||
*/
|
||||
private getOppositeDirection(dir: FaceDirection): FaceDirection {
|
||||
switch (dir) {
|
||||
case FaceDirection.Left:
|
||||
return FaceDirection.Right;
|
||||
case FaceDirection.Up:
|
||||
return FaceDirection.Down;
|
||||
case FaceDirection.Right:
|
||||
return FaceDirection.Left;
|
||||
case FaceDirection.Down:
|
||||
return FaceDirection.Up;
|
||||
case FaceDirection.LeftUp:
|
||||
return FaceDirection.RightDown;
|
||||
case FaceDirection.RightUp:
|
||||
return FaceDirection.LeftDown;
|
||||
case FaceDirection.LeftDown:
|
||||
return FaceDirection.RightUp;
|
||||
case FaceDirection.RightDown:
|
||||
return FaceDirection.LeftUp;
|
||||
case FaceDirection.Unknown:
|
||||
return FaceDirection.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据步骤内容预先同步移动器内部状态
|
||||
* @param step 当前步骤
|
||||
*/
|
||||
private prepareStep(step: ObjectMoveStep): void {
|
||||
switch (step.type) {
|
||||
case ObjectMoveStepType.Dir:
|
||||
this.moveDirection = step.move;
|
||||
this.faceDirection = step.move;
|
||||
break;
|
||||
case ObjectMoveStepType.DirFace:
|
||||
this.moveDirection = step.move;
|
||||
this.faceDirection = step.face;
|
||||
break;
|
||||
case ObjectMoveStepType.Face:
|
||||
this.faceDirection = step.value;
|
||||
break;
|
||||
case ObjectMoveStepType.Special: {
|
||||
const dir = this.getCurrentDirection();
|
||||
if (step.direction === ObjectSpecialStep.Backward) {
|
||||
const opposite = this.getOppositeDirection(dir);
|
||||
this.moveDirection = opposite;
|
||||
this.faceDirection = opposite;
|
||||
} else {
|
||||
this.moveDirection = dir;
|
||||
this.faceDirection = dir;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ObjectMoveStepType.AnimDir:
|
||||
this.currAnimDir = step.dir;
|
||||
break;
|
||||
case ObjectMoveStepType.Speed:
|
||||
this.currentSpeed = step.value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
step(dir: FaceDirection, count: number = 1): this {
|
||||
for (let i = 0; i < count; i++) {
|
||||
this.pushStep({
|
||||
type: ObjectMoveStepType.Dir,
|
||||
move: dir
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
stepFace(
|
||||
move: FaceDirection,
|
||||
face: FaceDirection,
|
||||
count: number = 1
|
||||
): this {
|
||||
for (let i = 0; i < count; i++) {
|
||||
this.pushStep({
|
||||
type: ObjectMoveStepType.DirFace,
|
||||
move,
|
||||
face
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
forward(count: number = 1): this {
|
||||
for (let i = 0; i < count; i++) {
|
||||
this.pushStep({
|
||||
type: ObjectMoveStepType.Special,
|
||||
direction: ObjectSpecialStep.Forward
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
backward(count: number = 1): this {
|
||||
for (let i = 0; i < count; i++) {
|
||||
this.pushStep({
|
||||
type: ObjectMoveStepType.Special,
|
||||
direction: ObjectSpecialStep.Backward
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
speed(value: number): this {
|
||||
this.pushStep({
|
||||
type: ObjectMoveStepType.Speed,
|
||||
value
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
face(dir: FaceDirection): this {
|
||||
this.pushStep({
|
||||
type: ObjectMoveStepType.Face,
|
||||
value: dir
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
animDir(dir: ObjectAnimDirection): this {
|
||||
this.pushStep({
|
||||
type: ObjectMoveStepType.AnimDir,
|
||||
dir
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
clear(): this {
|
||||
this.moveQueue.length = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始移动流程
|
||||
* @param queue 移动队列
|
||||
*/
|
||||
private async moveProgress(
|
||||
queue: ObjectMoveStep[],
|
||||
controller: Readonly<IMoverController>
|
||||
) {
|
||||
// 移动开始
|
||||
await this.onMoveStart(this.tile, controller);
|
||||
await Promise.all(
|
||||
this.forEachHook(hook => {
|
||||
return hook.onMoveStart?.(this.tile, this);
|
||||
})
|
||||
);
|
||||
|
||||
// 移动流程
|
||||
while (queue.length > 0) {
|
||||
const step = queue.shift();
|
||||
if (!step || !this.moving || this.shouldStop) break;
|
||||
this.prepareStep(step);
|
||||
const code = await this.onStepStart(step, this.tile, controller);
|
||||
const stepStartHooks = this.forEachHook(hook =>
|
||||
hook.onStepStart?.(code, step, this.tile, this)
|
||||
);
|
||||
await Promise.all(stepStartHooks);
|
||||
const loc = await this.onStepEnd(code, step, this.tile, controller);
|
||||
this.tile.setPos(loc.x, loc.y);
|
||||
const stepEndHooks = this.forEachHook(hook =>
|
||||
hook.onStepEnd?.(code, step, this.tile, this)
|
||||
);
|
||||
await Promise.all(stepEndHooks);
|
||||
}
|
||||
|
||||
// 移动结束
|
||||
await this.onMoveEnd(this.tile, controller);
|
||||
const moveEndHooks = this.forEachHook(hook =>
|
||||
hook.onMoveEnd?.(this.tile, this)
|
||||
);
|
||||
await Promise.all(moveEndHooks);
|
||||
}
|
||||
|
||||
start(): IMoverController | null {
|
||||
if (this.moving) return null;
|
||||
|
||||
const queue = this.moveQueue.slice();
|
||||
this.clear();
|
||||
this.shouldStop = false;
|
||||
|
||||
this.faceDirection = this.tile.getCurrentFaceDirection();
|
||||
this.moveDirection = FaceDirection.Unknown;
|
||||
|
||||
const { promise, resolve } = Promise.withResolvers<void>();
|
||||
|
||||
const controller: IMoverController = {
|
||||
done: false,
|
||||
onEnd: promise,
|
||||
push: (...steps: ObjectMoveStep[]) => {
|
||||
if (!this.moving || this.shouldStop || controller.done) {
|
||||
return;
|
||||
}
|
||||
queue.push(...steps);
|
||||
},
|
||||
insert: (...steps: ObjectMoveStep[]) => {
|
||||
if (!this.moving || this.shouldStop || controller.done) {
|
||||
return;
|
||||
}
|
||||
queue.unshift(...steps);
|
||||
},
|
||||
stop: () => {
|
||||
this.shouldStop = true;
|
||||
return controller.onEnd;
|
||||
}
|
||||
};
|
||||
|
||||
this.moving = true;
|
||||
const moving = this.moveProgress(queue, controller);
|
||||
moving.then(() => {
|
||||
this.moving = false;
|
||||
controller.done = true;
|
||||
resolve();
|
||||
});
|
||||
|
||||
return controller;
|
||||
}
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -0,0 +1,142 @@
|
||||
import {
|
||||
HookController,
|
||||
Hookable,
|
||||
IHookController,
|
||||
ITileLocator,
|
||||
logger
|
||||
} from '@motajs/common';
|
||||
import {
|
||||
IDynamicLayer,
|
||||
IDynamicLayerHooks,
|
||||
IDynamicTile,
|
||||
IMapLayer
|
||||
} from './types';
|
||||
import { FaceDirection, degradeFace } from '../common';
|
||||
import { DynamicTile } from './dynamicTile';
|
||||
|
||||
export class DynamicLayer
|
||||
extends Hookable<IDynamicLayerHooks>
|
||||
implements IDynamicLayer
|
||||
{
|
||||
/** 坐标到动态图块集合的映射,外层 key = y,内层 key = x,不使用 index 是为了支持地图外图块 */
|
||||
private readonly tilePosMap: Map<number, Map<number, Set<IDynamicTile>>> =
|
||||
new Map();
|
||||
/** 动态图块到其当前坐标的映射 */
|
||||
private readonly posTileMap: Map<IDynamicTile, ITileLocator> = new Map();
|
||||
|
||||
constructor(public readonly layer: IMapLayer) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IDynamicLayerHooks>
|
||||
): IHookController<IDynamicLayerHooks> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
createDynamic(num: number, x: number, y: number): IDynamicTile {
|
||||
const tile = new DynamicTile(num, x, y, this);
|
||||
this.addTileToPosMap(tile, x, y);
|
||||
this.posTileMap.set(tile, { x, y });
|
||||
this.forEachHook(hook => hook.onCreateTile?.(tile, this));
|
||||
return tile;
|
||||
}
|
||||
|
||||
transferToDynamic(x: number, y: number): IDynamicTile {
|
||||
const num = this.layer.getBlock(x, y);
|
||||
if (num === 0) {
|
||||
logger.warn(127, x.toString(), y.toString());
|
||||
}
|
||||
this.layer.setBlock(0, x, y);
|
||||
return this.createDynamic(num, x, y);
|
||||
}
|
||||
|
||||
transferToStatic(tile: IDynamicTile): void {
|
||||
const { x, y } = tile;
|
||||
const { width, height } = this.layer;
|
||||
if (x < 0 || y < 0 || x >= width || y >= height) {
|
||||
logger.warn(128, x.toString(), y.toString());
|
||||
return;
|
||||
}
|
||||
if (this.layer.getBlock(x, y) !== 0) {
|
||||
logger.warn(129, x.toString(), y.toString());
|
||||
}
|
||||
this.layer.setBlock(tile.num, x, y);
|
||||
this.removeTile(tile);
|
||||
this.forEachHook(hook => {
|
||||
void hook.onDeleteTile?.(tile, this);
|
||||
});
|
||||
}
|
||||
|
||||
transferToStaticIfSafe(tile: IDynamicTile): boolean {
|
||||
if (this.layer.getBlock(tile.x, tile.y) !== 0) return false;
|
||||
this.layer.setBlock(tile.num, tile.x, tile.y);
|
||||
this.deleteDynamic(tile);
|
||||
return true;
|
||||
}
|
||||
|
||||
async deleteDynamic(tile: IDynamicTile): Promise<void> {
|
||||
if (!this.posTileMap.has(tile)) {
|
||||
logger.warn(130);
|
||||
return;
|
||||
}
|
||||
this.removeTile(tile);
|
||||
const hooks = this.forEachHook(hook => hook.onDeleteTile?.(tile, this));
|
||||
await Promise.all(hooks);
|
||||
}
|
||||
|
||||
getDynamicTilesAt(x: number, y: number): Iterable<IDynamicTile> {
|
||||
return this.tilePosMap.get(y)?.get(x) ?? new Set();
|
||||
}
|
||||
|
||||
iterateDynamicTiles(): Iterable<IDynamicTile> {
|
||||
return this.posTileMap.keys();
|
||||
}
|
||||
|
||||
setDynamicDirection(tile: IDynamicTile, direction: FaceDirection): void {
|
||||
const numBefore = tile.num;
|
||||
tile.setFaceDirection(direction);
|
||||
if (tile.num !== numBefore) return;
|
||||
const degraded = degradeFace(direction);
|
||||
if (degraded !== direction) {
|
||||
tile.setFaceDirection(degraded);
|
||||
}
|
||||
}
|
||||
|
||||
updateDynamicTile(tile: IDynamicTile): void {
|
||||
const oldPos = this.posTileMap.get(tile);
|
||||
if (oldPos) {
|
||||
this.removeTileFromPosMap(tile, oldPos.x, oldPos.y);
|
||||
oldPos.x = tile.x;
|
||||
oldPos.y = tile.y;
|
||||
this.addTileToPosMap(tile, tile.x, tile.y);
|
||||
} else {
|
||||
this.addTileToPosMap(tile, tile.x, tile.y);
|
||||
this.posTileMap.set(tile, { x: tile.x, y: tile.y });
|
||||
}
|
||||
this.forEachHook(hook => hook.onUpdateTilePosition?.(tile, this));
|
||||
}
|
||||
|
||||
private addTileToPosMap(tile: IDynamicTile, x: number, y: number): void {
|
||||
const xMap = this.tilePosMap.getOrInsertComputed(y, () => new Map());
|
||||
const set = xMap.getOrInsertComputed(x, () => new Set());
|
||||
set.add(tile);
|
||||
}
|
||||
|
||||
private removeTileFromPosMap(
|
||||
tile: IDynamicTile,
|
||||
x: number,
|
||||
y: number
|
||||
): void {
|
||||
this.tilePosMap.get(y)?.get(x)?.delete(tile);
|
||||
}
|
||||
|
||||
/** 从两个内部映射中移除图块记录 */
|
||||
private removeTile(tile: IDynamicTile): void {
|
||||
const pos = this.posTileMap.get(tile);
|
||||
if (pos) {
|
||||
this.removeTileFromPosMap(tile, pos.x, pos.y);
|
||||
}
|
||||
this.posTileMap.delete(tile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import {
|
||||
FaceDirection,
|
||||
IMoverController,
|
||||
IObjectMover,
|
||||
IRoleFaceBinder
|
||||
} from '../common';
|
||||
import { IDynamicLayer, IDynamicTile } from './types';
|
||||
import { DynamicTileMover } from './mover';
|
||||
|
||||
export class DynamicTile implements IDynamicTile {
|
||||
readonly mover: IObjectMover<IDynamicTile>;
|
||||
|
||||
/** 当前的朝向绑定对象 */
|
||||
private face: IRoleFaceBinder | null = null;
|
||||
|
||||
constructor(
|
||||
public num: number,
|
||||
public x: number,
|
||||
public y: number,
|
||||
public readonly layer: IDynamicLayer
|
||||
) {
|
||||
this.mover = new DynamicTileMover(this);
|
||||
}
|
||||
|
||||
setFaceBinder(binder: IRoleFaceBinder | null): void {
|
||||
this.face = binder;
|
||||
}
|
||||
|
||||
setFaceDirection(direction: FaceDirection): number {
|
||||
if (!this.face) return this.num;
|
||||
const next = this.face.getFaceOf(this.num, direction);
|
||||
if (next) {
|
||||
this.num = next.identifier;
|
||||
}
|
||||
return this.num;
|
||||
}
|
||||
|
||||
delete(): Promise<void> {
|
||||
return this.layer.deleteDynamic(this);
|
||||
}
|
||||
|
||||
toStatic(): void {
|
||||
this.layer.transferToStatic(this);
|
||||
}
|
||||
|
||||
toStaticIfSafe(): boolean {
|
||||
return this.layer.transferToStaticIfSafe(this);
|
||||
}
|
||||
|
||||
step(dir: FaceDirection, count?: number): IMoverController | null {
|
||||
if (this.mover.moving) return null;
|
||||
this.mover.step(dir, count);
|
||||
return this.mover.start();
|
||||
}
|
||||
|
||||
setPos(x: number, y: number): void {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.layer.updateDynamicTile(this);
|
||||
}
|
||||
|
||||
getCurrentFaceDirection(): FaceDirection {
|
||||
if (this.face) {
|
||||
const face = this.face.getFaceDirection(this.num);
|
||||
if (isNil(face)) return FaceDirection.Unknown;
|
||||
else return face;
|
||||
} else {
|
||||
return FaceDirection.Down;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from './layerState';
|
||||
export * from './mapLayer';
|
||||
export * from './mapStore';
|
||||
export * from './mover';
|
||||
export * from './types';
|
||||
@@ -1,11 +1,13 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import {
|
||||
IDynamicLayer,
|
||||
IMapLayer,
|
||||
IMapLayerData,
|
||||
IMapLayerHookController,
|
||||
IMapLayerHooks
|
||||
} from './types';
|
||||
import { Hookable, HookController, logger } from '@motajs/common';
|
||||
import { DynamicLayer } from './dynamicLayer';
|
||||
|
||||
// todo: 提供 core.setBlock 等方法的替代方法,同时添加 setBlockList,以及前景背景的接口
|
||||
|
||||
@@ -23,6 +25,8 @@ export class MapLayer
|
||||
/** 地图数据引用 */
|
||||
private mapData: IMapLayerData;
|
||||
|
||||
readonly dynamicLayer: IDynamicLayer;
|
||||
|
||||
constructor(array: Uint32Array, width: number, height: number) {
|
||||
super();
|
||||
this.width = width;
|
||||
@@ -35,6 +39,7 @@ export class MapLayer
|
||||
expired: false,
|
||||
array: this.mapArray
|
||||
};
|
||||
this.dynamicLayer = new DynamicLayer(this);
|
||||
}
|
||||
|
||||
resize(width: number, height: number): void {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { ITileLocator, logger } from '@motajs/common';
|
||||
import {
|
||||
getFaceMovement,
|
||||
ObjectMover,
|
||||
ObjectMoveStep,
|
||||
ObjectMoveStepType
|
||||
} from '../common';
|
||||
import { IDynamicTile } from './types';
|
||||
|
||||
//#region 动态图块
|
||||
|
||||
const enum DynamicMoveCode {
|
||||
/** 正常执行 */
|
||||
Success
|
||||
}
|
||||
|
||||
export class DynamicTileMover extends ObjectMover<IDynamicTile> {
|
||||
constructor(public readonly tile: IDynamicTile) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected onMoveStart(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
protected onMoveEnd(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
protected onStepStart(): Promise<number> {
|
||||
return Promise.resolve(DynamicMoveCode.Success);
|
||||
}
|
||||
|
||||
protected onStepEnd(
|
||||
code: number,
|
||||
step: ObjectMoveStep,
|
||||
tile: IDynamicTile
|
||||
): Promise<ITileLocator> {
|
||||
if (code !== DynamicMoveCode.Success) {
|
||||
logger.warn(126, 'DynamicMoveCode.Success (0)', code.toString());
|
||||
return Promise.resolve({ x: tile.x, y: tile.y });
|
||||
}
|
||||
const locator: ITileLocator = {
|
||||
x: tile.x,
|
||||
y: tile.y
|
||||
};
|
||||
switch (step.type) {
|
||||
case ObjectMoveStepType.Dir: {
|
||||
const { x, y } = getFaceMovement(step.move);
|
||||
tile.setFaceDirection(step.move);
|
||||
locator.x += x;
|
||||
locator.y += y;
|
||||
break;
|
||||
}
|
||||
case ObjectMoveStepType.DirFace: {
|
||||
const { x, y } = getFaceMovement(step.move);
|
||||
tile.setFaceDirection(step.face);
|
||||
locator.x += x;
|
||||
locator.y += y;
|
||||
break;
|
||||
}
|
||||
case ObjectMoveStepType.Face: {
|
||||
tile.setFaceDirection(step.value);
|
||||
break;
|
||||
}
|
||||
case ObjectMoveStepType.Special: {
|
||||
const { x, y } = getFaceMovement(this.moveDirection);
|
||||
tile.setFaceDirection(this.faceDirection);
|
||||
locator.x += x;
|
||||
locator.y += y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Promise.resolve(locator);
|
||||
}
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -1,5 +1,14 @@
|
||||
import { IHookable, IHookBase, IHookController } from '@motajs/common';
|
||||
import { ISaveableContent } from '../common';
|
||||
import {
|
||||
FaceDirection,
|
||||
IMoverController,
|
||||
IObjectMovable,
|
||||
IObjectMover,
|
||||
IRoleFaceBinder,
|
||||
ISaveableContent
|
||||
} from '../common';
|
||||
|
||||
//#region 静态图层
|
||||
|
||||
export interface IMapLayerData {
|
||||
/** 当前引用是否过期,当地图图层内部的地图数组引用更新时,此项会变为 `true` */
|
||||
@@ -75,6 +84,9 @@ export interface IMapLayer extends IHookable<
|
||||
/** 图层纵深 */
|
||||
readonly zIndex: number;
|
||||
|
||||
/** 此图层对应的动态图块图层,z 层级与静态图块一致 */
|
||||
readonly dynamicLayer: IDynamicLayer;
|
||||
|
||||
/**
|
||||
* 设置某一点的图块
|
||||
* @param block 图块数字
|
||||
@@ -154,6 +166,10 @@ export interface IMapLayer extends IHookable<
|
||||
setMapRef(array: Uint32Array): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 图层管理
|
||||
|
||||
export interface ILayerStateHooks extends IHookBase {
|
||||
/**
|
||||
* 当设置背景图块时执行,如果设置的背景图块与原先一样,则不会执行
|
||||
@@ -288,6 +304,10 @@ export interface ILayerState extends IHookable<ILayerStateHooks> {
|
||||
setDirty(dirty: boolean): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 楼层管理
|
||||
|
||||
/** 单个 MapLayer 的存档数据 */
|
||||
export interface IMapLayerSave {
|
||||
readonly width: number;
|
||||
@@ -428,3 +448,149 @@ export interface IMapStore extends ISaveableContent<IMapStoreSave> {
|
||||
*/
|
||||
notifyEnterFloor(id: string): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 动态图块
|
||||
|
||||
export interface IDynamicLayerHooks extends IHookBase {
|
||||
/**
|
||||
* 当图块被创建(含从静态图块转换)时触发
|
||||
* @param tile 被创建的动态图块
|
||||
* @param layer 所属的动态图层
|
||||
*/
|
||||
onCreateTile(tile: IDynamicTile, layer: IDynamicLayer): void;
|
||||
|
||||
/**
|
||||
* 当图块被删除时触发
|
||||
* @param tile 被删除的动态图块
|
||||
* @param layer 所属的动态图层
|
||||
*/
|
||||
onDeleteTile(tile: IDynamicTile, layer: IDynamicLayer): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当更新动态图块的位置时触发(包括使用 `mover` 触发的移动)
|
||||
* @param tile 更新位置的图块
|
||||
* @param layer 所属的动态图层
|
||||
*/
|
||||
onUpdateTilePosition(tile: IDynamicTile, layer: IDynamicLayer): void;
|
||||
}
|
||||
|
||||
export interface IDynamicLayer extends IHookable<IDynamicLayerHooks> {
|
||||
/** 当前动态图层所属的静态图层 */
|
||||
readonly layer: IMapLayer;
|
||||
|
||||
/**
|
||||
* 在指定位置创建一个动态图块
|
||||
* @param num 图块数字
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
* @returns 创建的动态图块引用
|
||||
*/
|
||||
createDynamic(num: number, x: number, y: number): IDynamicTile;
|
||||
|
||||
/**
|
||||
* 从所属静态图层读取并清除指定位置的图块,创建对应动态图块并返回引用。
|
||||
* 若该位置图块为 0,则发出警告并仍然创建 `num = 0` 的动态图块
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
* @returns 创建的动态图块引用
|
||||
*/
|
||||
transferToDynamic(x: number, y: number): IDynamicTile;
|
||||
|
||||
/**
|
||||
* 将动态图块还原为静态图块。坐标越界则警告并放弃,
|
||||
* 否则写回静态图层并触发 {@link IDynamicLayerHooks.onDeleteTile}
|
||||
* @param tile 要还原的动态图块
|
||||
*/
|
||||
transferToStatic(tile: IDynamicTile): void;
|
||||
|
||||
/**
|
||||
* 仅当目标位置静态图块为 0(空白)时才还原为静态图块,否则不转换
|
||||
* @param tile 要还原的动态图块
|
||||
* @returns 是否转换成功
|
||||
*/
|
||||
transferToStaticIfSafe(tile: IDynamicTile): boolean;
|
||||
|
||||
/**
|
||||
* 删除指定动态图块,触发 {@link IDynamicLayerHooks.onDeleteTile} 钩子。
|
||||
* 若图块不属于此层则发出警告
|
||||
* @param tile 要删除的动态图块
|
||||
*/
|
||||
deleteDynamic(tile: IDynamicTile): Promise<void>;
|
||||
|
||||
/**
|
||||
* 获取指定格点上所有动态图块的可迭代对象
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
getDynamicTilesAt(x: number, y: number): Iterable<IDynamicTile>;
|
||||
|
||||
/**
|
||||
* 迭代所有的动态图块
|
||||
*/
|
||||
iterateDynamicTiles(): Iterable<IDynamicTile>;
|
||||
|
||||
/**
|
||||
* 手动设置动态图块的朝向,更新 `tile.num`(若有朝向绑定)。
|
||||
* 转向逻辑与移动时的转向逻辑相同,但不触发移动
|
||||
* @param tile 要设置朝向的动态图块
|
||||
* @param direction 目标朝向
|
||||
*/
|
||||
setDynamicDirection(tile: IDynamicTile, direction: FaceDirection): void;
|
||||
|
||||
/**
|
||||
* 更新图块内部存储位置
|
||||
* @param tile 动态图块
|
||||
*/
|
||||
updateDynamicTile(tile: IDynamicTile): void;
|
||||
}
|
||||
|
||||
export interface IDynamicTile extends IObjectMovable {
|
||||
/** 当前图块数字 */
|
||||
readonly num: number;
|
||||
/** 当前图块所属的动态图层 */
|
||||
readonly layer: IDynamicLayer;
|
||||
/** 当前动态图块的移动器 */
|
||||
readonly mover: IObjectMover<IDynamicTile>;
|
||||
|
||||
/**
|
||||
* 设置图块朝向,会一并修改 {@link num},返回设置后的当前图块数字
|
||||
* @param direction 图块朝向
|
||||
*/
|
||||
setFaceDirection(direction: FaceDirection): number;
|
||||
|
||||
/**
|
||||
* 直接删除此图块
|
||||
*/
|
||||
delete(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 将当前图块还原为静态图块
|
||||
*/
|
||||
toStatic(): void;
|
||||
|
||||
/**
|
||||
* 还原为静态图块,如果当前位置有东西则不转换
|
||||
*/
|
||||
toStaticIfSafe(): boolean;
|
||||
|
||||
/**
|
||||
* 单步便捷移动接口,适用于简单移动场景,复杂路径通过 `tile.mover` 访问。
|
||||
* 等价于:
|
||||
*
|
||||
* ```ts
|
||||
* mover.step(dir, count);
|
||||
* return mover.start();
|
||||
* ```
|
||||
*/
|
||||
step(dir: FaceDirection, count?: number): IMoverController | null;
|
||||
|
||||
/**
|
||||
* 注入朝向绑定器,初始状态视为无朝向绑定
|
||||
* @param binder 朝向绑定器
|
||||
*/
|
||||
setFaceBinder(binder: IRoleFaceBinder | null): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
Reference in new issue
Block a user