refactor: data-common/common

This commit is contained in:
unanmed 2026-09-23 16:04:50 +08:00
parent 9fb04e3129
commit 1fb92c3976
12 changed files with 51 additions and 233 deletions

View File

@ -1,4 +1,3 @@
import { isNil } from 'lodash-es';
import {
FaceDirection,
IMoverController,
@ -76,12 +75,7 @@ export class DynamicTile
}
getCurrentFaceDirection(): FaceDirection {
const curr = this.layer.faceBinder.getFaceDirection(this.tileNum);
if (isNil(curr)) {
return FaceDirection.Unknown;
} else {
return curr;
}
return this.layer.faceBinder.getFaceDirection(this.tileNum);
}
@shouldReplay('Transfering dynamic tile to static should be replayed.')

View File

@ -15,7 +15,6 @@ import {
} from './types';
import { Hookable, HookController, ITileLocator, logger } from '@motajs/common';
import {
degradeFace,
FaceDirection,
IDataCommon,
IRoleFaceBinder,
@ -525,14 +524,7 @@ export class MapLayer
@shouldReplay('Setting map layer block direction should be replayed.')
setDynamicDirection(tile: IDynamicTile, direction: FaceDirection): number {
const numBefore = tile.num();
tile.setFaceDirection(direction);
if (tile.num() !== numBefore) return tile.num();
const degraded = degradeFace(direction);
if (degraded !== direction) {
tile.setFaceDirection(degraded);
}
return tile.num();
return tile.setFaceDirection(direction);
}
@shouldReplay('Updating dynamic tile position should be replayed.')

View File

@ -1,6 +1,6 @@
import { ITileLocator, logger } from '@motajs/common';
import {
getFaceMovement,
FaceGroup,
ObjectMover,
ObjectMoveStep,
ObjectMoveType,
@ -46,16 +46,21 @@ export class DynamicTileMover extends ObjectMover<IDynamicTile> {
x: tile.x,
y: tile.y
};
const handler = tile.state.faceManager.get(FaceGroup.Dir8);
if (!handler) {
logger.warn(192);
return Promise.resolve({ x: tile.x, y: tile.y });
}
switch (step.type) {
case ObjectMoveType.Dir: {
const { x, y } = getFaceMovement(step.move);
const { x, y } = handler.movement(step.move);
tile.setFaceDirection(step.move);
locator.x += x;
locator.y += y;
break;
}
case ObjectMoveType.DirFace: {
const { x, y } = getFaceMovement(step.move);
const { x, y } = handler.movement(step.move);
tile.setFaceDirection(step.face);
locator.x += x;
locator.y += y;
@ -66,7 +71,7 @@ export class DynamicTileMover extends ObjectMover<IDynamicTile> {
break;
}
case ObjectMoveType.Special: {
const { x, y } = getFaceMovement(this.moveDirection);
const { x, y } = handler.movement(this.moveDirection);
tile.setFaceDirection(this.faceDirection);
locator.x += x;
locator.y += y;

View File

@ -1,6 +1,7 @@
import { ITileLocator } from '@motajs/common';
import {
FaceDirection,
FaceGroup,
IDataCommon,
IDataCommonExtended,
ISaveableContent,
@ -58,10 +59,17 @@ export abstract class MapTileBase<TSave extends IMapBlockSaveBase>
const cur = this.num();
const next = this.layer.faceBinder.getFaceOf(cur, direction);
if (next) {
// 先尝试在 dir8 中寻找
this.set(next.identifier);
return next.identifier;
} else {
return cur;
// 找不到则降级到 dir4
const handler = this.state.faceManager.get(FaceGroup.Dir4);
if (!handler) return cur;
const degraded = handler.degrade(direction);
const next = this.layer.faceBinder.getFaceOf(cur, degraded);
if (next) return next.identifier;
else return cur;
}
}

View File

@ -3,7 +3,7 @@ import { IFaceData, IRoleFaceBinder } from './types';
import { isNil } from 'lodash-es';
import { FaceDirection } from './types';
interface FaceInfo {
interface IFaceInfo {
/** 此图块的朝向 */
readonly face: FaceDirection;
/** 此图块对应的映射 */
@ -12,7 +12,7 @@ interface FaceInfo {
export class RoleFaceBinder implements IRoleFaceBinder {
/** 每个图块对应的朝向信息 */
private faceMap: Map<number, FaceInfo> = new Map();
private faceMap: Map<number, IFaceInfo> = new Map();
/** 主要朝向映射 */
private mainMap: Map<number, FaceDirection> = new Map();
@ -20,7 +20,7 @@ export class RoleFaceBinder implements IRoleFaceBinder {
this.mainMap.set(identifier, main);
const map = new Map<FaceDirection, number>();
map.set(main, identifier);
const info: FaceInfo = { face: main, map };
const info: IFaceInfo = { face: main, map };
this.faceMap.set(identifier, info);
}
@ -36,7 +36,7 @@ export class RoleFaceBinder implements IRoleFaceBinder {
}
const { map } = this.faceMap.get(main)!;
map.set(face, identifier);
const info: FaceInfo = { face, map };
const info: IFaceInfo = { face, map };
this.faceMap.set(identifier, info);
this.mainMap.set(identifier, mainFace);
}
@ -50,8 +50,8 @@ export class RoleFaceBinder implements IRoleFaceBinder {
return data;
}
getFaceDirection(identifier: number): FaceDirection | undefined {
return this.faceMap.get(identifier)?.face;
getFaceDirection(identifier: number): FaceDirection {
return this.faceMap.get(identifier)?.face ?? FaceDirection.Unknown;
}
getMainFace(identifier: number): IFaceData | null {

View File

@ -117,6 +117,13 @@ export class Dir8FaceHandler implements IFaceHandler<FaceDirection> {
return map.get(degraded) ?? FaceDirection.Unknown;
}
dirOf(dx: number, dy: number): FaceDirection {
for (const [dir, { x, y }] of DIR8_MOVEMENTS) {
if (x === dx && y === dy) return dir;
}
return FaceDirection.Unknown;
}
mapDirection(): Iterable<FaceDirection> {
return DIR8_MOVEMENTS.keys();
}
@ -192,6 +199,13 @@ export class Dir4FaceHandler implements IFaceHandler<FaceDirection> {
return map.get(degraded) ?? FaceDirection.Unknown;
}
dirOf(dx: number, dy: number): FaceDirection {
for (const [dir, { x, y }] of DIR8_MOVEMENTS) {
if (x === dx && y === dy) return dir;
}
return FaceDirection.Unknown;
}
mapDirection(): Iterable<FaceDirection> {
return DIR4_MOVEMENTS.keys();
}

View File

@ -3,4 +3,3 @@ export * from './faceManager';
export * from './indexer';
export * from './mover';
export * from './types';
export * from './utils';

View File

@ -463,20 +463,6 @@ export abstract class ObjectMover<T extends IObjectMovable>
this.moveQueue.push(step);
}
/**
* 获取相对移动(如前进)所使用的基准方向
*
* 优先取本步之前已确定的移动方向,使多步前进保持同轴;否则回退到当前朝向。
* 后退不使用此基准,一律以当前朝向为准
*/
private getCurrentDirection(): FaceDirection {
if (this.moveDirection !== FaceDirection.Unknown) {
return this.moveDirection;
} else {
return this.faceDirection;
}
}
/**
* 根据步骤内容预先同步移动器内部状态
*
@ -498,13 +484,11 @@ export abstract class ObjectMover<T extends IObjectMovable>
this.faceDirection = step.value;
break;
case ObjectMoveType.Special: {
const dir = this.faceDirection;
if (step.direction === ObjectSpecialStep.Backward) {
// 后退基准固定为当前朝向,避免被本步写入的反方向改写后摆动
const dir = this.faceDirection;
this.moveDirection = this.faceHandler.opposite(dir);
this.faceDirection = dir;
} else {
const dir = this.getCurrentDirection();
this.moveDirection = dir;
this.faceDirection = dir;
}

View File

@ -48,10 +48,10 @@ export interface IRoleFaceBinder {
getFaceOf(identifier: number, face: FaceDirection): IFaceData | null;
/**
* 获取指定图块数字是哪个朝向
* 获取指定图块数字是哪个朝向,如果没有指定图块的数据,那么返回 `FaceDirection.Unknown`
* @param identifier 图块数字
*/
getFaceDirection(identifier: number): FaceDirection | undefined;
getFaceDirection(identifier: number): FaceDirection;
/**
* 获取指定图块数字绑定至的主朝向

View File

@ -1,186 +0,0 @@
import { FaceDirection } from './types';
/**
* @deprecated 用 `IFaceHandler` 接口
* 获取指定朝向的坐标偏移量
* @param dir 朝向
*/
export function getFaceMovement(dir: FaceDirection): Loc {
switch (dir) {
case FaceDirection.Left:
return { x: -1, y: 0 };
case FaceDirection.Right:
return { x: 1, y: 0 };
case FaceDirection.Up:
return { x: 0, y: -1 };
case FaceDirection.Down:
return { x: 0, y: 1 };
case FaceDirection.LeftUp:
return { x: -1, y: -1 };
case FaceDirection.RightUp:
return { x: 1, y: -1 };
case FaceDirection.LeftDown:
return { x: -1, y: 1 };
case FaceDirection.RightDown:
return { x: 1, y: 1 };
case FaceDirection.Unknown:
return { x: 0, y: 0 };
}
}
/**
* @deprecated 用 `IFaceHandler` 接口
* 将八方向朝向降级为四方向朝向
* @param dir 朝向
* @param unknown 如果朝向是 `FaceDirection.Unknown`,那么会返回什么,默认值是未知
*/
export function degradeFace(
dir: FaceDirection,
unknown: FaceDirection = FaceDirection.Unknown
): FaceDirection {
switch (dir) {
case FaceDirection.LeftUp:
return FaceDirection.Left;
case FaceDirection.LeftDown:
return FaceDirection.Left;
case FaceDirection.RightUp:
return FaceDirection.Right;
case FaceDirection.RightDown:
return FaceDirection.Right;
case FaceDirection.Unknown:
return unknown;
}
return dir;
}
/**
* @deprecated 用 `IFaceHandler` 接口
* 获取指定朝向旋转后的朝向
* @param dir 当前朝向
* @param anticlockwise 是否逆时针旋转,默认顺时针
* @param face8 是否使用八朝向。为 `false` 时,旋转为九十度旋转,即 上->右->下->左,左上->右上->右下->左下。
* 为 `true` 时,旋转为四十五度旋转,即 上->右上->右->右下->下->左下->左->左上。逆时针反过来旋转。
*/
export function nextFaceDirection(
dir: FaceDirection,
anticlockwise: boolean = false,
face8: boolean = false
): FaceDirection {
if (face8) {
if (anticlockwise) {
switch (dir) {
case FaceDirection.Left:
return FaceDirection.LeftDown;
case FaceDirection.LeftDown:
return FaceDirection.Down;
case FaceDirection.Down:
return FaceDirection.RightDown;
case FaceDirection.RightDown:
return FaceDirection.Right;
case FaceDirection.Right:
return FaceDirection.RightUp;
case FaceDirection.RightUp:
return FaceDirection.Up;
case FaceDirection.Up:
return FaceDirection.LeftUp;
case FaceDirection.LeftUp:
return FaceDirection.Left;
case FaceDirection.Unknown:
return FaceDirection.Unknown;
}
} else {
switch (dir) {
case FaceDirection.Left:
return FaceDirection.LeftUp;
case FaceDirection.LeftUp:
return FaceDirection.Up;
case FaceDirection.Up:
return FaceDirection.RightUp;
case FaceDirection.RightUp:
return FaceDirection.Right;
case FaceDirection.Right:
return FaceDirection.RightDown;
case FaceDirection.RightDown:
return FaceDirection.Down;
case FaceDirection.Down:
return FaceDirection.LeftDown;
case FaceDirection.LeftDown:
return FaceDirection.Left;
case FaceDirection.Unknown:
return FaceDirection.Unknown;
}
}
} else {
if (anticlockwise) {
switch (dir) {
case FaceDirection.Left:
return FaceDirection.Down;
case FaceDirection.Down:
return FaceDirection.Right;
case FaceDirection.Right:
return FaceDirection.Up;
case FaceDirection.Up:
return FaceDirection.Left;
case FaceDirection.LeftUp:
return FaceDirection.LeftDown;
case FaceDirection.LeftDown:
return FaceDirection.RightDown;
case FaceDirection.RightDown:
return FaceDirection.RightUp;
case FaceDirection.RightUp:
return FaceDirection.LeftUp;
case FaceDirection.Unknown:
return FaceDirection.Unknown;
}
} else {
switch (dir) {
case FaceDirection.Left:
return FaceDirection.Up;
case FaceDirection.Up:
return FaceDirection.Right;
case FaceDirection.Right:
return FaceDirection.Down;
case FaceDirection.Down:
return FaceDirection.Left;
case FaceDirection.LeftUp:
return FaceDirection.RightUp;
case FaceDirection.RightUp:
return FaceDirection.RightDown;
case FaceDirection.RightDown:
return FaceDirection.LeftDown;
case FaceDirection.LeftDown:
return FaceDirection.LeftUp;
case FaceDirection.Unknown:
return FaceDirection.Unknown;
}
}
}
}
/**
* @deprecated 用 `IFaceHandler` 接口
* 根据朝向字符串获取朝向枚举值
* @param dir 朝向字符串
*/
export function fromDirectionString(dir: Dir2): FaceDirection {
switch (dir) {
case 'left':
return FaceDirection.Left;
case 'right':
return FaceDirection.Right;
case 'up':
return FaceDirection.Up;
case 'down':
return FaceDirection.Down;
case 'leftup':
return FaceDirection.LeftUp;
case 'rightup':
return FaceDirection.RightUp;
case 'leftdown':
return FaceDirection.LeftDown;
case 'rightdown':
return FaceDirection.RightDown;
default:
return FaceDirection.Unknown;
}
}

View File

@ -270,6 +270,7 @@
"189": "Cannot flatten autotile since it cannot be splitted evenly. Autotile width: $1, frames: $2.",
"190": "Cannot flatten autotile since its width or height cannot be divided evenly. Autotile frame size: [$1,$2].",
"191": "Cannot cache autotile since no its frame or type data stored.",
"192": "A FaceGroup.Dir8 face handler registration is expected.",
"1001": "Event(setBlock): Unknown tile '$1' when setting block."
}
}

View File

@ -185,6 +185,13 @@ export interface IFaceHandler<T extends number> {
*/
next(dir: number, anticlockwise?: boolean): T;
/**
* 获取对应于指定偏移量的朝向方向。如果有多个符合要求,那么会返回其中一个符合要求的朝向
* @param dx 横坐标移动偏移量
* @param dy 纵坐标移动偏移量
*/
dirOf(dx: number, dy: number): T;
/**
* 迭代本组支持的所有朝向,包含 `Unknown`
*/