2022-12-31 10:44:42 +00:00
|
|
|
use std::{rc::Rc, fmt::Display};
|
2022-12-31 13:03:24 +00:00
|
|
|
use rand::prelude::*;
|
2022-12-31 08:38:01 +00:00
|
|
|
|
2022-12-31 14:22:44 +00:00
|
|
|
use crate::{time::Time, world::Town, generators::PersonNameGenerator, state::GameState, entity::Entity};
|
2022-12-31 08:38:01 +00:00
|
|
|
|
2022-12-31 14:22:44 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2022-12-31 07:54:18 +00:00
|
|
|
pub enum Profession {
|
|
|
|
Peasant,
|
|
|
|
Adventurer,
|
|
|
|
Blacksmith,
|
|
|
|
}
|
|
|
|
|
2022-12-31 14:22:44 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2022-12-31 13:03:24 +00:00
|
|
|
pub enum Agenda {
|
2022-12-31 14:22:44 +00:00
|
|
|
Idle(u32),
|
2022-12-31 13:03:24 +00:00
|
|
|
Traveling([usize; 2]),
|
|
|
|
}
|
|
|
|
|
2022-12-31 14:22:44 +00:00
|
|
|
#[derive(Clone)]
|
2022-12-31 07:54:18 +00:00
|
|
|
pub struct Person {
|
2022-12-31 14:22:44 +00:00
|
|
|
pub id: Option<u32>,
|
2022-12-31 07:54:18 +00:00
|
|
|
pub name: String,
|
2022-12-31 08:38:01 +00:00
|
|
|
pub birth_date: Time,
|
|
|
|
pub birth_location: Rc<Town>,
|
2022-12-31 07:54:18 +00:00
|
|
|
pub profession: Profession,
|
|
|
|
pub location: [usize; 2],
|
2022-12-31 13:03:24 +00:00
|
|
|
pub agenda: Agenda,
|
2022-12-31 08:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Person {
|
|
|
|
pub fn new(birth_date: Time, birth_location: Rc<Town>, location: [usize; 2]) -> Person {
|
|
|
|
Person {
|
2022-12-31 14:22:44 +00:00
|
|
|
id: None,
|
2022-12-31 08:38:01 +00:00
|
|
|
name: PersonNameGenerator::name(),
|
|
|
|
birth_date: birth_date,
|
|
|
|
birth_location: birth_location,
|
|
|
|
profession: Profession::Peasant,
|
2022-12-31 14:22:44 +00:00
|
|
|
location: location,
|
|
|
|
agenda: Agenda::Idle(0),
|
2022-12-31 13:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(&mut self, state: &mut GameState) {
|
|
|
|
match &self.agenda {
|
2022-12-31 14:22:44 +00:00
|
|
|
Agenda::Idle(days) => {
|
2022-12-31 13:03:24 +00:00
|
|
|
// do nothing
|
2022-12-31 14:22:44 +00:00
|
|
|
if *days <= 0 {
|
|
|
|
// pick random destination
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let dest = state.world.structures.keys().choose(&mut rng);
|
|
|
|
self.agenda = Agenda::Traveling(*dest.unwrap());
|
|
|
|
} else {
|
|
|
|
self.agenda = Agenda::Idle(days - 1);
|
|
|
|
}
|
2022-12-31 13:03:24 +00:00
|
|
|
},
|
|
|
|
Agenda::Traveling(destination) => {
|
|
|
|
// TDOO: A* pathfinding with terrain costs
|
|
|
|
// move towards destination
|
|
|
|
if self.location[0] < destination[0] {
|
|
|
|
self.location[0] += 1;
|
|
|
|
} else if self.location[0] > destination[0] {
|
|
|
|
self.location[0] -= 1;
|
|
|
|
}
|
|
|
|
if self.location[1] < destination[1] {
|
|
|
|
self.location[1] += 1;
|
|
|
|
} else if self.location[1] > destination[1] {
|
|
|
|
self.location[1] -= 1;
|
|
|
|
}
|
|
|
|
if self.location == *destination {
|
2022-12-31 14:22:44 +00:00
|
|
|
self.agenda = Agenda::Idle(10);
|
2022-12-31 13:03:24 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-31 08:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-31 15:04:07 +00:00
|
|
|
|
|
|
|
pub fn say_agenda(&self, state: & GameState) -> String {
|
|
|
|
match &self.agenda {
|
|
|
|
Agenda::Idle(days) => format!("I'll stay here for {} days", days),
|
|
|
|
Agenda::Traveling(destination) => {
|
|
|
|
let dest_struct = state.world.get_structure_at(*destination);
|
|
|
|
match dest_struct {
|
|
|
|
Some(dest) => {
|
|
|
|
return format!("I'm traveling to {}", dest);
|
|
|
|
},
|
|
|
|
None => return format!("I'm traveling to an unknown location"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-31 10:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Person {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.name)
|
|
|
|
}
|
2022-12-31 14:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for Person {
|
|
|
|
fn id(&self) -> Option<u32> {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_id(&mut self, id: u32) {
|
|
|
|
self.id = Some(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use crate::world::{World, Structure};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_person_creation() {
|
|
|
|
let person = Person::new(Time { time: 0 }, Rc::new(Town::new()), [0, 0]);
|
|
|
|
assert_ne!(person.name, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_traveling() {
|
|
|
|
let mut person = Person::new(Time { time: 0 }, Rc::new(Town::new()), [0, 0]);
|
|
|
|
person.agenda = Agenda::Traveling([10, 0]);
|
|
|
|
person.step(&mut GameState::new(World::new(32)));
|
|
|
|
assert_eq!(person.location, [1, 0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_start_traveling() {
|
|
|
|
let mut person = Person::new(Time { time: 0 }, Rc::new(Town::new()), [0, 0]);
|
|
|
|
person.agenda = Agenda::Idle(0);
|
|
|
|
let mut state = GameState::new(World::new(32));
|
|
|
|
state.world.add_structure(10, 10, Structure::Town(Rc::new(Town::new())));
|
|
|
|
person.step(&mut state);
|
|
|
|
match &person.agenda {
|
|
|
|
Agenda::Traveling(_) => {},
|
|
|
|
_ => { panic!("Person should be traveling") },
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-12-31 07:54:18 +00:00
|
|
|
}
|