mirror of
https://github.com/kristoferssolo/maze-ascension.git
synced 2025-10-21 19:20:34 +00:00
refactor(floor): separate systems
This commit is contained in:
parent
3659ffa1a6
commit
433a3ce5e8
@ -5,13 +5,18 @@ mod spawn;
|
|||||||
use crate::maze::MazePluginLoaded;
|
use crate::maze::MazePluginLoaded;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use despawn::despawn_floor;
|
use despawn::despawn_floor;
|
||||||
use movement::floor_movement;
|
use movement::{handle_floor_transition_events, move_floors};
|
||||||
use spawn::spawn_floor;
|
use spawn::spawn_floor;
|
||||||
|
|
||||||
pub(super) fn plugin(app: &mut App) {
|
pub(super) fn plugin(app: &mut App) {
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(spawn_floor, despawn_floor, floor_movement)
|
(
|
||||||
|
spawn_floor,
|
||||||
|
despawn_floor,
|
||||||
|
handle_floor_transition_events,
|
||||||
|
move_floors,
|
||||||
|
)
|
||||||
.chain()
|
.chain()
|
||||||
.run_if(resource_exists::<MazePluginLoaded>),
|
.run_if(resource_exists::<MazePluginLoaded>),
|
||||||
);
|
);
|
||||||
|
|||||||
@ -3,21 +3,40 @@ use crate::{
|
|||||||
components::{CurrentFloor, MovementState, NextFloor},
|
components::{CurrentFloor, MovementState, NextFloor},
|
||||||
events::TransitionFloor,
|
events::TransitionFloor,
|
||||||
},
|
},
|
||||||
maze::{components::Maze, GlobalMazeConfig},
|
maze::components::Maze,
|
||||||
player::components::{MovementSpeed, Player},
|
player::components::{MovementSpeed, Player},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
const MOVEMENT_THRESHOLD: f32 = 0.001;
|
const MOVEMENT_THRESHOLD: f32 = 0.001;
|
||||||
|
|
||||||
pub(super) fn floor_movement(
|
pub(super) fn move_floors(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut maze_query: Query<(Entity, &mut Transform, Option<&mut MovementState>), With<Maze>>,
|
||||||
|
player_query: Query<&MovementSpeed, With<Player>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
let speed = player_query.get_single().map_or(100., |s| s.0);
|
||||||
|
let movement_distance = speed * time.delta_secs();
|
||||||
|
for (entity, mut transform, mut movement_state) in maze_query.iter_mut() {
|
||||||
|
if let Some(state) = movement_state.as_mut() {
|
||||||
|
let delta = state.target_y - transform.translation.y;
|
||||||
|
if delta.abs() > MOVEMENT_THRESHOLD {
|
||||||
|
let movement = delta.signum() * movement_distance.min(delta.abs());
|
||||||
|
transform.translation.y += movement;
|
||||||
|
} else {
|
||||||
|
transform.translation.y = state.target_y;
|
||||||
|
commands.entity(entity).remove::<MovementState>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn handle_floor_transition_events(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut maze_query: Query<(Entity, &mut Transform, Option<&mut MovementState>), With<Maze>>,
|
mut maze_query: Query<(Entity, &mut Transform, Option<&mut MovementState>), With<Maze>>,
|
||||||
current_query: Query<Entity, With<CurrentFloor>>,
|
current_query: Query<Entity, With<CurrentFloor>>,
|
||||||
next_query: Query<Entity, With<NextFloor>>,
|
next_query: Query<Entity, With<NextFloor>>,
|
||||||
player_query: Query<&MovementSpeed, With<Player>>,
|
|
||||||
time: Res<Time>,
|
|
||||||
_global_config: Res<GlobalMazeConfig>,
|
|
||||||
mut event_reader: EventReader<TransitionFloor>,
|
mut event_reader: EventReader<TransitionFloor>,
|
||||||
) {
|
) {
|
||||||
for event in event_reader.read() {
|
for event in event_reader.read() {
|
||||||
@ -46,21 +65,6 @@ pub(super) fn floor_movement(
|
|||||||
|
|
||||||
update_current_next_floor(&mut commands, current_entity, next_entity);
|
update_current_next_floor(&mut commands, current_entity, next_entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
let speed = player_query.get_single().map_or(100., |s| s.0);
|
|
||||||
let movement_distance = speed * time.delta_secs();
|
|
||||||
for (entity, mut transform, mut movement_state) in maze_query.iter_mut() {
|
|
||||||
if let Some(state) = movement_state.as_mut() {
|
|
||||||
let delta = state.target_y - transform.translation.y;
|
|
||||||
if delta.abs() > MOVEMENT_THRESHOLD {
|
|
||||||
let movement = delta.signum() * movement_distance.min(delta.abs());
|
|
||||||
transform.translation.y += movement;
|
|
||||||
} else {
|
|
||||||
transform.translation.y = state.target_y;
|
|
||||||
commands.entity(entity).remove::<MovementState>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_current_next_floor(commands: &mut Commands, current_entity: Entity, next_entity: Entity) {
|
fn update_current_next_floor(commands: &mut Commands, current_entity: Entity, next_entity: Entity) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user