roguelike-game/src/effects/targeting.rs

59 lines
1.7 KiB
Rust
Raw Normal View History

use ::specs::prelude::*;
2022-01-20 19:43:51 -05:00
use crate::components::{Equipped, InBackpack, Position};
use crate::map::Map;
pub fn entity_position(ecs: &World, target: Entity) -> Option<i32> {
if let Some(pos) = ecs.read_storage::<Position>().get(target) {
let map = ecs.fetch::<Map>();
return Some(map.xy_idx(pos.x, pos.y) as i32);
}
None
}
pub fn aoe_tiles(map: &Map, target: ::rltk::Point, radius: i32) -> Vec<i32> {
let mut blast_tiles = ::rltk::field_of_view(target, radius, &*map);
blast_tiles.retain(|p| p.x > 0 && p.x < map.width - 1 && p.y > 0 && p.y < map.height - 1);
let mut result = Vec::new();
for t in blast_tiles.iter() {
result.push(map.xy_idx(t.x, t.y) as i32);
}
result
}
2022-01-20 19:41:16 -05:00
2022-01-25 11:15:32 -05:00
pub fn find_item_position(ecs: &World, target: Entity, creator: Option<Entity>) -> Option<i32> {
2022-01-20 19:41:16 -05:00
let positions = ecs.read_storage::<Position>();
let map = ecs.fetch::<Map>();
// Easy - it has a position
if let Some(pos) = positions.get(target) {
return Some(map.xy_idx(pos.x, pos.y) as i32);
}
// Maybe it is carried?
if let Some(carried) = ecs.read_storage::<InBackpack>().get(target) {
if let Some(pos) = positions.get(carried.owner) {
return Some(map.xy_idx(pos.x, pos.y) as i32);
}
}
// Maybe it is equipped?
if let Some(equipped) = ecs.read_storage::<Equipped>().get(target) {
if let Some(pos) = positions.get(equipped.owner) {
return Some(map.xy_idx(pos.x, pos.y) as i32);
}
}
2022-01-25 11:15:32 -05:00
// Maybe the creator has a position?
if let Some(creator) = creator {
if let Some(pos) = positions.get(creator) {
return Some(map.xy_idx(pos.x, pos.y) as i32);
}
}
2022-01-20 19:41:16 -05:00
// No idea - give up
None
}