diff options
author | metamuffin <metamuffin@disroot.org> | 2024-08-10 22:56:09 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-08-10 22:56:09 +0200 |
commit | 44b444e67684b16eb338ef639b46d53c9eb214a1 (patch) | |
tree | e076948c97bc946262069208f79f5cc06ab1f568 | |
parent | 71499464dbb279bd268dd2dcd5a653c3a7f50a68 (diff) | |
download | hurrycurry-44b444e67684b16eb338ef639b46d53c9eb214a1.tar hurrycurry-44b444e67684b16eb338ef639b46d53c9eb214a1.tar.bz2 hurrycurry-44b444e67684b16eb338ef639b46d53c9eb214a1.tar.zst |
start with client lib crate
-rw-r--r-- | Cargo.lock | 14 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | pixel-client/src/game.rs | 7 | ||||
-rw-r--r-- | server/bot/Cargo.toml | 7 | ||||
-rw-r--r-- | server/bot/src/main.rs | 21 | ||||
-rw-r--r-- | server/client-lib/Cargo.toml | 7 | ||||
-rw-r--r-- | server/client-lib/src/lib.rs | 167 | ||||
-rw-r--r-- | server/client-lib/src/spatial_index.rs | 52 |
8 files changed, 273 insertions, 6 deletions
@@ -277,6 +277,13 @@ dependencies = [ ] [[package]] +name = "bot" +version = "0.1.0" +dependencies = [ + "hurrycurry-client-lib", +] + +[[package]] name = "built" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -802,6 +809,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] +name = "hurrycurry-client-lib" +version = "0.1.0" +dependencies = [ + "hurrycurry-protocol", +] + +[[package]] name = "hurrycurry-protocol" version = "0.1.0" dependencies = [ @@ -1,8 +1,10 @@ [workspace] members = [ - "server/replaytool", "server", "server/protocol", + "server/bot", + "server/client-lib", + "server/replaytool", "pixel-client", "pixel-client/tools", ] diff --git a/pixel-client/src/game.rs b/pixel-client/src/game.rs index 600ea2f1..fb9df21c 100644 --- a/pixel-client/src/game.rs +++ b/pixel-client/src/game.rs @@ -167,7 +167,7 @@ impl Game { if send_movement { self.network .queue_out - .push_back(player.movement.movement_packet(direction, self.my_id)); + .push_back(player.movement.movement_packet(self.my_id)); } player.interact_target_anim.exp_to( @@ -271,6 +271,7 @@ impl Game { item: None, movement: MovementBase { position, + direction: Vec2::ZERO, facing: Vec2::X, rotation: 0., velocity: Vec2::ZERO, @@ -329,10 +330,6 @@ impl Game { } => { self.get_item(item).as_mut().unwrap().progress = progress.map(|s| (s, warn)); } - PacketC::Collide { - player: _, - force: _, - } => (), PacketC::Communicate { .. } => { // TODO } diff --git a/server/bot/Cargo.toml b/server/bot/Cargo.toml new file mode 100644 index 00000000..259095ab --- /dev/null +++ b/server/bot/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "bot" +version = "0.1.0" +edition = "2021" + +[dependencies] +hurrycurry-client-lib = { path = "../client-lib" } diff --git a/server/bot/src/main.rs b/server/bot/src/main.rs new file mode 100644 index 00000000..e1be64ef --- /dev/null +++ b/server/bot/src/main.rs @@ -0,0 +1,21 @@ +/* + 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 <https://www.gnu.org/licenses/>. + +*/ + +fn main() { + +}
\ No newline at end of file diff --git a/server/client-lib/Cargo.toml b/server/client-lib/Cargo.toml new file mode 100644 index 00000000..6a3e7f98 --- /dev/null +++ b/server/client-lib/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hurrycurry-client-lib" +version = "0.1.0" +edition = "2021" + +[dependencies] +hurrycurry-protocol = { path = "../protocol" } diff --git a/server/client-lib/src/lib.rs b/server/client-lib/src/lib.rs new file mode 100644 index 00000000..74f6f10c --- /dev/null +++ b/server/client-lib/src/lib.rs @@ -0,0 +1,167 @@ +/* + 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 <https://www.gnu.org/licenses/>. + +*/ +pub mod spatial_index; + +use hurrycurry_protocol::{ + glam::IVec2, movement::MovementBase, ClientGamedata, ItemIndex, ItemLocation, Message, PacketC, + PlayerID, Score, TileIndex, +}; +use spatial_index::SpatialIndex; +use std::{ + collections::{HashMap, HashSet}, + sync::Arc, + time::Instant, +}; + +#[derive(Debug, PartialEq)] +pub struct Item { + pub kind: ItemIndex, + pub progress: Option<(f32, bool)>, +} + +pub struct Tile { + pub kind: TileIndex, + pub item: Option<Item>, +} + +pub struct Player { + pub name: String, + pub character: i32, + pub interacting: Option<IVec2>, + pub item: Option<Item>, + pub communicate_persist: Option<Message>, + + pub movement: MovementBase, +} + +pub struct Game { + pub data: Arc<ClientGamedata>, + 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, +} + +impl Game { + pub fn apply_packet(&mut self, packet: PacketC) { + match packet { + PacketC::Data { data } => { + self.data = Arc::new(data); + } + PacketC::AddPlayer { + id, + position, + character, + name, + } => { + self.players.insert( + id, + Player { + name, + character, + interacting: None, + item: None, + communicate_persist: None, + movement: MovementBase::new(position), + }, + ); + } + PacketC::RemovePlayer { id } => { + self.players.remove(&id); + } + PacketC::Position { + player, + pos, + rot, + boosting, + } => { + if let Some(p) = self.players.get_mut(&player) { + p.movement.position = pos; + p.movement.rotation = rot; + p.movement.boosting = boosting; + } + } + + PacketC::MoveItem { from, to } => { + *self.get_item(to) = self.get_item(from).take(); + } + PacketC::SetItem { location, item } => { + *self.get_item(location) = item.map(|kind| Item { + kind, + progress: None, + }); + } + PacketC::SetProgress { + item, + progress, + warn, + } => { + self.get_item(item).as_mut().unwrap().progress = progress.map(|s| (s, warn)); + } + PacketC::UpdateMap { + tile, + kind, + neighbors: _, + } => { + if let Some(kind) = kind { + self.tiles.insert(tile, Tile { kind, item: None }); + if self.data.tile_collide[kind.0] { + self.walkable.remove(&tile); + } else { + self.walkable.insert(tile); + } + } else { + self.tiles.remove(&tile); + self.walkable.remove(&tile); + } + } + PacketC::Communicate { + player, + message, + persist, + } => { + if persist { + if let Some(player) = self.players.get_mut(&player) { + player.communicate_persist = message; + } + } + } + PacketC::Score(score) => { + self.score = score; + } + PacketC::SetIngame { state: _, lobby } => { + self.lobby = lobby; + } + PacketC::Environment { effects } => { + self.environment_effects = effects; + } + _ => (), + } + } + pub fn get_item(&mut self, location: ItemLocation) -> &mut Option<Item> { + match location { + ItemLocation::Tile(pos) => &mut self.tiles.get_mut(&pos).unwrap().item, + ItemLocation::Player(pid) => &mut self.players.get_mut(&pid).unwrap().item, + } + } +} 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 <https://www.gnu.org/licenses/>. + +*/ +use hurrycurry_protocol::glam::Vec2; +use std::{collections::HashMap, hash::Hash}; + +// TODO stub implementation. please implement +pub struct SpatialIndex<T> { + entries: HashMap<T, Vec2>, +} + +impl<T: Eq + Hash + Copy> SpatialIndex<T> { + 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<T> Default for SpatialIndex<T> { + fn default() -> Self { + Self { + entries: Default::default(), + } + } +} |