2022-12-31 10:44:42 +00:00
|
|
|
use std::{rc::Rc, fmt::Display};
|
2022-12-31 13:03:24 +00:00
|
|
|
use rand::seq::SliceRandom;
|
|
|
|
use rand::prelude::*;
|
2022-12-31 08:38:01 +00:00
|
|
|
|
2022-12-31 13:03:24 +00:00
|
|
|
use crate::{time::Time, world::Town, generators::PersonNameGenerator, state::GameState};
|
2022-12-31 08:38:01 +00:00
|
|
|
|
2022-12-31 07:54:18 +00:00
|
|
|
pub enum Profession {
|
|
|
|
Peasant,
|
|
|
|
Adventurer,
|
|
|
|
Blacksmith,
|
|
|
|
}
|
|
|
|
|
2022-12-31 13:03:24 +00:00
|
|
|
pub enum Agenda {
|
|
|
|
Idle,
|
|
|
|
Traveling([usize; 2]),
|
|
|
|
}
|
|
|
|
|
2022-12-31 07:54:18 +00:00
|
|
|
pub struct Person {
|
|
|
|
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 {
|
|
|
|
name: PersonNameGenerator::name(),
|
|
|
|
birth_date: birth_date,
|
|
|
|
birth_location: birth_location,
|
|
|
|
profession: Profession::Peasant,
|
|
|
|
location: [0, 0],
|
2022-12-31 13:03:24 +00:00
|
|
|
agenda: Agenda::Idle,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(&mut self, state: &mut GameState) {
|
|
|
|
match &self.agenda {
|
|
|
|
Agenda::Idle => {
|
|
|
|
// do nothing
|
|
|
|
// pick random destination
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let dest = state.world.structures.keys().choose(&mut rng);
|
|
|
|
self.agenda = Agenda::Traveling(*dest.unwrap());
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
self.agenda = Agenda::Idle;
|
|
|
|
}
|
|
|
|
},
|
2022-12-31 08:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
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 07:54:18 +00:00
|
|
|
}
|