Add chasing system

This commit is contained in:
Daniel Lynn 2021-07-08 22:53:31 -05:00
parent 2310dd9218
commit 65cde7d8ab
4 changed files with 52 additions and 1 deletions

View File

@ -35,3 +35,6 @@ pub struct WantsToAttack {
pub attacker: Entity, pub attacker: Entity,
pub victim: Entity, pub victim: Entity,
} }
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ChasingPlayer;

View File

@ -38,4 +38,50 @@ impl Map {
Some(map_idx(point.x, point.y)) Some(map_idx(point.x, point.y))
} }
} }
fn valid_exit(&self, loc: Point, delta: Point) -> Option<usize> {
let destination = loc + delta;
if self.in_bounds(destination) && self.can_enter_tile(destination) {
let idx = self.point2d_to_index(destination);
Some(idx)
} else {
None
}
}
}
impl Algorithm2D for Map {
fn dimensions(&self) -> Point {
Point::new(SCREEN_WIDTH, SCREEN_HEIGHT)
}
fn in_bounds(&self, point: Point) -> bool {
self.in_bounds(point)
}
}
impl BaseMap for Map {
fn get_available_exits(&self, idx: usize) -> SmallVec<[(usize, f32); 10]> {
let mut exits = SmallVec::new();
let location = self.index_to_point2d(idx);
if let Some(idx) = self.valid_exit(location, Point::new(-1, 0)) {
exits.push((idx, 1.0));
}
if let Some(idx) = self.valid_exit(location, Point::new(1, 0)) {
exits.push((idx, 1.0));
}
if let Some(idx) = self.valid_exit(location, Point::new(0, -1)) {
exits.push((idx, 1.0));
}
if let Some(idx) = self.valid_exit(location, Point::new(0, 1)) {
exits.push((idx, 1.0));
}
exits
}
fn get_pathing_distance(&self, idx1: usize, idx2: usize) -> f32 {
DistanceAlg::Pythagoras.distance2d(self.index_to_point2d(idx1), self.index_to_point2d(idx2))
}
} }

View File

@ -29,7 +29,7 @@ pub fn spawn_monster(ecs: &mut World, rng: &mut RandomNumberGenerator, pos: Poin
color: ColorPair::new(WHITE, BLACK), color: ColorPair::new(WHITE, BLACK),
glyph, glyph,
}, },
MovingRandomly, ChasingPlayer,
Health { Health {
current: hp, current: hp,
max: hp, max: hp,

View File

@ -1,3 +1,4 @@
mod chasing;
mod combat; mod combat;
mod end_turn; mod end_turn;
mod entity_render; mod entity_render;
@ -37,6 +38,7 @@ pub fn build_player_scheduler() -> Schedule {
pub fn build_monster_scheduler() -> Schedule { pub fn build_monster_scheduler() -> Schedule {
Schedule::builder() Schedule::builder()
.add_system(chasing::chasing_system())
.add_system(random_move::random_move_system()) .add_system(random_move::random_move_system())
.flush() .flush()
.add_system(combat::combat_system()) .add_system(combat::combat_system())