improving map slightly

This commit is contained in:
2020-03-26 23:20:49 -04:00
parent ea5041d85a
commit 6132fa522b
3 changed files with 180 additions and 66 deletions

View File

@@ -1,7 +1,11 @@
use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
use specs::prelude::*;
use std::cmp::{max, min};
mod rect;
pub use rect::*;
mod map;
pub use map::*;
#[macro_use]
extern crate specs_derive;
@@ -25,70 +29,6 @@ struct State {
#[derive(Component)]
struct LeftMover {}
#[derive(PartialEq, Copy, Clone)]
enum TileType {
Wall, Floor
}
pub fn xy_idx(x: i32, y: i32) -> usize {
(y as usize * 80) + x as usize
}
fn new_map() -> Vec<TileType> {
let mut map = vec![TileType::Floor; 80*50];
// Make the boundaries walls
for x in 0..80 {
map[xy_idx(x, 0)] = TileType::Wall;
map[xy_idx(x, 49)] = TileType::Wall;
}
for y in 0..50 {
map[xy_idx(0, y)] = TileType::Wall;
map[xy_idx(79, y)] = TileType::Wall;
}
// Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
// First, obtain the thread-local RNG:
let mut rng = rltk::RandomNumberGenerator::new();
for _i in 0..400 {
let x = rng.roll_dice(1, 79);
let y = rng.roll_dice(1, 49);
let idx = xy_idx(x, y);
if idx != xy_idx(40, 25) {
map[idx] = TileType::Wall;
}
}
map
}
fn draw_map(map: &[TileType], ctx : &mut Rltk) {
let mut y = 0;
let mut x = 0;
for tile in map.iter() {
// Render a tile depending upon the tile type
match tile {
TileType::Floor => {
ctx.set(x, y, RGB::from_f32(0.5, 0.5, 0.5), RGB::from_f32(0., 0., 0.), rltk::to_cp437('.'));
}
TileType::Wall => {
ctx.set(x, y, RGB::from_f32(0.0, 1.0, 0.0), RGB::from_f32(0., 0., 0.), rltk::to_cp437('#'));
}
}
// Move the coordinates
x += 1;
if x > 79 {
x = 0;
y += 1;
}
}
}
#[derive(Component, Debug)]
struct Player {}
@@ -197,7 +137,7 @@ fn main() {
.build();
}
gs.ecs.insert(new_map());
gs.ecs.insert(new_map_rooms_and_corridors());
rltk::main_loop(context, gs);
}