Bsp: format

This commit is contained in:
Kasper Juul Hermansen 2022-01-29 22:38:38 +01:00
parent 0511c00f09
commit c1893aec74
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
4 changed files with 49 additions and 20 deletions

View File

@ -24,7 +24,6 @@ use crate::particle_system::ParticleSpawnSystem;
use crate::state::{RunState, State};
use crate::trigger_system::TriggerSystem;
mod components;
mod damage_system;
mod gamelog;

View File

@ -4,8 +4,6 @@ use rltk::{Algorithm2D, BaseMap, FontCharType, Point, Rltk, RGB};
use serde::{Deserialize, Serialize};
use specs::prelude::*;
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum TileType {
Wall,

View File

@ -1,9 +1,9 @@
use rltk::{Point, RandomNumberGenerator};
use crate::{Map, Position, SHOW_MAPGEN_VISUALIZER, spawner, TileType, World};
use crate::map_builders::common::apply_room_to_map;
use crate::map_builders::MapBuilder;
use crate::rect::Rect;
use crate::{spawner, Map, Position, TileType, World, SHOW_MAPGEN_VISUALIZER};
pub struct BspDungeonBuilder {
map: Map,
@ -37,7 +37,8 @@ impl BspDungeonBuilder {
}
fn place_rooms(&mut self, rng: &mut RandomNumberGenerator) {
self.rects.push(Rect::new(2, 2, self.map.width - 5, self.map.height - 5));
self.rects
.push(Rect::new(2, 2, self.map.width - 5, self.map.height - 5));
let first_room = self.rects[0];
self.add_subrects(first_room);
@ -65,8 +66,10 @@ impl BspDungeonBuilder {
let next_room = self.rooms[i + 1];
let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2)) - 1);
let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2)) - 1);
let end_x = next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1);
let end_y = next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1);
let end_x =
next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1);
let end_y =
next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1);
self.draw_corridor(start_x, start_y, end_x, end_y);
self.take_snapshot();
}
@ -74,7 +77,10 @@ impl BspDungeonBuilder {
fn place_start(&mut self) {
let start = self.rooms[0].center();
self.starting_position = Position { x: start.0, y: start.1 };
self.starting_position = Position {
x: start.0,
y: start.1,
};
}
fn place_stairs(&mut self) {
@ -89,13 +95,31 @@ impl BspDungeonBuilder {
let half_width = i32::max(width / 2, 1);
let half_height = i32::max(height / 2, 1);
self.rects.push(Rect::new(rect.x1, rect.y1, half_width, half_height));
self.rects.push(Rect::new(rect.x1, rect.y1 + half_height, half_width, half_height));
self.rects.push(Rect::new(rect.x1 + half_width, rect.y1, half_width, half_height));
self.rects.push(Rect::new(rect.x1 + half_width, rect.y1 + half_height, half_width, half_height));
self.rects
.push(Rect::new(rect.x1, rect.y1, half_width, half_height));
self.rects.push(Rect::new(
rect.x1,
rect.y1 + half_height,
half_width,
half_height,
));
self.rects.push(Rect::new(
rect.x1 + half_width,
rect.y1,
half_width,
half_height,
));
self.rects.push(Rect::new(
rect.x1 + half_width,
rect.y1 + half_height,
half_width,
half_height,
));
}
fn get_random_rect(&mut self, rng: &mut RandomNumberGenerator) -> Rect {
if self.rects.len() == 1 { return self.rects[0]; }
if self.rects.len() == 1 {
return self.rects[0];
}
let idx = (rng.roll_dice(1, self.rects.len() as i32) - 1) as usize;
self.rects[idx]
}
@ -127,10 +151,18 @@ impl BspDungeonBuilder {
for y in expanded.y1..=expanded.y2 {
for x in expanded.x1..=expanded.x2 {
if x > self.map.width - 2 { can_build = false; }
if y > self.map.height - 2 { can_build = false; }
if x < 1 { can_build = false; }
if y < 1 { can_build = false; }
if x > self.map.width - 2 {
can_build = false;
}
if y > self.map.height - 2 {
can_build = false;
}
if x < 1 {
can_build = false;
}
if y < 1 {
can_build = false;
}
if can_build {
let idx = self.map.xy_idx(x, y);
if self.map.tiles[idx] != TileType::Wall {

View File

@ -1,12 +1,12 @@
use specs::World;
use crate::map_builders::bsp_dungeon::BspDungeonBuilder;
use crate::map_builders::simple_map::SimpleMapBuilder;
use crate::{Map, Position};
use crate::map_builders::bsp_dungeon::BspDungeonBuilder;
mod bsp_dungeon;
mod common;
mod simple_map;
mod bsp_dungeon;
pub trait MapBuilder {
fn build_map(&mut self);