Use mapgen::Map directly.

This commit is contained in:
Nolan Darilek 2021-06-09 14:53:48 -05:00
parent 173630edab
commit 2635dcbb9c
5 changed files with 34 additions and 69 deletions

View File

@ -284,9 +284,9 @@ fn exploration_focus<A: 'static>(
}
if orig != exploring
&& exploring.0 >= 0.
&& exploring.0 < map.width() as f32
&& exploring.0 < map.width as f32
&& exploring.1 >= 0.
&& exploring.1 < map.height() as f32
&& exploring.1 < map.height as f32
{
commands.entity(entity).insert(Exploring(exploring));
}
@ -308,7 +308,7 @@ fn navigate_to_explored<A: 'static>(
for (entity, exploring) in explorers.iter() {
for (map, revealed_tiles) in map.iter() {
let point = **exploring;
let idx = point.to_index(map.width());
let idx = point.to_index(map.width);
let known = revealed_tiles[idx];
if input.just_active(navigate_to_explored.clone()) && known {
commands
@ -338,7 +338,7 @@ fn exploration_changed_announcement(
let coordinates = (coordinates.0.floor(), coordinates.1.floor());
for (map, revealed_tiles, visible_tiles) in map.iter() {
let point = **exploring;
let idx = point.to_index(map.width());
let idx = point.to_index(map.width);
let shape = Cuboid::new(Vec2::new(0.5, 0.5).into());
let known = revealed_tiles[idx];
let visible = visible_tiles[idx];
@ -374,7 +374,7 @@ fn exploration_changed_announcement(
},
);
if tokens.is_empty() {
match map.base.tiles[idx] {
match map.tiles[idx] {
TileType::Floor => "Floor".to_string(),
TileType::Wall => "Wall".to_string(),
}

View File

@ -3,7 +3,8 @@ use std::collections::{HashMap, HashSet};
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use derive_more::{Deref, DerefMut};
use mapgen::{geometry::Rect as MRect, Map as MapgenMap, MapFilter, TileType};
pub use mapgen::Map;
use mapgen::{geometry::Rect as MRect, MapFilter, TileType};
use maze_generator::{prelude::*, recursive_backtracking::RbGenerator};
use rand::prelude::StdRng;
@ -26,38 +27,6 @@ pub struct Areas(pub Vec<Area>);
#[reflect(Component)]
pub struct Portal;
#[derive(Clone, Default)]
pub struct Map {
pub base: MapgenMap,
}
impl Map {
pub fn new(base: MapgenMap) -> Self {
let count = (base.width * base.height) as usize;
Self { base }
}
pub fn width(&self) -> usize {
self.base.width
}
pub fn height(&self) -> usize {
self.base.height
}
pub fn count(&self) -> usize {
self.width() * self.height()
}
pub fn start(&self) -> Option<mapgen::geometry::Point> {
self.base.starting_point
}
pub fn exit(&self) -> Option<mapgen::geometry::Point> {
self.base.exit_point
}
}
pub trait ITileType {
fn blocks_motion(&self) -> bool;
fn blocks_visibility(&self) -> bool;
@ -153,7 +122,7 @@ impl GridBuilder {
}
impl MapFilter for GridBuilder {
fn modify_map(&self, _rng: &mut StdRng, map: &MapgenMap) -> MapgenMap {
fn modify_map(&self, _rng: &mut StdRng, map: &Map) -> Map {
let mut map = map.clone();
let mut generator = RbGenerator::new(None);
let maze = generator.generate(self.width_in_rooms as i32, self.height_in_rooms as i32);
@ -210,9 +179,9 @@ fn add_map_colliders(mut commands: Commands, maps: Query<(Entity, &Map), Added<M
..Default::default()
})
.id();
for x in 0..map.width() {
for y in 0..map.height() {
let tile = map.base.at(x, y);
for x in 0..map.width {
for y in 0..map.height {
let tile = map.at(x, y);
if tile.blocks_motion() {
let id = commands
.spawn()
@ -242,28 +211,24 @@ fn portal_spawner(
for (entity, map) in map.iter() {
if config.autospawn_portals {
let mut portals: Vec<(f32, f32)> = vec![];
for x in 1..map.width() {
for y in 1..map.height() {
for x in 1..map.width {
for y in 1..map.height {
let mut spawn_portal = false;
if map.base.get_available_exits(x, y).len() > 2 {
let idx = (x, y).to_index(map.width());
if map.base.tiles[idx] == TileType::Floor
&& (x > 1 && map.base.tiles[idx - 1] == TileType::Floor)
&& (x < map.width() - 2 && map.base.tiles[idx + 1] == TileType::Floor)
&& (y > 1
&& map.base.tiles[idx - map.width() as usize] == TileType::Wall)
&& (y < map.height() - 2
&& map.base.tiles[idx + map.width() as usize] == TileType::Wall)
if map.get_available_exits(x, y).len() > 2 {
let idx = (x, y).to_index(map.width);
if map.tiles[idx] == TileType::Floor
&& (x > 1 && map.tiles[idx - 1] == TileType::Floor)
&& (x < map.width - 2 && map.tiles[idx + 1] == TileType::Floor)
&& (y > 1 && map.tiles[idx - map.width] == TileType::Wall)
&& (y < map.height - 2 && map.tiles[idx + map.width] == TileType::Wall)
{
spawn_portal = true;
}
if map.base.tiles[idx] == TileType::Floor
&& (x > 1 && map.base.tiles[idx - 1] == TileType::Wall)
&& (x < map.width() - 2 && map.base.tiles[idx + 1] == TileType::Wall)
&& (y > 1
&& map.base.tiles[idx - map.width() as usize] == TileType::Floor)
&& (y < map.height() - 2
&& map.base.tiles[idx + map.width() as usize] == TileType::Floor)
if map.tiles[idx] == TileType::Floor
&& (x > 1 && map.tiles[idx - 1] == TileType::Wall)
&& (x < map.width - 2 && map.tiles[idx + 1] == TileType::Wall)
&& (y > 1 && map.tiles[idx - map.width] == TileType::Floor)
&& (y < map.height - 2 && map.tiles[idx + map.width] == TileType::Floor)
{
spawn_portal = true;
}
@ -357,7 +322,7 @@ fn area_description(
fn add_areas(mut commands: Commands, query: Query<(Entity, &Map), (Added<Map>, Without<Areas>)>) {
for (entity, map) in query.iter() {
let mut v = vec![];
for room in &map.base.rooms {
for room in &map.rooms {
v.push(Area {
rect: *room,
description: None,

View File

@ -31,7 +31,7 @@ pub fn find_path(
&start.into(),
|p| {
let mut successors: Vec<((i32, i32), u32)> = vec![];
for tile in map.base.get_available_exits(p.0 as usize, p.1 as usize) {
for tile in map.get_available_exits(p.0 as usize, p.1 as usize) {
successors.push(((tile.0 as i32, tile.1 as i32), (tile.2 * 100.) as u32));
}
successors

View File

@ -163,11 +163,11 @@ fn sound_icon<S>(
) where
S: Component + Clone + Debug + Eq + Hash,
{
if !(*config).sound_icon_states.is_empty() {
if !config.sound_icon_states.contains(state.current()) {
if !(*config).sound_icon_states.is_empty()
&& !config.sound_icon_states.contains(state.current())
{
return;
}
}
for viewer in viewers.iter() {
for (mut icon, coordinates, parent, mut sound) in icons.iter_mut() {
let coords = if let Some(coordinates) = coordinates {

View File

@ -55,7 +55,7 @@ fn add_visibility_indices(
map_config: Res<MapConfig>,
) {
for (entity, map) in query.iter() {
let count = map.count();
let count = map.width * map.height;
commands
.entity(entity)
.insert(VisibleTiles(vec![false; count]));
@ -76,7 +76,7 @@ where
type Opacity = u8;
fn size(&self, grid: &Self::Grid) -> Size {
Size::new(grid.0.width() as u32, grid.0.height() as u32)
Size::new(grid.0.width as u32, grid.0.height as u32)
}
fn get_opacity(&self, _grid: &Self::Grid, coord: Coord) -> Self::Opacity {
@ -139,7 +139,7 @@ fn update_visible_and_revealed_tiles(
*t = false
}
for v in viewshed.visible.iter() {
let idx = v.to_index(map.width());
let idx = v.to_index(map.width);
revealed_tiles[idx] = true;
visible_tiles[idx] = true;
}