mirror of
https://github.com/He4eT/DotDashPit.git
synced 2026-05-04 17:37:23 +00:00
game: extract getDistance
This commit is contained in:
parent
dd0e24288c
commit
d96e0861c0
1 changed files with 11 additions and 14 deletions
25
game.js
25
game.js
|
|
@ -156,7 +156,7 @@ function gameover() {
|
|||
/* Gameplay */
|
||||
|
||||
function gameScreen() {
|
||||
checkColisions()
|
||||
checkCollisions()
|
||||
spawnEnemies()
|
||||
|
||||
handleMoves()
|
||||
|
|
@ -395,7 +395,7 @@ function spawnEnemies() {
|
|||
do {
|
||||
x = rnd(arena.bounds.left + b, arena.bounds.right - b)
|
||||
y = rnd(arena.bounds.top + b, arena.bounds.bottom - b)
|
||||
distance = Math.hypot(x - player.position.x, y - player.position.y)
|
||||
distance = getDistance({ x, y }, player.position)
|
||||
} while (distance < minDistance)
|
||||
|
||||
return { x, y }
|
||||
|
|
@ -451,18 +451,11 @@ function destroyEnemiesByLetter(letter) {
|
|||
enemies = enemies.filter((enemy) => enemy.letter !== letter)
|
||||
}
|
||||
|
||||
function checkColisions() {
|
||||
if (
|
||||
enemies
|
||||
.map((enemy) => [
|
||||
enemy,
|
||||
Math.hypot(
|
||||
player.position.x - enemy.positions[0].x,
|
||||
player.position.y - enemy.positions[0].y,
|
||||
),
|
||||
])
|
||||
.some(([enemy, distance]) => distance < enemy.dangerZone)
|
||||
) {
|
||||
function checkCollisions() {
|
||||
const collide = (enemy) =>
|
||||
getDistance(player.position, enemy.positions[0]) < enemy.dangerZone
|
||||
|
||||
if (enemies.some(collide)) {
|
||||
gameover()
|
||||
}
|
||||
}
|
||||
|
|
@ -592,6 +585,10 @@ function rnd(from, to) {
|
|||
return Math.floor(Math.random() * (to - from + 1)) + from
|
||||
}
|
||||
|
||||
function getDistance(from, to) {
|
||||
return Math.hypot(from.x - to.x, from.y - to.y)
|
||||
}
|
||||
|
||||
function getDirection(from, to) {
|
||||
const dx = to.x - from.x
|
||||
const dy = to.y - from.y
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue