feat: 地图矩形检查

This commit is contained in:
unanmed 2024-08-04 19:57:39 +08:00
parent c1c5d29e89
commit bb656f8169
3 changed files with 47 additions and 18 deletions

View File

@ -598,6 +598,47 @@ export class Layer extends Container {
}); });
} }
/**
*
* @param x
* @param y
*/
isPointOutside(x: number, y: number) {
return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight;
}
/**
*
* @param x
* @param y
* @param width
* @param height
*/
isRectOutside(x: number, y: number, width: number, height: number) {
return (
x >= this.mapWidth ||
y >= this.mapHeight ||
x + width < 0 ||
y + height < 0
);
}
/**
*
* @param x
* @param y
* @param width
* @param height
*/
containsRect(x: number, y: number, width: number, height: number) {
return (
x + width <= this.mapWidth &&
y + height <= this.mapHeight &&
x >= 0 &&
y >= 0
);
}
/** /**
* *
* @param background * @param background
@ -674,21 +715,21 @@ export class Layer extends Container {
8, 8,
`Incomplete render data is put. None will be filled to the lacked data.` `Incomplete render data is put. None will be filled to the lacked data.`
); );
data.push(...Array(data.length % width).fill(0)); data.push(...Array(width - (data.length % width)).fill(0));
} }
const height = Math.round(data.length / width); const height = Math.round(data.length / width);
if (width + x > this.mapWidth || height + y > this.mapHeight) { if (!this.containsRect(x, y, width, height)) {
logger.warn( logger.warn(
9, 9,
`Data transfered is partially (or totally) out of range. Overflowed data will be ignored.` `Data transfered is partially (or totally) out of range. Overflowed data will be ignored.`
); );
if (x >= this.mapWidth || y >= this.mapHeight) return; if (this.isRectOutside(x, y, width, height)) return;
} }
for (let nx = 0; nx < width; nx++) { for (let nx = 0; nx < width; nx++) {
for (let ny = 0; ny < height; ny++) { for (let ny = 0; ny < height; ny++) {
const dx = nx + x; const dx = nx + x;
const dy = ny + y; const dy = ny + y;
if (dx >= this.mapWidth || dy >= this.mapHeight) { if (this.isPointOutside(dx, dy)) {
continue; continue;
} }
const index = nx + ny * width; const index = nx + ny * width;
@ -864,10 +905,6 @@ export class Layer extends Container {
} }
} }
updateFloor(): void {
this.updateDataFromFloor();
}
/** /**
* putRenderData * putRenderData
* @param width * @param width
@ -1147,7 +1184,7 @@ export class Layer extends Container {
* @param fn 0-1 * @param fn 0-1
* *
* *
* *
* @param time * @param time
* @param relative * @param relative
*/ */

View File

@ -1,9 +1,7 @@
import { logger } from '@/core/common/logger'; import { logger } from '@/core/common/logger';
import { EventEmitter } from 'eventemitter3'; import { EventEmitter } from 'eventemitter3';
import { cloneDeep, isNil } from 'lodash-es'; import { cloneDeep, isNil } from 'lodash-es';
import { GameState } from './state';
import { ItemState } from './item'; import { ItemState } from './item';
import { MonoStore } from '@/common/struct';
/** /**
* *
@ -386,12 +384,6 @@ export class Hero<T extends object = IHeroStatusDefault>
this.y = y; this.y = y;
this.floorId = floorId; this.floorId = floorId;
this.state = state; this.state = state;
// const list = gameState.get<MonoStore<Hero<any>>>('hero')!.list;
// if (list.has(id)) {
// logger.warn(11, `Repeated hero: ${id}.`);
// }
// list.set(id, this);
} }
/** /**

View File

@ -100,7 +100,7 @@ export class ItemState<
this.useItemEffectFn?.(state, hero); this.useItemEffectFn?.(state, hero);
if (this.useItemEvent) core.insertAction(this.useItemEvent); if (this.useItemEvent) core.insertAction(this.useItemEvent);
if (!this.noRoute) { if (!this.noRoute) {
state.get<string[]>('route')!.push(`item:${this.id}`); core.status.route.push(`item:${this.id}`);
} }
hero.addItem(this.id, -1); hero.addItem(this.id, -1);