mirror of
https://github.com/He4eT/DotDashPit.git
synced 2026-05-05 01:47:22 +00:00
game: extract enemyBehaviors
This commit is contained in:
parent
168aba5b52
commit
bcc0e4b0b0
1 changed files with 53 additions and 55 deletions
108
game.js
108
game.js
|
|
@ -78,16 +78,12 @@ let player = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {'point' | 'fidget' | 'bounce' | 'zombie'} EnemyType
|
|
||||||
*/
|
|
||||||
/** @type EnemyType[] */
|
|
||||||
const enemyTypes = ['point', 'fidget', 'bounce', 'zombie']
|
const enemyTypes = ['point', 'fidget', 'bounce', 'zombie']
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
* letter: string,
|
* letter: string,
|
||||||
* type: EnemyType,
|
* type: keyof typeof enemyBehaviors,
|
||||||
* dangerZone: number,
|
* dangerZone: number,
|
||||||
* positions: Point[],
|
* positions: Point[],
|
||||||
* }} Enemy
|
* }} Enemy
|
||||||
|
|
@ -221,7 +217,9 @@ function spawn() {
|
||||||
const enemyCount = 1 + Math.floor(arena.wave / 2)
|
const enemyCount = 1 + Math.floor(arena.wave / 2)
|
||||||
|
|
||||||
const getType = (wave) => {
|
const getType = (wave) => {
|
||||||
if (wave < 2) return 'point'
|
if (wave <= 2) return 'point'
|
||||||
|
|
||||||
|
const enemyTypes = Object.keys(enemyBehaviors)
|
||||||
return enemyTypes[rnd(0, enemyTypes.length - 1)]
|
return enemyTypes[rnd(0, enemyTypes.length - 1)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,60 +256,60 @@ function spawn() {
|
||||||
effects.unshift({
|
effects.unshift({
|
||||||
type: 'detection',
|
type: 'detection',
|
||||||
to: enemy.positions[0],
|
to: enemy.positions[0],
|
||||||
frames: Array(5).fill(4),
|
frames: Array(10).fill(4),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const enemyBehaviors = {
|
||||||
|
point: () => {},
|
||||||
|
fidget: (enemy) => {},
|
||||||
|
bounce: (enemy) => {
|
||||||
|
const speed = 1
|
||||||
|
const current = enemy.positions[0]
|
||||||
|
const previous = enemy.positions[1]
|
||||||
|
|
||||||
|
const d = getDirection(previous, current)
|
||||||
|
|
||||||
|
let dx = d.x * speed
|
||||||
|
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 },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
zombie: (enemy) => {
|
||||||
|
const speed = 0.5
|
||||||
|
const current = enemy.positions[0]
|
||||||
|
const target = player.position
|
||||||
|
|
||||||
|
const d = getDirection(current, target)
|
||||||
|
|
||||||
|
enemy.positions = [
|
||||||
|
{
|
||||||
|
x: current.x + d.x * speed,
|
||||||
|
y: current.y + d.y * speed,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
function moveEnemies() {
|
function moveEnemies() {
|
||||||
enemies.forEach((enemy) =>
|
enemies.forEach((enemy) => enemyBehaviors[enemy.type](enemy))
|
||||||
({
|
|
||||||
point: () => {},
|
|
||||||
fidget: () => {},
|
|
||||||
bounce: () => {
|
|
||||||
const speed = 1
|
|
||||||
const current = enemy.positions[0]
|
|
||||||
const previous = enemy.positions[1]
|
|
||||||
|
|
||||||
let d = getDirection(previous, current)
|
|
||||||
|
|
||||||
dx = d.x * speed
|
|
||||||
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 },
|
|
||||||
]
|
|
||||||
},
|
|
||||||
zombie: () => {
|
|
||||||
const speed = 0.5
|
|
||||||
const current = enemy.positions[0]
|
|
||||||
const target = player.position
|
|
||||||
|
|
||||||
const d = getDirection(current, target)
|
|
||||||
|
|
||||||
enemy.positions = [
|
|
||||||
{
|
|
||||||
x: current.x + d.x * speed,
|
|
||||||
y: current.y + d.y * speed,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
})[enemy.type](),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkColisions() {
|
function checkColisions() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue