examples: make the 2048 game update rate, independent from the frame rate as well

This commit is contained in:
Delyan Angelov 2025-03-09 09:18:52 +02:00
parent aafbda4bc4
commit 03d033fa4b
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -31,6 +31,7 @@ mut:
state GameState = .play
tile_format TileFormat = .normal
moves int
updates u64
is_ai_mode bool
ai_fpm u64 = 8
@ -956,14 +957,23 @@ fn on_event(e &gg.Event, mut app App) {
}
fn frame(mut app App) {
mut do_update := false
if app.gg.timer.elapsed().milliseconds() > 15 {
app.gg.timer.restart()
do_update = true
app.updates++
}
app.gg.begin()
app.update_tickers()
if do_update {
app.update_tickers()
}
app.draw()
app.gg.end()
if app.is_ai_mode && app.state in [.play, .freeplay] && app.gg.frame % app.ai_fpm == 0 {
if do_update && app.is_ai_mode && app.state in [.play, .freeplay]
&& app.updates % app.ai_fpm == 0 {
app.ai_move()
}
if app.gg.frame % 120 == 0 {
if app.updates % 120 == 0 {
// do GC once per 2 seconds
// eprintln('> gc_memory_use: ${gc_memory_use()}')
if gc_is_enabled() {