game: safe spawn

This commit is contained in:
He4eT 2025-06-06 20:43:17 +02:00
commit 0c8670a0cf

29
game.js
View file

@ -40,6 +40,7 @@ let currentStage = 'mainMenu'
* screenPosition: Point, * screenPosition: Point,
* bounds: Bounds, * bounds: Bounds,
* spriteHalfSize: number, * spriteHalfSize: number,
* wave: number,
* }} Arena * }} Arena
*/ */
@ -56,6 +57,7 @@ let arena = {
left: 0, left: 0,
}, },
spriteHalfSize: 3, spriteHalfSize: 3,
wave: 0,
} }
/** /**
@ -204,15 +206,27 @@ function destroyEnemiesByLetter(letter) {
/* Enemies */ /* Enemies */
function spawn() { function spawn() {
if (enemies.length === 0) { if (enemies.length > 0) {
return
}
arena.wave += 1
const getSpawnPosition = () => {
const minDistance = 50
let x, y, distance
do {
x = rnd(arena.bounds.left, arena.bounds.right)
y = rnd(arena.bounds.top, arena.bounds.bottom)
distance = Math.hypot(x - player.position.x, y - player.position.y)
} while (distance < minDistance)
return { x, y }
}
enemies = [ enemies = [
{ {
positions: [ positions: [getSpawnPosition()],
{
x: Math.random() * arena.bounds.right,
y: Math.random() * arena.bounds.bottom,
},
],
type: 'zombie', type: 'zombie',
letter: 'e', letter: 'e',
dangerZone: 8, dangerZone: 8,
@ -226,7 +240,6 @@ function spawn() {
frames: Array(5).fill(4), frames: Array(5).fill(4),
}) })
}) })
}
} }
function checkColisions() { function checkColisions() {