refactor: Range

This commit is contained in:
AncTe committed 2024-09-30 18:07:20 +08:00
1 parent 39f38cd703
commit 42f611b357
13 files changed
+117 -263

No files matched your search

-126
View File
@@ -1,126 +0,0 @@
import { has } from './utils';
type RangeScanFn<C extends Partial<Loc>> = (
collection: Range<C>,
data: any
) => C[];
type InRangeFn<C extends Partial<Loc>> = (
collection: Range<C>,
data: any,
item: Partial<Loc>
) => boolean;
interface RangeType<C extends Partial<Loc>> {
scan: RangeScanFn<C>;
inRange: InRangeFn<C>;
}
export interface RangeCollection<I extends Partial<Loc>> {
list: I[];
range: Range<I>;
}
export class Range<C extends Partial<Loc>> {
collection: RangeCollection<C>;
cache: Record<string, any> = {};
static rangeType: Record<string, RangeType<Partial<Loc>>> = {};
constructor(collection: RangeCollection<C>) {
this.collection = collection;
}
/**
* 扫描 collection 中在范围内的物品
* @param type 范围类型
* @param data 范围数据
* @returns 在范围内的物品列表
*/
scan(type: string, data: any): C[] {
const t = Range.rangeType[type];
if (!t) {
throw new Error(`Unknown range type: ${type}.`);
}
return t.scan(this, data) as C[];
}
inRange(type: string, data: any, item: Partial<Loc>) {
const t = Range.rangeType[type];
if (!t) {
throw new Error(`Unknown range type: ${type}.`);
}
return t.inRange(this, data, item);
}
clearCache() {
this.cache = {};
}
static registerRangeType(
type: string,
scan: RangeScanFn<Partial<Loc>>,
inRange: InRangeFn<Partial<Loc>>
) {
Range.rangeType[type] = {
scan,
inRange
};
}
}
// ----- 默认的范围类型
// 方形区域
Range.registerRangeType(
'square',
(col, { x, y, d }) => {
const list = col.collection.list;
const r = Math.floor(d / 2);
return list.filter(v => {
return (
has(v.x) &&
has(v.y) &&
Math.abs(v.x - x) <= r &&
Math.abs(v.y - y) <= r
);
});
},
(col, { x, y, d }, item) => {
const r = Math.floor(d / 2);
return (
has(item.x) &&
has(item.y) &&
Math.abs(item.x - x) <= r &&
Math.abs(item.y - y) <= r
);
}
);
Range.registerRangeType(
'rect',
(col, { x, y, w, h }) => {
const list = col.collection.list;
const ex = x + w;
const ey = y + h;
return list.filter(v => {
return (
has(v.x) &&
has(v.y) &&
v.x >= x &&
v.y >= y &&
v.x < ex &&
v.y < ey
);
});
},
(col, { x, y, w, h }, item) => {
const ex = x + w;
const ey = y + h;
const v = item;
return (
has(v.x) && has(v.y) && v.x >= x && v.y >= y && v.x < ex && v.y < ey
);
}
);
-2
View File
@@ -1,6 +1,4 @@
import { canStudySkill, studySkill } from '@/game/mechanism/study';
import { upgradeSkill } from './skillTree';
import { ensureFloorDamage } from '@/game/enemy/damage';
const replayableSettings = ['autoSkill'];
+8 -4
View File
@@ -524,7 +524,7 @@ export class MinimapDrawer {
ctx.fillRect(x - 6, y - 2, 12, 4);
ctx.fillStyle = 'white';
const enemy = core.status.maps[floorId].enemy.list;
if (enemy.length === 0) {
if (enemy.size === 0) {
ctx.strokeStyle = 'lightgreen';
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
@@ -533,8 +533,12 @@ export class MinimapDrawer {
ctx.lineTo(x - 0.5, y + 1);
ctx.lineTo(x + 1.5, y - 1);
ctx.stroke();
} else if (enemy.length <= 2) {
const ids = [...new Set(enemy.map(v => v.id))];
} else if (enemy.size <= 2) {
const idSet = new Set<EnemyIds>();
enemy.forEach(v => {
idSet.add(v.id);
});
const ids: EnemyIds[] = [...idSet];
if (ids.length === 1) {
core.drawIcon(ctx, ids[0], x - 2, y - 2, 4, 4);
} else if (ids.length === 2) {
@@ -552,7 +556,7 @@ export class MinimapDrawer {
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(`+${enemy.length}`, x, y);
ctx.fillText(`+${enemy.size}`, x, y);
}
ctx.restore();