mirror of
https://github.com/motajs/template.git
synced 2026-05-02 12:23:13 +08:00
refactor: Range 接口重构
This commit is contained in:
parent
a24911400f
commit
4c0960fff7
@ -32,11 +32,11 @@ export class CommonAuraConverter implements IAuraConverter<IEnemyAttributes> {
|
||||
}
|
||||
|
||||
convert(
|
||||
special: ISpecial<any>,
|
||||
special: ISpecial<IHaloValue>,
|
||||
enemy: IReadonlyEnemy<IEnemyAttributes>,
|
||||
locator: ITileLocator
|
||||
): CommonAura {
|
||||
return new CommonAura(enemy, special as ISpecial<IHaloValue>, locator);
|
||||
return new CommonAura(enemy, special, locator);
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,17 +134,12 @@ export class GuardAuraConverter implements IAuraConverter<IEnemyAttributes> {
|
||||
}
|
||||
|
||||
convert(
|
||||
special: ISpecial<any>,
|
||||
special: ISpecial<void>,
|
||||
enemy: IReadonlyEnemy<IEnemyAttributes>,
|
||||
locator: ITileLocator,
|
||||
context: IEnemyContext<IEnemyAttributes>
|
||||
): GuardAura {
|
||||
return new GuardAura(
|
||||
context,
|
||||
enemy,
|
||||
special as ISpecial<void>,
|
||||
locator
|
||||
);
|
||||
return new GuardAura(context, enemy, special, locator);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export * from './aura';
|
||||
export * from './damage';
|
||||
export * from './mapSpecials';
|
||||
export * from './mapDamage';
|
||||
export * from './special';
|
||||
|
||||
@ -74,20 +74,6 @@ abstract class BaseMapDamageView<T> implements IMapDamageView<T> {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个点是否在上下文范围内
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
protected isInBounds(x: number, y: number): boolean {
|
||||
return (
|
||||
x >= 0 &&
|
||||
y >= 0 &&
|
||||
x < this.context.width &&
|
||||
y < this.context.height
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export class ZoneDamageView extends BaseMapDamageView<
|
||||
@ -232,7 +218,9 @@ export class BetweenDamageView extends BaseMapDamageView<IManhattanRangeParam> {
|
||||
|
||||
const otherX = locator.x + deltaX;
|
||||
const otherY = locator.y + deltaY;
|
||||
if (!this.isInBounds(otherX, otherY)) {
|
||||
const range = this.getRange();
|
||||
range.bindHost(this.context);
|
||||
if (!range.inBound(otherX, otherY)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ export abstract class BaseRange<T> implements IRange<T> {
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
protected isInBounds(x: number, y: number) {
|
||||
inBound(x: number, y: number) {
|
||||
const { width, height } = this.host;
|
||||
return x >= 0 && y >= 0 && x < width && y < height;
|
||||
}
|
||||
@ -29,7 +29,7 @@ export abstract class BaseRange<T> implements IRange<T> {
|
||||
* 判断一个坐标索引是否在宿主对象范围内
|
||||
* @param index 坐标索引
|
||||
*/
|
||||
protected isValidIndex(index: number) {
|
||||
inBoundIndex(index: number) {
|
||||
const { width, height } = this.host;
|
||||
return index >= 0 && index < width * height;
|
||||
}
|
||||
@ -96,7 +96,7 @@ export class RectRange extends BaseRange<IRectRangeParam> {
|
||||
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;
|
||||
return this.inBound(x, y) && x >= sx && y >= sy && x < ex && y < ey;
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ export class ManhattanRange extends BaseRange<IManhattanRangeParam> {
|
||||
param: Readonly<IManhattanRangeParam>
|
||||
): boolean {
|
||||
return (
|
||||
this.isInBounds(x, y) &&
|
||||
this.inBound(x, y) &&
|
||||
Math.abs(x - param.cx) + Math.abs(y - param.cy) <= param.radius
|
||||
);
|
||||
}
|
||||
@ -193,7 +193,7 @@ export class RayRange extends BaseRange<IRayRangeParam> {
|
||||
const { width } = this.host;
|
||||
const yielded = new Set<number>();
|
||||
|
||||
if (this.isInBounds(param.cx, param.cy)) {
|
||||
if (this.inBound(param.cx, param.cy)) {
|
||||
const centerIndex = param.cy * width + param.cx;
|
||||
yielded.add(centerIndex);
|
||||
yield centerIndex;
|
||||
@ -206,7 +206,7 @@ export class RayRange extends BaseRange<IRayRangeParam> {
|
||||
|
||||
let x = param.cx + direction.x;
|
||||
let y = param.cy + direction.y;
|
||||
while (this.isInBounds(x, y)) {
|
||||
while (this.inBound(x, y)) {
|
||||
const index = y * width + x;
|
||||
if (!yielded.has(index)) {
|
||||
yielded.add(index);
|
||||
@ -219,7 +219,7 @@ export class RayRange extends BaseRange<IRayRangeParam> {
|
||||
}
|
||||
|
||||
inRange(x: number, y: number, param: Readonly<IRayRangeParam>): boolean {
|
||||
if (!this.isInBounds(x, y)) {
|
||||
if (!this.inBound(x, y)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -263,11 +263,11 @@ export class FullRange extends BaseRange<void> {
|
||||
}
|
||||
|
||||
inRange(x: number, y: number): boolean {
|
||||
return this.isInBounds(x, y);
|
||||
return this.inBound(x, y);
|
||||
}
|
||||
|
||||
inRangeIndex(index: number): boolean {
|
||||
return this.isValidIndex(index);
|
||||
return this.inBoundIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -87,6 +87,19 @@ export interface IRange<T> {
|
||||
* @param param 传递给范围对象的参数
|
||||
*/
|
||||
inRangeIndex(index: number, param: Readonly<T>): boolean;
|
||||
|
||||
/**
|
||||
* 判断一个点是否在宿主对象矩形范围内
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
inBound(x: number, y: number): boolean;
|
||||
|
||||
/**
|
||||
* 判断一个点索引是否在宿主对象矩形范围内
|
||||
* @param index 索引
|
||||
*/
|
||||
inBoundIndex(index: number): boolean;
|
||||
}
|
||||
|
||||
export interface IRectRangeParam {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user