From 639a822da48154e555fbfd536034699e2af56d32 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Mon, 3 Oct 2022 01:42:29 +0100 Subject: [PATCH] Outline floors with wall tiles, base offsets on enum --- game_core/src/debug.rs | 1 + game_core/src/lib.rs | 2 + game_core/src/world/generators/blobular.rs | 9 +++- .../src/world/generators/drunkard_corridor.rs | 52 +++++++++++++------ game_core/src/world/generators/utils.rs | 30 +++++++++++ game_core/src/world/handlers.rs | 6 ++- game_core/src/world/level_map.rs | 30 ++++++++++- 7 files changed, 109 insertions(+), 21 deletions(-) diff --git a/game_core/src/debug.rs b/game_core/src/debug.rs index 2722f4b..2216469 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 9bf52a2..96e9124 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 b552923..5484713 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 2161d99..6e815a8 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 51b926c..b83d389 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 b528375..63b622b 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 57be529..633689c 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, } } -- GitLab