From 901a746cd017ea1c5bcd34d211bb06e1f61b29b9 Mon Sep 17 00:00:00 2001 From: He4eT Date: Sat, 7 Jun 2025 00:57:02 +0200 Subject: [PATCH] game: fidget enemy --- game.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/game.js b/game.js index 3606a08..67b196e 100644 --- a/game.js +++ b/game.js @@ -174,7 +174,42 @@ function drawPlayer() { const enemyBehaviors = { point: () => {}, - fidget: (enemy) => {}, + fidget: (enemy) => { + const speed = 0.5 + const current = enemy.positions[0] + const previous = enemy.positions[1] || current + + let d = getDirection(previous, current) + + if (Math.random() < 0.05 || (d.x === 0 && d.y === 0)) { + const angle = Math.random() * 2 * Math.PI + d = { + x: Math.cos(angle), + y: Math.sin(angle), + } + } + + let dx = d.x * speed + let dy = d.y * 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 + } + + 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 }, + ] + }, bounce: (enemy) => { const speed = 1 const current = enemy.positions[0]