fix(floor): spawning

This commit is contained in:
Kristofers Solo 2024-12-19 18:38:18 +02:00
parent 86bbee8cb8
commit f8ea1edd87
8 changed files with 55 additions and 24 deletions

View File

@ -19,7 +19,9 @@ pub struct DespawnFloor {
pub floor: u8,
}
#[derive(Debug, Reflect, Event)]
pub struct TransitionFloor {
pub floor: u8,
#[derive(Debug, Reflect, Event, Default)]
pub enum TransitionFloor {
#[default]
Ascend,
Descent,
}

View File

@ -1,11 +1,36 @@
use bevy::prelude::*;
use crate::{floor::events::TransitionFloor, maze::events::SpawnMaze};
use crate::{
floor::{
components::{CurrentFloor, Floor},
events::TransitionFloor,
},
maze::events::SpawnMaze,
};
pub(super) fn spawn_floor(
mut commands: Commands,
query: Query<(Entity, &Floor), With<CurrentFloor>>,
mut event_reader: EventReader<TransitionFloor>,
) {
let Ok((entity, floor)) = query.get_single() else {
return;
};
pub(super) fn spawn_floor(mut commands: Commands, mut event_reader: EventReader<TransitionFloor>) {
for event in event_reader.read() {
let floor = event.floor;
dbg!(event);
let floor = match event {
TransitionFloor::Ascend => *floor.increase(),
TransitionFloor::Descent => *floor.decrease(),
};
if floor == 1 {
return;
}
info!("Creating level for floor {}", floor);
commands.entity(entity).remove::<CurrentFloor>();
commands.trigger(SpawnMaze { floor, ..default() });
}
}

View File

@ -1,8 +1,7 @@
use super::components::MazeConfig;
use bevy::prelude::*;
use super::components::MazeConfig;
#[derive(Debug, Reflect, Event, Default)]
#[derive(Debug, Reflect, Event)]
pub struct SpawnMaze {
pub floor: u8,
pub config: MazeConfig,
@ -18,3 +17,12 @@ pub struct RespawnMaze {
pub struct DespawnMaze {
pub floor: u8,
}
impl Default for SpawnMaze {
fn default() -> Self {
Self {
floor: 1,
config: MazeConfig::default(),
}
}
}

View File

@ -1,7 +1,6 @@
use crate::maze::{components::MazeConfig, events::SpawnMaze};
use crate::maze::events::SpawnMaze;
use bevy::prelude::*;
pub(crate) fn setup(mut commands: Commands) {
let config = MazeConfig::default();
commands.trigger(SpawnMaze { floor: 1, config });
commands.trigger(SpawnMaze::default());
}

View File

@ -26,6 +26,7 @@ pub(super) fn spawn_maze(
) {
let SpawnMaze { floor, config } = trigger.event();
if maze_query.iter().any(|(_, f, _)| f.0 == *floor) {
warn!("Floor {} already exists, skipping creation", floor);
return;
}

View File

@ -1,28 +1,24 @@
use bevy::prelude::*;
use crate::{
floor::{
components::{CurrentFloor, Floor},
events::TransitionFloor,
},
floor::{components::CurrentFloor, events::TransitionFloor},
maze::components::MazeConfig,
player::components::{CurrentPosition, Player},
};
pub(super) fn ascend_player(
query: Query<&CurrentPosition, With<Player>>,
maze_config_query: Query<(&MazeConfig, &Floor), With<CurrentFloor>>,
maze_config_query: Query<&MazeConfig, With<CurrentFloor>>,
mut event_writer: EventWriter<TransitionFloor>,
) {
let Ok((config, floor)) = maze_config_query.get_single() else {
let Ok(config) = maze_config_query.get_single() else {
warn!("Failed to get maze configuration for current floor - cannot ascend player");
return;
};
for current_hex in query.iter() {
if current_hex.0 == config.end_pos {
event_writer.send(TransitionFloor {
floor: *floor.increase(),
});
event_writer.send(TransitionFloor::Ascend);
return;
}
}

View File

@ -15,14 +15,13 @@ pub(super) fn descend_player(
mut event_writer: EventWriter<TransitionFloor>,
) {
let Ok((config, floor)) = maze_config_query.get_single() else {
warn!("Failed to get maze configuration for current floor - cannot descend player");
return;
};
for current_hex in query.iter() {
if current_hex.0 == config.start_pos && floor.0 != 1 {
event_writer.send(TransitionFloor {
floor: *floor.decrease(),
});
event_writer.send(TransitionFloor::Descent);
return;
}
}

View File

@ -3,6 +3,7 @@ mod descend;
mod input;
mod movement;
pub mod setup;
mod transition_floor;
use crate::maze::MazePluginLoaded;
use ascend::ascend_player;