mirror of
https://github.com/motajs/template.git
synced 2026-11-10 13:04:30 +08:00
feat: 将 HumanBreak 新内容移植入该样板
This commit is contained in:
1 parent
6bf32174d8
commit
bea2725d6a
128 files changed
+11984
-573
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { IFaceData, IRoleFaceBinder } from './types';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { FaceDirection } from '.';
|
||||
|
||||
interface FaceInfo {
|
||||
/** 此图块的朝向 */
|
||||
readonly face: FaceDirection;
|
||||
/** 此图块对应的映射 */
|
||||
readonly map: Map<FaceDirection, number>;
|
||||
}
|
||||
|
||||
export class RoleFaceBinder implements IRoleFaceBinder {
|
||||
/** 每个图块对应的朝向信息 */
|
||||
private faceMap: Map<number, FaceInfo> = new Map();
|
||||
/** 主要朝向映射 */
|
||||
private mainMap: Map<number, FaceDirection> = new Map();
|
||||
|
||||
malloc(identifier: number, main: FaceDirection): void {
|
||||
this.mainMap.set(identifier, main);
|
||||
const map = new Map<FaceDirection, number>();
|
||||
map.set(main, identifier);
|
||||
const info: FaceInfo = { face: main, map };
|
||||
this.faceMap.set(identifier, info);
|
||||
}
|
||||
|
||||
bind(identifier: number, main: number, face: FaceDirection): void {
|
||||
const mainFace = this.mainMap.get(main);
|
||||
if (isNil(mainFace)) {
|
||||
logger.error(43, main.toString());
|
||||
return;
|
||||
}
|
||||
if (mainFace === face) {
|
||||
logger.error(44, main.toString());
|
||||
return;
|
||||
}
|
||||
const { map } = this.faceMap.get(main)!;
|
||||
map.set(face, identifier);
|
||||
const info: FaceInfo = { face, map };
|
||||
this.faceMap.set(identifier, info);
|
||||
this.mainMap.set(identifier, mainFace);
|
||||
}
|
||||
|
||||
getFaceOf(identifier: number, face: FaceDirection): IFaceData | null {
|
||||
const info = this.faceMap.get(identifier);
|
||||
if (!info) return null;
|
||||
const target = info.map.get(face);
|
||||
if (isNil(target)) return null;
|
||||
const data: IFaceData = { identifier: target, face };
|
||||
return data;
|
||||
}
|
||||
|
||||
getFaceDirection(identifier: number): FaceDirection | undefined {
|
||||
return this.faceMap.get(identifier)?.face;
|
||||
}
|
||||
|
||||
getMainFace(identifier: number): IFaceData | null {
|
||||
const face = this.mainMap.get(identifier);
|
||||
if (isNil(face)) return null;
|
||||
const data: IFaceData = { identifier, face };
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './face';
|
||||
export * from './types';
|
||||
export * from './utils';
|
||||
@@ -0,0 +1,54 @@
|
||||
export const enum FaceDirection {
|
||||
Unknown,
|
||||
Left,
|
||||
Up,
|
||||
Right,
|
||||
Down,
|
||||
LeftUp,
|
||||
RightUp,
|
||||
LeftDown,
|
||||
RightDown
|
||||
}
|
||||
|
||||
export interface IFaceData {
|
||||
/** 图块数字 */
|
||||
readonly identifier: number;
|
||||
/** 图块朝向 */
|
||||
readonly face: FaceDirection;
|
||||
}
|
||||
|
||||
export interface IRoleFaceBinder {
|
||||
/**
|
||||
* 给指定的图块分配朝向绑定
|
||||
* @param identifier 图块数字
|
||||
* @param main 主图块朝向,一般是朝下
|
||||
*/
|
||||
malloc(identifier: number, main: FaceDirection): void;
|
||||
|
||||
/**
|
||||
* 将一个图块与另一个图块绑定朝向,需要注意要先调用 {@link malloc} 分配朝向信息
|
||||
* @param identifier 当前图块数字
|
||||
* @param main 主图块数字,即当前图块与目标图块属于主图块的另一个朝向
|
||||
* @param face 当前图块的朝向方向
|
||||
*/
|
||||
bind(identifier: number, main: number, face: FaceDirection): void;
|
||||
|
||||
/**
|
||||
* 获取一个图块指定朝向的图块数字
|
||||
* @param identifier 图块数字,可以是任意朝向的图块数字
|
||||
* @param face 要获取的朝向
|
||||
*/
|
||||
getFaceOf(identifier: number, face: FaceDirection): IFaceData | null;
|
||||
|
||||
/**
|
||||
* 获取指定图块数字是哪个朝向
|
||||
* @param identifier 图块数字
|
||||
*/
|
||||
getFaceDirection(identifier: number): FaceDirection | undefined;
|
||||
|
||||
/**
|
||||
* 获取指定图块数字绑定至的主朝向
|
||||
* @param identifier 图块数字,可以是任意朝向的图块数字
|
||||
*/
|
||||
getMainFace(identifier: number): IFaceData | null;
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import { FaceDirection } from './types';
|
||||
|
||||
/**
|
||||
* 获取指定朝向的坐标偏移量
|
||||
* @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 };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将八方向朝向降级为四方向朝向
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定朝向旋转后的朝向
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据朝向字符串获取朝向枚举值
|
||||
* @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