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,
* bounds: Bounds,
* spriteHalfSize: number,
* wave: number,
* }} Arena
*/
@ -56,6 +57,7 @@ let arena = {
left: 0,
},
spriteHalfSize: 3,
wave: 0,
}
/**
@ -204,15 +206,27 @@ function destroyEnemiesByLetter(letter) {
/* Enemies */
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 = [
{
positions: [
{
x: Math.random() * arena.bounds.right,
y: Math.random() * arena.bounds.bottom,
},
],
positions: [getSpawnPosition()],
type: 'zombie',
letter: 'e',
dangerZone: 8,
@ -227,7 +241,6 @@ function spawn() {
})
})
}
}
function checkColisions() {
if (