diff --git a/src/game/player.rs b/src/game/player.rs index 4552bb1..123be38 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -9,6 +9,7 @@ use bevy::{ }; use bevy_rapier2d::prelude::*; +use death::player_death_plugin; use crate::{AppState, METER}; @@ -18,13 +19,18 @@ use super::{ PLAYER_DEPTH, }; -mod animation; +mod camera { + use bevy::prelude::*; -#[derive(Event)] -enum PlayerDeathEvent { - Void, - EggDied, + #[derive(Component)] + struct CameraAttached; } + +mod movement {} + +mod animation; +mod death; + #[derive(Event)] struct RespawnPointSetEvent; @@ -62,6 +68,7 @@ pub(super) fn player_plugin(app: &mut App) { ) .in_set(IngameSet), ) + .add_plugins(player_death_plugin) .init_resource::(); } @@ -75,6 +82,13 @@ enum PlayerAnimations { fn debug_player_pos(query: Query<&Transform, With>) { let trans = query.single(); } + +/// what this system actually does: +/// - move player +/// - act as camera controller +/// - control player animations (fair? i guess??) +/// - set respawn points?! +/// - check for player deaths to the void?!?!?? fn move_player( mut commands: Commands, kb_input: Res>, @@ -147,11 +161,11 @@ fn move_player( } } - if p_transform.translation.y < -10. * player_coords.block_size { - commands.trigger(PlayerDeathEvent::Void); - p_transform.translation = - Vec2::new(player_coords.x, player_coords.y).extend(PLAYER_DEPTH); - } + // if p_transform.translation.y < -10. * player_coords.block_size { + // commands.trigger(death::PlayerDeathEvent::Void); + // p_transform.translation = + // Vec2::new(player_coords.x, player_coords.y).extend(PLAYER_DEPTH); + // } player.last_grounded += 1; } } @@ -215,24 +229,11 @@ fn add_player( }), )) .insert(KinematicCharacterController { - //translation: todo!(), - //custom_shape: todo!(), custom_mass: Some(10000.0), - //up: todo!(), offset: CharacterLength::Absolute(0.01), slide: true, - //autostep: Some(CharacterAutostep { - // max_height: CharacterLength::Relative(0.1), - // min_width: CharacterLength::Absolute(1.0), - // include_dynamic_bodies: false, - //}), - //max_slope_climb_angle: todo!(), - //min_slope_slide_angle: todo!(), apply_impulse_to_dynamic_bodies: true, snap_to_ground: Some(CharacterLength::Absolute(0.5)), - //filter_flags: todo!(), - //filter_groups: todo!(), - // normal_nudge_factor: 1., ..Default::default() }); } diff --git a/src/game/player/death.rs b/src/game/player/death.rs new file mode 100644 index 0000000..87f306d --- /dev/null +++ b/src/game/player/death.rs @@ -0,0 +1,49 @@ +use bevy::prelude::*; + +use crate::game::{scene::PlayerCoords, set::IngameSet, PLAYER_DEPTH}; + +use super::Player; + +const VOID_DEATH_Y_THRESHOLD: f32 = -10.; + +#[derive(Event)] +pub(crate) enum PlayerDeathEvent { + Void, + // this should probably be handled as a game over instead? + // do we even keep this in the game? probably? + EggDied, +} + +pub fn player_death_plugin(app: &mut App) { + app.add_event::() + .add_systems(Update, (check_void_death, respawn_player).in_set(IngameSet)); +} + +fn check_void_death( + mut ev_death: EventWriter, + query: Query<&Transform, With>, + // i am currently unsure wtf this actually does + // my suspicions: + // - store the player respawn point coords + // - store the world blocks size, whyever that's not a const??? + player_coords: Res, +) { + let player_transform = query.single(); + + // why multiply with block_size here? + if player_transform.translation.y < VOID_DEATH_Y_THRESHOLD * player_coords.block_size { + ev_death.send(PlayerDeathEvent::Void); + } +} + +fn respawn_player( + mut ev_death: EventReader, + mut query: Query<&mut Transform, With>, + player_coords: Res, +) { + let mut player_transform = query.single_mut(); + for ev in ev_death.read() { + player_transform.translation = + Vec2::new(player_coords.x, player_coords.y).extend(PLAYER_DEPTH); + } +}