refactor: 地图 MapLayer

This commit is contained in:
AncTe committed 2026-09-29 20:21:50 +08:00
1 parent 122567b050
commit b262918082
15 files changed
+281 -267

No files matched your search

@@ -425,7 +425,7 @@ describe('EnemyContext registry and lookups', () => {
expect(fixture.context.width).toBe(5);
expect(fixture.context.height).toBe(2);
expect(fixture.context.indexer.locToIndex(3, 1)).toBe(8);
expect(fixture.context.indexer.index(3, 1)).toBe(8);
expect(fixture.context.getEnemyByLocator({ x: 1, y: 0 })).toBeNull();
});
+12 -12
View File
@@ -222,7 +222,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
getEnemyLocator(enemy: IEnemy<TEnemy>): Readonly<ITileLocator> | null {
const index = this.locatorEnemyMap.get(enemy);
if (index === undefined) return null;
return this.indexer.indexToLocator(index);
return this.indexer.locator(index);
}
getEnemyLocatorByView(
@@ -230,16 +230,16 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
): Readonly<ITileLocator> | null {
const index = this.locatorViewMap.get(view);
if (index === undefined) return null;
return this.indexer.indexToLocator(index);
return this.indexer.locator(index);
}
getEnemyByLocator(locator: ITileLocator): IEnemyView<TEnemy> | null {
const index = this.indexer.locToIndex(locator.x, locator.y);
const index = this.indexer.index(locator.x, locator.y);
return this.enemyViewMap.get(index) ?? null;
}
getEnemyByLoc(x: number, y: number): IEnemyView<TEnemy> | null {
const index = this.indexer.locToIndex(x, y);
const index = this.indexer.index(x, y);
return this.enemyViewMap.get(index) ?? null;
}
@@ -278,7 +278,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
}
setEnemyAt(locator: ITileLocator, enemy: IEnemy<TEnemy>): void {
const index = this.indexer.locToIndex(locator.x, locator.y);
const index = this.indexer.index(locator.x, locator.y);
this.deleteEnemyAt(index);
const view = new EnemyView<TEnemy>(enemy, this);
@@ -299,7 +299,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
}
deleteEnemy(locator: ITileLocator): void {
const index = this.indexer.locToIndex(locator.x, locator.y);
const index = this.indexer.index(locator.x, locator.y);
this.deleteEnemyAt(index);
}
@@ -319,7 +319,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
for (const index of matched) {
const view = viewMap.get(index);
if (view) {
const locator = this.indexer.indexToLocator(index);
const locator = this.indexer.locator(index);
yield [locator, view];
}
}
@@ -334,7 +334,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
*iterateEnemy(): Iterable<[ITileLocator, IEnemyView<TEnemy>]> {
for (const [index, view] of this.enemyViewMap) {
const locator = this.indexer.indexToLocator(index);
const locator = this.indexer.locator(index);
yield [locator, view];
}
}
@@ -514,7 +514,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
const modifier = effect.for(this);
for (const [index, view] of this.enemyViewMap) {
const locator = this.indexer.indexToLocator(index);
const locator = this.indexer.locator(index);
const enemy = view.getComputingEnemy();
const handler = this.createHandler(enemy, locator);
@@ -568,7 +568,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
for (const [index, view] of this.enemyViewMap) {
const enemy = view.getComputingEnemy();
const locator = this.indexer.indexToLocator(index);
const locator = this.indexer.locator(index);
const handler = this.createHandler(enemy, locator);
for (const special of enemy.iterateSpecials()) {
@@ -647,7 +647,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
private buildupQuery(): void {
for (const [index, view] of this.enemyViewMap) {
const enemy = view.getComputingEnemy();
const locator = this.indexer.indexToLocator(index);
const locator = this.indexer.locator(index);
const handler = this.createHandler(enemy, locator);
let queried = false;
const query = () => {
@@ -673,7 +673,7 @@ export class EnemyContext<TEnemy, THero> implements IEnemyContext<
private buildupFinal(): void {
for (const [index, view] of this.enemyViewMap) {
const enemy = view.getComputingEnemy();
const locator = this.indexer.indexToLocator(index);
const locator = this.indexer.locator(index);
const handler = this.createHandler(enemy, locator);
for (const effect of this.finalEffects) {
effect.apply(handler);
@@ -326,9 +326,7 @@ function createFixture(): MapDamageFixture {
markDirty: () => {}
};
enemies.set(view, locator);
const damageView = new FakeView(7, [
indexer.locToIndex(locator.x, locator.y)
]);
const damageView = new FakeView(7, [indexer.index(locator.x, locator.y)]);
const context = {
state,
indexer,
@@ -611,11 +609,11 @@ describe('MapDamage sourced conversion and reduction', () => {
// 验证伤害范围收缩后跌出范围的坐标不再保留旧缓存(CR-02 范围收缩)
it('drops stale damage on indexes that fall out of a shrunken range', () => {
const fixture = createFixture();
const indexA = fixture.context.indexer.locToIndex(
const indexA = fixture.context.indexer.index(
fixture.locator.x,
fixture.locator.y
);
const indexB = fixture.context.indexer.locToIndex(2, 1);
const indexB = fixture.context.indexer.index(2, 1);
const locatorB: ITileLocator = { x: 2, y: 1 };
const view = new FakeView(7, [indexA, indexB]);
fixture.damage.useReducer(fixture.reducer);
@@ -674,7 +672,7 @@ describe('MapDamage multi-source stacking', () => {
// 验证同一点上两条有来源伤害会分别保留并求和合并
it('stacks two sourced damages at the same point', () => {
const fixture = createFixture();
const index = fixture.context.indexer.locToIndex(
const index = fixture.context.indexer.index(
fixture.locator.x,
fixture.locator.y
);
@@ -717,7 +715,7 @@ describe('MapDamage multi-source stacking', () => {
// 验证多来源(有来源与无来源混合)叠加后伤害求和、类型取最大、额外标记取并集
it('merges mixed sourced and sourceless damages into one reduced result', () => {
const fixture = createFixture();
const index = fixture.context.indexer.locToIndex(
const index = fixture.context.indexer.index(
fixture.locator.x,
fixture.locator.y
);
@@ -102,7 +102,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
}
addMapDamage(locator: ITileLocator, info: IMapDamageInfo): void {
const index = this.indexer.locaterToIndex(locator);
const index = this.indexer.locatorToIndex(locator);
const store = this.sourcelessDamage.getOrInsertComputed(index, () => ({
affectedBy: new Set(),
damages: new Set()
@@ -112,7 +112,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
}
deleteMapDamage(locator: ITileLocator, info: IMapDamageInfo): void {
const index = this.indexer.locaterToIndex(locator);
const index = this.indexer.locatorToIndex(locator);
const current = this.sourcelessDamage.get(index);
if (!current) return;
current.damages.delete(info);
@@ -132,7 +132,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
}
markDirty(locator: ITileLocator): void {
this.markDirtyIndex(this.indexer.locaterToIndex(locator));
this.markDirtyIndex(this.indexer.locatorToIndex(locator));
}
markEnemyDirty(view: IEnemyView<TEnemy>): void {
@@ -179,7 +179,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
return null;
}
const index = this.indexer.locaterToIndex(locator);
const index = this.indexer.locatorToIndex(locator);
if (this.dirtyIndexes.has(index)) {
this.refreshIndex(index);
}
@@ -221,7 +221,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
getSeparatedDamage(
locator: ITileLocator
): Iterable<Readonly<IMapDamageInfo>> {
const index = this.indexer.locaterToIndex(locator);
const index = this.indexer.locatorToIndex(locator);
if (this.dirtyIndexes.has(index)) {
this.refreshIndex(index);
}
@@ -319,7 +319,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
const param = viewItem.getRangeParam();
range.bindHost(this.context);
for (const index of range.iterateLoc(param)) {
const loc = this.indexer.indexToLocator(index);
const loc = this.indexer.locator(index);
const point = this.sourcedDamage.getOrInsertComputed(
index,
() => ({
@@ -383,7 +383,7 @@ export class MapDamage<TEnemy, THero> implements IMapDamage<TEnemy, THero> {
this.dirtyIndexes.delete(index);
this.reducedCache.delete(index);
const locator = this.indexer.indexToLocator(index);
const locator = this.indexer.locator(index);
const point = this.sourcedDamage.get(index);
if (!point) return;
+3 -3
View File
@@ -138,8 +138,8 @@ export class PathFinder implements IPathFinder {
target: ITileLocator
): IPathfindingStep[] {
const indexer = this.graph.indexer;
const startIndex = indexer.locaterToIndex(start);
const targetIndex = indexer.locaterToIndex(target);
const startIndex = indexer.locatorToIndex(start);
const targetIndex = indexer.locatorToIndex(target);
if (startIndex === targetIndex) return [];
if (!graph.nodes.has(startIndex) || !graph.nodes.has(targetIndex)) {
return [];
@@ -190,7 +190,7 @@ export class PathFinder implements IPathFinder {
const step = prev.get(curr);
if (!step) return [];
steps.push(step);
curr = indexer.locaterToIndex(step.from);
curr = indexer.locatorToIndex(step.from);
}
steps.reverse();
return steps;
+4 -4
View File
@@ -89,7 +89,7 @@ export class MapGraphBuilder implements IMapGraphBuilder {
const terminals: Set<number> = new Set();
const adjacency: Map<number, IPathGraphEdge[]> = new Map();
const mapped: Set<number> = new Set();
const startIndex = indexer.locaterToIndex(start);
const startIndex = indexer.locatorToIndex(start);
adjacency.set(startIndex, []);
// 以起始位置为中心 BFS,仅沿可通行有向边扩展,不可达区域不入图
@@ -97,7 +97,7 @@ export class MapGraphBuilder implements IMapGraphBuilder {
let head = 0;
while (head < queue.length) {
const { x, y } = queue[head++]!;
const index = indexer.locToIndex(x, y);
const index = indexer.index(x, y);
const edges: IPathGraphEdge[] = [];
for (const [dir, desc] of face.mapMovement()) {
// 如果连接原地,那么应该忽略,避免陷入死循环
@@ -106,7 +106,7 @@ export class MapGraphBuilder implements IMapGraphBuilder {
const ny = y + desc.y;
if (!layer.inMap(nx, ny)) continue;
const nextIndex = indexer.locToIndex(nx, ny);
const nextIndex = indexer.index(nx, ny);
const next: ITileLocator = { x: nx, y: ny };
const handler: IPassCheckHandler = {
currLoc: { x, y },
@@ -132,7 +132,7 @@ export class MapGraphBuilder implements IMapGraphBuilder {
const nodes: Map<number, IPathGraphNode> = new Map();
for (const index of mapped) {
const { x, y } = indexer.indexToLocator(index);
const { x, y } = indexer.locator(index);
const block = layer.getLocationData(x, y)!;
nodes.set(index, {
x,