Goodbye exits, hello portals.

This commit is contained in:
Nolan Darilek 2021-05-20 14:54:13 -05:00
parent 3dfb7c1bc7
commit c49b6ac33f
2 changed files with 34 additions and 33 deletions

View File

@ -21,7 +21,7 @@ pub struct ExplorationFocused;
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Reflect)] #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Reflect)]
pub enum ExplorationType { pub enum ExplorationType {
Exit = 0, Portal = 0,
Item = 1, Item = 1,
Character = 2, Character = 2,
Ally = 3, Ally = 3,
@ -33,7 +33,7 @@ pub enum ExplorationType {
impl Into<String> for ExplorationType { impl Into<String> for ExplorationType {
fn into(self) -> String { fn into(self) -> String {
match self { match self {
ExplorationType::Exit => "Exit".into(), ExplorationType::Portal => "Portal".into(),
ExplorationType::Item => "Item".into(), ExplorationType::Item => "Item".into(),
ExplorationType::Character => "Character".into(), ExplorationType::Character => "Character".into(),
ExplorationType::Ally => "Ally".into(), ExplorationType::Ally => "Ally".into(),
@ -47,7 +47,7 @@ impl Into<String> for ExplorationType {
impl Into<&str> for ExplorationType { impl Into<&str> for ExplorationType {
fn into(self) -> &'static str { fn into(self) -> &'static str {
match self { match self {
ExplorationType::Exit => "exit", ExplorationType::Portal => "exit",
ExplorationType::Item => "item", ExplorationType::Item => "item",
ExplorationType::Character => "character", ExplorationType::Character => "character",
ExplorationType::Ally => "ally", ExplorationType::Ally => "ally",

View File

@ -20,9 +20,9 @@ impl From<mapgen::geometry::Point> for Coordinates {
#[derive(Clone, Debug, Default, Deref, DerefMut)] #[derive(Clone, Debug, Default, Deref, DerefMut)]
pub struct Areas(pub Vec<Area>); pub struct Areas(pub Vec<Area>);
#[derive(Clone, Copy, Debug, Default, Reflect)] #[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)] #[reflect(Component)]
pub struct Exit; pub struct Portal(Vec<(i32, i32)>);
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct Map { pub struct Map {
@ -83,7 +83,7 @@ impl ITileType for TileType {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct MapConfig { pub struct MapConfig {
pub autospawn_exits: bool, pub autospawn_portals: bool,
pub describe_undescribed_areas: bool, pub describe_undescribed_areas: bool,
pub speak_area_descriptions: bool, pub speak_area_descriptions: bool,
pub start_revealed: bool, pub start_revealed: bool,
@ -92,7 +92,7 @@ pub struct MapConfig {
impl Default for MapConfig { impl Default for MapConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
autospawn_exits: true, autospawn_portals: true,
describe_undescribed_areas: false, describe_undescribed_areas: false,
speak_area_descriptions: true, speak_area_descriptions: true,
start_revealed: false, start_revealed: false,
@ -101,21 +101,21 @@ impl Default for MapConfig {
} }
#[derive(Bundle)] #[derive(Bundle)]
pub struct ExitBundle { pub struct PortalBundle {
pub coordinates: Coordinates, pub coordinates: Coordinates,
pub exit: Exit, pub portal: Portal,
pub exploration_type: ExplorationType, pub exploration_type: ExplorationType,
pub mappable: Mappable, pub mappable: Mappable,
pub transform: Transform, pub transform: Transform,
pub global_transform: GlobalTransform, pub global_transform: GlobalTransform,
} }
impl Default for ExitBundle { impl Default for PortalBundle {
fn default() -> Self { fn default() -> Self {
Self { Self {
coordinates: Default::default(), coordinates: Default::default(),
exit: Default::default(), portal: Default::default(),
exploration_type: ExplorationType::Exit, exploration_type: ExplorationType::Portal,
mappable: Default::default(), mappable: Default::default(),
transform: Default::default(), transform: Default::default(),
global_transform: Default::default(), global_transform: Default::default(),
@ -203,17 +203,17 @@ impl MapFilter for GridBuilder {
} }
} }
fn exit_spawner( fn portal_spawner(
mut commands: Commands, mut commands: Commands,
map: Query<(Entity, &Map), Added<Map>>, map: Query<(Entity, &Map), Added<Map>>,
config: Res<MapConfig>, config: Res<MapConfig>,
) { ) {
for (entity, map) in map.iter() { for (entity, map) in map.iter() {
if config.autospawn_exits { if config.autospawn_portals {
let mut exits: 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_exit = false; let mut spawn_portal = false;
if map.base.get_available_exits(x, y).len() > 2 { if map.base.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.base.tiles[idx] == TileType::Floor
@ -224,7 +224,7 @@ fn exit_spawner(
&& (y < map.height() - 2 && (y < map.height() - 2
&& map.base.tiles[idx + map.width() as usize] == TileType::Wall) && map.base.tiles[idx + map.width() as usize] == TileType::Wall)
{ {
spawn_exit = true; spawn_portal = true;
} }
if map.base.tiles[idx] == TileType::Floor if map.base.tiles[idx] == TileType::Floor
&& (x > 1 && map.base.tiles[idx - 1] == TileType::Wall) && (x > 1 && map.base.tiles[idx - 1] == TileType::Wall)
@ -234,31 +234,32 @@ fn exit_spawner(
&& (y < map.height() - 2 && (y < map.height() - 2
&& map.base.tiles[idx + map.width() as usize] == TileType::Floor) && map.base.tiles[idx + map.width() as usize] == TileType::Floor)
{ {
spawn_exit = true; spawn_portal = true;
} }
} }
if spawn_exit { if spawn_portal {
let x = x as f32; let x = x as f32;
let y = y as f32; let y = y as f32;
if !exits.contains(&(x, y)) { if !portals.contains(&(x, y)) {
exits.push((x, y)); portals.push((x, y));
} }
} }
} }
} }
for exit in exits { for portal in portals {
let x = exit.0 as f32; let x = portal.0 as f32;
let y = exit.1 as f32; let y = portal.1 as f32;
let exit = commands let coordinates = Coordinates((x, y));
let portal = commands
.spawn() .spawn()
.insert_bundle(ExitBundle { .insert_bundle(PortalBundle {
coordinates: Coordinates((x, y)), coordinates,
exit: Default::default(), portal: Portal(vec![coordinates.i32()]),
transform: Transform::from_translation(Vec3::new(x, y, 0.)), transform: Transform::from_translation(Vec3::new(x, y, 0.)),
..Default::default() ..Default::default()
}) })
.id(); .id();
commands.entity(entity).push_children(&[exit]); commands.entity(entity).push_children(&[portal]);
} }
} }
} }
@ -358,14 +359,14 @@ impl Plugin for MapPlugin {
app.insert_resource(MapConfig::default()); app.insert_resource(MapConfig::default());
} }
let config = app.world().get_resource::<MapConfig>().unwrap().clone(); let config = app.world().get_resource::<MapConfig>().unwrap().clone();
const SPAWN_EXITS: &str = "SPAWN_EXITS"; const SPAWN_PORTALS: &str = "SPAWN_PORTALS";
app.register_type::<Exit>() app.register_type::<Portal>()
.insert_resource(PreviousIndex::default()) .insert_resource(PreviousIndex::default())
.add_system(entity_indexing.system().label(UPDATE_ENTITY_INDEX_LABEL)) .add_system(entity_indexing.system().label(UPDATE_ENTITY_INDEX_LABEL))
.add_system( .add_system(
exit_spawner portal_spawner
.system() .system()
.label(SPAWN_EXITS) .label(SPAWN_PORTALS)
.before(UPDATE_ENTITY_INDEX_LABEL), .before(UPDATE_ENTITY_INDEX_LABEL),
) )
.add_system_to_stage( .add_system_to_stage(