aboutsummaryrefslogtreecommitdiff
path: root/server/client-lib/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/client-lib/src/lib.rs')
-rw-r--r--server/client-lib/src/lib.rs44
1 files changed, 41 insertions, 3 deletions
diff --git a/server/client-lib/src/lib.rs b/server/client-lib/src/lib.rs
index 649e71b2..d949ac6e 100644
--- a/server/client-lib/src/lib.rs
+++ b/server/client-lib/src/lib.rs
@@ -15,6 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+#![feature(map_many_mut)]
pub mod network;
pub mod spatial_index;
@@ -63,6 +64,22 @@ pub struct Game {
pub score: Score,
}
+impl Default for Game {
+ fn default() -> Self {
+ Self {
+ data: Default::default(),
+ tiles: HashMap::new(),
+ walkable: HashSet::new(),
+ players: HashMap::new(),
+ players_spatial_index: SpatialIndex::default(),
+ end: None,
+ lobby: false,
+ environment_effects: HashSet::new(),
+ score: Score::default(),
+ }
+ }
+}
+
impl Game {
pub fn apply_packet(&mut self, packet: PacketC) {
match packet {
@@ -90,16 +107,17 @@ impl Game {
PacketC::RemovePlayer { id } => {
self.players.remove(&id);
}
- PacketC::Position {
+ PacketC::Movement {
player,
pos,
rot,
- boosting,
+ boost,
+ dir,
} => {
if let Some(p) = self.players.get_mut(&player) {
+ p.movement.input(dir, boost);
p.movement.position = pos;
p.movement.rotation = rot;
- p.movement.boosting = boosting;
}
}
@@ -159,6 +177,26 @@ impl Game {
_ => (),
}
}
+
+ pub fn tick(&mut self, dt: f32) {
+ self.score.time_remaining -= dt as f64;
+ self.score.time_remaining -= self.score.time_remaining.max(0.);
+
+ for (&pid, player) in &mut self.players {
+ player.movement.update(&self.walkable, dt);
+ self.players_spatial_index
+ .update_entry(pid, player.movement.position);
+ }
+
+ self.players_spatial_index.all(|p1, pos1| {
+ self.players_spatial_index.query(pos1, 2., |p2, _pos2| {
+ if let Some([a, b]) = self.players.get_many_mut([&p1, &p2]) {
+ a.movement.collide(&mut b.movement, dt)
+ }
+ })
+ });
+ }
+
pub fn get_item(&mut self, location: ItemLocation) -> &mut Option<Item> {
match location {
ItemLocation::Tile(pos) => &mut self.tiles.get_mut(&pos).unwrap().item,