Refactor map module
This commit is contained in:
parent
cde079a029
commit
8e23e09b3a
20
src/main.rs
20
src/main.rs
@ -14,6 +14,12 @@ pub struct State {
|
|||||||
ecs: World,
|
ecs: World,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn run_systems(&mut self) {
|
||||||
|
self.ecs.maintain();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl GameState for State {
|
impl GameState for State {
|
||||||
fn tick(&mut self, ctx: &mut Rltk) {
|
fn tick(&mut self, ctx: &mut Rltk) {
|
||||||
ctx.cls();
|
ctx.cls();
|
||||||
@ -21,8 +27,7 @@ impl GameState for State {
|
|||||||
player_input(self, ctx);
|
player_input(self, ctx);
|
||||||
self.run_systems();
|
self.run_systems();
|
||||||
|
|
||||||
let map = self.ecs.fetch::<Vec<TileType>>();
|
draw_map(&self.ecs, ctx);
|
||||||
draw_map(&map, ctx);
|
|
||||||
|
|
||||||
let positions = self.ecs.read_storage::<Position>();
|
let positions = self.ecs.read_storage::<Position>();
|
||||||
let renderables = self.ecs.read_storage::<Renderable>();
|
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 {
|
fn main() -> rltk::BError {
|
||||||
use rltk::RltkBuilder;
|
use rltk::RltkBuilder;
|
||||||
|
|
||||||
@ -52,12 +51,11 @@ fn main() -> rltk::BError {
|
|||||||
gs.ecs.register::<Renderable>();
|
gs.ecs.register::<Renderable>();
|
||||||
gs.ecs.register::<Player>();
|
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);
|
gs.ecs.insert(map);
|
||||||
|
|
||||||
let (player_x, player_y) = rooms[0].center();
|
|
||||||
|
|
||||||
gs.ecs
|
gs.ecs
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { x: player_x, y: player_y })
|
.with(Position { x: player_x, y: player_y })
|
||||||
|
177
src/map.rs
177
src/map.rs
@ -1,6 +1,7 @@
|
|||||||
use rltk::{ RGB, Rltk, RandomNumberGenerator };
|
use rltk::{ RGB, Rltk, RandomNumberGenerator };
|
||||||
use super::{Rect};
|
use super::{Rect};
|
||||||
use std::cmp::{min, max};
|
use std::cmp::{min, max};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
pub enum TileType {
|
pub enum TileType {
|
||||||
@ -8,123 +9,109 @@ pub enum TileType {
|
|||||||
Floor,
|
Floor,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn xy_idx(x: i32, y: i32) -> usize {
|
pub struct Map {
|
||||||
(y as usize * 80) + x as usize
|
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.
|
impl Map {
|
||||||
/// No guarantees that it won't look awful.
|
pub fn xy_idx(&self, x: i32, y: i32) -> usize {
|
||||||
pub fn new_map_text() -> Vec<TileType> {
|
(y as usize * self.width as usize) + x as usize
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now randomly add a bunch of walls.
|
fn apply_room_to_map(&mut self, room: &Rect) {
|
||||||
// First get the random number generator
|
for y in room.y1+1 ..= room.y2 {
|
||||||
let mut rng = rltk::RandomNumberGenerator::new();
|
for x in room.x1+1 ..= room.x2 {
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
for _i in 0..400 {
|
self.tiles[idx] = TileType::Floor;
|
||||||
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<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();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ok {
|
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
||||||
apply_room_to_map(&new_room, &mut map);
|
for x in min(x1, x2) ..= max(x1, x2) {
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
|
||||||
if ! rooms.is_empty() {
|
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||||
let (new_x, new_y) = new_room.center();
|
self.tiles[idx as usize] = TileType::Floor;
|
||||||
let (prev_x, prev_y) = rooms[rooms.len()-1].center();
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if rng.range(0, 2) == 1 {
|
fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
|
||||||
apply_horizontal_tunnel(&mut map, prev_x, new_x, prev_y);
|
for y in min(y1, y2) ..= max(y1, y2) {
|
||||||
apply_vertical_tunnel(&mut map, prev_y, new_y, new_x);
|
let idx = self.xy_idx(x, y);
|
||||||
} else {
|
|
||||||
apply_vertical_tunnel(&mut map, prev_y, new_y, prev_x);
|
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||||
apply_horizontal_tunnel(&mut map, prev_x, new_x, new_y);
|
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]) {
|
if rng.range(0, 2) == 1 {
|
||||||
for y in room.y1+1 ..= room.y2 {
|
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
|
||||||
for x in room.x1+1 ..= room.x2 {
|
map.apply_vertical_tunnel(prev_y, new_y, new_x);
|
||||||
map[xy_idx(x, y)] = TileType::Floor;
|
} 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) {
|
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||||
for x in min(x1, x2) ..= max(x1, x2) {
|
let map = ecs.fetch::<Map>();
|
||||||
let idx = xy_idx(x, y);
|
|
||||||
|
|
||||||
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 y = 0;
|
||||||
let mut x = 0;
|
let mut x = 0;
|
||||||
|
|
||||||
for tile in map.iter() {
|
for (_idx, tile) in map.tiles.iter().enumerate() {
|
||||||
match tile {
|
match tile {
|
||||||
TileType::Floor => {
|
TileType::Floor => {
|
||||||
ctx.set(
|
ctx.set(
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
use rltk::{VirtualKeyCode, Rltk};
|
use rltk::{VirtualKeyCode, Rltk};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use super::{Position, Player, TileType, xy_idx, State};
|
use super::{Position, Player, TileType, State, Map};
|
||||||
use std::cmp::{min, max};
|
use std::cmp::{min, max};
|
||||||
|
|
||||||
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||||
let mut positions = ecs.write_storage::<Position>();
|
let mut positions = ecs.write_storage::<Position>();
|
||||||
let mut players = ecs.write_storage::<Player>();
|
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() {
|
for (_player, pos) in (&mut players, &mut positions).join() {
|
||||||
let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
|
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||||
if map[destination_idx] != TileType::Wall {
|
if map.tiles[destination_idx] != TileType::Wall {
|
||||||
pos.x = min(79, max(0, pos.x + delta_x));
|
pos.x = min(79, max(0, pos.x + delta_x));
|
||||||
pos.y = min(49, max(0, pos.y + delta_y));
|
pos.y = min(49, max(0, pos.y + delta_y));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user