summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Barrett <tombarrett@siu.edu>2017-11-23 04:35:03 -0600
committerTom Barrett <tombarrett@siu.edu>2017-11-23 04:35:03 -0600
commitaed9ad18c72c016da7894328710e9996c5629c0a (patch)
treebac1f73fef4845ea7729801f085aa599120f5abd
parent72ea9deb1cf959602a038e5141a86228186a35b3 (diff)
-added giving a destination to units
-rw-r--r--src/character.rs47
-rw-r--r--src/list.rs6
-rw-r--r--src/main.rs20
3 files changed, 45 insertions, 28 deletions
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);
- }
- }
- }
}
diff --git a/src/list.rs b/src/list.rs
index a429ab2..c22c229 100644
--- a/src/list.rs
+++ b/src/list.rs
@@ -34,6 +34,12 @@ impl List {
}
}
+ pub fn give_destination(&mut self, destination : Location) {
+ for i in 0..self.men.len() {
+ self.men[i].give_destination(destination)
+ }
+ }
+
fn get_free_locations(&mut self, location : Location) -> Vec<(Location, usize)> {
let mut potential_locations = location.neighbours(Vec::new());
diff --git a/src/main.rs b/src/main.rs
index 7044467..f847253 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -41,18 +41,24 @@ fn main() {
let mut list = List::new(map.impassable.to_vec());
loop{
- match main.getch() {
+ let order = match main.getch() {
Some(Input::Character(c)) => {
match c {
- 'h' => cursor.location.1 -= 1,
- 'l' => cursor.location.1 += 1,
- 'k' => cursor.location.0 -= 1,
- 'j' => cursor.location.0 += 1,
+ 'h' => {cursor.location.1 -= 1; None}
+ 'l' => {cursor.location.1 += 1; None}
+ 'k' => {cursor.location.0 -= 1; None}
+ 'j' => {cursor.location.0 += 1; None}
'q' => break,
- _ => (),
+ 'o' => Some(cursor.location),
+ _ => None,
}
},
- _ => ()
+ _ => None
+ };
+
+ match order {
+ None => (),
+ Some(location) => (list.give_destination(location)),
}
list.action();