From a853a837b8cb991d8c55496401bef02275139c4e Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Fri, 24 Nov 2017 01:31:41 -0600 Subject: -added giving a grid placement order --- src/character.rs | 2 +- src/constants.rs | 8 +++++++- src/list.rs | 38 +++++++++++++++++++++++++++++++++++++- src/main.rs | 51 ++++++++++++++++++++++++++++++++++++++++----------- src/map.rs | 27 +++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 14 deletions(-) diff --git a/src/character.rs b/src/character.rs index a5f05b5..1e68dc9 100644 --- a/src/character.rs +++ b/src/character.rs @@ -92,7 +92,7 @@ impl Character { } else { self.desired_location = None; - self.order = Orders::Wander as u8; + self.order = Orders::Wait as u8; } } } diff --git a/src/constants.rs b/src/constants.rs index 84e7653..fe8aa0c 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,4 +1,4 @@ -#[allow(dead_code)] +#![allow(dead_code)] pub enum Colors { None, @@ -13,3 +13,9 @@ pub enum Orders { Move, Wander, } + +pub enum Commands { + Go, + Grid, + Finish, +} diff --git a/src/list.rs b/src/list.rs index 6c07c01..2340385 100644 --- a/src/list.rs +++ b/src/list.rs @@ -12,7 +12,7 @@ pub struct List{ impl List { pub fn new(impassable_locations : Vec) -> List { let mut men = Vec::new(); - for i in 0..3 { + for i in 0..10 { men.push(Character::new('@', Colors::BlueUnit as u8, Location(150,150+i))); } List { @@ -42,6 +42,42 @@ impl List { } } + pub fn give_grid(&mut self, first_location : Location, second_location : Location) { + let mut index = 0; + for i in first_location.0..second_location.0 + 1 { + for j in first_location.1..second_location.1 + 1 { + if index < self.men.len() { + self.men[index].give_destination(Location(i,j)); + index += 1; + } + } + } + for i in second_location.0..first_location.0 { + for j in second_location.1..first_location.1 { + if index < self.men.len() { + self.men[index].give_destination(Location(i,j)); + index += 1; + } + } + } + for i in first_location.0..second_location.0 + 1 { + for j in second_location.1..first_location.1 { + if index < self.men.len() { + self.men[index].give_destination(Location(i,j)); + index += 1; + } + } + } + for i in second_location.0..first_location.0 { + for j in first_location.1..second_location.1 + 1 { + if index < self.men.len() { + self.men[index].give_destination(Location(i,j)); + index += 1; + } + } + } + } + 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 f847253..6148aac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use view::View; use list::List; use location::Location; use character::Character; -use constants::Colors; +use constants::{Colors, Commands}; fn init() -> pancurses::Window { let main = initscr(); @@ -40,35 +40,64 @@ fn main() { let mut list = List::new(map.impassable.to_vec()); + let mut paused = false; + let mut first_location = None; + let mut draw_box = false; loop{ - let order = match main.getch() { - Some(Input::Character(c)) => { - match c { + let command = match main.getch() { + Some(Input::Character(ch)) => { + match ch { '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), + 'o' => Some(Commands::Go), + 's' => Some(Commands::Grid), + '\n' => Some(Commands::Finish), _ => None, } }, - _ => None + Some(Input::KeyEnter) => Some(Commands::Finish), + _ => None, }; - match order { + map.fill(); + + match command { + Some(Commands::Go) => list.give_destination(cursor.location), + Some(Commands::Grid) => { + paused = true; + draw_box = true; + first_location = Some(cursor.location); + }, + Some(Commands::Finish) => { + paused = false; + draw_box = false; + match first_location { + Some(first_location) => list.give_grid(first_location, cursor.location), + None => (), + } + }, None => (), - Some(location) => (list.give_destination(location)), } - list.action(); + if !paused { + list.action(); + } - map.fill(); + map.draw(&cursor); + + if draw_box { + match first_location { + Some(first_location) => map.draw_box(first_location, cursor.location), + None => (), + } + } for man in list.men.iter() { map.draw(man); } - map.draw(&cursor); view.center(cursor.clone(), &map.window); } diff --git a/src/map.rs b/src/map.rs index a108929..6546917 100644 --- a/src/map.rs +++ b/src/map.rs @@ -64,6 +64,33 @@ impl Map { self.window.mvaddch(character.location.0, character.location.1, character.symbol); } + pub fn draw_box(&self, first_location : Location, second_location : Location) { + for i in first_location.0..second_location.0 + 1 { + for j in first_location.1..second_location.1 + 1 { + self.window.attron(ColorPair(Colors::White as u8)); + self.window.mvaddch(i as i32, j as i32, 'X'); + } + } + for i in second_location.0..first_location.0 { + for j in second_location.1..first_location.1 { + self.window.attron(ColorPair(Colors::White as u8)); + self.window.mvaddch(i as i32, j as i32, 'X'); + } + } + for i in first_location.0..second_location.0 + 1 { + for j in second_location.1..first_location.1 { + self.window.attron(ColorPair(Colors::White as u8)); + self.window.mvaddch(i as i32, j as i32, 'X'); + } + } + for i in second_location.0..first_location.0 { + for j in first_location.1..second_location.1 + 1 { + self.window.attron(ColorPair(Colors::White as u8)); + self.window.mvaddch(i as i32, j as i32, 'X'); + } + } + } + pub fn fill(&mut self) { for (i, row) in self.map_data.iter().enumerate() { for (j, index) in row.chars().enumerate() { -- cgit v1.2.3