adventure-sim/src/sim/travel_group.rs

78 lines
1.8 KiB
Rust

use std::{fmt::Display, time::Duration};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::prelude::*;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct TravelGroupId(Uuid);
impl TravelGroupId {
pub fn generate() -> Self {
Self(Uuid::new_v4())
}
}
impl Display for TravelGroupId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "trav-{}", self.0)
}
}
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum WorldPoint {
Coords(WorldCoords),
Site(SiteId),
}
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct TravelGroup {
id: TravelGroupId,
accumulated_movement: Kilometer,
pub creatures: Vec<Creature>,
pub position: WorldCoords,
pub origin: Option<WorldPoint>,
pub destination: Option<WorldPoint>,
}
impl TravelGroup {
pub fn new(
creatures: Vec<Creature>,
position: WorldCoords,
origin: Option<WorldPoint>,
destination: Option<WorldPoint>,
) -> Self {
Self {
id: TravelGroupId::generate(),
accumulated_movement: 0.,
creatures,
position,
origin,
destination,
}
}
pub fn id(&self) -> TravelGroupId {
self.id
}
/// The travel speed of the group in km/h
pub fn speed(&self) -> Kilometer {
// TODO: hard-coded, should depend on the creatures in the group
5.0
}
pub fn advance_time(&mut self, time: Duration) {
self.accumulated_movement += time.as_secs_f32() / 3600. * self.speed();
}
pub fn insert(&mut self, creature: Creature) {
self.creatures.push(creature)
}
pub fn remove(&mut self, id: CreatureId) {
self.creatures.retain(|creature| creature.id() != id);
}
}