From 168aba5b520cd64942921c9e71281eada3dc0d35 Mon Sep 17 00:00:00 2001 From: He4eT Date: Fri, 6 Jun 2025 22:58:31 +0200 Subject: [PATCH] game: extract getDirection --- game.js | 92 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/game.js b/game.js index fdba3e6..40691c1 100644 --- a/game.js +++ b/game.js @@ -264,55 +264,54 @@ function spawn() { } function moveEnemies() { - enemies.forEach((enemy) => ({ - 'point': () => {}, - 'fidget': () => {}, - 'bounce': () => { - const speed = 1 - const current = enemy.positions[0] - const previous = enemy.positions[1] + enemies.forEach((enemy) => + ({ + point: () => {}, + fidget: () => {}, + bounce: () => { + const speed = 1 + const current = enemy.positions[0] + const previous = enemy.positions[1] - /**/ - let dx = current.x - previous.x - let dy = current.y - previous.y + let d = getDirection(previous, current) - const length = Math.hypot(dx, dy) || 1 - dx /= length - dy /= length - /**/ + dx = d.x * speed + dy = d.y * speed - let newX = current.x + dx * speed - let newY = current.y + dy * speed + let newX = current.x + dx + let newY = current.y + dy - if (newX < arena.bounds.left || newX > arena.bounds.right) { - dx = -dx - newX = current.x + dx * speed - } + if (newX < arena.bounds.left || newX > arena.bounds.right) { + dx = -dx + newX = current.x + dx + } - if (newY < arena.bounds.top || newY > arena.bounds.bottom) { - dy = -dy - newY = current.y + dy * speed - } + if (newY < arena.bounds.top || newY > arena.bounds.bottom) { + dy = -dy + newY = current.y + dy + } - enemy.positions = [ - { x: newX, y: newY }, - { x: current.x, y: current.y } - ] - }, - 'zombie': () => { - const speed = 0.5 + enemy.positions = [ + { x: newX, y: newY }, + { x: current.x, y: current.y }, + ] + }, + zombie: () => { + const speed = 0.5 + const current = enemy.positions[0] + const target = player.position - const currentPosition = enemy.positions[0] - const dx = player.position.x - currentPosition.x - const dy = player.position.y - currentPosition.y - const dist = Math.hypot(dx, dy) + const d = getDirection(current, target) - enemy.positions = [{ - x: currentPosition.x + (dx / dist) * speed, - y: currentPosition.y + (dy / dist) * speed, - }] - }, - }[enemy.type]())) + enemy.positions = [ + { + x: current.x + d.x * speed, + y: current.y + d.y * speed, + }, + ] + }, + })[enemy.type](), + ) } function checkColisions() { @@ -429,6 +428,17 @@ function rnd(from, to) { return Math.floor(Math.random() * (to - from + 1)) + from } +function getDirection(from, to) { + const dx = to.x - from.x + const dy = to.y - from.y + const distance = Math.hypot(dx, dy) || 1 + + return { + x: dx / distance, + y: dy / distance, + } +} + /* Constants */ /* Screen */