实体不会移动,因为它们正在检查它们尝试移动到的图块上的另一个实体,并错误地返回日志,表明由于玩家占用的空间而无法移动,尽管它是一个空图块
如果空间被另一个实体占用,则尝试编写阻止实体移动的代码,当尝试移动到空的空间时,他们都说“被玩家阻止”
perform(engine: Engine, entity: Entity) {
const destX = entity.x + this.dx; //}calculates player destination
const destY = entity.y + this.dy; //}
//----------------------------------player's turn-----------------------
if (!engine.gameMap.isInBounds(destX, destY)) return; //checks destination, if not in bounds: return
if (!engine.gameMap.tiles[destY][destX].walkable) return; //checks destination, if not on walkable tile: return
if (engine.entityAtPosition(destX, destY) !== null && engine.entityAtPosition(destX, destY) !== undefined) {
entity.h = entity.h - engine.entityAtPosition(destX, destY)?.attk
console.log(entity.name + " attacks " + engine.entityAtPosition(destX, destY)?.name + " for " + entity.attk + " damage")
entityTurn(); //enemy turn
} else { //^checks destination, if occupied by an entity: attack
entity.move(this.dx, this.dy); //if not attacking, move
console.log(entity.name + " moves to " + entity.x + ", " + entity.y)
entityTurn(); //enemy turn
}
//----------------------------------enemy's turn------------------------
function entityTurn() {
engine.entities.forEach((e) => { //enemy pathfinding (enemies can move diagonally)
if (e.name !== 'player') { //every entity other than the player
console.log("-----" + e.name + "'s turn-----")
const distanceToPlayer = Math.abs(e.x - engine.player.x) + Math.abs(e.y - engine.player.y);
const dirX = Math.sign(engine.player.x - e.x); //}maths to calculate difference in position from player. math.sign returns -1 if negative, 0 if 0, 1 if positive
const dirY = Math.sign(engine.player.y - e.y); //}
if (distanceToPlayer < 10) {
console.log(e.name + " sees player")
if (!engine.gameMap.isInBounds(destX, destY)) { console.log("not in bounds"); return }; //checks destination, if not in bounds: return
if (!engine.gameMap.tiles[destY][destX].walkable) { console.log("tile not walkable"); return }; //checks destination, if not on walkable tile: return
if (engine.entityAtPosition(destX, destY) !== null && engine.entityAtPosition(destX, destY) !== undefined) { //checks destination, if occupied by an entity: return
console.log("space occupied by " + engine.entityAtPosition(destX, destY)?.name); return
};
e.move(dirX, dirY);
console.log(e.name + " moves to " + e.x + ", " + e.y)
}
}
});
console.log("-----player's turn-----")
}
}
entityAtPosition(x: number, y: number): Entity | null {
return this.entities.find((e) => e.x === x && e.y === y) || null;
}
这不是发牢骚的地方,但我没有足够的声誉来发表评论,抱歉。我希望可以向您询问代码中的更多上下文。
我最好的猜测是,您没有正确计算 destX 和 destY,因为当您在调用 [ 的同时监听 keyDown、keyUp 或事件时,this.dx 和 this.dy 在某些情况下显示为 0执行]功能。当 this.dx 和 this.dy 为 0 时,它可以解释为什么 [entityAtPosition] 函数不断查找并返回你的玩家作为返回值,这也可以解释为什么第 9 行的长 [if] 语句的计算结果为 true,导致 [entityTurn] 中的其余代码正确运行,直到出现您在第 31 行提出的问题。
当然,这都是猜测。如果您可以提供足够的代码让我在浏览器上本地重现问题,我将不胜感激,并希望有人能够用具体事实为您提供更好的答案。谢谢,祝你好运。
如果发生了一些奇怪的事情,我的猜测是正确的,那么解决问题的方法可能涉及将 this.dx 替换为 keydown/keyup/无论事件的预期方向在 x 和 y 方向上的增量,例如'w' 对应于 {x, (y-1)},'a' 对应于 {(x-1), y},'s' 对应于... 我不认为知道您的键绑定设计。