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

View File

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