tavern_keeper/src/game.rs

83 lines
2.2 KiB
Rust

use crate::{state::{GameState, Event}, entity::EntityId};
/**
* 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
let ids: Vec<EntityId> = self.state.creatures.keys().map(|id| *id).collect();
// step each person
for id in ids {
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 {
self.state.apply_action(action);
}
self.state.creatures.insert(id, p);
self.state.pay_upkeep(id);
}
}
// increment time
self.state.time.time += 1;
}
}
#[cfg(test)]
mod tests {
use crate::{world::{World, Terrain}, creature::{Creature, Agenda}, time::Time, entity::Location};
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);
}
#[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 },
crate::creature::Profession::Adventurer,
);
creature.set_agenda(Agenda::Traveling(Location { x: 2, y: 2 }));
let id = state.add_person(creature);
let mut game = Game::new(state);
game.step();
assert_eq!(game.state.creatures.len(), 1);
assert_eq!(game.state.creatures[&id].loc, Location { x: 1, y: 1 });
}
}