~starkingdoms/starkingdoms

ref: 042d48c0414586735443cef771cb1f227fa16461 starkingdoms/server/src/timer.rs -rw-r--r-- 798 bytes
042d48c0 — ghostlyzsh yay broken, player initialization 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::{time::Duration, sync::Arc};
use log::{error};

use rapier2d::prelude::{PhysicsPipeline};
use tokio::{time::sleep, sync::RwLock};
use crate::{manager::{ClientHandlerMessage, ClientManager, PhysicsData}, SCALE};

pub async fn timer_main(mgr: ClientManager, physics_data: Arc<RwLock<PhysicsData>>) {
    let mut pipeline = PhysicsPipeline::new();
    loop {
        sleep(Duration::from_millis(5)).await;

        physics_data.write().await.tick(&mut pipeline);

        for (_addr, client_thread) in mgr.handlers.read().await.iter() {
            match client_thread.tx.send(ClientHandlerMessage::Tick).await {
                Ok(_) => (),
                Err(e) => {
                    error!("unable to update a client thread: {}", e);
                }
            }
        }
    }
}