map_indexing_system.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. use crate::{BlocksTile, Map, Position};
  2. use specs::prelude::*;
  3. pub struct MapIndexingSystem;
  4. impl<'a> System<'a> for MapIndexingSystem {
  5. type SystemData = (
  6. WriteExpect<'a, Map>,
  7. ReadStorage<'a, Position>,
  8. ReadStorage<'a, BlocksTile>,
  9. Entities<'a>,
  10. );
  11. fn run(&mut self, data: Self::SystemData) {
  12. let (mut map, position, blockers, entities) = data;
  13. map.populate_blocked();
  14. map.clear_content_index();
  15. for (entity, position) in (&entities, &position).join() {
  16. let idx = map.xy_idx(position.x, position.y);
  17. // If they block, update the blocking list
  18. let _p: Option<&BlocksTile> = blockers.get(entity);
  19. if let Some(_p) = _p {
  20. map.blocked[idx] = true;
  21. }
  22. // Push the entity to the appropriate index slot. It's a Copy
  23. // type, so we don't need to clone it (we want to avoid moving it out of the ECS!)
  24. map.tile_content[idx].push(entity);
  25. }
  26. }
  27. }