mirror of
https://github.com/He4eT/DotDashPit.git
synced 2026-05-04 17:37:23 +00:00
game: extract getDirection
This commit is contained in:
parent
e30340b548
commit
168aba5b52
1 changed files with 59 additions and 49 deletions
92
game.js
92
game.js
|
|
@ -264,55 +264,54 @@ function spawn() {
|
|||
}
|
||||
|
||||
function moveEnemies() {
|
||||
enemies.forEach((enemy) => ({
|
||||
'point': () => {},
|
||||
'fidget': () => {},
|
||||
'bounce': () => {
|
||||
const speed = 1
|
||||
const current = enemy.positions[0]
|
||||
const previous = enemy.positions[1]
|
||||
enemies.forEach((enemy) =>
|
||||
({
|
||||
point: () => {},
|
||||
fidget: () => {},
|
||||
bounce: () => {
|
||||
const speed = 1
|
||||
const current = enemy.positions[0]
|
||||
const previous = enemy.positions[1]
|
||||
|
||||
/**/
|
||||
let dx = current.x - previous.x
|
||||
let dy = current.y - previous.y
|
||||
let d = getDirection(previous, current)
|
||||
|
||||
const length = Math.hypot(dx, dy) || 1
|
||||
dx /= length
|
||||
dy /= length
|
||||
/**/
|
||||
dx = d.x * speed
|
||||
dy = d.y * speed
|
||||
|
||||
let newX = current.x + dx * speed
|
||||
let newY = current.y + dy * 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 * speed
|
||||
}
|
||||
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 * speed
|
||||
}
|
||||
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 }
|
||||
]
|
||||
},
|
||||
'zombie': () => {
|
||||
const speed = 0.5
|
||||
enemy.positions = [
|
||||
{ x: newX, y: newY },
|
||||
{ x: current.x, y: current.y },
|
||||
]
|
||||
},
|
||||
zombie: () => {
|
||||
const speed = 0.5
|
||||
const current = enemy.positions[0]
|
||||
const target = player.position
|
||||
|
||||
const currentPosition = enemy.positions[0]
|
||||
const dx = player.position.x - currentPosition.x
|
||||
const dy = player.position.y - currentPosition.y
|
||||
const dist = Math.hypot(dx, dy)
|
||||
const d = getDirection(current, target)
|
||||
|
||||
enemy.positions = [{
|
||||
x: currentPosition.x + (dx / dist) * speed,
|
||||
y: currentPosition.y + (dy / dist) * speed,
|
||||
}]
|
||||
},
|
||||
}[enemy.type]()))
|
||||
enemy.positions = [
|
||||
{
|
||||
x: current.x + d.x * speed,
|
||||
y: current.y + d.y * speed,
|
||||
},
|
||||
]
|
||||
},
|
||||
})[enemy.type](),
|
||||
)
|
||||
}
|
||||
|
||||
function checkColisions() {
|
||||
|
|
@ -429,6 +428,17 @@ function rnd(from, to) {
|
|||
return Math.floor(Math.random() * (to - from + 1)) + from
|
||||
}
|
||||
|
||||
function getDirection(from, to) {
|
||||
const dx = to.x - from.x
|
||||
const dy = to.y - from.y
|
||||
const distance = Math.hypot(dx, dy) || 1
|
||||
|
||||
return {
|
||||
x: dx / distance,
|
||||
y: dy / distance,
|
||||
}
|
||||
}
|
||||
|
||||
/* Constants */
|
||||
|
||||
/* Screen */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue