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(
|
convert(
|
||||||
special: ISpecial<any>,
|
special: ISpecial<IHaloValue>,
|
||||||
enemy: IReadonlyEnemy<IEnemyAttributes>,
|
enemy: IReadonlyEnemy<IEnemyAttributes>,
|
||||||
locator: ITileLocator
|
locator: ITileLocator
|
||||||
): CommonAura {
|
): 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(
|
convert(
|
||||||
special: ISpecial<any>,
|
special: ISpecial<void>,
|
||||||
enemy: IReadonlyEnemy<IEnemyAttributes>,
|
enemy: IReadonlyEnemy<IEnemyAttributes>,
|
||||||
locator: ITileLocator,
|
locator: ITileLocator,
|
||||||
context: IEnemyContext<IEnemyAttributes>
|
context: IEnemyContext<IEnemyAttributes>
|
||||||
): GuardAura {
|
): GuardAura {
|
||||||
return new GuardAura(
|
return new GuardAura(context, enemy, special, locator);
|
||||||
context,
|
|
||||||
enemy,
|
|
||||||
special as ISpecial<void>,
|
|
||||||
locator
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
export * from './aura';
|
export * from './aura';
|
||||||
export * from './damage';
|
export * from './damage';
|
||||||
export * from './mapSpecials';
|
export * from './mapDamage';
|
||||||
export * from './special';
|
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<
|
export class ZoneDamageView extends BaseMapDamageView<
|
||||||
@ -232,7 +218,9 @@ export class BetweenDamageView extends BaseMapDamageView<IManhattanRangeParam> {
|
|||||||
|
|
||||||
const otherX = locator.x + deltaX;
|
const otherX = locator.x + deltaX;
|
||||||
const otherY = locator.y + deltaY;
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ export abstract class BaseRange<T> implements IRange<T> {
|
|||||||
* @param x 横坐标
|
* @param x 横坐标
|
||||||
* @param y 纵坐标
|
* @param y 纵坐标
|
||||||
*/
|
*/
|
||||||
protected isInBounds(x: number, y: number) {
|
inBound(x: number, y: number) {
|
||||||
const { width, height } = this.host;
|
const { width, height } = this.host;
|
||||||
return x >= 0 && y >= 0 && x < width && y < height;
|
return x >= 0 && y >= 0 && x < width && y < height;
|
||||||
}
|
}
|
||||||
@ -29,7 +29,7 @@ export abstract class BaseRange<T> implements IRange<T> {
|
|||||||
* 判断一个坐标索引是否在宿主对象范围内
|
* 判断一个坐标索引是否在宿主对象范围内
|
||||||
* @param index 坐标索引
|
* @param index 坐标索引
|
||||||
*/
|
*/
|
||||||
protected isValidIndex(index: number) {
|
inBoundIndex(index: number) {
|
||||||
const { width, height } = this.host;
|
const { width, height } = this.host;
|
||||||
return index >= 0 && index < width * height;
|
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 ex = Math.max(param.x, param.x + param.w);
|
||||||
const ey = Math.max(param.y, param.y + param.h);
|
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>
|
param: Readonly<IManhattanRangeParam>
|
||||||
): boolean {
|
): boolean {
|
||||||
return (
|
return (
|
||||||
this.isInBounds(x, y) &&
|
this.inBound(x, y) &&
|
||||||
Math.abs(x - param.cx) + Math.abs(y - param.cy) <= param.radius
|
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 { width } = this.host;
|
||||||
const yielded = new Set<number>();
|
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;
|
const centerIndex = param.cy * width + param.cx;
|
||||||
yielded.add(centerIndex);
|
yielded.add(centerIndex);
|
||||||
yield centerIndex;
|
yield centerIndex;
|
||||||
@ -206,7 +206,7 @@ export class RayRange extends BaseRange<IRayRangeParam> {
|
|||||||
|
|
||||||
let x = param.cx + direction.x;
|
let x = param.cx + direction.x;
|
||||||
let y = param.cy + direction.y;
|
let y = param.cy + direction.y;
|
||||||
while (this.isInBounds(x, y)) {
|
while (this.inBound(x, y)) {
|
||||||
const index = y * width + x;
|
const index = y * width + x;
|
||||||
if (!yielded.has(index)) {
|
if (!yielded.has(index)) {
|
||||||
yielded.add(index);
|
yielded.add(index);
|
||||||
@ -219,7 +219,7 @@ export class RayRange extends BaseRange<IRayRangeParam> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inRange(x: number, y: number, param: Readonly<IRayRangeParam>): boolean {
|
inRange(x: number, y: number, param: Readonly<IRayRangeParam>): boolean {
|
||||||
if (!this.isInBounds(x, y)) {
|
if (!this.inBound(x, y)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,11 +263,11 @@ export class FullRange extends BaseRange<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inRange(x: number, y: number): boolean {
|
inRange(x: number, y: number): boolean {
|
||||||
return this.isInBounds(x, y);
|
return this.inBound(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
inRangeIndex(index: number): boolean {
|
inRangeIndex(index: number): boolean {
|
||||||
return this.isValidIndex(index);
|
return this.inBoundIndex(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -87,6 +87,19 @@ export interface IRange<T> {
|
|||||||
* @param param 传递给范围对象的参数
|
* @param param 传递给范围对象的参数
|
||||||
*/
|
*/
|
||||||
inRangeIndex(index: number, param: Readonly<T>): boolean;
|
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 {
|
export interface IRectRangeParam {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user