game: morse data in player object

This commit is contained in:
He4eT 2025-06-08 09:41:43 +02:00
commit 2f02c37edb

52
game.js
View file

@ -35,12 +35,6 @@ let morseTable = {
'--..': 'Z', '--..': 'Z',
} }
let keyDownAt = 0
let keyUpAt = 0
let isKeyDown = false
let morseBuffer = ''
function TIC() { function TIC() {
gameStages[currentStage]() gameStages[currentStage]()
} }
@ -77,6 +71,12 @@ const arena = {
/** @type {Player} */ /** @type {Player} */
const player = { const player = {
state: 'default', state: 'default',
key: {
buffer: '',
isDown: false,
downAt: 0,
upAt: 0,
},
position: { position: {
x: rnd(arena.bounds.left, arena.bounds.right), x: rnd(arena.bounds.left, arena.bounds.right),
y: rnd(arena.bounds.top, arena.bounds.bottom), y: rnd(arena.bounds.top, arena.bounds.bottom),
@ -233,30 +233,28 @@ function handleMorse() {
const buttonPressed = btn(4) const buttonPressed = btn(4)
// const buttonPressed = [BTN_A, BTN_B, BTN_X, BTN_Y].map(btn).some(Boolean) // const buttonPressed = [BTN_A, BTN_B, BTN_X, BTN_Y].map(btn).some(Boolean)
// player.sprite = buttonPressed ? 65 : 64 const { key } = player
// player.speed = buttonPressed ? 2 : 1
const now = time() const now = time()
const dotDashThreshold = 200 const dotDashThreshold = 200
const idleTimeout = 500 const idleTimeout = 500
// Start // Start
if (buttonPressed && !isKeyDown) { if (buttonPressed && !key.isDown) {
isKeyDown = true key.isDown = true
keyDownAt = now key.downAt = now
} }
if (buttonPressed && isKeyDown) { if (buttonPressed && key.isDown) {
const dash = now - keyDownAt > dotDashThreshold const dash = now - key.downAt > dotDashThreshold
player.state = dash ? 'dash' : 'dot' player.state = dash ? 'dash' : 'dot'
} }
// Release // Release
if (!buttonPressed && isKeyDown) { if (!buttonPressed && key.isDown) {
player.state = 'default' player.state = 'default'
isKeyDown = false key.isDown = false
keyUpAt = now key.upAt = now
morseBuffer += keyUpAt - keyDownAt < dotDashThreshold ? '.' : '-' key.buffer += key.upAt - key.downAt < dotDashThreshold ? '.' : '-'
effects.unshift({ effects.unshift({
type: 'detection', type: 'detection',
@ -266,16 +264,16 @@ function handleMorse() {
} }
// Flush by Timeout // Flush by Timeout
if (!buttonPressed && morseBuffer.length > 0 && now - keyUpAt > idleTimeout) { if (!buttonPressed && key.buffer.length > 0 && now - key.upAt > idleTimeout) {
if (morseTable[morseBuffer]) { if (morseTable[key.buffer]) {
destroyEnemiesByLetter(morseTable[morseBuffer]) destroyEnemiesByLetter(morseTable[key.buffer])
} }
morseBuffer = '' key.buffer = ''
} }
// Debug // Debug
print('Current Code: ' + morseBuffer, 10, 100) print('Current Code: ' + key.buffer, 10, 100)
print('Current Code Length: ' + morseBuffer.length, 10, 106) print('Current Code Length: ' + key.buffer.length, 10, 106)
} }
function drawPlayer() { function drawPlayer() {
@ -641,6 +639,12 @@ const BTN_Y = 7
* *
* @typedef {{ * @typedef {{
* state: keyof typeof playerStates, * state: keyof typeof playerStates,
* key: {
* buffer: string,
* isDown: boolean,
* downAt: number,
* upAt: number,
* },
* position: Point, * position: Point,
* }} Player * }} Player
* *