From 8e23e09b3a06118b3e5afda65af7541c1eb538f8 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 25 Oct 2021 14:23:19 -0400 Subject: [PATCH] Refactor map module --- src/main.rs | 20 +++--- src/map.rs | 177 +++++++++++++++++++++++--------------------------- src/player.rs | 8 +-- 3 files changed, 95 insertions(+), 110 deletions(-) diff --git a/src/main.rs b/src/main.rs index ae71e34..bee6662 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,12 @@ pub struct State { ecs: World, } +impl State { + fn run_systems(&mut self) { + self.ecs.maintain(); + } +} + impl GameState for State { fn tick(&mut self, ctx: &mut Rltk) { ctx.cls(); @@ -21,8 +27,7 @@ impl GameState for State { player_input(self, ctx); self.run_systems(); - let map = self.ecs.fetch::>(); - draw_map(&map, ctx); + draw_map(&self.ecs, ctx); let positions = self.ecs.read_storage::(); let renderables = self.ecs.read_storage::(); @@ -33,12 +38,6 @@ impl GameState for State { } } -impl State { - fn run_systems(&mut self) { - self.ecs.maintain(); - } -} - fn main() -> rltk::BError { use rltk::RltkBuilder; @@ -52,12 +51,11 @@ fn main() -> rltk::BError { gs.ecs.register::(); gs.ecs.register::(); - let (rooms, map) = new_map_rooms_and_corridors(); + let map: Map = Map::new_map_rooms_and_corridors(); + let (player_x, player_y) = map.rooms[0].center(); gs.ecs.insert(map); - let (player_x, player_y) = rooms[0].center(); - gs.ecs .create_entity() .with(Position { x: player_x, y: player_y }) diff --git a/src/map.rs b/src/map.rs index d200e71..86664d1 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,6 +1,7 @@ use rltk::{ RGB, Rltk, RandomNumberGenerator }; use super::{Rect}; use std::cmp::{min, max}; +use specs::prelude::*; #[derive(PartialEq, Copy, Clone)] pub enum TileType { @@ -8,123 +9,109 @@ pub enum TileType { Floor, } -pub fn xy_idx(x: i32, y: i32) -> usize { - (y as usize * 80) + x as usize +pub struct Map { + pub tiles: Vec, + pub rooms: Vec, + pub width: i32, + pub height: i32, } -/// Makes a map with solid boundaries and 400 randomly placed walls. -/// No guarantees that it won't look awful. -pub fn new_map_text() -> Vec { - let mut map = vec![TileType::Floor; 80 * 50]; - - // Make the boundary 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; +impl Map { + pub fn xy_idx(&self, x: i32, y: i32) -> usize { + (y as usize * self.width as usize) + x as usize } - // Now randomly add a bunch of walls. - // First get the random number generator - 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 -} - -pub fn new_map_rooms_and_corridors() -> (Vec, Vec) { - let mut map = vec![TileType::Wall; 80 * 50]; - - let mut rooms: Vec = Vec::new(); - const MAX_ROOMS :i32 = 30; - const MIN_SIZE :i32 = 6; - const MAX_SIZE :i32 = 10; - - let mut rng = RandomNumberGenerator::new(); - - for _ in 0..MAX_ROOMS { - 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; - - for other_room in rooms.iter() { - if new_room.intersect(other_room) { - ok = false; + fn apply_room_to_map(&mut self, room: &Rect) { + for y in room.y1+1 ..= room.y2 { + for x in room.x1+1 ..= room.x2 { + let idx = self.xy_idx(x, y); + self.tiles[idx] = TileType::Floor; } } + } - if ok { - apply_room_to_map(&new_room, &mut map); + fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) { + for x in min(x1, x2) ..= max(x1, x2) { + let idx = self.xy_idx(x, y); - if ! rooms.is_empty() { - let (new_x, new_y) = new_room.center(); - let (prev_x, prev_y) = rooms[rooms.len()-1].center(); + if idx > 0 && idx < self.width as usize * self.height as usize { + self.tiles[idx as usize] = TileType::Floor; + } + } + } - if rng.range(0, 2) == 1 { - apply_horizontal_tunnel(&mut map, prev_x, new_x, prev_y); - apply_vertical_tunnel(&mut map, prev_y, new_y, new_x); - } else { - apply_vertical_tunnel(&mut map, prev_y, new_y, prev_x); - apply_horizontal_tunnel(&mut map, prev_x, new_x, new_y); + fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) { + for y in min(y1, y2) ..= max(y1, y2) { + 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; + } + } + } + + /// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/ + /// This gives a handful of random rooms and corridors joining them together + pub fn new_map_rooms_and_corridors() -> Map { + let mut map = Map { + tiles: vec![TileType::Wall; 80 * 50], + rooms: Vec::new(), + width: 80, + height: 50 + }; + + const MAX_ROOMS :i32 = 30; + const MIN_SIZE :i32 = 6; + const MAX_SIZE :i32 = 10; + + let mut rng = RandomNumberGenerator::new(); + + for _ in 0..MAX_ROOMS { + 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; + + for other_room in map.rooms.iter() { + if new_room.intersect(other_room) { + ok = false; } } - rooms.push(new_room); - } - } + if ok { + map.apply_room_to_map(&new_room); - (rooms, map) -} + 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(); -fn apply_room_to_map(room: &Rect, map: &mut [TileType]) { - for y in room.y1+1 ..= room.y2 { - for x in room.x1+1 ..= room.x2 { - map[xy_idx(x, y)] = TileType::Floor; + if rng.range(0, 2) == 1 { + map.apply_horizontal_tunnel(prev_x, new_x, prev_y); + map.apply_vertical_tunnel(prev_y, new_y, new_x); + } else { + map.apply_vertical_tunnel(prev_y, new_y, prev_x); + map.apply_horizontal_tunnel(prev_x, new_x, new_y); + } + } + + map.rooms.push(new_room); + } } + + map } } -fn apply_horizontal_tunnel(map: &mut [TileType], x1: i32, x2: i32, y: i32) { - for x in min(x1, x2) ..= max(x1, x2) { - let idx = xy_idx(x, y); +pub fn draw_map(ecs: &World, ctx: &mut Rltk) { + let map = ecs.fetch::(); - if idx > 0 && idx < 80*50 { - map[idx as usize] = TileType::Floor; - } - } -} - -fn apply_vertical_tunnel(map: &mut [TileType], y1: i32, y2: i32, x: i32) { - for y in min(y1, y2) ..= max(y1, y2) { - let idx = xy_idx(x, y); - - if idx > 0 && idx < 80*50 { - map[idx as usize] = TileType::Floor; - } - } -} - -pub fn draw_map(map: &[TileType], ctx: &mut Rltk) { let mut y = 0; let mut x = 0; - for tile in map.iter() { + for (_idx, tile) in map.tiles.iter().enumerate() { match tile { TileType::Floor => { ctx.set( diff --git a/src/player.rs b/src/player.rs index dc90414..dc5a4d9 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,16 +1,16 @@ use rltk::{VirtualKeyCode, Rltk}; use specs::prelude::*; -use super::{Position, Player, TileType, xy_idx, State}; +use super::{Position, Player, TileType, State, Map}; use std::cmp::{min, max}; pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { let mut positions = ecs.write_storage::(); let mut players = ecs.write_storage::(); - let map = ecs.fetch::>(); + let map = ecs.fetch::(); for (_player, pos) in (&mut players, &mut positions).join() { - let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y); - if map[destination_idx] != TileType::Wall { + let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); + if map.tiles[destination_idx] != TileType::Wall { pos.x = min(79, max(0, pos.x + delta_x)); pos.y = min(49, max(0, pos.y + delta_y)); }