add main menu
This commit is contained in:
51
www/js/MenuFrontEnd.js
Normal file
51
www/js/MenuFrontEnd.js
Normal file
@@ -0,0 +1,51 @@
|
||||
let menu_start_game_button = document.getElementById("StartGameButton")
|
||||
let menu_start_time = document.getElementById("MenuStartTime")
|
||||
let menu_start_move_left = document.getElementById("MenuStartMoveLeft")
|
||||
let menu_block_time = document.getElementById("MenuBlockTime")
|
||||
let menu_max_increment = document.getElementById("MenuMaxIncrement")
|
||||
let menu_last_block = document.getElementById("MenuLastBlock")
|
||||
|
||||
function clamp_number(value, fallback = 0) {
|
||||
const parsed = Number(value)
|
||||
if (Number.isNaN(parsed) || parsed < 0) return fallback
|
||||
return parsed
|
||||
}
|
||||
|
||||
function ms_to_minutes(ms) {
|
||||
return Math.max(0, Math.round(ms / 60000))
|
||||
}
|
||||
|
||||
function seconds_to_ms(seconds) {
|
||||
return clamp_number(seconds) * 1000
|
||||
}
|
||||
|
||||
function minutes_to_ms(minutes) {
|
||||
return clamp_number(minutes) * 60000
|
||||
}
|
||||
|
||||
function sync_menu_with_config() {
|
||||
menu_start_time.value = ms_to_minutes(config.StartTimeWhite)
|
||||
menu_start_move_left.value = clamp_number(config.StartMoveLeft)
|
||||
menu_block_time.value = ms_to_minutes(config.BlockTime)
|
||||
menu_max_increment.value = Math.round(clamp_number(config.MaxIncrement) / 1000)
|
||||
menu_last_block.value = String(config.last_block)
|
||||
}
|
||||
|
||||
function apply_menu_settings_to_config() {
|
||||
config.StartTimeWhite = minutes_to_ms(menu_start_time.value)
|
||||
config.StartTimeBlack = minutes_to_ms(menu_start_time.value)
|
||||
config.StartMoveLeft = clamp_number(menu_start_move_left.value)
|
||||
config.BlockTime = minutes_to_ms(menu_block_time.value)
|
||||
config.MaxIncrement = seconds_to_ms(menu_max_increment.value)
|
||||
config.last_block = menu_last_block.value === "true"
|
||||
}
|
||||
|
||||
function on_menu_start_game_press() {
|
||||
apply_menu_settings_to_config()
|
||||
hide_scene("Menu")
|
||||
load_Clock_scene()
|
||||
}
|
||||
|
||||
menu_start_game_button.addEventListener("click", () => {
|
||||
on_menu_start_game_press()
|
||||
})
|
||||
Reference in New Issue
Block a user