mirror of
https://github.com/He4eT/DotDashPit.git
synced 2026-05-04 17:37:23 +00:00
game: extract enemy params
This commit is contained in:
parent
4e2319bd1c
commit
727ea7a260
1 changed files with 125 additions and 102 deletions
227
game.js
227
game.js
|
|
@ -116,6 +116,119 @@ const playerStates = {
|
||||||
/** @type {Enemy[]} */
|
/** @type {Enemy[]} */
|
||||||
let enemies = []
|
let enemies = []
|
||||||
|
|
||||||
|
const enemyTypes = {
|
||||||
|
point: {
|
||||||
|
sprite: 272,
|
||||||
|
spawnDistance: 20,
|
||||||
|
maxSpeed: 0,
|
||||||
|
dangerZone: 8,
|
||||||
|
value: 1,
|
||||||
|
behaviour: () => {},
|
||||||
|
},
|
||||||
|
fidget: {
|
||||||
|
sprite: 273,
|
||||||
|
spawnDistance: 40,
|
||||||
|
maxSpeed: 0.9,
|
||||||
|
dangerZone: 8,
|
||||||
|
value: 3,
|
||||||
|
behaviour: (enemy) => {
|
||||||
|
const minSpeed = 0.4
|
||||||
|
const speed = Math.max(minSpeed, enemy.speed)
|
||||||
|
const current = enemy.positions[0]
|
||||||
|
const previous = enemy.positions[1] || current
|
||||||
|
|
||||||
|
let d = getDirection(previous, current)
|
||||||
|
|
||||||
|
if (Math.random() < 0.05 || (d.x === 0 && d.y === 0)) {
|
||||||
|
const angle = Math.random() * 2 * Math.PI
|
||||||
|
d = {
|
||||||
|
x: Math.cos(angle),
|
||||||
|
y: Math.sin(angle),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
bounce: {
|
||||||
|
sprite: 274,
|
||||||
|
spawnDistance: 100,
|
||||||
|
maxSpeed: 1.6,
|
||||||
|
dangerZone: 8,
|
||||||
|
value: 5,
|
||||||
|
behaviour: (enemy) => {
|
||||||
|
const minSpeed = 0.7
|
||||||
|
const speed = Math.max(minSpeed, enemy.speed)
|
||||||
|
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: {
|
||||||
|
sprite: 275,
|
||||||
|
spawnDistance: 80,
|
||||||
|
maxSpeed: 1,
|
||||||
|
dangerZone: 6,
|
||||||
|
value: 10,
|
||||||
|
behaviour: (enemy) => {
|
||||||
|
const minSpeed = 0.5
|
||||||
|
const speed = Math.max(minSpeed, enemy.speed)
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
/** @type {Effect[]} */
|
/** @type {Effect[]} */
|
||||||
let effects = []
|
let effects = []
|
||||||
|
|
||||||
|
|
@ -297,88 +410,6 @@ function drawPlayer() {
|
||||||
|
|
||||||
/* Enemies */
|
/* Enemies */
|
||||||
|
|
||||||
const enemyBehaviors = {
|
|
||||||
point: () => {},
|
|
||||||
fidget: (enemy) => {
|
|
||||||
const speed = 0.5
|
|
||||||
const current = enemy.positions[0]
|
|
||||||
const previous = enemy.positions[1] || current
|
|
||||||
|
|
||||||
let d = getDirection(previous, current)
|
|
||||||
|
|
||||||
if (Math.random() < 0.05 || (d.x === 0 && d.y === 0)) {
|
|
||||||
const angle = Math.random() * 2 * Math.PI
|
|
||||||
d = {
|
|
||||||
x: Math.cos(angle),
|
|
||||||
y: Math.sin(angle),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 },
|
|
||||||
]
|
|
||||||
},
|
|
||||||
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 spawnEnemies() {
|
function spawnEnemies() {
|
||||||
if (enemies.length > 0) {
|
if (enemies.length > 0) {
|
||||||
return
|
return
|
||||||
|
|
@ -392,17 +423,12 @@ function spawnEnemies() {
|
||||||
const getType = (wave) => {
|
const getType = (wave) => {
|
||||||
if (wave <= 2) return 'point'
|
if (wave <= 2) return 'point'
|
||||||
|
|
||||||
const enemyTypes = Object.keys(enemyBehaviors)
|
const availableTypes = Object.keys(enemyTypes)
|
||||||
return enemyTypes[rnd(0, enemyTypes.length - 1)]
|
return availableTypes[rnd(0, availableTypes.length - 1)]
|
||||||
}
|
}
|
||||||
|
|
||||||
const getDangerZone = (type) => {
|
const getSpawnPosition = (type) => {
|
||||||
if (type === 'zombie') return 6
|
const minDistance = enemyTypes[type].spawnDistance
|
||||||
return 8
|
|
||||||
}
|
|
||||||
|
|
||||||
const getSpawnPosition = () => {
|
|
||||||
const minDistance = 12 * arena.spriteHalfSize
|
|
||||||
const b = 4 * arena.spriteHalfSize
|
const b = 4 * arena.spriteHalfSize
|
||||||
let x, y, distance
|
let x, y, distance
|
||||||
|
|
||||||
|
|
@ -419,10 +445,11 @@ function spawnEnemies() {
|
||||||
const type = getType(arena.wave)
|
const type = getType(arena.wave)
|
||||||
return {
|
return {
|
||||||
type,
|
type,
|
||||||
value: 100,
|
speed: enemyTypes[type].maxSpeed * Math.random(),
|
||||||
|
value: enemyTypes[type].value,
|
||||||
letter: alphabet[rnd(0, alphabet.length - 1)],
|
letter: alphabet[rnd(0, alphabet.length - 1)],
|
||||||
positions: [getSpawnPosition(), getSpawnPosition()],
|
positions: [getSpawnPosition(type), getSpawnPosition(type)],
|
||||||
dangerZone: getDangerZone(type),
|
dangerZone: enemyTypes[type].dangerZone,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -436,7 +463,7 @@ function spawnEnemies() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveEnemies() {
|
function moveEnemies() {
|
||||||
enemies.forEach((enemy) => enemyBehaviors[enemy.type](enemy))
|
enemies.forEach((enemy) => enemyTypes[enemy.type].behaviour(enemy))
|
||||||
}
|
}
|
||||||
|
|
||||||
function destroyEnemiesByLetter(letter) {
|
function destroyEnemiesByLetter(letter) {
|
||||||
|
|
@ -479,12 +506,7 @@ function checkCollisions() {
|
||||||
function drawEnemies() {
|
function drawEnemies() {
|
||||||
enemies
|
enemies
|
||||||
.map((enemy) => [
|
.map((enemy) => [
|
||||||
{
|
enemyTypes[enemy.type].sprite,
|
||||||
point: 80,
|
|
||||||
fidget: 81,
|
|
||||||
bounce: 82,
|
|
||||||
zombie: 83,
|
|
||||||
}[enemy.type],
|
|
||||||
enemy.positions[0].x,
|
enemy.positions[0].x,
|
||||||
enemy.positions[0].y,
|
enemy.positions[0].y,
|
||||||
])
|
])
|
||||||
|
|
@ -703,7 +725,8 @@ const BTN_Y = 7
|
||||||
* }} Player
|
* }} Player
|
||||||
*
|
*
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
* type: keyof typeof enemyBehaviors,
|
* type: keyof typeof enemyTypes,
|
||||||
|
* speed: number,
|
||||||
* positions: Point[],
|
* positions: Point[],
|
||||||
* letter: string,
|
* letter: string,
|
||||||
* dangerZone: number,
|
* dangerZone: number,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue