use bevy::prelude::*;
use crate::GeneratedMaps;
pub struct LayerPlugin;
impl Plugin for LayerPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
PostUpdate,
(update_layer_depth, update_z_coordinate_based_on_layer).chain(),
);
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Component)]
pub enum Layer {
Background(i16),
Foreground(i16),
}
const LAYER_MODIFIER: f32 = 1000.;
fn update_z_coordinate_based_on_layer(mut query: Query<(&mut Transform, &Layer), Changed<Layer>>) {
for (mut transform, layer) in &mut query {
transform.translation.z = match layer {
Layer::Background(order_in_layer) => -50. + f32::from(*order_in_layer) / LAYER_MODIFIER,
Layer::Foreground(order_in_layer) => 50. + f32::from(*order_in_layer) / LAYER_MODIFIER,
}
}
}
#[allow(clippy::needless_pass_by_value)]
fn update_layer_depth(
mut query: Query<(&mut Layer, &Transform), Changed<Transform>>,
generated_map: Res<GeneratedMaps>,
) {
for (mut layer, transform) in &mut query {
let depth = (transform.translation.y / (0.5 * generated_map.dimensions().1 as f32)
* f32::from(i16::MAX)) as i16;
*layer = match *layer {
Layer::Background(_) => Layer::Background(depth),
Layer::Foreground(_) => Layer::Foreground(depth),
};
}
}