diff options
author | Tom Barrett <tombarrett@siu.edu> | 2017-10-25 09:56:36 -0500 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-10-25 09:56:36 -0500 |
commit | 5c495b75f25c324b5e468d0aa80d0699bb5f3fd2 (patch) | |
tree | 0572763449f700fc407d17f185a2ce1c1495114f /src | |
parent | 5b7a80d0e1fc0fcddbfad976a3f0fbbf7f6cd2ab (diff) |
-moved drawing to the map and out of character
-now will just compute free spaces at list level and pass to characters
Diffstat (limited to 'src')
-rw-r--r-- | src/character.rs | 33 | ||||
-rw-r--r-- | src/list.rs | 51 | ||||
-rw-r--r-- | src/location.rs | 32 | ||||
-rw-r--r-- | src/main.rs | 15 | ||||
-rw-r--r-- | src/map.rs | 12 |
5 files changed, 95 insertions, 48 deletions
diff --git a/src/character.rs b/src/character.rs index 7d5e951..2f2289f 100644 --- a/src/character.rs +++ b/src/character.rs @@ -1,14 +1,11 @@ -extern crate pancurses; -use pancurses::ColorPair; - extern crate rand; use character::rand::Rng; use location::Location; pub struct Character{ - symbol : char, - color : u8, + pub symbol : char, + pub color : u8, pub location : Location, } @@ -28,28 +25,12 @@ impl Character { } } - pub fn draw(self, window : &pancurses::Window) { - window.attron(ColorPair(self.color)); - window.mvaddch(self.location.x, self.location.y, self.symbol); - } - - pub fn action(self, men : Vec<Character>, impassable : Vec<Location>){ - self.wander(men, impassable); - } - - fn wander(mut self, men : Vec<Character>, impassable : Vec<Location>){ - let direction = rand::thread_rng().gen_range(0,9); - let mut desired_location = self.location; - if direction == 0 { - desired_location.x = desired_location.x+1; - } - - if self.free_space(desired_location, men, impassable) { - self.location = desired_location; - } + pub fn action(&mut self, free_spaces : Vec<Location>){ + self.wander(free_spaces); } - fn free_space(self, location : Location, men : Vec<Character>, impassable : Vec<Location>) -> bool { - true + 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 cdf00a8..d622d5c 100644 --- a/src/list.rs +++ b/src/list.rs @@ -4,33 +4,60 @@ use character::Character; use location::Location; pub struct List{ - men : Vec<Character>, - impassable : Vec<Location>, + pub men : Vec<Character>, + impassable_locations : Vec<Location>, } -impl List{ - pub fn new(impassable : Vec<Location>) -> List { +impl List { + pub fn new(impassable_locations : Vec<Location>) -> 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); } - List{ + List { men : men, - impassable : impassable, + impassable_locations : impassable_locations, } } - pub fn draw(&self, window : &pancurses::Window) { - for man in self.men.iter(){ - man.draw(window); + 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); + self.men[i].action(free_locations); } } - pub fn action(&self) { - for man in self.men.iter(){ - man.action(self.men.to_vec(), self.impassable.to_vec()); + 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.down()); + 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() { + if potential_location == &man.location { + indexes.push(index); + } + } + } + + for impassable_location in self.impassable_locations.iter() { + for (index, potential_location) in potential_locations.iter().enumerate() { + if potential_location == impassable_location { + indexes.push(index); + } + } } + + //for index in indexes.iter() { + // potential_locations.remove(index); + //} + + potential_locations } } diff --git a/src/location.rs b/src/location.rs index b1b9144..f17965e 100644 --- a/src/location.rs +++ b/src/location.rs @@ -1,3 +1,5 @@ +use std::cmp; + pub struct Location{ pub x : i32, pub y : i32 @@ -9,3 +11,33 @@ impl Clone for Location { *self } } + +impl cmp::PartialEq for Location { + fn eq(&self, rhs : &Location) -> bool { + if self.x == rhs.x && self.y == rhs.y { + true + } + else { + false + } + } +} + +impl Location { + pub fn up(mut self) -> Location { + self.y += 1; + self + } + pub fn down(mut self) -> Location { + self.y -= 1; + self + } + pub fn right(mut self) -> Location { + self.x += 1; + self + } + pub fn left(mut self) -> Location { + self.x -= 1; + self + } +} diff --git a/src/main.rs b/src/main.rs index 2e8ca3e..cd06942 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,9 +36,8 @@ fn main() { let mut cursor = Character::new('X', 3, Location{x:150,y:150}); - let list = List::new(map.impassable.to_vec()); + let mut list = List::new(map.impassable.to_vec()); - let paused = false; loop{ match main.getch() { Some(Input::Character(c)) => { @@ -54,13 +53,15 @@ fn main() { _ => () } - if !paused { - list.action(); - } + list.action(); map.fill(); - list.draw(&map.window); - cursor.draw(&map.window); + + for man in list.men.iter(){ + map.draw(man); + } + map.draw(&cursor); + view.center(cursor, &map.window); } @@ -5,6 +5,7 @@ use std::io::prelude::*; extern crate pancurses; use pancurses::{newwin, ColorPair}; +use character::Character; use location::Location; pub struct Map{ @@ -15,8 +16,8 @@ pub struct Map{ pub impassable :Vec<Location>, } -impl Map{ - pub fn new() -> Map{ +impl Map { + pub fn new() -> Map { let file = File::open("data/map.txt").expect("Cant open map file !"); let reader = BufReader::new(file); @@ -48,7 +49,7 @@ impl Map{ impassable.push(Location{x : x as i32, y : height-1 as i32}); } - Map{ + Map { height : height, width : width, window: newwin(height, width, 0, 0), @@ -57,6 +58,11 @@ impl Map{ } } + pub fn draw(&self, character : &Character) { + self.window.attron(ColorPair(character.color)); + self.window.mvaddch(character.location.x, character.location.y, character.symbol); + } + pub fn fill(&mut self) { for (i, row) in self.map_data.iter().enumerate() { for (j, index) in row.chars().enumerate() { |