Add initial monster chase mechanism
This commit is contained in:
parent
30b0449e99
commit
45ab89f795
41
src/map.rs
41
src/map.rs
@ -1,5 +1,5 @@
|
||||
use super::Rect;
|
||||
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, RGB};
|
||||
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, SmallVec, RGB};
|
||||
use specs::prelude::*;
|
||||
use std::cmp::{max, min};
|
||||
|
||||
@ -107,6 +107,15 @@ impl Map {
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
fn is_exit_valid(&self, x: i32, y: i32) -> bool {
|
||||
if x < 1 || x > self.width - 1 || y < 1 || y > self.height - 1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let idx = self.xy_idx(x, y);
|
||||
self.tiles[idx as usize] != TileType::Wall
|
||||
}
|
||||
}
|
||||
|
||||
impl Algorithm2D for Map {
|
||||
@ -119,6 +128,36 @@ impl BaseMap for Map {
|
||||
fn is_opaque(&self, idx: usize) -> bool {
|
||||
self.tiles[idx as usize] == TileType::Wall
|
||||
}
|
||||
|
||||
fn get_available_exits(&self, idx: usize) -> SmallVec<[(usize, f32); 10]> {
|
||||
let mut exits = rltk::SmallVec::new();
|
||||
let x = idx as i32 % self.width;
|
||||
let y = idx as i32 / self.width;
|
||||
let w = self.width as usize;
|
||||
|
||||
if self.is_exit_valid(x - 1, y) {
|
||||
exits.push((idx - 1, 1.0))
|
||||
};
|
||||
if self.is_exit_valid(x + 1, y) {
|
||||
exits.push((idx + 1, 1.0))
|
||||
};
|
||||
if self.is_exit_valid(x, y - 1) {
|
||||
exits.push((idx - w, 1.0))
|
||||
};
|
||||
if self.is_exit_valid(x, y + 1) {
|
||||
exits.push((idx + w, 1.0))
|
||||
};
|
||||
|
||||
exits
|
||||
}
|
||||
|
||||
fn get_pathing_distance(&self, idx1: usize, idx2: usize) -> f32 {
|
||||
let w = self.width as usize;
|
||||
let p1 = Point::new(idx1 % w, idx1 / w);
|
||||
let p2 = Point::new(idx2 % w, idx2 / w);
|
||||
|
||||
rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||
|
@ -1,23 +1,41 @@
|
||||
use super::{Monster, Name, Viewshed};
|
||||
use super::{Map, Monster, Name, Position, Viewshed};
|
||||
use rltk::{console, Point};
|
||||
use specs::prelude::*;
|
||||
|
||||
pub struct MonsterAI {}
|
||||
|
||||
impl<'a> System<'a> for MonsterAI {
|
||||
#[allow(clippy::type_complexity)]
|
||||
type SystemData = (
|
||||
WriteExpect<'a, Map>,
|
||||
ReadExpect<'a, Point>,
|
||||
ReadStorage<'a, Viewshed>,
|
||||
WriteStorage<'a, Viewshed>,
|
||||
ReadStorage<'a, Monster>,
|
||||
ReadStorage<'a, Name>,
|
||||
WriteStorage<'a, Position>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (player_pos, viewshed, monster, name) = data;
|
||||
let (mut map, player_pos, mut viewshed, monster, name, mut position) = data;
|
||||
|
||||
for (viewshed, _monster, name) in (&viewshed, &monster, &name).join() {
|
||||
for (mut viewshed, _monster, name, mut pos) in
|
||||
(&mut viewshed, &monster, &name, &mut position).join()
|
||||
{
|
||||
if viewshed.visible_tiles.contains(&*player_pos) {
|
||||
console::log(format!("{} shouts insults", name.name));
|
||||
console::log(&format!("{} shouts insults", name.name));
|
||||
|
||||
let path = rltk::a_star_search(
|
||||
map.xy_idx(pos.x, pos.y) as i32,
|
||||
map.xy_idx(player_pos.x, player_pos.y) as i32,
|
||||
&mut *map,
|
||||
);
|
||||
|
||||
if path.success && path.steps.len() > 1 {
|
||||
pos.x = path.steps[1] as i32 % map.width;
|
||||
pos.y = path.steps[1] as i32 / map.width;
|
||||
|
||||
viewshed.dirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user