From bb656f81692786e2621b1b065490e3f136a06f16 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sun, 4 Aug 2024 19:57:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=B0=E5=9B=BE=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/render/preset/layer.ts | 55 +++++++++++++++++++++++++++------ src/game/state/hero.ts | 8 ----- src/game/state/item.ts | 2 +- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/core/render/preset/layer.ts b/src/core/render/preset/layer.ts index b8be31e..69f73c4 100644 --- a/src/core/render/preset/layer.ts +++ b/src/core/render/preset/layer.ts @@ -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 背景图块 @@ -674,21 +715,21 @@ export class Layer extends Container { 8, `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); - if (width + x > this.mapWidth || height + y > this.mapHeight) { + if (!this.containsRect(x, y, width, height)) { logger.warn( 9, `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 ny = 0; ny < height; ny++) { const dx = nx + x; const dy = ny + y; - if (dx >= this.mapWidth || dy >= this.mapHeight) { + if (this.isPointOutside(dx, dy)) { continue; } const index = nx + ny * width; @@ -864,10 +905,6 @@ export class Layer extends Container { } } - updateFloor(): void { - this.updateDataFromFloor(); - } - /** * 设置地图大小,会清空渲染数据,因此后面应当紧跟 putRenderData,以保证渲染正常进行 * @param width 地图宽度 @@ -1147,7 +1184,7 @@ export class Layer extends Container { * @param fn 移动函数,传入一个完成度(范围0-1),返回一个三元素数组,表示横纵格子坐标,可以是小数。 * 第三个元素表示图块纵深,一般图块的纵深就是其纵坐标,当地图上有大怪物时,此举可以辅助渲染, * 否则可能会导致移动过程中与大怪物的层级关系不正确,比如全在大怪物身后。注意不建议频繁改动这个值, - * 因为此举会导致层级的重新排序,降低渲染性能 + * 因为此举会导致层级的重新排序,降低渲染性能。当移动结束时,会对最终位置取整得到移动后的坐标 * @param time 移动总时长 * @param relative 是否是相对模式 */ diff --git a/src/game/state/hero.ts b/src/game/state/hero.ts index 4daf8f4..435d57e 100644 --- a/src/game/state/hero.ts +++ b/src/game/state/hero.ts @@ -1,9 +1,7 @@ import { logger } from '@/core/common/logger'; import { EventEmitter } from 'eventemitter3'; import { cloneDeep, isNil } from 'lodash-es'; -import { GameState } from './state'; import { ItemState } from './item'; -import { MonoStore } from '@/common/struct'; /** * 获取勇士在某一点的属性 @@ -386,12 +384,6 @@ export class Hero this.y = y; this.floorId = floorId; this.state = state; - - // const list = gameState.get>>('hero')!.list; - // if (list.has(id)) { - // logger.warn(11, `Repeated hero: ${id}.`); - // } - // list.set(id, this); } /** diff --git a/src/game/state/item.ts b/src/game/state/item.ts index c826534..f92d7b5 100644 --- a/src/game/state/item.ts +++ b/src/game/state/item.ts @@ -100,7 +100,7 @@ export class ItemState< this.useItemEffectFn?.(state, hero); if (this.useItemEvent) core.insertAction(this.useItemEvent); if (!this.noRoute) { - state.get('route')!.push(`item:${this.id}`); + core.status.route.push(`item:${this.id}`); } hero.addItem(this.id, -1);