2023-01-04 13:37:25 +00:00
|
|
|
use crate::state::{GameState, Event};
|
2023-01-01 20:11:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the main game struct.
|
|
|
|
* It takes care of stepping the game forward.
|
|
|
|
* Changes are made to the state struct through the step function.
|
|
|
|
*/
|
|
|
|
pub struct Game {
|
|
|
|
pub state: GameState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Game {
|
|
|
|
pub fn new(state: GameState) -> Game {
|
|
|
|
Game {
|
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(&mut self) {
|
|
|
|
// get list of all people ids
|
2023-01-04 13:37:25 +00:00
|
|
|
let ids: Vec<u32> = self.state.creatures.keys().map(|id| *id).collect();
|
2023-01-01 20:11:38 +00:00
|
|
|
|
|
|
|
// step each person
|
|
|
|
for id in ids {
|
2023-01-04 13:37:25 +00:00
|
|
|
let person = self.state.creatures.get(&id);
|
|
|
|
if let Some(p) = person {
|
|
|
|
let mut p = p.clone();
|
|
|
|
let actions = p.step(&self.state.world);
|
|
|
|
for action in actions {
|
|
|
|
action.apply(&mut self.state);
|
|
|
|
if action.notable() {
|
|
|
|
self.state.events.push(Box::new(Event{
|
|
|
|
time: self.state.time,
|
|
|
|
effect: action
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.state.creatures.insert(p.entity.id, p);
|
2023-01-01 20:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// increment time
|
|
|
|
self.state.time.time += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
2023-01-04 13:37:25 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2023-01-04 13:54:57 +00:00
|
|
|
use crate::{world::{World, Terrain, Town}, creature::{Creature, Agenda}, time::Time, entity::Location};
|
2023-01-01 20:11:38 +00:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_step() {
|
|
|
|
let state = GameState::new(World::new(100));
|
|
|
|
let mut game = Game::new(state);
|
|
|
|
game.step();
|
|
|
|
assert_eq!(game.state.time.time, 1);
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:37:25 +00:00
|
|
|
#[test]
|
|
|
|
fn test_step_creature() {
|
|
|
|
let mut state = GameState::new(World::new(2));
|
|
|
|
state.world.map[0][0].terrain = Terrain::Flats;
|
|
|
|
state.world.map[0][1].terrain = Terrain::Flats;
|
|
|
|
state.world.map[1][0].terrain = Terrain::Hills;
|
|
|
|
state.world.map[1][1].terrain = Terrain::Hills;
|
|
|
|
|
|
|
|
let mut creature = Creature::new(
|
|
|
|
Time { time: 0 },
|
|
|
|
Location { x: 0, y: 0 }
|
|
|
|
);
|
|
|
|
creature.set_agenda(Agenda::Traveling(Location { x: 2, y: 2 }));
|
|
|
|
state.add_person(creature);
|
|
|
|
|
|
|
|
let mut game = Game::new(state);
|
|
|
|
game.step();
|
|
|
|
|
|
|
|
assert_eq!(game.state.creatures.len(), 1);
|
|
|
|
assert_eq!(game.state.creatures[&1].entity.loc, Location { x: 1, y: 1 });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-01 20:11:38 +00:00
|
|
|
}
|