IT WORKS
This commit is contained in:
parent
36b5063637
commit
f7255b3617
2 changed files with 20 additions and 6 deletions
10
src/game.rs
10
src/game.rs
|
@ -1,11 +1,8 @@
|
|||
use crate::network::NetworkMessage;
|
||||
use async_std::sync::{Receiver, Sender};
|
||||
use shipyard::World;
|
||||
use shipyard::{error, World};
|
||||
use std::collections::HashMap;
|
||||
|
||||
// Minimum delay between updates, in milliseconds
|
||||
const MIN_UPDATE_MS: u64 = 10;
|
||||
|
||||
pub struct Player {}
|
||||
|
||||
pub struct Game {
|
||||
|
@ -29,10 +26,13 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn update(&self) -> Result<(), error::RunWorkload> {
|
||||
self.world.try_run_workload("update")
|
||||
}
|
||||
|
||||
pub async fn read_loop(&self, net_in: Receiver<NetworkMessage>) {
|
||||
loop {
|
||||
let message = net_in.recv().await.expect("could not read from channel");
|
||||
log::info!("Message received: {:?}", &message);
|
||||
self.net_out
|
||||
.send(NetworkMessage {
|
||||
conn_id: message.conn_id,
|
||||
|
|
16
src/main.rs
16
src/main.rs
|
@ -10,6 +10,10 @@ use crate::network::listen;
|
|||
use async_std::sync::Arc;
|
||||
use async_std::{sync::channel, task};
|
||||
use env_logger::Env;
|
||||
use std::time::Duration;
|
||||
|
||||
// Minimum delay between updates, in milliseconds
|
||||
const MIN_UPDATE_MS: u64 = 10;
|
||||
|
||||
async fn run() {
|
||||
let env = Env::default().filter_or("LOG_LEVEL", "info");
|
||||
|
@ -21,8 +25,18 @@ async fn run() {
|
|||
let (out_s, out_r) = channel(10);
|
||||
|
||||
let game = Arc::new(Game::new(out_s));
|
||||
|
||||
let game_read_loop = game.clone();
|
||||
task::spawn(async move {
|
||||
game.clone().read_loop(in_r).await;
|
||||
game_read_loop.read_loop(in_r).await;
|
||||
});
|
||||
|
||||
task::spawn(async move {
|
||||
loop {
|
||||
let game = game.clone();
|
||||
task::spawn(async move { game.update().expect("update failed") });
|
||||
task::sleep(Duration::from_millis(MIN_UPDATE_MS)).await;
|
||||
}
|
||||
});
|
||||
|
||||
task::block_on(listen(settings.bind, in_s, out_r));
|
||||
|
|
Loading…
Reference in a new issue