From aed9ad18c72c016da7894328710e9996c5629c0a Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Thu, 23 Nov 2017 04:35:03 -0600 Subject: -added giving a destination to units --- src/character.rs | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'src/character.rs') diff --git a/src/character.rs b/src/character.rs index c8101f5..e2e2af0 100644 --- a/src/character.rs +++ b/src/character.rs @@ -23,10 +23,10 @@ impl Character { Character { symbol : symbol, color : color, - order : Orders::Move as u8, + order : Orders::Wander as u8, location : location, - desired_location : Some(Location(200,200)), needs_path : false, + desired_location : None, path : None, } } @@ -37,16 +37,38 @@ impl Character { self.wander(free_spaces); } else if self.order == Orders::Move as u8 { - self.move_toward_desired(free_spaces); + self.move_along_path(free_spaces); } } + pub fn calculate_path(&mut self, impassable : Vec<(Location, usize)>) { + match self.desired_location { + None => self.order = Orders::Wander as u8, + 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("No way to get to target.").0; + result.reverse(); + result.pop(); + self.path = Some(result); + } + } + } + + pub fn give_destination(&mut self, destination : Location) { + self.desired_location = Some(destination); + self.order = Orders::Move as u8; + } + 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].0; } - fn move_toward_desired(&mut self, free_spaces : Vec<(Location, usize)>) { + fn move_along_path(&mut self, free_spaces : Vec<(Location, usize)>) { let mut moved = false; match self.path { None => self.needs_path = true, @@ -71,21 +93,4 @@ impl Character { 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); - } - } - } } -- cgit v1.2.3