From 239f139e7cdc2ee9f2658a8038d2870293e20aa4 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 19 Oct 2025 20:16:38 +0200 Subject: Start moving game logic to client-lib. Moved set_tile and gamedata index --- server/client-lib/src/lib.rs | 52 +++++++++++++++++++++++----------- server/client-lib/src/spatial_index.rs | 2 -- 2 files changed, 36 insertions(+), 18 deletions(-) (limited to 'server/client-lib/src') diff --git a/server/client-lib/src/lib.rs b/server/client-lib/src/lib.rs index f2c47ea0..79c2ed01 100644 --- a/server/client-lib/src/lib.rs +++ b/server/client-lib/src/lib.rs @@ -25,7 +25,7 @@ use hurrycurry_protocol::{ }; use spatial_index::SpatialIndex; use std::{ - collections::{BTreeSet, HashMap, HashSet}, + collections::{BTreeSet, HashMap, HashSet, VecDeque}, sync::Arc, time::Instant, }; @@ -67,17 +67,20 @@ pub struct Player { pub struct Game { pub data: Arc, pub data_index: GamedataIndex, + pub tiles: HashMap, - pub walkable: HashSet, pub players: HashMap, - pub players_spatial_index: SpatialIndex, pub end: Option, pub lobby: bool, - pub environment_effects: HashSet, pub score: Score, - pub player_id_counter: i64, + + pub players_spatial_index: SpatialIndex, + pub walkable: HashSet, + pub tile_index: HashMap>, + + pub events: VecDeque, } impl Game { @@ -164,17 +167,7 @@ impl Game { kind, neighbors: _, } => { - if let Some(kind) = kind { - self.tiles.insert(tile, Tile { kind, item: None }); - if self.data_index.tile_collide[kind.0] { - self.walkable.remove(&tile); - } else { - self.walkable.insert(tile); - } - } else { - self.tiles.remove(&tile); - self.walkable.remove(&tile); - } + self.set_tile(tile, kind); } PacketC::Communicate { player, @@ -200,6 +193,33 @@ impl Game { } } + pub fn set_tile(&mut self, pos: IVec2, kind: Option) { + self.tiles.remove(&pos); + self.walkable.remove(&pos); + if let Some(prev) = self.tiles.get(&pos) { + if let Some(set) = self.tile_index.get_mut(&prev.kind) { + set.remove(&pos); + } + } + if let Some(kind) = kind { + self.tiles.insert(pos, Tile { kind, item: None }); + if self.data_index.tile_collide[kind.0] { + self.walkable.insert(pos); + } + self.tile_index.entry(kind).or_default().insert(pos); + } + self.events.push_back(PacketC::UpdateMap { + tile: pos, + kind, + neighbors: [ + self.tiles.get(&(pos + IVec2::NEG_Y)).map(|e| e.kind), + self.tiles.get(&(pos + IVec2::NEG_X)).map(|e| e.kind), + self.tiles.get(&(pos + IVec2::Y)).map(|e| e.kind), + self.tiles.get(&(pos + IVec2::X)).map(|e| e.kind), + ], + }); + } + pub fn tick(&mut self, dt: f32) { self.score.time_remaining -= dt as f64; self.score.time_remaining -= self.score.time_remaining.max(0.); diff --git a/server/client-lib/src/spatial_index.rs b/server/client-lib/src/spatial_index.rs index 8b717ca3..8dd0cc22 100644 --- a/server/client-lib/src/spatial_index.rs +++ b/server/client-lib/src/spatial_index.rs @@ -23,8 +23,6 @@ pub struct SpatialIndex { bins: HashMap>, } -// TODO maybe dont use 1x1 bins but something bigger like 10x10 for better perf - impl SpatialIndex { pub fn update_entry(&mut self, id: T, position: Vec2) { self.remove_entry(id); -- cgit v1.3