diff options
-rw-r--r-- | .vscode/launch.json | 19 | ||||
-rw-r--r-- | src/main.rs | 75 | ||||
-rw-r--r-- | src/world.rs | 61 |
3 files changed, 103 insertions, 52 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..20cd69a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{
+ // Use IntelliSense to learn about possible attributes.
+ // Hover to view descriptions of existing attributes.
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": "(Windows) Launch",
+ "type": "cppvsdbg",
+ "request": "launch",
+ "program": "${workspaceFolder}/target/debug/roguelike.exe",
+ "args": [],
+ "stopAtEntry": false,
+ "cwd": "${workspaceFolder}",
+ "environment": [],
+ "externalConsole": true
+ }
+ ]
+}
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 65ada01..7edc654 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,61 +10,46 @@ mod world; use character::Player; use computer::Enemy; -use pancurses::{initscr, endwin}; +use pancurses::{Window, initscr, endwin}; use rand::Rng; use std::io; +use std::convert::TryFrom; +use world::{World, GameWorld, BlockType}; -fn main() { - let window = initscr(); - window.printw("Hello Rust"); - window.refresh(); - window.mv(2, 2); - window.printw("toto"); - window.refresh(); - window.getch(); - endwin(); -} - // println!( - // "=== Welcome to RRL {} the {}! ===\n", - // env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_VERSION") - // ); - - // let characters: [character::Character; 5] = [ - // character::Character::new("".to_string(), "Cleric".to_string(), 7, 5, 6, 7), - // character::Character::new("".to_string(), "Warrior".to_string(), 10, 5, 5, 5), - // character::Character::new("".to_string(), "Hunter".to_string(), 5, 7, 7, 6), - // character::Character::new("".to_string(), "Wizard".to_string(), 3, 10, 5, 7), - // character::Character::new("".to_string(), "Thief".to_string(), 4, 5, 6, 10), - // ]; +fn draw_block(window: &Window, block: &BlockType) { + let repr = match block { + BlockType::Floor => ".", + BlockType::Wall => "0", + BlockType::Nothing => " " + }; - // let _luck_amount = rand::thread_rng().gen_range(2, 6); + print!("{}", repr); - // println!("You enter the Ephemeral Plane of Creation..."); - // println!("Please enter your name."); + window.printw(repr); +} - // let mut input_text = String::new(); +fn render_world(window: &Window, world: &World) { + let grid = world.get_world(); - // io::stdin() - // .read_line(&mut input_text) - // .expect("Failed to read line"); - // let _character_name = input_text.trim(); + for (linenum, line) in grid.iter().enumerate() { + for block in line.iter() { + draw_block(&window, block); + } + window.mv(linenum as i32, 0); + println!(""); + } +} - // println!("Please select your character type:"); - // for (i, elem) in characters.iter().enumerate() { - // print!("\n{}. {}\n\n", i + 1, elem.info()); - // } +fn main() { + let mut world = World::new(30); + world.generate(); - // let mut character_index: usize = 100; - // while character_index > characters.len() { - // character_index = read!(); - // } + let window = initscr(); - // let mut player = characters[character_index].select(_character_name.to_string(), _luck_amount); + render_world(&window, &world); - // play(&mut player); -// } + window.refresh(); -fn play(player: &mut character::Character) { - println!("=== Welcome to RRL {} the {}! ===\n", player.name, player.class); - println!("Your unique stats: {}", player.stats()); + window.getch(); + endwin(); } diff --git a/src/world.rs b/src/world.rs index fe89c3d..fbbc6be 100644 --- a/src/world.rs +++ b/src/world.rs @@ -20,12 +20,58 @@ pub trait GameWorld<'a> { }
impl World {
- fn make_corridor(&mut self, start: (usize, usize), end: (usize, usize)) {
- for x in start.0..end.0 {
- for y in start.1..end.1 {
- self.world[x - 1][y] = BlockType::Wall;
- self.world[x][y] = BlockType::Floor;
- self.world[x + 1][y] = BlockType::Wall;
+ fn make_vertical_corridor(&mut self, start: (usize, usize), length: usize) {
+ let x = start.0;
+ let endy = start.1 + length;
+ for y in start.1..endy {
+ self.wall_up(x - 1, y);
+ self.set_tile(x, y, BlockType::Floor);
+ self.wall_up(x + 1, y);
+ }
+ }
+
+ fn make_horizontal_corridor(&mut self, start: (usize, usize), length: usize) {
+ let y = start.1;
+ let endx = start.0 + length;
+ for x in start.0..endx {
+ self.wall_up(x, y - 1);
+ self.set_tile(x, y, BlockType::Floor);
+ self.wall_up(x, y - 1);
+ }
+ }
+
+ fn set_tile(&mut self, x: usize, y: usize, block: BlockType) {
+ self.world[y][x] = block;
+ }
+
+ /// Puts a wall on the coordinates if it isn't a floor.
+ fn wall_up(&mut self, x: usize, y: usize) {
+ self.set_tile(x, y, match self.world[y][x] {
+ BlockType::Floor => BlockType::Floor,
+ _ => BlockType::Wall
+ })
+ }
+
+ /// Creates a room at the given coordinates of the given size.
+ fn make_room(&mut self, start: (usize, usize), width: usize, height: usize) {
+ let endx = start.0 + width;
+ let endy = start.1 + height;
+
+ // Draw the walls
+ for x in start.0..endx {
+ self.wall_up(x, start.1);
+ self.wall_up(x, endy);
+ }
+
+ for y in start.1..endy {
+ self.wall_up(start.0, y);
+ self.wall_up(endx, y);
+ }
+
+ // Fill the room
+ for x in (start.0 + 1)..(endx) {
+ for y in (start.1 + 1)..(endy) {
+ self.set_tile(x, y, BlockType::Floor);
}
}
}
@@ -48,7 +94,8 @@ impl<'a> GameWorld<'a> for World { self.world.push(subvec);
}
- self.make_corridor((1, 10), (1, 13))
+ self.make_room((1, 13), 5, 7);
+ self.make_vertical_corridor((3, 5), 10);
}
fn get_world(&'a self) -> &'a Vec<Vec<BlockType>> {
|