mirror of
https://github.com/motajs/template.git
synced 2026-11-09 03:34:30 +08:00
refactor: data-common/common
This commit is contained in:
1 parent
9fb04e3129
commit
1fb92c3976
12 files changed
+51
-233
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -3,4 +3,3 @@ export * from './faceManager';
|
||||
export * from './indexer';
|
||||
export * from './mover';
|
||||
export * from './types';
|
||||
export * from './utils';
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
* 获取指定图块数字绑定至的主朝向
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user