/*
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 .
*/
use super::{Entity, EntityContext};
use anyhow::Result;
use hurrycurry_protocol::{glam::Vec2, Character, PacketS, PlayerClass, PlayerID};
use rand::{random, rng};
use rand_distr::Distribution;
use std::collections::HashMap;
pub struct Pedestrians {
pub players: HashMap,
pub points: Vec,
pub spawn_delay_distr: rand_distr::Normal,
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: Character {
color: random(),
hairstyle: random(),
headwear: 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 })
}
}
}