mirror of
https://github.com/motajs/template.git
synced 2026-11-24 14:54:30 +08:00
refactor: 地图 MapLayer
This commit is contained in:
1 parent
122567b050
commit
b262918082
15 files changed
+281
-267
No files matched your search
@@ -14,11 +14,11 @@ describe('MapLocIndexer', () => {
|
||||
it('agrees between the coordinate and locator forms', () => {
|
||||
const indexer = createIndexer(5);
|
||||
|
||||
expect(indexer.locToIndex(0, 0)).toBe(0);
|
||||
expect(indexer.locToIndex(2, 3)).toBe(17);
|
||||
expect(indexer.locaterToIndex({ x: 2, y: 3 })).toBe(17);
|
||||
expect(indexer.locaterToIndex({ x: 2, y: 3 })).toBe(
|
||||
indexer.locToIndex(2, 3)
|
||||
expect(indexer.index(0, 0)).toBe(0);
|
||||
expect(indexer.index(2, 3)).toBe(17);
|
||||
expect(indexer.locatorToIndex({ x: 2, y: 3 })).toBe(17);
|
||||
expect(indexer.locatorToIndex({ x: 2, y: 3 })).toBe(
|
||||
indexer.index(2, 3)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -27,9 +27,9 @@ describe('MapLocIndexer', () => {
|
||||
const indexer = createIndexer(5);
|
||||
|
||||
for (let index = 0; index < 20; index++) {
|
||||
const locator = indexer.indexToLocator(index);
|
||||
expect(indexer.locToIndex(locator.x, locator.y)).toBe(index);
|
||||
expect(indexer.locaterToIndex(locator)).toBe(index);
|
||||
const locator = indexer.locator(index);
|
||||
expect(indexer.index(locator.x, locator.y)).toBe(index);
|
||||
expect(indexer.locatorToIndex(locator)).toBe(index);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -37,21 +37,21 @@ describe('MapLocIndexer', () => {
|
||||
it('splits a flat index into its column and row', () => {
|
||||
const indexer = createIndexer(4);
|
||||
|
||||
expect(indexer.indexToLocator(0)).toEqual({ x: 0, y: 0 });
|
||||
expect(indexer.indexToLocator(3)).toEqual({ x: 3, y: 0 });
|
||||
expect(indexer.indexToLocator(4)).toEqual({ x: 0, y: 1 });
|
||||
expect(indexer.indexToLocator(9)).toEqual({ x: 1, y: 2 });
|
||||
expect(indexer.locator(0)).toEqual({ x: 0, y: 0 });
|
||||
expect(indexer.locator(3)).toEqual({ x: 3, y: 0 });
|
||||
expect(indexer.locator(4)).toEqual({ x: 0, y: 1 });
|
||||
expect(indexer.locator(9)).toEqual({ x: 1, y: 2 });
|
||||
});
|
||||
|
||||
// 验证 setWidth 改变行步长后,同一坐标映射到新索引
|
||||
it('changes the row stride after setWidth', () => {
|
||||
const indexer = createIndexer(4);
|
||||
|
||||
expect(indexer.locToIndex(1, 2)).toBe(9);
|
||||
expect(indexer.index(1, 2)).toBe(9);
|
||||
|
||||
indexer.setWidth(10);
|
||||
|
||||
expect(indexer.locToIndex(1, 2)).toBe(21);
|
||||
expect(indexer.indexToLocator(21)).toEqual({ x: 1, y: 2 });
|
||||
expect(indexer.index(1, 2)).toBe(21);
|
||||
expect(indexer.locator(21)).toEqual({ x: 1, y: 2 });
|
||||
});
|
||||
});
|
||||
@@ -8,19 +8,19 @@ export interface ILocationHelper {
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
locToIndex(x: number, y: number): number;
|
||||
index(x: number, y: number): number;
|
||||
|
||||
/**
|
||||
* 定位符 -> 索引
|
||||
* @param locator 定位符
|
||||
*/
|
||||
locaterToIndex(locator: ITileLocator): number;
|
||||
locatorToIndex(locator: ITileLocator): number;
|
||||
|
||||
/**
|
||||
* 索引 -> 定位符
|
||||
* @param index 索引
|
||||
*/
|
||||
indexToLocator(index: number): ITileLocator;
|
||||
locator(index: number): ITileLocator;
|
||||
}
|
||||
|
||||
export interface ILocationIndexer extends ILocationHelper {
|
||||
@@ -42,15 +42,15 @@ export class MapLocIndexer implements ILocationIndexer {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
locToIndex(x: number, y: number): number {
|
||||
index(x: number, y: number): number {
|
||||
return y * this.width + x;
|
||||
}
|
||||
|
||||
locaterToIndex(locator: ITileLocator): number {
|
||||
locatorToIndex(locator: ITileLocator): number {
|
||||
return locator.y * this.width + locator.x;
|
||||
}
|
||||
|
||||
indexToLocator(index: number): ITileLocator {
|
||||
locator(index: number): ITileLocator {
|
||||
return {
|
||||
x: index % this.width,
|
||||
y: Math.floor(index / this.width)
|
||||
|
||||
Reference in new issue
Block a user