1
0
Fork 0
roguelike-game/src/systems/map_indexing_system.rs

53 lines
1.7 KiB
Rust
Raw Permalink Normal View History

2021-12-24 10:38:44 -05:00
use ::specs::prelude::*;
2021-10-29 11:11:17 -04:00
2022-01-28 11:48:25 -05:00
use crate::components::{BlocksTile, Pools, Position, TileSize};
2022-01-12 11:12:59 -05:00
use crate::{spatial, Map};
2021-12-10 20:16:48 -05:00
2021-10-29 11:11:17 -04:00
pub struct MapIndexingSystem {}
impl<'a> System<'a> for MapIndexingSystem {
2022-01-28 11:48:25 -05:00
#[allow(clippy::type_complexity)]
2021-10-29 11:11:17 -04:00
type SystemData = (
ReadExpect<'a, Map>,
2021-10-29 11:11:17 -04:00
ReadStorage<'a, Position>,
ReadStorage<'a, BlocksTile>,
2022-01-12 11:12:59 -05:00
ReadStorage<'a, Pools>,
2022-01-28 11:48:25 -05:00
ReadStorage<'a, TileSize>,
2021-10-29 15:15:22 -04:00
Entities<'a>,
2021-10-29 11:11:17 -04:00
);
fn run(&mut self, data: Self::SystemData) {
2022-01-28 11:48:25 -05:00
let (map, position, blockers, pools, sizes, entities) = data;
2021-10-29 11:11:17 -04:00
spatial::clear();
spatial::populate_blocked_from_map(&*map);
2021-10-29 15:15:22 -04:00
for (entity, position) in (&entities, &position).join() {
2022-01-12 11:12:59 -05:00
let mut alive = true;
if let Some(pools) = pools.get(entity) {
if pools.hit_points.current < 1 {
alive = false;
}
}
if alive {
2022-01-28 11:48:25 -05:00
if let Some(size) = sizes.get(entity) {
// Multi-tile
for y in position.y..position.y + size.y {
for x in position.x..position.x + size.x {
if x > 0 && x < map.width - 1 && y > 0 && y < map.height - 1 {
let idx = map.xy_idx(x, y);
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
}
}
}
} else {
// Single tile
let idx = map.xy_idx(position.x, position.y);
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
}
2022-01-12 11:12:59 -05:00
}
2021-10-29 11:11:17 -04:00
}
}
}