game: reuse bounce behaviour for fidget

This commit is contained in:
He4eT 2025-06-14 06:44:20 +02:00
commit 2a921c439e

40
game.js
View file

@ -132,41 +132,17 @@ const enemyBlueprints = {
dangerZone: 8, dangerZone: 8,
value: 3, value: 3,
behaviour: (enemy) => { behaviour: (enemy) => {
const minSpeed = 0.4 if (Math.random() < 0.05) {
const speed = Math.max(minSpeed, enemy.speed)
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 const angle = Math.random() * 2 * Math.PI
d = { const current = enemy.positions[0]
x: Math.cos(angle), const randomPrevious = {
y: Math.sin(angle), x: current.x - Math.cos(angle),
y: current.y - Math.sin(angle),
} }
enemy.positions[1] = randomPrevious
} }
let dx = d.x * speed enemyBlueprints.bounce.behaviour(enemy)
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: { bounce: {
@ -176,7 +152,7 @@ const enemyBlueprints = {
dangerZone: 8, dangerZone: 8,
value: 5, value: 5,
behaviour: (enemy) => { behaviour: (enemy) => {
const minSpeed = 0.7 const minSpeed = 0.6
const speed = Math.max(minSpeed, enemy.speed) const speed = Math.max(minSpeed, enemy.speed)
const current = enemy.positions[0] const current = enemy.positions[0]
const previous = enemy.positions[1] const previous = enemy.positions[1]