diff options
author | Tom Barrett <tombarrett@siu.edu> | 2017-11-02 05:51:32 -0500 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-11-02 05:51:32 -0500 |
commit | 2e5f1363e6efff3daefee9fe6388be2332e6fb62 (patch) | |
tree | f0e255472120577d3b8699df9bda746b847ed730 /src | |
parent | 5c495b75f25c324b5e468d0aa80d0699bb5f3fd2 (diff) |
-units now move respect impassable locations, syntax more uniform
Diffstat (limited to 'src')
-rw-r--r-- | src/character.rs | 18 | ||||
-rw-r--r-- | src/list.rs | 34 | ||||
-rw-r--r-- | src/location.rs | 34 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/map.rs | 24 | ||||
-rw-r--r-- | src/view.rs | 26 |
6 files changed, 72 insertions, 66 deletions
diff --git a/src/character.rs b/src/character.rs index 2f2289f..4c7f7dc 100644 --- a/src/character.rs +++ b/src/character.rs @@ -4,9 +4,9 @@ use character::rand::Rng; use location::Location; pub struct Character{ - pub symbol : char, - pub color : u8, - pub location : Location, + pub symbol : char, + pub color : u8, + pub location : Location, } impl Copy for Character {} @@ -19,18 +19,18 @@ impl Clone for Character { impl Character { pub fn new(symbol : char, color : u8, location : Location) -> Character { Character { - symbol : symbol, - color : color, - location : location, + symbol : symbol, + color : color, + location : location, } } - pub fn action(&mut self, free_spaces : Vec<Location>){ + pub fn action(&mut self, free_spaces : Vec<Location>) { self.wander(free_spaces); } - fn wander(&mut self, free_spaces : Vec<Location>){ - let direction = rand::thread_rng().gen_range(0,free_spaces.len()); + fn wander(&mut self, free_spaces : Vec<Location>) { + let direction = rand::thread_rng().gen_range(0, free_spaces.len()); self.location = free_spaces[direction]; } } diff --git a/src/list.rs b/src/list.rs index d622d5c..23d23df 100644 --- a/src/list.rs +++ b/src/list.rs @@ -4,8 +4,8 @@ use character::Character; use location::Location; pub struct List{ - pub men : Vec<Character>, - impassable_locations : Vec<Location>, + pub men : Vec<Character>, + impassable_locations : Vec<Location>, } impl List { @@ -17,8 +17,8 @@ impl List { men.push(c); } List { - men : men, - impassable_locations : impassable_locations, + men : men, + impassable_locations : impassable_locations, } } @@ -33,30 +33,28 @@ impl List { fn get_free_locations(&mut self, location : Location) -> Vec<Location> { let mut potential_locations = Vec::new(); potential_locations.push(location.up()); + potential_locations.push(location.upleft()); + potential_locations.push(location.upright()); potential_locations.push(location.down()); + potential_locations.push(location.downleft()); + potential_locations.push(location.downright()); potential_locations.push(location.left()); potential_locations.push(location.right()); - let mut indexes = Vec::new(); - for man in self.men.iter() { - for (index, potential_location) in potential_locations.iter().enumerate() { + potential_locations.retain(|potential_location| { + let mut keep = true; + for man in self.men.iter() { if potential_location == &man.location { - indexes.push(index); + keep = false; } } - } - - for impassable_location in self.impassable_locations.iter() { - for (index, potential_location) in potential_locations.iter().enumerate() { + for impassable_location in self.impassable_locations.iter() { if potential_location == impassable_location { - indexes.push(index); + keep = false; } } - } - - //for index in indexes.iter() { - // potential_locations.remove(index); - //} + keep + }); potential_locations } diff --git a/src/location.rs b/src/location.rs index f17965e..4a98ef6 100644 --- a/src/location.rs +++ b/src/location.rs @@ -1,6 +1,6 @@ use std::cmp; -pub struct Location{ +pub struct Location { pub x : i32, pub y : i32 } @@ -24,20 +24,28 @@ impl cmp::PartialEq for Location { } impl Location { - pub fn up(mut self) -> Location { - self.y += 1; - self + pub fn up(self) -> Location { + Location{ x : self.x, y : self.y + 1 } } - pub fn down(mut self) -> Location { - self.y -= 1; - self + pub fn upleft(self) -> Location { + Location{ x : self.x - 1, y : self.y + 1 } } - pub fn right(mut self) -> Location { - self.x += 1; - self + pub fn upright(self) -> Location { + Location{ x : self.x + 1, y : self.y + 1 } } - pub fn left(mut self) -> Location { - self.x -= 1; - self + pub fn down(self) -> Location { + Location{ x : self.x, y : self.y - 1 } + } + pub fn downleft(self) -> Location { + Location{ x : self.x - 1, y : self.y - 1 } + } + pub fn downright(self) -> Location { + Location{ x : self.x + 1, y : self.y - 1 } + } + pub fn right(self) -> Location { + Location{ x : self.x + 1, y : self.y } + } + pub fn left(self) -> Location { + Location{ x : self.x - 1, y : self.y } } } diff --git a/src/main.rs b/src/main.rs index cd06942..bed39ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,7 @@ fn main() { map.fill(); - for man in list.men.iter(){ + for man in list.men.iter() { map.draw(man); } map.draw(&cursor); @@ -8,12 +8,12 @@ use pancurses::{newwin, ColorPair}; use character::Character; use location::Location; -pub struct Map{ - pub height : i32, - pub width : i32, - pub window : pancurses::Window, - map_data : Vec<String>, - pub impassable :Vec<Location>, +pub struct Map { + pub height : i32, + pub width : i32, + pub window : pancurses::Window, + map_data : Vec<String>, + pub impassable : Vec<Location>, } impl Map { @@ -22,7 +22,7 @@ impl Map { let reader = BufReader::new(file); let mut map_data = Vec::new(); - for line in reader.lines(){ + for line in reader.lines() { map_data.push(line.unwrap()); } @@ -50,11 +50,11 @@ impl Map { } Map { - height : height, - width : width, - window: newwin(height, width, 0, 0), - map_data: map_data, - impassable: impassable, + height : height, + width : width, + window : newwin(height, width, 0, 0), + map_data : map_data, + impassable : impassable, } } diff --git a/src/view.rs b/src/view.rs index 8884e18..c19ee63 100644 --- a/src/view.rs +++ b/src/view.rs @@ -2,23 +2,23 @@ extern crate pancurses; use character::Character; -pub struct View{ - width: i32, - height: i32, - row: i32, - col: i32, - window: pancurses::Window, +pub struct View { + width : i32, + height : i32, + row : i32, + col : i32, + window : pancurses::Window, } -impl View{ +impl View { pub fn new((x,y) : (i32, i32), map_window : &pancurses::Window) -> View { let window = map_window.derwin(x,y,0,0).expect("Cannot create derwin."); - View{ - width : x, - height : y, - row : 0, - col : 0, - window: window, + View { + width : x, + height : y, + row : 0, + col : 0, + window : window, } } |