aboutsummaryrefslogtreecommitdiff
path: root/server/client-lib/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-10-19 20:16:38 +0200
committermetamuffin <metamuffin@disroot.org>2025-10-19 20:16:38 +0200
commit239f139e7cdc2ee9f2658a8038d2870293e20aa4 (patch)
tree9b54500dd4bcb50088c60376ad1465e3349f8e70 /server/client-lib/src
parent6979c0a1b6503cb4b4e96e66cba5fc10a2d89b4b (diff)
downloadhurrycurry-239f139e7cdc2ee9f2658a8038d2870293e20aa4.tar
hurrycurry-239f139e7cdc2ee9f2658a8038d2870293e20aa4.tar.bz2
hurrycurry-239f139e7cdc2ee9f2658a8038d2870293e20aa4.tar.zst
Start moving game logic to client-lib. Moved set_tile and gamedata index
Diffstat (limited to 'server/client-lib/src')
-rw-r--r--server/client-lib/src/lib.rs52
-rw-r--r--server/client-lib/src/spatial_index.rs2
2 files changed, 36 insertions, 18 deletions
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<Gamedata>,
pub data_index: GamedataIndex,
+
pub tiles: HashMap<IVec2, Tile>,
- pub walkable: HashSet<IVec2>,
pub players: HashMap<PlayerID, Player>,
- pub players_spatial_index: SpatialIndex<PlayerID>,
pub end: Option<Instant>,
pub lobby: bool,
-
pub environment_effects: HashSet<String>,
pub score: Score,
-
pub player_id_counter: i64,
+
+ pub players_spatial_index: SpatialIndex<PlayerID>,
+ pub walkable: HashSet<IVec2>,
+ pub tile_index: HashMap<TileIndex, HashSet<IVec2>>,
+
+ pub events: VecDeque<PacketC>,
}
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<TileIndex>) {
+ 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<T> {
bins: HashMap<IVec2, Vec<T>>,
}
-// TODO maybe dont use 1x1 bins but something bigger like 10x10 for better perf
-
impl<T: Eq + Hash + Copy> SpatialIndex<T> {
pub fn update_entry(&mut self, id: T, position: Vec2) {
self.remove_entry(id);