From 65cde7d8abd4d54d6cd87cf78ad5e65b4a3669a1 Mon Sep 17 00:00:00 2001 From: Daniel Lynn Date: Thu, 8 Jul 2021 22:53:31 -0500 Subject: [PATCH] Add chasing system --- src/components.rs | 3 +++ src/map.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/spawner.rs | 2 +- src/systems/mod.rs | 2 ++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/components.rs b/src/components.rs index 079ffa3..22fb0cd 100644 --- a/src/components.rs +++ b/src/components.rs @@ -35,3 +35,6 @@ pub struct WantsToAttack { pub attacker: Entity, pub victim: Entity, } + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ChasingPlayer; diff --git a/src/map.rs b/src/map.rs index 710f1a9..60bf9ca 100644 --- a/src/map.rs +++ b/src/map.rs @@ -38,4 +38,50 @@ impl Map { Some(map_idx(point.x, point.y)) } } + + fn valid_exit(&self, loc: Point, delta: Point) -> Option { + 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)) + } } diff --git a/src/spawner.rs b/src/spawner.rs index accfcc6..42044a9 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -29,7 +29,7 @@ pub fn spawn_monster(ecs: &mut World, rng: &mut RandomNumberGenerator, pos: Poin color: ColorPair::new(WHITE, BLACK), glyph, }, - MovingRandomly, + ChasingPlayer, Health { current: hp, max: hp, diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 20b5faf..10fa58b 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -1,3 +1,4 @@ +mod chasing; mod combat; mod end_turn; mod entity_render; @@ -37,6 +38,7 @@ pub fn build_player_scheduler() -> Schedule { pub fn build_monster_scheduler() -> Schedule { Schedule::builder() + .add_system(chasing::chasing_system()) .add_system(random_move::random_move_system()) .flush() .add_system(combat::combat_system())