I'm working on a real time multiplayer browser-based game.
The game is top down on variable size tile based maps. There is no central map where all players come together, the entire game plays out in multiple maps. (Player chooses one on login, but has the ability to switch whenever they want). Let's take the following loop that runs on the server side of things:
let tick_time = Duration::from_millis(500); // e.g. Tick 2 times per second.
let mut next_tick = Instant::now();
loop {
let now = Instant::now();
if now >= next_tick {
// Parse user input
// Update state
// Update to state to users in map
next_tick += tick_time;
} else {
let wait_time = next_tick - now;
sleep(wait_time - Duration::from_millis(1)).await;
}
}
In my 'map based' gameplay scenario, would it make sense to spawn a thread for every map where one of these loops runs? Or would it be recommended to have only a singular thread with a singular game loop that updates every map? Or is there perhaps a better, already existing, pattern for this?