From 1c3ec15a9a8b7cef3c544af225d028b3de13d75e Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Thu, 2 Nov 2017 08:36:26 -0500 Subject: - added color constants, fixed some syntax, and changed method of getting potential locations --- src/constants.rs | 8 ++++++++ src/list.rs | 21 ++++++--------------- src/location.rs | 35 ++++++++++++----------------------- src/main.rs | 16 +++++++++------- src/map.rs | 19 ++++++++++--------- 5 files changed, 45 insertions(+), 54 deletions(-) create mode 100644 src/constants.rs (limited to 'src') diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..7219cd4 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,8 @@ +#[allow(dead_code)] +pub enum Colors { + None, + Grass, + Tree, + White, + BlueUnit, +} diff --git a/src/list.rs b/src/list.rs index 23d23df..cf0607f 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,7 +1,8 @@ extern crate pancurses; -use character::Character; use location::Location; +use character::Character; +use constants::Colors; pub struct List{ pub men : Vec, @@ -12,9 +13,7 @@ impl List { pub fn new(impassable_locations : Vec) -> List { let mut men = Vec::new(); for i in 0..3 { - let l = Location{x:150,y:150+i}; - let c = Character::new('@',4,l); - men.push(c); + men.push(Character::new('@', Colors::BlueUnit as u8, Location{ x : 150, y : 150+i })); } List { men : men, @@ -24,22 +23,14 @@ impl List { pub fn action(&mut self) { for i in 0..self.men.len() { - let tmp = self.men[i].location.clone(); - let free_locations = self.get_free_locations(tmp); + let location = self.men[i].location.clone(); + let free_locations = self.get_free_locations(location); self.men[i].action(free_locations); } } fn get_free_locations(&mut self, location : Location) -> Vec { - 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 potential_locations = location.get_around(); potential_locations.retain(|potential_location| { let mut keep = true; diff --git a/src/location.rs b/src/location.rs index 4a98ef6..eb8110c 100644 --- a/src/location.rs +++ b/src/location.rs @@ -24,28 +24,17 @@ impl cmp::PartialEq for Location { } impl Location { - pub fn up(self) -> Location { - Location{ x : self.x, y : self.y + 1 } - } - pub fn upleft(self) -> Location { - Location{ x : self.x - 1, y : self.y + 1 } - } - pub fn upright(self) -> Location { - Location{ x : self.x + 1, y : self.y + 1 } - } - 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 } + pub fn get_around(self) -> Vec { + let mut around = Vec::new(); + around.push(Location { x : self.x, y : self.y }); + around.push(Location { x : self.x+1, y : self.y }); + around.push(Location { x : self.x-1, y : self.y }); + around.push(Location { x : self.x, y : self.y+1 }); + around.push(Location { x : self.x, y : self.y-1 }); + around.push(Location { x : self.x+1, y : self.y+1 }); + around.push(Location { x : self.x-1, y : self.y-1 }); + around.push(Location { x : self.x+1, y : self.y-1 }); + around.push(Location { x : self.x-1, y : self.y+1 }); + around } } diff --git a/src/main.rs b/src/main.rs index bed39ec..ac74781 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,14 +4,16 @@ use pancurses::{initscr, endwin, noecho, start_color, Input, init_pair, COLOR_YE mod map; mod view; mod list; -mod character; mod location; +mod character; +mod constants; use map::Map; use view::View; use list::List; -use character::Character; use location::Location; +use character::Character; +use constants::Colors; fn init() -> pancurses::Window { let main = initscr(); @@ -21,10 +23,10 @@ fn init() -> pancurses::Window { curs_set(0); main.timeout(100); start_color(); - init_pair(1, COLOR_GREEN, COLOR_BLACK); - init_pair(2, COLOR_YELLOW, COLOR_BLACK); - init_pair(3, COLOR_WHITE, COLOR_WHITE); - init_pair(4, COLOR_WHITE, COLOR_BLUE); + init_pair(Colors::Grass as i16, COLOR_GREEN, COLOR_BLACK); + init_pair(Colors::Tree as i16, COLOR_YELLOW, COLOR_BLACK); + init_pair(Colors::White as i16, COLOR_WHITE, COLOR_WHITE); + init_pair(Colors::BlueUnit as i16, COLOR_WHITE, COLOR_BLUE); main } @@ -34,7 +36,7 @@ fn main() { let mut map = Map::new(); let mut view = View::new(main.get_max_yx(), &map.window); - let mut cursor = Character::new('X', 3, Location{x:150,y:150}); + let mut cursor = Character::new('X', Colors::White as u8, Location{ x : 150, y : 150}); let mut list = List::new(map.impassable.to_vec()); diff --git a/src/map.rs b/src/map.rs index c1e87a5..59d53c7 100644 --- a/src/map.rs +++ b/src/map.rs @@ -5,8 +5,9 @@ use std::io::prelude::*; extern crate pancurses; use pancurses::{newwin, ColorPair}; -use character::Character; use location::Location; +use character::Character; +use constants::Colors; pub struct Map { pub height : i32, @@ -33,20 +34,20 @@ impl Map { for (i, row) in map_data.iter().enumerate() { for (j, index) in row.chars().enumerate() { match index { - '0' | 'O' => impassable.push(Location{x : i as i32, y : j as i32}), + '0' | 'O' => impassable.push(Location{ x : i as i32, y : j as i32 }), _ => (), } } } for y in 0..height { - impassable.push(Location{x : 0 as i32, y : y as i32}); - impassable.push(Location{x : width-1 as i32, y : y as i32}); + impassable.push(Location{ x : 0 as i32, y : y as i32 }); + impassable.push(Location{ x : width-1 as i32, y : y as i32} ); } for x in 0..width { - impassable.push(Location{x : x as i32, y : 0 as i32}); - impassable.push(Location{x : x as i32, y : height-1 as i32}); + impassable.push(Location{ x : x as i32, y : 0 as i32 }); + impassable.push(Location{ x : x as i32, y : height-1 as i32 }); } Map { @@ -68,18 +69,18 @@ impl Map { for (j, index) in row.chars().enumerate() { match index { '0' | 'O' => { - self.window.attron(ColorPair(2)); + self.window.attron(ColorPair(Colors::Tree as u8)); self.window.mvaddch(i as i32, j as i32, index); } _ => { - self.window.attron(ColorPair(1)); + self.window.attron(ColorPair(Colors::Grass as u8)); self.window.mvaddch(i as i32, j as i32, index); } } } } - self.window.attron(ColorPair(3)); + self.window.attron(ColorPair(Colors::White as u8)); for y in 0..self.height { self.window.mvaddch(y, 0, '-'); self.window.mvaddch(y, self.width-1, '-'); -- cgit v1.2.3