forked from tutorials/rust-sokoban
45 lines
1.3 KiB
Rust
45 lines
1.3 KiB
Rust
|
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;
|
||
|
}
|
||
|
}
|