diff --git a/game_core/src/debug.rs b/game_core/src/debug.rs index 2722f4b0a7d2800f350e7f094d7cfab2f86dd38a..2216469ff3edf8a4d7ca145489241775ca4e6f75 100644 --- a/game_core/src/debug.rs +++ b/game_core/src/debug.rs @@ -5,6 +5,7 @@ use iyes_loopless::state::NextState; use crate::assets::AssetHandles; use crate::system::flow::AppState; +use crate::world::generators::blobular::Blobular; use crate::world::generators::drunkard_corridor::DrunkardGenerator; use crate::world::level_map::LevelMapBundle; diff --git a/game_core/src/lib.rs b/game_core/src/lib.rs index 9bf52a28ce185a0c746a0e95195eb59eeffd45ba..96e91248bc7a603e2c359f06aef45aa6ebb9e66b 100644 --- a/game_core/src/lib.rs +++ b/game_core/src/lib.rs @@ -1,3 +1,5 @@ +extern crate core; + pub mod assets; pub mod control; pub mod debug; diff --git a/game_core/src/world/generators/blobular.rs b/game_core/src/world/generators/blobular.rs index b552923c0a729bc5006585ba1cb74cc5c0ad6628..548471358c4e94fba521d71a669065df0fd42628 100644 --- a/game_core/src/world/generators/blobular.rs +++ b/game_core/src/world/generators/blobular.rs @@ -1,9 +1,9 @@ use bevy::math::uvec2; use fastrand::Rng; -use crate::world::generators::utils::{draw_box, GenerationDirection}; +use crate::world::generators::utils::{draw_box, outline_floors, GenerationDirection}; use crate::world::generators::{MapGenerator, TMP_FLOOR_GROUP}; -use crate::world::level_map::{Indexer, LevelMap, MapLayer, MapTile}; +use crate::world::level_map::{Indexer, LevelMap, MapLayer, MapLayerType, MapTile}; pub struct Blobular; impl MapGenerator for Blobular { @@ -111,6 +111,8 @@ impl MapGenerator for Blobular { } } + outline_floors(&floor_layer, &mut wall_layer, indexer); + LevelMap { spawn: uvec2(initial_x as u32, initial_y as u32), width, @@ -126,6 +128,7 @@ impl MapGenerator for Blobular { rest => Some(MapTile::new_floor(rest)), }) .collect(), + MapLayerType::Floor, ), MapLayer::from_sized_list( width, @@ -137,6 +140,7 @@ impl MapGenerator for Blobular { rest => Some(MapTile::new_wall(rest)), }) .collect(), + MapLayerType::Wall, ), MapLayer::from_sized_list( width, @@ -148,6 +152,7 @@ impl MapGenerator for Blobular { rest => Some(MapTile::new_obstacle(rest)), }) .collect(), + MapLayerType::None, ), ], } diff --git a/game_core/src/world/generators/drunkard_corridor.rs b/game_core/src/world/generators/drunkard_corridor.rs index 2161d994d8d69ebd5d9ff6d51a3e82bf75cfd3c7..6e815a8bb7b0438b5a7bb104f8bfbc63af483f44 100644 --- a/game_core/src/world/generators/drunkard_corridor.rs +++ b/game_core/src/world/generators/drunkard_corridor.rs @@ -1,8 +1,9 @@ use bevy::math::uvec2; use fastrand::Rng; +use crate::world::generators::utils::outline_floors; use crate::world::generators::{MapGenerator, TMP_FLOOR_GROUP}; -use crate::world::level_map::{Indexer, LevelMap, MapLayer, MapTile}; +use crate::world::level_map::{Indexer, LevelMap, MapLayer, MapLayerType, MapTile}; pub struct DrunkardGenerator; impl MapGenerator for DrunkardGenerator { @@ -47,24 +48,45 @@ impl MapGenerator for DrunkardGenerator { } } + let mut walls = vec![0; floor.len()]; + outline_floors(&floor, &mut walls, &indexer); + LevelMap::new( map_width, map_height, start, - vec![MapLayer::from_sized_list( - map_width, - map_height, - floor - .into_iter() - .map(|tile| { - if tile == 0 { - None - } else { - Some(MapTile::new_floor(tile)) - } - }) - .collect(), - )], + vec![ + MapLayer::from_sized_list( + map_width, + map_height, + floor + .into_iter() + .map(|tile| { + if tile == 0 { + None + } else { + Some(MapTile::new_floor(tile)) + } + }) + .collect(), + MapLayerType::Floor, + ), + MapLayer::from_sized_list( + map_width, + map_height, + walls + .into_iter() + .map(|tile| { + if tile == 0 { + None + } else { + Some(MapTile::new_floor(tile)) + } + }) + .collect(), + MapLayerType::Wall, + ), + ], ) } } diff --git a/game_core/src/world/generators/utils.rs b/game_core/src/world/generators/utils.rs index 51b926c6f9c7f74adc73c793ad57b15b1071248e..b83d3896ca577fcdd059e7a2c2b6982791c667fd 100644 --- a/game_core/src/world/generators/utils.rs +++ b/game_core/src/world/generators/utils.rs @@ -1,3 +1,4 @@ +use crate::world::generators::TMP_WALL_GROUP; use crate::world::level_map::Indexer; #[derive(Copy, Clone, Debug, Default)] @@ -61,3 +62,32 @@ pub fn validate_box( true } + +pub fn outline_floors(floors: &Vec<usize>, walls: &mut Vec<usize>, indexer: &Indexer) { + if floors.len() != walls.len() { + panic!("Trying to work with irregular lists"); + } + + let max_index = floors.len() - 1; + + for (idx, value) in floors.iter().enumerate() { + if value == &0 { + let (x, y) = indexer.reverse(idx); + if floors[max_index.min(indexer.index(x.saturating_sub(1), y))] != 0 + || floors[max_index.min(indexer.index(x.saturating_add(1), y))] != 0 + || floors[max_index.min(indexer.index(x, y.saturating_sub(1)))] != 0 + || floors[max_index.min(indexer.index(x, y.saturating_add(1)))] != 0 + || floors[max_index.min(indexer.index(x.saturating_sub(1), y.saturating_sub(1)))] + != 0 || floors + [max_index.min(indexer.index(x.saturating_add(1), y.saturating_add(1)))] + != 0 || floors + [max_index.min(indexer.index(x.saturating_sub(1), y.saturating_add(1)))] + != 0 || floors + [max_index.min(indexer.index(x.saturating_add(1), y.saturating_sub(1)))] + != 0 + { + walls[idx] = TMP_WALL_GROUP; + } + } + } +} diff --git a/game_core/src/world/handlers.rs b/game_core/src/world/handlers.rs index b528375ada7ab566c45d1c758a08dbb60b8e94ef..63b622b8f6c47e320e08901c269c3c78a2542e38 100644 --- a/game_core/src/world/handlers.rs +++ b/game_core/src/world/handlers.rs @@ -26,8 +26,10 @@ pub fn spawn_new_world( .spawn_bundle(SpriteSheetBundle { texture_atlas: assets.atlas("environs"), sprite: TextureAtlasSprite::new( - data.tile_group - + get_floor_sprite_offset(layer.get_tile_adjacency(x, y)), + layer.layer_type.transform_adjacency( + data.tile_group, + layer.get_tile_adjacency(x, y), + ), ), transform: Transform::from_xyz( x as f32 * WORLD_TILE_SIZE, diff --git a/game_core/src/world/level_map.rs b/game_core/src/world/level_map.rs index 57be529f07c5d962c65d203e4e76ff5901a37563..633689c905a3a3b670fab28bac38e7cbafbbf216 100644 --- a/game_core/src/world/level_map.rs +++ b/game_core/src/world/level_map.rs @@ -5,7 +5,9 @@ use bevy::math::UVec2; use bevy::prelude::*; use fastrand::Rng; -use crate::world::adjacency::{BOTTOM, LEFT, NONE, RIGHT, TOP}; +use crate::world::adjacency::{ + get_floor_sprite_offset, get_wall_sprite_offset, BOTTOM, LEFT, NONE, RIGHT, TOP, +}; use crate::world::generators::MapGenerator; pub const WORLD_TILE_SIZE: f32 = 16.0; @@ -82,6 +84,23 @@ impl MapTile { } } +#[derive(Clone, Eq, PartialEq)] +pub enum MapLayerType { + Wall, + Floor, + None, +} + +impl MapLayerType { + pub fn transform_adjacency(&self, value: usize, adjacency: u8) -> usize { + match self { + MapLayerType::Wall => value + get_wall_sprite_offset(adjacency), + MapLayerType::Floor => value + get_floor_sprite_offset(adjacency), + MapLayerType::None => value, + } + } +} + #[derive(Clone, Eq, PartialEq)] pub struct MapLayer { pub width: usize, @@ -89,16 +108,23 @@ pub struct MapLayer { pub offset_horizontal: usize, pub offset_vertical: usize, pub tiles: Vec<Option<MapTile>>, + pub layer_type: MapLayerType, } impl MapLayer { - pub fn from_sized_list(width: usize, height: usize, tiles: Vec<Option<MapTile>>) -> Self { + pub fn from_sized_list( + width: usize, + height: usize, + tiles: Vec<Option<MapTile>>, + layer_type: MapLayerType, + ) -> Self { MapLayer { width, height, offset_horizontal: 0, offset_vertical: 0, tiles, + layer_type, } }