From 44b444e67684b16eb338ef639b46d53c9eb214a1 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 10 Aug 2024 22:56:09 +0200 Subject: start with client lib crate --- server/client-lib/src/spatial_index.rs | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 server/client-lib/src/spatial_index.rs (limited to 'server/client-lib/src/spatial_index.rs') diff --git a/server/client-lib/src/spatial_index.rs b/server/client-lib/src/spatial_index.rs new file mode 100644 index 00000000..d4bd1776 --- /dev/null +++ b/server/client-lib/src/spatial_index.rs @@ -0,0 +1,52 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +use hurrycurry_protocol::glam::Vec2; +use std::{collections::HashMap, hash::Hash}; + +// TODO stub implementation. please implement +pub struct SpatialIndex { + entries: HashMap, +} + +impl SpatialIndex { + pub fn update_entry(&mut self, id: T, position: Vec2) { + self.entries.insert(id, position); + } + pub fn remove_entry(&mut self, id: T) { + self.entries.remove(&id); + } + pub fn all(&self, mut cb: impl FnMut(T, Vec2)) { + for (&e, &pos) in &self.entries { + cb(e, pos) + } + } + pub fn query(&self, position: Vec2, radius: f32, mut cb: impl FnMut(T, Vec2)) { + self.all(|pl, p| { + if p.distance(position) < radius { + cb(pl, p) + } + }) + } +} +impl Default for SpatialIndex { + fn default() -> Self { + Self { + entries: Default::default(), + } + } +} -- cgit v1.2.3-70-g09d2