diff --git a/src/systems/gameplay_state_sytem.rs b/src/systems/gameplay_state_sytem.rs new file mode 100644 index 0000000..90535e3 --- /dev/null +++ b/src/systems/gameplay_state_sytem.rs @@ -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; + } +}