diff options
Diffstat (limited to 'server/src/entity/pedestrians.rs')
-rw-r--r-- | server/src/entity/pedestrians.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/server/src/entity/pedestrians.rs b/server/src/entity/pedestrians.rs new file mode 100644 index 00000000..9b433ded --- /dev/null +++ b/server/src/entity/pedestrians.rs @@ -0,0 +1,86 @@ +use std::collections::HashMap; + +/* + Hurry Curry! - a game about cooking + Copyright 2025 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 super::{Entity, EntityContext}; +use anyhow::Result; +use hurrycurry_protocol::{glam::Vec2, PacketS, PlayerClass, PlayerID}; +use rand::{random, rng}; +use rand_distr::Distribution; + +pub struct Pedestrians { + pub players: HashMap<PlayerID, usize>, + pub points: Vec<Vec2>, + pub spawn_delay_distr: rand_distr::Normal<f32>, + pub cooldown: f32, +} + +impl Entity for Pedestrians { + fn finished(&self) -> bool { + false + } + fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + self.cooldown -= c.dt; + if self.cooldown <= 0. && self.players.len() < 32 { + let id = PlayerID(random()); + c.packet_in.push_back(PacketS::Join { + name: "Pedestrian".to_string(), + character: 0, + class: PlayerClass::Customer, + id: Some(id), + position: self.points.get(0).copied(), + }); + self.players.insert(id, 0); + self.cooldown += self.spawn_delay_distr.sample(&mut rng()).max(0.1); + } + + let mut remove = Vec::new(); + + for (id, index) in &mut self.players { + if let Some(player) = c.game.players.get(id) { + let diff = self.points[*index] - player.movement.position; + if diff.length() < 0.8 { + *index += 1; + if *index >= self.points.len() { + remove.push(*id) + } + } else { + c.packet_in.push_back(PacketS::Movement { + player: *id, + dir: diff, + boost: false, + pos: None, + }); + } + } + } + + for id in remove { + if self.players.remove(&id).is_some() { + c.packet_in.push_back(PacketS::Leave { player: id }); + } + } + + Ok(()) + } + fn destructor(&mut self, c: EntityContext<'_>) { + for (id, _) in self.players.drain() { + c.packet_in.push_back(PacketS::Leave { player: id }) + } + } +} |