From e30340b54834c51d66484249d96bae1cd035e9ec Mon Sep 17 00:00:00 2001 From: He4eT Date: Fri, 6 Jun 2025 22:39:28 +0200 Subject: [PATCH] game: moving enemies --- game.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/game.js b/game.js index b243aff..fdba3e6 100644 --- a/game.js +++ b/game.js @@ -145,7 +145,7 @@ function gameplay() { handleMorse() spawn() - /* moveEnemies() */ + moveEnemies() drawInterface() drawArena() @@ -222,7 +222,7 @@ function spawn() { const getType = (wave) => { if (wave < 2) return 'point' - return enemyTypes[rnd(0, enemyTypes.length)] + return enemyTypes[rnd(0, enemyTypes.length - 1)] } const getDangerZone = (type) => { @@ -249,7 +249,7 @@ function spawn() { return { type, letter: 'e', - positions: [getSpawnPosition()], + positions: [getSpawnPosition(), getSpawnPosition()], dangerZone: getDangerZone(type), } }) @@ -263,6 +263,58 @@ function spawn() { }) } +function moveEnemies() { + 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 + + const length = Math.hypot(dx, dy) || 1 + dx /= length + dy /= length + /**/ + + let newX = current.x + dx * speed + let newY = current.y + dy * speed + + if (newX < arena.bounds.left || newX > arena.bounds.right) { + dx = -dx + newX = current.x + dx * speed + } + + if (newY < arena.bounds.top || newY > arena.bounds.bottom) { + dy = -dy + newY = current.y + dy * speed + } + + enemy.positions = [ + { x: newX, y: newY }, + { x: current.x, y: current.y } + ] + }, + 'zombie': () => { + const speed = 0.5 + + 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) + + enemy.positions = [{ + x: currentPosition.x + (dx / dist) * speed, + y: currentPosition.y + (dy / dist) * speed, + }] + }, + }[enemy.type]())) +} + function checkColisions() { if ( enemies