refactored maps
This commit is contained in:
parent
89a7034965
commit
1e7fb1e98b
|
@ -11,6 +11,7 @@ mod rect;
|
|||
mod state;
|
||||
|
||||
use crate::entity::{Player, Position, Renderable};
|
||||
use crate::map::Map;
|
||||
use crate::state::State;
|
||||
|
||||
fn main() {
|
||||
|
@ -24,11 +25,11 @@ fn main() {
|
|||
gs.ecs.register::<Renderable>();
|
||||
gs.ecs.register::<Player>();
|
||||
|
||||
let (rooms, map) = map::new_room_corridors();
|
||||
gs.ecs.insert(map);
|
||||
let map = Map::new_room_corridors();
|
||||
gs.ecs.insert(map.clone());
|
||||
|
||||
// start player in center of room 1
|
||||
let (p_x, p_y) = rooms[0].center();
|
||||
let (p_x, p_y) = map.rooms[0].center();
|
||||
gs.ecs
|
||||
.create_entity()
|
||||
.with(Position { x: p_x, y: p_y })
|
||||
|
|
182
src/map.rs
182
src/map.rs
|
@ -5,21 +5,114 @@ use rltk::{Console, RandomNumberGenerator, Rltk, RGB};
|
|||
use crate::entity::TileType;
|
||||
use crate::rect::Rect;
|
||||
|
||||
pub fn xy_idx(x: i32, y: i32) -> usize {
|
||||
(y as usize * 80) + x as usize
|
||||
#[derive(Clone)]
|
||||
pub struct Map {
|
||||
pub tiles: Vec<TileType>,
|
||||
pub rooms: Vec<Rect>,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
}
|
||||
|
||||
impl Map {
|
||||
pub fn xy_idx(&self, x: i32, y: i32) -> usize {
|
||||
(y as usize * 80) + x as usize
|
||||
}
|
||||
|
||||
pub fn apply_room(&mut self, room: &Rect) {
|
||||
(room.y1 + 1..=room.y2).into_iter().for_each(|y| {
|
||||
(room.x1 + 1..=room.x2).into_iter().for_each(|x| {
|
||||
let idx = self.xy_idx(x, y);
|
||||
self.tiles[idx] = TileType::Floor;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn apply_horiz_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
||||
(min(x1, x2)..=max(x1, x2)).into_iter().for_each(|x| {
|
||||
let idx = self.xy_idx(x, y);
|
||||
if idx > 0
|
||||
&& idx < self.width as usize * self.height as usize
|
||||
{
|
||||
self.tiles[idx as usize] = TileType::Floor;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn apply_vert_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
|
||||
(min(y1, y2)..=max(y1, y2)).into_iter().for_each(|y| {
|
||||
let idx = self.xy_idx(x, y);
|
||||
if idx > 0
|
||||
&& idx < self.width as usize * self.height as usize
|
||||
{
|
||||
self.tiles[idx as usize] = TileType::Floor;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn new_room_corridors() -> Self {
|
||||
let mut map = Self {
|
||||
tiles: vec![TileType::Wall; 80 * 50],
|
||||
rooms: vec![],
|
||||
width: 80,
|
||||
height: 50,
|
||||
};
|
||||
|
||||
const MAX_ROOMS: i32 = 30;
|
||||
const MIN_SIZE: i32 = 6;
|
||||
const MAX_SIZE: i32 = 10;
|
||||
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
|
||||
(0..MAX_ROOMS).into_iter().for_each(|_| {
|
||||
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let x = rng.roll_dice(1, 80 - w - 1) - 1;
|
||||
let y = rng.roll_dice(1, 50 - h - 1) - 1;
|
||||
|
||||
let new_room = Rect::new(x, y, w, h);
|
||||
let mut ok = true;
|
||||
map.rooms.iter().for_each(|other| {
|
||||
if new_room.intersect(other) {
|
||||
ok = false;
|
||||
}
|
||||
});
|
||||
|
||||
if ok {
|
||||
map.apply_room(&new_room);
|
||||
|
||||
if !map.rooms.is_empty() {
|
||||
let (new_x, new_y) = new_room.center();
|
||||
let (prev_x, prev_y) =
|
||||
map.rooms[map.rooms.len() - 1].center();
|
||||
|
||||
if rng.range(0, 1) == 1 {
|
||||
map.apply_horiz_tunnel(prev_x, new_x, prev_y);
|
||||
map.apply_vert_tunnel(prev_y, new_y, new_x);
|
||||
} else {
|
||||
map.apply_vert_tunnel(prev_y, new_y, prev_x);
|
||||
map.apply_horiz_tunnel(prev_x, new_x, new_y);
|
||||
}
|
||||
}
|
||||
map.rooms.push(new_room);
|
||||
}
|
||||
});
|
||||
|
||||
map
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn new_test() -> Vec<TileType> {
|
||||
let mut map = vec![TileType::Floor; 80 * 50];
|
||||
|
||||
(0..80).into_iter().for_each(|x| {
|
||||
map[xy_idx(x, 0)] = TileType::Wall;
|
||||
map[xy_idx(x, 49)] = TileType::Wall;
|
||||
map[Map::xy_idx(x, 0)] = TileType::Wall;
|
||||
map[Map::xy_idx(x, 49)] = TileType::Wall;
|
||||
});
|
||||
|
||||
(0..50).into_iter().for_each(|y| {
|
||||
map[xy_idx(0, y)] = TileType::Wall;
|
||||
map[xy_idx(79, y)] = TileType::Wall;
|
||||
map[Map::xy_idx(0, y)] = TileType::Wall;
|
||||
map[Map::xy_idx(79, y)] = TileType::Wall;
|
||||
});
|
||||
|
||||
//rando-splat of walls
|
||||
|
@ -28,60 +121,15 @@ pub fn new_test() -> Vec<TileType> {
|
|||
(0..400).into_iter().for_each(|_| {
|
||||
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) {
|
||||
let idx = Map::xy_idx(x, y);
|
||||
if idx != Map::xy_idx(40, 25) {
|
||||
map[idx] = TileType::Wall;
|
||||
}
|
||||
});
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
pub fn new_room_corridors() -> (Vec<Rect>, Vec<TileType>) {
|
||||
let mut map = vec![TileType::Wall; 80 * 50];
|
||||
|
||||
let mut rooms: Vec<Rect> = Vec::new();
|
||||
const MAX_ROOMS: i32 = 30;
|
||||
const MIN_SIZE: i32 = 6;
|
||||
const MAX_SIZE: i32 = 10;
|
||||
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
|
||||
(0..MAX_ROOMS).into_iter().for_each(|_| {
|
||||
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let x = rng.roll_dice(1, 80 - w - 1) - 1;
|
||||
let y = rng.roll_dice(1, 50 - h - 1) - 1;
|
||||
|
||||
let new_room = Rect::new(x, y, w, h);
|
||||
let mut ok = true;
|
||||
rooms.iter().for_each(|other| {
|
||||
if new_room.intersect(other) {
|
||||
ok = false;
|
||||
}
|
||||
});
|
||||
|
||||
if ok {
|
||||
apply_room(&new_room, &mut map);
|
||||
|
||||
if !rooms.is_empty() {
|
||||
let (new_x, new_y) = new_room.center();
|
||||
let (prev_x, prev_y) = rooms[rooms.len() - 1].center();
|
||||
|
||||
if rng.range(0, 1) == 1 {
|
||||
apply_horiz_tunnel(&mut map, prev_x, new_x, prev_y);
|
||||
apply_vert_tunnel(&mut map, prev_y, new_y, new_x);
|
||||
} else {
|
||||
apply_vert_tunnel(&mut map, prev_y, new_y, prev_x);
|
||||
apply_horiz_tunnel(&mut map, prev_x, new_x, new_y);
|
||||
}
|
||||
}
|
||||
rooms.push(new_room);
|
||||
}
|
||||
});
|
||||
|
||||
(rooms, map)
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn draw(map: &[TileType], ctx: &mut Rltk) {
|
||||
let mut y = 0;
|
||||
|
@ -115,29 +163,3 @@ pub fn draw(map: &[TileType], ctx: &mut Rltk) {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn apply_room(room: &Rect, map: &mut [TileType]) {
|
||||
(room.y1 + 1..=room.y2).into_iter().for_each(|y| {
|
||||
(room.x1 + 1..=room.x2).into_iter().for_each(|x| {
|
||||
map[xy_idx(x, y)] = TileType::Floor;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn apply_horiz_tunnel(map: &mut [TileType], x1: i32, x2: i32, y: i32) {
|
||||
(min(x1, x2)..=max(x1, x2)).into_iter().for_each(|x| {
|
||||
let idx = xy_idx(x, y);
|
||||
if idx > 0 && idx < 80 * 50 {
|
||||
map[idx as usize] = TileType::Floor;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn apply_vert_tunnel(map: &mut [TileType], y1: i32, y2: i32, x: i32) {
|
||||
(min(y1, y2)..=max(y1, y2)).into_iter().for_each(|y| {
|
||||
let idx = xy_idx(x, y);
|
||||
if idx > 0 && idx < 80 * 50 {
|
||||
map[idx as usize] = TileType::Floor;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,19 +2,18 @@ use rltk::{Rltk, VirtualKeyCode};
|
|||
use specs::prelude::*;
|
||||
|
||||
use crate::entity::{Player, Position, TileType};
|
||||
use crate::map;
|
||||
use crate::player;
|
||||
use crate::map::Map;
|
||||
use crate::state::State;
|
||||
|
||||
pub fn try_move(dx: i32, dy: i32, ecs: &mut World) {
|
||||
let mut posns = ecs.write_storage::<Position>();
|
||||
let mut players = ecs.write_storage::<Player>();
|
||||
let map = ecs.fetch::<Vec<TileType>>();
|
||||
let map = ecs.fetch::<Map>();
|
||||
|
||||
(&mut players, &mut posns).join().into_iter().for_each(
|
||||
|(_player, pos)| {
|
||||
let dest_idx = map::xy_idx(pos.x + dx, pos.y + dy);
|
||||
if map[dest_idx] != TileType::Wall {
|
||||
let dest_idx = map.xy_idx(pos.x + dx, pos.y + dy);
|
||||
if map.tiles[dest_idx] != TileType::Wall {
|
||||
pos.x += dx;
|
||||
pos.y += dy;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Rect {
|
||||
pub x1: i32,
|
||||
pub x2: i32,
|
||||
|
|
Loading…
Reference in New Issue