game: extract FONT_SIZE

This commit is contained in:
He4eT 2025-06-23 18:33:38 +02:00
commit 4a47806c80

37
game.js
View file

@ -11,6 +11,7 @@
const DOT_DASH_THRESHOLD = 200 const DOT_DASH_THRESHOLD = 200
const DOT_DASH_IDLE_TIMEOUT = 500 const DOT_DASH_IDLE_TIMEOUT = 500
const FONT_SIZE = 1
const HINT_DISTANCE = 30 const HINT_DISTANCE = 30
/* Game Screens */ /* Game Screens */
@ -437,40 +438,50 @@ function drawEnemies() {
} }
function drawEnemyLetters() { function drawEnemyLetters() {
enemies.forEach((enemy) => { const backgroundColor = 2
const bgColor = 2
const borderColor = 5 const borderColor = 5
const letterWidth = 8 * FONT_SIZE
const letterHeight = 7 * FONT_SIZE
const boxWidth = letterWidth + 2 * FONT_SIZE
const boxHeight = letterHeight + 4 * FONT_SIZE
const offset = 9 + 2 * SPRITE_RADIUS * FONT_SIZE
enemies.forEach((enemy) => {
const enemyPosition = enemy.positions[0] const enemyPosition = enemy.positions[0]
const d = getDirection(player.position, enemyPosition) const d = getDirection(player.position, enemyPosition)
const letterPos = { const letterPosition = {
x: enemyPosition.x + d.x * 15, x: enemyPosition.x + d.x * offset,
y: enemyPosition.y + d.y * 15, y: enemyPosition.y + d.y * offset,
} }
const screenPos = worldToScreen(letterPos) const screenPosition = worldToScreen(letterPosition)
const boxX = screenPosition.x - boxWidth / 2 + FONT_SIZE
const boxY = screenPosition.y - boxHeight / 2 + FONT_SIZE
const letterX = screenPosition.x - letterWidth / 2 + FONT_SIZE
const letterY = screenPosition.y - letterHeight / 2 + FONT_SIZE
rect(screenPos.x - 4, screenPos.y - 5, 10, 11, bgColor) rect(boxX, boxY, boxWidth, boxHeight, backgroundColor)
rectb(screenPos.x - 4, screenPos.y - 5, 10, 11, borderColor) rectb(boxX, boxY, boxWidth, boxHeight, borderColor)
font(enemy.letter, screenPos.x - 3, screenPos.y - 3, 0, 8, 8, true) font(enemy.letter, letterX, letterY, 0, 8, 8, true, FONT_SIZE)
if (getDistance(player.position, enemy.positions[0]) < HINT_DISTANCE) { if (getDistance(player.position, enemy.positions[0]) < HINT_DISTANCE) {
const code = letterToMorse[enemy.letter].split('') const code = letterToMorse[enemy.letter].split('')
const hintWidth = MORSE_GAP + morseWidth(code) const hintWidth = MORSE_GAP + morseWidth(code)
const hintPosition = { const hintPosition = {
x: screenPos.x - 4, x: boxX,
y: screenPos.y + 8, y: boxY + boxHeight + 2,
} }
rect( rect(
hintPosition.x, hintPosition.x,
hintPosition.y - 1, hintPosition.y - 1,
Math.max(10, hintWidth), Math.max(boxWidth, hintWidth),
MORSE_GAP + 2, MORSE_GAP + 2,
borderColor, borderColor,
) )
drawMorse(code, hintPosition.x, hintPosition.y, 2) drawMorse(code, hintPosition.x, hintPosition.y, backgroundColor)
} }
}) })
} }