generated from GarandPLG/rust-flake-template
Add tick thread and Skirmish state update
Introduce `AppEvent::Tick` and a background thread that emits timed events. `App` now handles `Tick` by calling a new `update` method, which advances the board and increments a `turn_counter` in `SkirmishState`. Add required imports, fields, and the `spawn_tick_thread` call in `main`.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
use crate::app::threads::AppEvent;
|
||||
use std::{
|
||||
sync::mpsc::Sender,
|
||||
thread,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
pub fn spawn_tick_thread(tx: Sender<AppEvent>, interval_ms: u64) {
|
||||
thread::spawn(move || {
|
||||
let interval: Duration = Duration::from_millis(interval_ms);
|
||||
loop {
|
||||
if tx.send(AppEvent::Tick).is_err() {
|
||||
break;
|
||||
}
|
||||
|
||||
let elapsed: Duration = Instant::now().elapsed();
|
||||
if interval > elapsed {
|
||||
thread::sleep(interval - elapsed);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user