diff options
Diffstat (limited to 'src/game/mod.rs')
-rw-r--r-- | src/game/mod.rs | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/game/mod.rs b/src/game/mod.rs index 27faa27..f59b5d5 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1,34 +1,34 @@ use glam::IVec2; +use map::Map; use protocol::Direction; use serde::Deserialize; use std::{collections::HashMap, net::SocketAddr, ops::ControlFlow}; +pub mod map; pub mod protocol; pub mod server; -#[derive(Deserialize)] +#[derive(Deserialize, Clone)] pub struct Config { bind: SocketAddr, + tickrate: f32, } pub struct Game { pub heads: HashMap<u32, (Direction, IVec2, String)>, - pub map: HashMap<IVec2, u32>, - pub size: IVec2, + pub map: Map, pub dead: Vec<u32>, } impl Game { - pub fn new(players: Vec<String>) -> Self { - let mut map = HashMap::new(); + pub fn new(players: Vec<(u32, String)>) -> Self { + let mut map = Map::new(players.len() * 2, players.len() * 2); let mut heads = HashMap::new(); - let plen = players.len(); - for (p, name) in players.into_iter().enumerate() { + for (p, name) in players { let pos = IVec2::ONE * p as i32 * 2; - map.insert(pos, p as u32); - heads.insert(p as u32, (Direction::Up, pos, name)); + map[pos] = Some(p); + heads.insert(p, (Direction::Up, pos, name)); } Self { - size: IVec2::ONE * plen as i32 * 2, heads, map, dead: Vec::new(), @@ -36,14 +36,14 @@ impl Game { } pub fn tick(&mut self) -> ControlFlow<Option<u32>, ()> { for (_player, (dir, head, _)) in &mut self.heads { - *head = (*head + dir.vector()).rem_euclid(self.size) + *head = (*head + dir.vector()).rem_euclid(self.map.size) } self.dead.clear(); let mut h = HashMap::<IVec2, Vec<u32>>::new(); for (player, (_, head, _)) in &self.heads { h.entry(*head).or_default().push(*player); - if self.map.contains_key(head) { + if self.map[*head].is_some() { self.dead.push(*player); } } @@ -54,11 +54,11 @@ impl Game { } for (player, (_, head, _)) in &mut self.heads { - self.map.insert(*head, *player); + self.map[*head] = Some(*player); } for d in &self.dead { - self.map.retain(|_, p| *d != *p); + self.map.clear_player(*d); self.heads.remove(&d); } |