Use mapgen::Map
directly.
This commit is contained in:
parent
173630edab
commit
2635dcbb9c
|
@ -284,9 +284,9 @@ fn exploration_focus<A: 'static>(
|
||||||
}
|
}
|
||||||
if orig != exploring
|
if orig != exploring
|
||||||
&& exploring.0 >= 0.
|
&& exploring.0 >= 0.
|
||||||
&& exploring.0 < map.width() as f32
|
&& exploring.0 < map.width as f32
|
||||||
&& exploring.1 >= 0.
|
&& exploring.1 >= 0.
|
||||||
&& exploring.1 < map.height() as f32
|
&& exploring.1 < map.height as f32
|
||||||
{
|
{
|
||||||
commands.entity(entity).insert(Exploring(exploring));
|
commands.entity(entity).insert(Exploring(exploring));
|
||||||
}
|
}
|
||||||
|
@ -308,7 +308,7 @@ fn navigate_to_explored<A: 'static>(
|
||||||
for (entity, exploring) in explorers.iter() {
|
for (entity, exploring) in explorers.iter() {
|
||||||
for (map, revealed_tiles) in map.iter() {
|
for (map, revealed_tiles) in map.iter() {
|
||||||
let point = **exploring;
|
let point = **exploring;
|
||||||
let idx = point.to_index(map.width());
|
let idx = point.to_index(map.width);
|
||||||
let known = revealed_tiles[idx];
|
let known = revealed_tiles[idx];
|
||||||
if input.just_active(navigate_to_explored.clone()) && known {
|
if input.just_active(navigate_to_explored.clone()) && known {
|
||||||
commands
|
commands
|
||||||
|
@ -338,7 +338,7 @@ fn exploration_changed_announcement(
|
||||||
let coordinates = (coordinates.0.floor(), coordinates.1.floor());
|
let coordinates = (coordinates.0.floor(), coordinates.1.floor());
|
||||||
for (map, revealed_tiles, visible_tiles) in map.iter() {
|
for (map, revealed_tiles, visible_tiles) in map.iter() {
|
||||||
let point = **exploring;
|
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 shape = Cuboid::new(Vec2::new(0.5, 0.5).into());
|
||||||
let known = revealed_tiles[idx];
|
let known = revealed_tiles[idx];
|
||||||
let visible = visible_tiles[idx];
|
let visible = visible_tiles[idx];
|
||||||
|
@ -374,7 +374,7 @@ fn exploration_changed_announcement(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if tokens.is_empty() {
|
if tokens.is_empty() {
|
||||||
match map.base.tiles[idx] {
|
match map.tiles[idx] {
|
||||||
TileType::Floor => "Floor".to_string(),
|
TileType::Floor => "Floor".to_string(),
|
||||||
TileType::Wall => "Wall".to_string(),
|
TileType::Wall => "Wall".to_string(),
|
||||||
}
|
}
|
||||||
|
|
77
src/map.rs
77
src/map.rs
|
@ -3,7 +3,8 @@ use std::collections::{HashMap, HashSet};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
use derive_more::{Deref, DerefMut};
|
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 maze_generator::{prelude::*, recursive_backtracking::RbGenerator};
|
||||||
use rand::prelude::StdRng;
|
use rand::prelude::StdRng;
|
||||||
|
|
||||||
|
@ -26,38 +27,6 @@ pub struct Areas(pub Vec<Area>);
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Portal;
|
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 {
|
pub trait ITileType {
|
||||||
fn blocks_motion(&self) -> bool;
|
fn blocks_motion(&self) -> bool;
|
||||||
fn blocks_visibility(&self) -> bool;
|
fn blocks_visibility(&self) -> bool;
|
||||||
|
@ -153,7 +122,7 @@ impl GridBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MapFilter for 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 map = map.clone();
|
||||||
let mut generator = RbGenerator::new(None);
|
let mut generator = RbGenerator::new(None);
|
||||||
let maze = generator.generate(self.width_in_rooms as i32, self.height_in_rooms as i32);
|
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()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.id();
|
.id();
|
||||||
for x in 0..map.width() {
|
for x in 0..map.width {
|
||||||
for y in 0..map.height() {
|
for y in 0..map.height {
|
||||||
let tile = map.base.at(x, y);
|
let tile = map.at(x, y);
|
||||||
if tile.blocks_motion() {
|
if tile.blocks_motion() {
|
||||||
let id = commands
|
let id = commands
|
||||||
.spawn()
|
.spawn()
|
||||||
|
@ -242,28 +211,24 @@ fn portal_spawner(
|
||||||
for (entity, map) in map.iter() {
|
for (entity, map) in map.iter() {
|
||||||
if config.autospawn_portals {
|
if config.autospawn_portals {
|
||||||
let mut portals: Vec<(f32, f32)> = vec![];
|
let mut portals: Vec<(f32, f32)> = vec![];
|
||||||
for x in 1..map.width() {
|
for x in 1..map.width {
|
||||||
for y in 1..map.height() {
|
for y in 1..map.height {
|
||||||
let mut spawn_portal = false;
|
let mut spawn_portal = false;
|
||||||
if map.base.get_available_exits(x, y).len() > 2 {
|
if map.get_available_exits(x, y).len() > 2 {
|
||||||
let idx = (x, y).to_index(map.width());
|
let idx = (x, y).to_index(map.width);
|
||||||
if map.base.tiles[idx] == TileType::Floor
|
if map.tiles[idx] == TileType::Floor
|
||||||
&& (x > 1 && map.base.tiles[idx - 1] == TileType::Floor)
|
&& (x > 1 && map.tiles[idx - 1] == TileType::Floor)
|
||||||
&& (x < map.width() - 2 && map.base.tiles[idx + 1] == TileType::Floor)
|
&& (x < map.width - 2 && map.tiles[idx + 1] == TileType::Floor)
|
||||||
&& (y > 1
|
&& (y > 1 && map.tiles[idx - map.width] == TileType::Wall)
|
||||||
&& map.base.tiles[idx - map.width() as usize] == TileType::Wall)
|
&& (y < map.height - 2 && map.tiles[idx + map.width] == TileType::Wall)
|
||||||
&& (y < map.height() - 2
|
|
||||||
&& map.base.tiles[idx + map.width() as usize] == TileType::Wall)
|
|
||||||
{
|
{
|
||||||
spawn_portal = true;
|
spawn_portal = true;
|
||||||
}
|
}
|
||||||
if map.base.tiles[idx] == TileType::Floor
|
if map.tiles[idx] == TileType::Floor
|
||||||
&& (x > 1 && map.base.tiles[idx - 1] == TileType::Wall)
|
&& (x > 1 && map.tiles[idx - 1] == TileType::Wall)
|
||||||
&& (x < map.width() - 2 && map.base.tiles[idx + 1] == TileType::Wall)
|
&& (x < map.width - 2 && map.tiles[idx + 1] == TileType::Wall)
|
||||||
&& (y > 1
|
&& (y > 1 && map.tiles[idx - map.width] == TileType::Floor)
|
||||||
&& map.base.tiles[idx - map.width() as usize] == TileType::Floor)
|
&& (y < map.height - 2 && map.tiles[idx + map.width] == TileType::Floor)
|
||||||
&& (y < map.height() - 2
|
|
||||||
&& map.base.tiles[idx + map.width() as usize] == TileType::Floor)
|
|
||||||
{
|
{
|
||||||
spawn_portal = true;
|
spawn_portal = true;
|
||||||
}
|
}
|
||||||
|
@ -357,7 +322,7 @@ fn area_description(
|
||||||
fn add_areas(mut commands: Commands, query: Query<(Entity, &Map), (Added<Map>, Without<Areas>)>) {
|
fn add_areas(mut commands: Commands, query: Query<(Entity, &Map), (Added<Map>, Without<Areas>)>) {
|
||||||
for (entity, map) in query.iter() {
|
for (entity, map) in query.iter() {
|
||||||
let mut v = vec![];
|
let mut v = vec![];
|
||||||
for room in &map.base.rooms {
|
for room in &map.rooms {
|
||||||
v.push(Area {
|
v.push(Area {
|
||||||
rect: *room,
|
rect: *room,
|
||||||
description: None,
|
description: None,
|
||||||
|
|
|
@ -31,7 +31,7 @@ pub fn find_path(
|
||||||
&start.into(),
|
&start.into(),
|
||||||
|p| {
|
|p| {
|
||||||
let mut successors: Vec<((i32, i32), u32)> = vec![];
|
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.push(((tile.0 as i32, tile.1 as i32), (tile.2 * 100.) as u32));
|
||||||
}
|
}
|
||||||
successors
|
successors
|
||||||
|
|
|
@ -163,11 +163,11 @@ fn sound_icon<S>(
|
||||||
) where
|
) where
|
||||||
S: Component + Clone + Debug + Eq + Hash,
|
S: Component + Clone + Debug + Eq + Hash,
|
||||||
{
|
{
|
||||||
if !(*config).sound_icon_states.is_empty() {
|
if !(*config).sound_icon_states.is_empty()
|
||||||
if !config.sound_icon_states.contains(state.current()) {
|
&& !config.sound_icon_states.contains(state.current())
|
||||||
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
for viewer in viewers.iter() {
|
for viewer in viewers.iter() {
|
||||||
for (mut icon, coordinates, parent, mut sound) in icons.iter_mut() {
|
for (mut icon, coordinates, parent, mut sound) in icons.iter_mut() {
|
||||||
let coords = if let Some(coordinates) = coordinates {
|
let coords = if let Some(coordinates) = coordinates {
|
||||||
|
|
|
@ -55,7 +55,7 @@ fn add_visibility_indices(
|
||||||
map_config: Res<MapConfig>,
|
map_config: Res<MapConfig>,
|
||||||
) {
|
) {
|
||||||
for (entity, map) in query.iter() {
|
for (entity, map) in query.iter() {
|
||||||
let count = map.count();
|
let count = map.width * map.height;
|
||||||
commands
|
commands
|
||||||
.entity(entity)
|
.entity(entity)
|
||||||
.insert(VisibleTiles(vec![false; count]));
|
.insert(VisibleTiles(vec![false; count]));
|
||||||
|
@ -76,7 +76,7 @@ where
|
||||||
type Opacity = u8;
|
type Opacity = u8;
|
||||||
|
|
||||||
fn size(&self, grid: &Self::Grid) -> Size {
|
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 {
|
fn get_opacity(&self, _grid: &Self::Grid, coord: Coord) -> Self::Opacity {
|
||||||
|
@ -139,7 +139,7 @@ fn update_visible_and_revealed_tiles(
|
||||||
*t = false
|
*t = false
|
||||||
}
|
}
|
||||||
for v in viewshed.visible.iter() {
|
for v in viewshed.visible.iter() {
|
||||||
let idx = v.to_index(map.width());
|
let idx = v.to_index(map.width);
|
||||||
revealed_tiles[idx] = true;
|
revealed_tiles[idx] = true;
|
||||||
visible_tiles[idx] = true;
|
visible_tiles[idx] = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user