summaryrefslogtreecommitdiff
path: root/src/character.rs
diff options
context:
space:
mode:
authorTom Barrett <tombarrett@siu.edu>2017-11-23 02:58:34 -0600
committerTom Barrett <tombarrett@siu.edu>2017-11-23 02:58:34 -0600
commit72ea9deb1cf959602a038e5141a86228186a35b3 (patch)
treea2169071db31acf4c8810a6b6ee740fcf0ab3a0b /src/character.rs
parent1c3ec15a9a8b7cef3c544af225d028b3de13d75e (diff)
-added pathfinding
Diffstat (limited to 'src/character.rs')
-rw-r--r--src/character.rs91
1 files changed, 73 insertions, 18 deletions
diff --git a/src/character.rs b/src/character.rs
index 4c7f7dc..c8101f5 100644
--- a/src/character.rs
+++ b/src/character.rs
@@ -1,36 +1,91 @@
extern crate rand;
use character::rand::Rng;
-use location::Location;
+extern crate pathfinding;
+use self::pathfinding::astar;
-pub struct Character{
- pub symbol : char,
- pub color : u8,
- pub location : Location,
-}
+use constants::Orders;
+use location::Location;
-impl Copy for Character {}
-impl Clone for Character {
- fn clone(&self) -> Character {
- *self
- }
+#[derive(Clone)]
+pub struct Character {
+ pub symbol : char,
+ pub color : u8,
+ pub order : u8,
+ pub location : Location,
+ pub needs_path : bool,
+ desired_location : Option<Location>,
+ path : Option<Vec<Location>>,
}
impl Character {
pub fn new(symbol : char, color : u8, location : Location) -> Character {
Character {
- symbol : symbol,
- color : color,
- location : location,
+ symbol : symbol,
+ color : color,
+ order : Orders::Move as u8,
+ location : location,
+ desired_location : Some(Location(200,200)),
+ needs_path : false,
+ path : None,
}
}
- pub fn action(&mut self, free_spaces : Vec<Location>) {
- self.wander(free_spaces);
+ pub fn action(&mut self, free_spaces : Vec<(Location,usize)>) {
+ if self.order == Orders::Wander as u8 {
+ self.needs_path == false;
+ self.wander(free_spaces);
+ }
+ else if self.order == Orders::Move as u8 {
+ self.move_toward_desired(free_spaces);
+ }
}
- fn wander(&mut self, free_spaces : Vec<Location>) {
+ fn wander(&mut self, free_spaces : Vec<(Location, usize)>) {
let direction = rand::thread_rng().gen_range(0, free_spaces.len());
- self.location = free_spaces[direction];
+ self.location = free_spaces[direction].0;
+ }
+
+ fn move_toward_desired(&mut self, free_spaces : Vec<(Location, usize)>) {
+ let mut moved = false;
+ match self.path {
+ None => self.needs_path = true,
+ Some(ref mut calculated_path) => {
+ self.needs_path = false;
+ if calculated_path.len() > 0 {
+ let next_location = calculated_path.pop().unwrap();
+ for free_space in free_spaces {
+ if next_location == free_space.0 {
+ self.location = next_location;
+ moved = true;
+ }
+ }
+ }
+ else {
+ self.desired_location = None;
+ self.order = Orders::Wander as u8;
+ }
+ }
+ }
+ if !moved {
+ self.path = None;
+ }
+ }
+
+ pub fn calculate_path(&mut self, impassable : Vec<(Location, usize)>) {
+ match self.desired_location {
+ None => (),
+ Some(target) => {
+ let location = self.location;
+ let result = astar(&location,
+ |l| l.neighbours(impassable.clone()),
+ |l| l.distance(&target),
+ |l| *l == target);
+ let mut result = result.expect("zz").0;
+ result.reverse();
+ result.pop();
+ self.path = Some(result);
+ }
+ }
}
}