diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/character.rs | 55 | ||||
-rw-r--r-- | src/list.rs | 36 | ||||
-rw-r--r-- | src/location.rs | 11 | ||||
-rw-r--r-- | src/main.rs | 61 | ||||
-rw-r--r-- | src/map.rs | 87 | ||||
-rw-r--r-- | src/view.rs | 61 |
6 files changed, 311 insertions, 0 deletions
diff --git a/src/character.rs b/src/character.rs new file mode 100644 index 0000000..7d5e951 --- /dev/null +++ b/src/character.rs @@ -0,0 +1,55 @@ +extern crate pancurses; +use pancurses::ColorPair; + +extern crate rand; +use character::rand::Rng; + +use location::Location; + +pub struct Character{ + symbol : char, + color : u8, + pub location : Location, +} + +impl Copy for Character {} +impl Clone for Character { + fn clone(&self) -> Character { + *self + } +} + +impl Character { + pub fn new(symbol : char, color : u8, location : Location) -> Character { + Character { + symbol : symbol, + color : color, + location : location, + } + } + + 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; + } + } + + fn free_space(self, location : Location, men : Vec<Character>, impassable : Vec<Location>) -> bool { + true + } +} diff --git a/src/list.rs b/src/list.rs new file mode 100644 index 0000000..cdf00a8 --- /dev/null +++ b/src/list.rs @@ -0,0 +1,36 @@ +extern crate pancurses; + +use character::Character; +use location::Location; + +pub struct List{ + men : Vec<Character>, + impassable : Vec<Location>, +} + +impl List{ + pub fn new(impassable : 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{ + men : men, + impassable : impassable, + } + } + + pub fn draw(&self, window : &pancurses::Window) { + for man in self.men.iter(){ + man.draw(window); + } + } + + pub fn action(&self) { + for man in self.men.iter(){ + man.action(self.men.to_vec(), self.impassable.to_vec()); + } + } +} diff --git a/src/location.rs b/src/location.rs new file mode 100644 index 0000000..b1b9144 --- /dev/null +++ b/src/location.rs @@ -0,0 +1,11 @@ +pub struct Location{ + pub x : i32, + pub y : i32 +} + +impl Copy for Location {} +impl Clone for Location { + fn clone(&self) -> Location { + *self + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..81583b8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,61 @@ +extern crate pancurses; +use pancurses::{initscr, endwin, noecho, start_color, Input, init_pair, COLOR_YELLOW, COLOR_BLACK, COLOR_WHITE, COLOR_BLUE, COLOR_GREEN, cbreak, curs_set}; + +mod map; +mod view; +mod list; +mod character; +mod location; + +use map::Map; +use view::View; +use list::List; +use character::Character; +use location::Location; + +fn init() -> pancurses::Window { + let main = initscr(); + main.clear(); + noecho(); + cbreak(); + 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); + main +} + +fn main() { + + let main = init(); + + let mut map = Map::new(); + let mut view = View::new(main.get_max_yx(), &map.window); + let cursor = Character::new('X', 3, Location{x:150,y:150}); + + map.fill(); + let list = List::new(map.impassable.to_vec()); + list.draw(&map.window); + cursor.draw(&map.window); + + let paused = false; + loop{ + match main.getch() { + Some(Input::Character(c)) => break, + _ => () + } + + if !paused { + list.action(); + } + map.fill(); + list.draw(&map.window); + cursor.draw(&map.window); + view.center(cursor, map.window.get_max_yx()); + } + + endwin(); +} diff --git a/src/map.rs b/src/map.rs new file mode 100644 index 0000000..2505747 --- /dev/null +++ b/src/map.rs @@ -0,0 +1,87 @@ +use std::fs::File; +use std::io::BufReader; +use std::io::prelude::*; + +extern crate pancurses; +use pancurses::{newwin, ColorPair}; + +use location::Location; + +pub struct Map{ + pub height : i32, + pub width : i32, + pub window : pancurses::Window, + map_data : Vec<String>, + pub impassable :Vec<Location>, +} + +impl Map{ + pub fn new() -> Map{ + let file = File::open("data/map.txt").expect("Cant open map file !"); + let reader = BufReader::new(file); + + let mut map_data = Vec::new(); + for line in reader.lines(){ + map_data.push(line.unwrap()); + } + + let height = map_data.len() as i32; + let width = map_data[0].len() as i32; + + let mut impassable = Vec::new(); + 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}), + _ => (), + } + } + } + + 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}); + } + + 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}); + } + + Map{ + height : height, + width : width, + window: newwin(height, width, 0, 0), + map_data: map_data, + impassable: impassable, + } + } + + pub fn fill(&mut self) { + for (i, row) in self.map_data.iter().enumerate() { + for (j, index) in row.chars().enumerate() { + match index { + '0' | 'O' => { + self.window.attron(ColorPair(2)); + self.window.mvaddch(i as i32, j as i32, index); + } + _ => { + self.window.attron(ColorPair(1)); + self.window.mvaddch(i as i32, j as i32, index); + } + } + } + } + + self.window.attron(ColorPair(3)); + for y in 0..self.height { + self.window.mvaddch(y, 0, '-'); + self.window.mvaddch(y, self.width-1, '-'); + } + + for x in 0..self.width { + self.window.mvaddch(0, x, '|'); + self.window.mvaddch(self.height-1, x, '|'); + } + } +} diff --git a/src/view.rs b/src/view.rs new file mode 100644 index 0000000..51a052b --- /dev/null +++ b/src/view.rs @@ -0,0 +1,61 @@ +extern crate pancurses; + +use character::Character; + +pub struct View{ + width: i32, + height: i32, + row: i32, + col: i32, + window: pancurses::Window, +} + +impl View{ + pub fn new((x,y) : (i32, i32), map_window : &pancurses::Window) -> View { + let window = map_window.derwin(x,y,0,0).expect("help"); + View{ + width : x, + height : y, + row : 0, + col : 0, + window: window, + } + } + + pub fn center(&mut self, character : Character, (hh, ww) : (i32,i32)) { + let mut rr = self.row; + let mut cc = self.col; + let r = character.location.x - self.height/2; + let c = character.location.y - self.width/2; + + if c + self.width >= ww { + let delta = ww - (c + self.width); + cc = c + delta; + } + else { + cc = c; + } + + if r + self.height >= hh { + let delta = hh - (r + self.height); + rr = r + delta; + } + else { + rr = r; + } + + if r < 0 { + rr = 0; + } + + if c < 0 { + cc = 0; + } + + self.window.refresh(); + self.window.mv(rr, cc); + self.row = rr; + self.col = cc; + self.window.refresh(); + } +} |