fix z index of stuff

This commit is contained in:
Rowan 2024-05-24 23:17:45 -04:00
parent 7518ceff39
commit 6d142e863a
3 changed files with 292 additions and 8 deletions

File diff suppressed because one or more lines are too long

View file

@ -2,6 +2,25 @@ use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*; use bevy_ecs_ldtk::prelude::*;
use bevy_xpbd_2d::prelude::*; use bevy_xpbd_2d::prelude::*;
#[derive(Component, Default, Reflect)]
pub struct ZIndex(i32);
impl ZIndex {
pub fn from_field(instance: &EntityInstance) -> ZIndex {
Self(
*instance
.get_int_field("ZIndex")
.expect("expected entity to have Z Index field")
)
}
}
impl<T: Into<i32>> From<T> for ZIndex {
fn from(value: T) -> Self {
ZIndex(value.into())
}
}
#[derive(Component, Default)] #[derive(Component, Default)]
pub struct Tile; pub struct Tile;
@ -27,9 +46,29 @@ pub struct TileIntCell {
tile_bundle: TileBundle, tile_bundle: TileBundle,
} }
#[derive(Default, Bundle, LdtkEntity)]
pub struct PlatformEntity {
#[with(ZIndex::from_field)]
z_index: ZIndex,
#[sprite_sheet_bundle]
sprite_sheet_bundle: SpriteSheetBundle
}
pub struct LevelPlugin; pub struct LevelPlugin;
impl Plugin for LevelPlugin { impl Plugin for LevelPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.register_default_ldtk_int_cell_for_layer::<TileIntCell>("Ground"); app
.register_type::<ZIndex>()
.add_systems(Last, update_z_index)
.register_ldtk_entity::<PlatformEntity>("Platforms")
.register_default_ldtk_int_cell_for_layer::<TileIntCell>("Ground");
}
}
fn update_z_index(mut query: Query<(&mut Transform, &ZIndex), Changed<ZIndex>>) {
for (mut transform, z_index) in query.iter_mut() {
transform.translation.z = z_index.0 as f32;
} }
} }

View file

@ -2,15 +2,16 @@ mod controller;
mod input; mod input;
mod movement; mod movement;
use bevy::prelude::*; use bevy::{prelude::*, render::view::RenderLayers};
use bevy_ecs_ldtk::prelude::*; use bevy_ecs_ldtk::prelude::*;
use leafwing_input_manager::{action_state::ActionState, InputManagerBundle}; use leafwing_input_manager::{action_state::ActionState, InputManagerBundle};
use self::{ use self::{
controller::{CharacterControllerBundle, CharacterControllerPlugin, Jumping}, controller::{CharacterControllerBundle, CharacterControllerPlugin, Jumping},
input::{InputPlugin, PlayerAction}, input::{InputPlugin, PlayerAction},
movement::MovementDirection, movement::MovementDirection
}; };
use crate::level::ZIndex;
#[derive(Component, Deref)] #[derive(Component, Deref)]
pub struct Animation(benimator::Animation); pub struct Animation(benimator::Animation);
@ -48,6 +49,8 @@ impl Default for PlayerBundle {
#[derive(Default, Bundle, LdtkEntity)] #[derive(Default, Bundle, LdtkEntity)]
pub struct PlayerEntity { pub struct PlayerEntity {
player_bundle: PlayerBundle, player_bundle: PlayerBundle,
#[with(ZIndex::from_field)]
z_index: ZIndex,
#[sprite_sheet_bundle] #[sprite_sheet_bundle]
sprite_sheet_bundle: SpriteSheetBundle, sprite_sheet_bundle: SpriteSheetBundle,
} }
@ -56,7 +59,14 @@ pub struct PlayerPlugin;
impl Plugin for PlayerPlugin { impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins((InputPlugin, CharacterControllerPlugin)) app.add_plugins((InputPlugin, CharacterControllerPlugin))
.add_systems(Update, (apply_movement_input, apply_jumping_input, animate)) .add_systems(
Update,
(
apply_movement_input,
apply_jumping_input,
animate,
),
)
.register_ldtk_entity::<PlayerEntity>("Player"); .register_ldtk_entity::<PlayerEntity>("Player");
} }
} }