Refactor map

This commit is contained in:
Kasper Juul Hermansen 2022-01-29 20:21:13 +01:00
parent 0af23af3d3
commit fa71410453
Signed by: kjuulh
GPG Key ID: DCD9397082D97069
7 changed files with 89 additions and 53 deletions

File diff suppressed because one or more lines are too long

View File

@ -91,7 +91,7 @@ impl<'a> System<'a> for ItemUseSystem {
healing,
inflicts_damage,
_ranged,
mut map,
map,
mut suffer_damage,
aoe,
mut confused,

View File

@ -30,7 +30,7 @@ mod healing_system;
mod hunger_system;
mod inventory_system;
mod map;
mod map_builders;
pub mod map_builders;
mod map_indexing_system;
mod melee_combat_system;
mod monster_ai_system;
@ -84,20 +84,22 @@ impl State {
}
// Build a new map and place the player
let mut map_builder = map_builders::new_random_builder(1);
let worldmap;
let player_start;
{
let mut worldmap_resource = self.ecs.write_resource::<Map>();
*worldmap_resource = map_builders::build_random_map(1);
map_builder.build_map();
*worldmap_resource = map_builder.get_map();
player_start = map_builder.get_starting_position();
worldmap = worldmap_resource.clone();
}
// Spawn bad guys
for room in worldmap.rooms.iter().skip(1) {
spawner::spawn_room(&mut self.ecs, room, 1);
}
map_builder.spawn_entities(&mut self.ecs);
// Place the player and update resources
let (player_x, player_y) = worldmap.rooms[0].center();
let (player_x, player_y) = (player_start.x, player_start.y);
let player_entity = spawner::player(&mut self.ecs, player_x, player_y);
let mut player_position = self.ecs.write_resource::<Point>();
*player_position = Point::new(player_x, player_y);
@ -165,20 +167,23 @@ impl State {
.expect("Unable to delete entity")
}
let mut map_builder;
let worldmap;
let current_depth;
let player_start;
{
let mut worldmap_resource = self.ecs.write_resource::<Map>();
current_depth = worldmap_resource.depth;
*worldmap_resource = map_builders::build_random_map(current_depth + 1);
map_builder = map_builders::new_random_builder(current_depth + 1);
map_builder.build_map();
*worldmap_resource = map_builder.get_map();
player_start = map_builder.get_starting_position();
worldmap = worldmap_resource.clone();
}
for room in worldmap.rooms.iter().skip(1) {
spawner::spawn_room(&mut self.ecs, room, current_depth + 1);
}
map_builder.spawn_entities(&mut self.ecs);
let (player_x, player_y) = worldmap.rooms[0].center();
let (player_x, player_y) = (player_start.x, player_start.y);
let mut player_position = self.ecs.write_resource::<Point>();
*player_position = Point::new(player_x, player_y);
let mut position_components = self.ecs.write_storage::<Position>();
@ -513,14 +518,14 @@ fn main() -> rltk::BError {
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
let map = map_builders::build_random_map(1);
let (player_x, player_y) = map.rooms[0].center();
let mut map_builder = map_builders::new_random_builder(1);
map_builder.build_map();
let player_pos = map_builder.get_starting_position();
let mut map = map_builder.get_map();
let (player_x, player_y) = (player_pos.x, player_pos.y);
let player_entity = spawner::player(&mut gs.ecs, player_x, player_y);
for room in map.rooms.iter().skip(1) {
spawner::spawn_room(&mut gs.ecs, room, 1);
}
map_builder.spawn_entities(&mut gs.ecs);
gs.ecs.insert(map);
gs.ecs.insert(Point::new(player_x, player_y));

View File

@ -1,7 +1,6 @@
use std::cmp::{max, min};
use std::collections::HashSet;
use rltk::{Algorithm2D, BaseMap, FontCharType, Point, RandomNumberGenerator, Rltk, RGB};
use rltk::{Algorithm2D, BaseMap, FontCharType, Point, RGB, Rltk};
use serde::{Deserialize, Serialize};
use specs::prelude::*;
@ -22,7 +21,6 @@ pub const MAX_MONSTER: i32 = 4;
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct Map {
pub tiles: Vec<TileType>,
pub rooms: Vec<Rect>,
pub width: i32,
pub height: i32,
pub revealed_tiles: Vec<bool>,
@ -64,7 +62,6 @@ impl Map {
pub fn new(new_depth: i32) -> Map {
Map {
tiles: vec![TileType::Wall; MAP_COUNT],
rooms: Vec::new(),
width: MAP_WIDTH as i32,
height: MAP_HEIGHT as i32,
revealed_tiles: vec![false; MAP_COUNT],

View File

@ -1,13 +1,18 @@
use specs::World;
use crate::{Map, Position};
use crate::map_builders::simple_map::SimpleMapBuilder;
use crate::Map;
mod common;
mod simple_map;
pub mod common;
pub mod simple_map;
trait MapBuilder {
fn build(new_depth: i32) -> Map;
pub trait MapBuilder {
fn build_map(&mut self);
fn spawn_entities(&mut self, ecs: &mut World);
fn get_map(&self) -> Map;
fn get_starting_position(&self) -> Position;
}
pub fn build_random_map(new_depth: i32) -> Map {
SimpleMapBuilder::build(new_depth)
}
pub fn new_random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
Box::new(SimpleMapBuilder::new(new_depth))
}

View File

@ -1,14 +1,28 @@
use rltk::RandomNumberGenerator;
use specs::prelude::*;
use crate::{Map, Position, spawner, TileType};
use crate::map_builders::{common, MapBuilder};
use crate::rect::Rect;
use crate::{Map, TileType};
pub struct SimpleMapBuilder {}
pub struct SimpleMapBuilder {
map: Map,
starting_position: Position,
depth: i32,
rooms: Vec<Rect>,
}
impl SimpleMapBuilder {
fn rooms_and_corridors(map: &mut Map) {
pub fn new(new_depth: i32) -> Self {
Self {
starting_position: Position { x: 0, y: 0 },
map: Map::new(new_depth),
depth: new_depth,
rooms: Vec::new(),
}
}
fn rooms_and_corridors(&mut self) {
const MAX_ROOMS: i32 = 30;
const MIN_SIZE: i32 = 6;
const MAX_SIZE: i32 = 10;
@ -18,44 +32,59 @@ impl SimpleMapBuilder {
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, map.width - w - 1) - 1;
let y = rng.roll_dice(1, map.height - h - 1) - 1;
let x = rng.roll_dice(1, self.map.width - w - 1) - 1;
let y = rng.roll_dice(1, self.map.height - h - 1) - 1;
let new_room = Rect::new(x, y, w, h);
let mut ok = true;
for other_room in map.rooms.iter() {
for other_room in self.rooms.iter() {
if new_room.intersect(other_room) {
ok = false
};
}
if ok {
common::apply_room_to_map(map, &new_room);
common::apply_room_to_map(&mut self.map, &new_room);
if !map.rooms.is_empty() {
if !self.rooms.is_empty() {
let (new_x, new_y) = new_room.center();
let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center();
let (prev_x, prev_y) = self.rooms[self.rooms.len() - 1].center();
if rng.range(0, 2) == 1 {
common::apply_horizontal_tunnel(map, prev_x, new_x, prev_y);
common::apply_vertical_tunnel(map, prev_y, new_y, new_x);
common::apply_horizontal_tunnel(&mut self.map, prev_x, new_x, prev_y);
common::apply_vertical_tunnel(&mut self.map, prev_y, new_y, new_x);
} else {
common::apply_vertical_tunnel(map, prev_y, new_y, prev_x);
common::apply_horizontal_tunnel(map, prev_x, new_x, new_y);
common::apply_vertical_tunnel(&mut self.map, prev_y, new_y, prev_x);
common::apply_horizontal_tunnel(&mut self.map, prev_x, new_x, new_y);
}
}
map.rooms.push(new_room);
self.rooms.push(new_room);
}
}
let stairs_position = map.rooms[map.rooms.len() - 1].center();
let stairs_idx = map.xy_idx(stairs_position.0, stairs_position.1);
map.tiles[stairs_idx] = TileType::DownStairs;
let stairs_position = self.rooms[self.rooms.len() - 1].center();
let stairs_idx = self.map.xy_idx(stairs_position.0, stairs_position.1);
self.map.tiles[stairs_idx] = TileType::DownStairs;
let start_pos = self.rooms[0].center();
self.starting_position = Position { x: start_pos.0, y: start_pos.1 }
}
}
impl MapBuilder for SimpleMapBuilder {
fn build(new_depth: i32) -> Map {
let mut map = Map::new(new_depth);
SimpleMapBuilder::rooms_and_corridors(&mut map);
map
fn build_map(&mut self) {
self.rooms_and_corridors();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for room in self.rooms.iter().skip(1) {
spawner::spawn_room(ecs, room, self.depth);
}
}
fn get_map(&self) -> Map {
self.map.clone()
}
fn get_starting_position(&self) -> Position {
self.starting_position.clone()
}
}

View File

@ -37,7 +37,7 @@ impl<'a> System<'a> for TriggerSystem {
inflicts_damage,
mut particle_builder,
mut inflict_damage,
mut single_activation,
single_activation,
) = data;
let mut remove_entities: Vec<Entity> = Vec::new();