Refactor map module

This commit is contained in:
Timothy Warren 2021-10-25 14:23:19 -04:00
parent cde079a029
commit 8e23e09b3a
3 changed files with 95 additions and 110 deletions

View File

@ -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::<Vec<TileType>>();
draw_map(&map, ctx);
draw_map(&self.ecs, ctx);
let positions = self.ecs.read_storage::<Position>();
let renderables = self.ecs.read_storage::<Renderable>();
@ -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::<Renderable>();
gs.ecs.register::<Player>();
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 })

View File

@ -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,46 +9,57 @@ 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<TileType>,
pub rooms: Vec<Rect>,
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<TileType> {
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;
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;
}
}
}
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 idx > 0 && idx < self.width as usize * self.height as usize {
self.tiles[idx as usize] = TileType::Floor;
}
}
}
pub fn new_map_rooms_and_corridors() -> (Vec<Rect>, Vec<TileType>) {
let mut map = vec![TileType::Wall; 80 * 50];
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
};
let mut rooms: Vec<Rect> = Vec::new();
const MAX_ROOMS :i32 = 30;
const MIN_SIZE :i32 = 6;
const MAX_SIZE :i32 = 10;
@ -63,68 +75,43 @@ pub fn new_map_rooms_and_corridors() -> (Vec<Rect>, Vec<TileType>) {
let new_room = Rect::new(x, y, w, h);
let mut ok = true;
for other_room in rooms.iter() {
for other_room in map.rooms.iter() {
if new_room.intersect(other_room) {
ok = false;
}
}
if ok {
apply_room_to_map(&new_room, &mut map);
map.apply_room_to_map(&new_room);
if ! rooms.is_empty() {
if ! map.rooms.is_empty() {
let (new_x, new_y) = new_room.center();
let (prev_x, prev_y) = rooms[rooms.len()-1].center();
let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
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);
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
map.apply_vertical_tunnel(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);
map.apply_vertical_tunnel(prev_y, new_y, prev_x);
map.apply_horizontal_tunnel(prev_x, new_x, new_y);
}
}
rooms.push(new_room);
map.rooms.push(new_room);
}
}
(rooms, map)
}
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;
}
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::<Map>();
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(

View File

@ -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::<Position>();
let mut players = ecs.write_storage::<Player>();
let map = ecs.fetch::<Vec<TileType>>();
let map = ecs.fetch::<Map>();
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));
}