58 lines
1.2 KiB
Rust
58 lines
1.2 KiB
Rust
use crate::state::GameState;
|
|
|
|
/**
|
|
* 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<u32> = self.state.people.keys().map(|id| *id).collect();
|
|
|
|
// step each person
|
|
for id in ids {
|
|
let person = self.state.people.get_mut(&id);
|
|
match person {
|
|
Some(p) => {
|
|
let mut p = p.clone();
|
|
p.step(&mut self.state);
|
|
self.state.people.insert(id, p);
|
|
},
|
|
// person is dead
|
|
None => (),
|
|
}
|
|
|
|
}
|
|
// increment time
|
|
self.state.time.time += 1;
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
use crate::world::World;
|
|
|
|
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);
|
|
}
|
|
|
|
} |