mirror of
https://github.com/motajs/template.git
synced 2026-11-17 09:34:30 +08:00
feat: 怪物及地图伤害系统
This commit is contained in:
1 parent
fe67939a05
commit
057d5ab813
28 files changed
+3638
-264
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import {
|
||||
IDirectionDescriptor,
|
||||
IDirectionMapper,
|
||||
InternalDirectionGroup
|
||||
} from './types';
|
||||
|
||||
export class DirectionMapper implements IDirectionMapper {
|
||||
private readonly groups: Map<number, IDirectionDescriptor[]> = new Map();
|
||||
|
||||
constructor() {
|
||||
this.registerGroup(InternalDirectionGroup.Dir4, [
|
||||
{ x: 0, y: -1 },
|
||||
{ x: 0, y: 1 },
|
||||
{ x: -1, y: 0 },
|
||||
{ x: 1, y: 0 }
|
||||
]);
|
||||
this.registerGroup(InternalDirectionGroup.Dir8, [
|
||||
{ x: 0, y: -1 },
|
||||
{ x: 0, y: 1 },
|
||||
{ x: -1, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: -1, y: -1 },
|
||||
{ x: 1, y: -1 },
|
||||
{ x: -1, y: 1 },
|
||||
{ x: 1, y: 1 }
|
||||
]);
|
||||
}
|
||||
|
||||
registerGroup(group: number, dir: Iterable<IDirectionDescriptor>): void {
|
||||
this.groups.set(group, [...dir]);
|
||||
}
|
||||
|
||||
map(group: number): Iterable<IDirectionDescriptor> {
|
||||
return this.groups.get(group) ?? [];
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
export * from './dir';
|
||||
export * from './func';
|
||||
export * from './range';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,302 @@
|
||||
import { clamp } from 'lodash-es';
|
||||
import {
|
||||
IDirectionDescriptor,
|
||||
IManhattanRangeParam,
|
||||
IRangeHost,
|
||||
IRange,
|
||||
IRayRangeParam,
|
||||
IRectRangeParam
|
||||
} from './types';
|
||||
|
||||
export abstract class BaseRange<T> implements IRange<T> {
|
||||
protected host: IRangeHost = { width: 0, height: 0 };
|
||||
|
||||
bindHost(host: IRangeHost): void {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个点是否在宿主对象范围内
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
protected isInBounds(x: number, y: number) {
|
||||
const { width, height } = this.host;
|
||||
return x >= 0 && y >= 0 && x < width && y < height;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个坐标索引是否在宿主对象范围内
|
||||
* @param index 坐标索引
|
||||
*/
|
||||
protected isValidIndex(index: number) {
|
||||
const { width, height } = this.host;
|
||||
return index >= 0 && index < width * height;
|
||||
}
|
||||
|
||||
/**
|
||||
* 估算范围内总共有多少个点
|
||||
* @param param 传递的参数
|
||||
*/
|
||||
protected abstract estimatePointCount(param: Readonly<T>): number;
|
||||
|
||||
abstract iterateLoc(param: Readonly<T>): Iterable<number>;
|
||||
|
||||
abstract inRange(x: number, y: number, param: Readonly<T>): boolean;
|
||||
|
||||
inRangeIndex(index: number, param: Readonly<T>): boolean {
|
||||
const { width } = this.host;
|
||||
return this.inRange(index % width, Math.floor(index / width), param);
|
||||
}
|
||||
|
||||
*iterate(list: Iterable<number>, param: Readonly<T>): Iterable<number> {
|
||||
for (const index of list) {
|
||||
if (this.inRangeIndex(index, param)) yield index;
|
||||
}
|
||||
}
|
||||
|
||||
*scan(list: Set<number>, param: Readonly<T>): Iterable<number> {
|
||||
for (const index of this.iterateLoc(param)) {
|
||||
if (list.has(index)) yield index;
|
||||
}
|
||||
}
|
||||
|
||||
autoDetect(list: Set<number>, param: Readonly<T>): Iterable<number> {
|
||||
if (this.estimatePointCount(param) < list.size) {
|
||||
return this.scan(list, param);
|
||||
} else {
|
||||
return this.iterate(list, param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class RectRange extends BaseRange<IRectRangeParam> {
|
||||
protected estimatePointCount(param: Readonly<IRectRangeParam>): number {
|
||||
return Math.abs(param.w) * Math.abs(param.h);
|
||||
}
|
||||
|
||||
*iterateLoc(param: Readonly<IRectRangeParam>): Iterable<number> {
|
||||
const { width, height } = this.host;
|
||||
const sx = clamp(Math.min(param.x, param.x + param.w), 0, width);
|
||||
const sy = clamp(Math.min(param.y, param.y + param.h), 0, height);
|
||||
const ex = clamp(Math.max(param.x, param.x + param.w), 0, width);
|
||||
const ey = clamp(Math.max(param.y, param.y + param.h), 0, height);
|
||||
if (sx >= ex || sy >= ey) return;
|
||||
|
||||
for (let y = sy; y < ey; y++) {
|
||||
for (let x = sx; x < ex; x++) {
|
||||
yield y * width + x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inRange(x: number, y: number, param: Readonly<IRectRangeParam>): boolean {
|
||||
const sx = Math.min(param.x, param.x + param.w);
|
||||
const sy = Math.min(param.y, param.y + param.h);
|
||||
const ex = Math.max(param.x, param.x + param.w);
|
||||
const ey = Math.max(param.y, param.y + param.h);
|
||||
|
||||
return this.isInBounds(x, y) && x >= sx && y >= sy && x < ex && y < ey;
|
||||
}
|
||||
}
|
||||
|
||||
export class ManhattanRange extends BaseRange<IManhattanRangeParam> {
|
||||
protected estimatePointCount(
|
||||
param: Readonly<IManhattanRangeParam>
|
||||
): number {
|
||||
const radius = Math.max(0, param.radius);
|
||||
return 1 + 2 * radius * (radius + 1);
|
||||
}
|
||||
|
||||
*iterateLoc(param: Readonly<IManhattanRangeParam>): Iterable<number> {
|
||||
const { width, height } = this.host;
|
||||
for (let dy = -param.radius; dy <= param.radius; dy++) {
|
||||
const y = param.cy + dy;
|
||||
if (y < 0 || y >= height) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const span = param.radius - Math.abs(dy);
|
||||
const startX = Math.max(0, param.cx - span);
|
||||
const endX = Math.min(width - 1, param.cx + span);
|
||||
for (let x = startX; x <= endX; x++) {
|
||||
yield y * width + x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inRange(
|
||||
x: number,
|
||||
y: number,
|
||||
param: Readonly<IManhattanRangeParam>
|
||||
): boolean {
|
||||
return (
|
||||
this.isInBounds(x, y) &&
|
||||
Math.abs(x - param.cx) + Math.abs(y - param.cy) <= param.radius
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export class RayRange extends BaseRange<IRayRangeParam> {
|
||||
protected estimatePointCount(param: Readonly<IRayRangeParam>): number {
|
||||
const { width, height } = this.host;
|
||||
// 考虑到这种范围的 `inRange` 判断要更加耗时,因此将返回值略微降低,更倾向于使用 `scan` 方式
|
||||
return ((width + height) * param.dir.length) / 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个点是否在某条射线上
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
* @param direction 方向对象
|
||||
* @param param 范围参数
|
||||
*/
|
||||
private isPointOnRay(
|
||||
x: number,
|
||||
y: number,
|
||||
direction: IDirectionDescriptor,
|
||||
param: Readonly<IRayRangeParam>
|
||||
): boolean {
|
||||
if (direction.x === 0 && direction.y === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const deltaX = x - param.cx;
|
||||
const deltaY = y - param.cy;
|
||||
|
||||
if (direction.x === 0) {
|
||||
return (
|
||||
deltaX === 0 &&
|
||||
deltaY % direction.y === 0 &&
|
||||
deltaY / direction.y >= 0
|
||||
);
|
||||
}
|
||||
|
||||
if (direction.y === 0) {
|
||||
return (
|
||||
deltaY === 0 &&
|
||||
deltaX % direction.x === 0 &&
|
||||
deltaX / direction.x >= 0
|
||||
);
|
||||
}
|
||||
|
||||
if (deltaX % direction.x !== 0 || deltaY % direction.y !== 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const stepX = deltaX / direction.x;
|
||||
const stepY = deltaY / direction.y;
|
||||
return stepX >= 0 && stepX === stepY;
|
||||
}
|
||||
|
||||
*iterateLoc(param: Readonly<IRayRangeParam>): Iterable<number> {
|
||||
const { width } = this.host;
|
||||
const yielded = new Set<number>();
|
||||
|
||||
if (this.isInBounds(param.cx, param.cy)) {
|
||||
const centerIndex = param.cy * width + param.cx;
|
||||
yielded.add(centerIndex);
|
||||
yield centerIndex;
|
||||
}
|
||||
|
||||
for (const direction of param.dir) {
|
||||
if (direction.x === 0 && direction.y === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let x = param.cx + direction.x;
|
||||
let y = param.cy + direction.y;
|
||||
while (this.isInBounds(x, y)) {
|
||||
const index = y * width + x;
|
||||
if (!yielded.has(index)) {
|
||||
yielded.add(index);
|
||||
yield index;
|
||||
}
|
||||
x += direction.x;
|
||||
y += direction.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inRange(x: number, y: number, param: Readonly<IRayRangeParam>): boolean {
|
||||
if (!this.isInBounds(x, y)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x === param.cx && y === param.cy) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const direction of param.dir) {
|
||||
if (this.isPointOnRay(x, y, direction, param)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class FullRange extends BaseRange<void> {
|
||||
protected estimatePointCount(): number {
|
||||
const { width, height } = this.host;
|
||||
return width * height;
|
||||
}
|
||||
|
||||
iterate(list: Iterable<number>): Iterable<number> {
|
||||
return list;
|
||||
}
|
||||
|
||||
*iterateLoc(): Iterable<number> {
|
||||
const { width, height } = this.host;
|
||||
for (let index = 0; index < width * height; index++) {
|
||||
yield index;
|
||||
}
|
||||
}
|
||||
|
||||
scan(list: Set<number>): Iterable<number> {
|
||||
return list;
|
||||
}
|
||||
|
||||
autoDetect(list: Set<number>): Iterable<number> {
|
||||
return list;
|
||||
}
|
||||
|
||||
inRange(x: number, y: number): boolean {
|
||||
return this.isInBounds(x, y);
|
||||
}
|
||||
|
||||
inRangeIndex(index: number): boolean {
|
||||
return this.isValidIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
export class NoneRange extends BaseRange<void> {
|
||||
protected estimatePointCount(): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
iterate(): Iterable<number> {
|
||||
return new Set();
|
||||
}
|
||||
|
||||
iterateLoc(): Iterable<number> {
|
||||
return new Set();
|
||||
}
|
||||
|
||||
scan(): Iterable<number> {
|
||||
return new Set();
|
||||
}
|
||||
|
||||
autoDetect(): Iterable<number> {
|
||||
return new Set();
|
||||
}
|
||||
|
||||
inRange(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
inRangeIndex(): boolean {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -27,3 +27,128 @@ export interface ISearchable8Dir {
|
||||
/** 获取右下元素 */
|
||||
rightDown(): ISearchable8Dir | null;
|
||||
}
|
||||
|
||||
//#region 范围
|
||||
|
||||
export interface IRangeHost {
|
||||
/** 区域整体宽度 */
|
||||
readonly width: number;
|
||||
/** 区域整体高度 */
|
||||
readonly height: number;
|
||||
}
|
||||
|
||||
export interface IRange<T> {
|
||||
/**
|
||||
* 绑定宿主对象,宽度和高度将会使用此宿主对象的宽高
|
||||
* @param host 宿主对象
|
||||
*/
|
||||
bindHost(host: IRangeHost): void;
|
||||
|
||||
/**
|
||||
* 扫描一个可迭代对象,依次输出列表中在此范围内的对象。
|
||||
* 算法是依次迭代 `list` 的内容并判断其是否在范围内,计算次数为传入的列表长度。
|
||||
* @param list 要扫描的列表,每一项的值为 `y * width + x`
|
||||
* @param param 传递给范围对象的参数
|
||||
*/
|
||||
iterate(list: Iterable<number>, param: Readonly<T>): Iterable<number>;
|
||||
|
||||
/**
|
||||
* 迭代范围内的所有坐标,输出值为 `y * width + x`
|
||||
* @param param 传入范围对象的参数
|
||||
*/
|
||||
iterateLoc(param: Readonly<T>): Iterable<number>;
|
||||
|
||||
/**
|
||||
* 指定一个列表,按照一定的顺序判定这一点是否在列表中。
|
||||
* 算法是按照范围的内置顺序依次变量,然后判断这一点是否在列表中,计算次数为范围内包含的坐标数。
|
||||
* @param list 要扫描的列表,每一项的值为 `y * width + x`
|
||||
* @param param 传递给范围对象的参数
|
||||
*/
|
||||
scan(list: Set<number>, param: Readonly<T>): Iterable<number>;
|
||||
|
||||
/**
|
||||
* 自动决定是使用 {@link iterate} 来迭代还是使用 {@link scan} 来扫描。
|
||||
* @param list 要扫描的列表,每一项的值为 `y * width + x`
|
||||
* @param param 传递给范围对象的参数
|
||||
*/
|
||||
autoDetect(list: Set<number>, param: Readonly<T>): Iterable<number>;
|
||||
|
||||
/**
|
||||
* 判断一个点是否在范围内
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
* @param param 传递给范围对象的参数
|
||||
*/
|
||||
inRange(x: number, y: number, param: Readonly<T>): boolean;
|
||||
|
||||
/**
|
||||
* 判断一个索引是否在范围内
|
||||
* @param index 索引,值表示 y * width + x
|
||||
* @param param 传递给范围对象的参数
|
||||
*/
|
||||
inRangeIndex(index: number, param: Readonly<T>): boolean;
|
||||
}
|
||||
|
||||
export interface IRectRangeParam {
|
||||
/** 左上角横坐标 */
|
||||
x: number;
|
||||
/** 左上角纵坐标 */
|
||||
y: number;
|
||||
/** 范围宽度 */
|
||||
w: number;
|
||||
/** 范围高度 */
|
||||
h: number;
|
||||
}
|
||||
|
||||
export interface IManhattanRangeParam {
|
||||
/** 中心横坐标 */
|
||||
cx: number;
|
||||
/** 中心纵坐标 */
|
||||
cy: number;
|
||||
/** 半径 */
|
||||
radius: number;
|
||||
}
|
||||
|
||||
export interface IRayRangeParam {
|
||||
/** 中心点横坐标 */
|
||||
cx: number;
|
||||
/** 中心点纵坐标 */
|
||||
cy: number;
|
||||
/** 方向列表 */
|
||||
dir: IDirectionDescriptor[];
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 实用接口
|
||||
|
||||
export const enum InternalDirectionGroup {
|
||||
/** 上下左右四方向 */
|
||||
Dir4,
|
||||
/** 上下左右+左上+右上+左下+右下八方向 */
|
||||
Dir8
|
||||
}
|
||||
|
||||
export interface IDirectionDescriptor {
|
||||
/** 横坐标增量 */
|
||||
readonly x: number;
|
||||
/** 纵坐标增量 */
|
||||
readonly y: number;
|
||||
}
|
||||
|
||||
export interface IDirectionMapper {
|
||||
/**
|
||||
* 注册一个方向组别
|
||||
* @param group 方向组别
|
||||
* @param dir 方向锁包含的描述器
|
||||
*/
|
||||
registerGroup(group: number, dir: Iterable<IDirectionDescriptor>): void;
|
||||
|
||||
/**
|
||||
* 根据指定方向组别进行遍历
|
||||
* @param group 方向组别
|
||||
*/
|
||||
map(group: number): Iterable<IDirectionDescriptor>;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
Reference in new issue
Block a user