Add missing system for completing chapter 2

This commit is contained in:
Timothy Warren 2020-07-23 20:35:06 -04:00
parent f0a871381c
commit 53406aa9b4

View File

@ -0,0 +1,44 @@
use specs::{Join, ReadStorage, System, Write};
use std::collections::HashMap;
use crate::{
components::{Box, BoxSpot, Position},
resources::{Gameplay, GameplayState},
};
pub struct GameplayStateSystem {}
impl<'a> System<'a> for GameplayStateSystem {
// Data
type SystemData = (
Write<'a, Gameplay>,
ReadStorage<'a, Position>,
ReadStorage<'a, Box>,
ReadStorage<'a, BoxSpot>,
);
fn run(&mut self, data: Self::SystemData) {
let (mut gameplay_state, positions, boxes, box_spots) = data;
// get all boxes indexed by position
let boxes_by_position: HashMap<(u8, u8), &Box> = (&positions, &boxes)
.join()
.map(|t| ((t.0.x, t.0.y), t.1))
.collect();
// loop through all box spots and check if there is a corresponding
// box at that position
for (_box_spot, position) in (&box_spots, &positions).join() {
if boxes_by_position.contains_key(&(position.x, position.y)) {
// continue
} else {
gameplay_state.state = GameplayState::Playing;
return;
}
}
// If we made it this far, then all box spots have boxes on them, and the
// game has been won
gameplay_state.state = GameplayState::Won;
}
}