game: extract config

This commit is contained in:
He4eT 2025-06-16 10:45:14 +02:00
commit 5755cb71cc

91
game.js
View file

@ -10,9 +10,13 @@
/* Config */ /* Config */
const HISTORY_LENGTH = 14 const DOT_DASH_THRESHOLD = 200
const IDLE_TIMEOUT = 500
const HINT_DISTANCE = 30 const HINT_DISTANCE = 30
/* */
const morseCode = [ const morseCode = [
['A', ' .- '], ['A', ' .- '],
['B', ' -... '], ['B', ' -... '],
@ -267,6 +271,18 @@ const effectRenderers = {
}, },
} }
function drawFX() {
effects
.map((effect) => ({
...effect,
from: worldToScreen(effect.from ?? {}),
to: worldToScreen(effect.to ?? {}),
}))
.forEach((effect) => effectRenderers[effect.type](effect))
effects = effects.filter(({ frames }) => frames.length > 0)
}
/* Main Menu */ /* Main Menu */
function startScreen() { function startScreen() {
@ -275,8 +291,9 @@ function startScreen() {
font('DOT DASH PIT', 15, 15, 0, 7, 8, false, 1) font('DOT DASH PIT', 15, 15, 0, 7, 8, false, 1)
print('HIGH SCORE:', 7, 118, 6, false) print('HIGH SCORE:', HISTORY_X, HISTORY_Y, 6, false)
print('0'.padStart(14, ' '), 152, 125, 6, true) // TODO highScore
print('0'.padStart(14, ' '), SCORE_X, SCORE_Y, 6, true)
if (anyKeyPressed()) { if (anyKeyPressed()) {
currentScreen = 'gameScreen' currentScreen = 'gameScreen'
@ -293,9 +310,8 @@ function gameoverScreen() {
drawPlayer() drawPlayer()
if (effects.length === 0) { if (effects.length === 0) {
const title = 'GAME OVER' print('GAME OVER', HISTORY_X, HISTORY_Y, 10, false)
print(title, 7, 118, 10, false) print(player.score.toString().padStart(14, ' '), SCORE_X, SCORE_Y, 6, true)
print(player.score.toString().padStart(14, ' '), 152, 125, 6, true)
if (anyKeyPressed()) { if (anyKeyPressed()) {
reset() reset()
@ -344,8 +360,14 @@ function drawArena() {
function drawInterface() { function drawInterface() {
drawMorse(player.key.buffer, 100, 123, 15, 36) drawMorse(player.key.buffer, 100, 123, 15, 36)
print(player.key.history.padStart(HISTORY_LENGTH, ' '), 7, 118, 6, true) print(
print(player.score.toString().padStart(14, ' '), 152, 125, 6, true) player.key.history.padStart(HISTORY_LENGTH, ' '),
HISTORY_X,
HISTORY_Y,
6,
true,
)
print(player.score.toString().padStart(14, ' '), SCORE_X, SCORE_Y, 6, true)
} }
function drawMorse(codeString, x, y, color, width) { function drawMorse(codeString, x, y, color, width) {
@ -379,14 +401,16 @@ function handleMoves() {
playerStates[player.state].speed / playerStates[player.state].speed /
([dx, dy].every((d) => d !== 0) ? Math.SQRT2 : 1) ([dx, dy].every((d) => d !== 0) ? Math.SQRT2 : 1)
const clamp = (value) => (min, max) => Math.max(min, Math.min(max, value))
player.position = { player.position = {
x: Math.max( x: clamp(player.position.x + dx * norm)(
arena.bounds.left, arena.bounds.left,
Math.min(arena.bounds.right, player.position.x + dx * norm), arena.bounds.right,
), ),
y: Math.max( y: clamp(player.position.y + dy * norm)(
arena.bounds.top, arena.bounds.top,
Math.min(arena.bounds.bottom, player.position.y + dy * norm), arena.bounds.bottom,
), ),
} }
} }
@ -401,9 +425,6 @@ function playMorseKey(seed) {
} }
function handleMorse() { function handleMorse() {
const DOT_DASH_THRESHOLD = 200
const IDLE_TIMEOUT = 500
const { key } = player const { key } = player
const now = time() const now = time()
@ -464,6 +485,15 @@ function drawPlayer() {
/* Enemies */ /* Enemies */
function checkCollisions() {
const collide = (enemy) =>
getDistance(player.position, enemy.positions[0]) < enemy.dangerZone
if (enemies.some(collide)) {
gameover()
}
}
function spawnEnemies() { function spawnEnemies() {
if (enemies.length > 0) { if (enemies.length > 0) {
return return
@ -550,15 +580,6 @@ function destroyEnemiesByLetter(letter) {
enemies = enemies.filter((enemy) => enemy.letter !== letter) enemies = enemies.filter((enemy) => enemy.letter !== letter)
} }
function checkCollisions() {
const collide = (enemy) =>
getDistance(player.position, enemy.positions[0]) < enemy.dangerZone
if (enemies.some(collide)) {
gameover()
}
}
function drawEnemies() { function drawEnemies() {
enemies enemies
.map((enemy) => [ .map((enemy) => [
@ -610,20 +631,6 @@ function drawEnemyLetters() {
}) })
} }
/* Effects */
function drawFX() {
effects
.map((effect) => ({
...effect,
from: worldToScreen(effect.from ?? {}),
to: worldToScreen(effect.to ?? {}),
}))
.forEach((effect) => effectRenderers[effect.type](effect))
effects = effects.filter(({ frames }) => frames.length > 0)
}
/* Utils */ /* Utils */
function arr(n, filler) { function arr(n, filler) {
@ -683,6 +690,14 @@ function anyKeyPressed() {
/* Constants */ /* Constants */
/* Interface */
const HISTORY_LENGTH = 14
const HISTORY_X = 7
const HISTORY_Y = 118
const SCORE_X = 152
const SCORE_Y = 125
/* Screen */ /* Screen */
const SCREEN_W = 240 const SCREEN_W = 240
const SCREEN_H = 136 const SCREEN_H = 136