extract player death from move_player
This commit is contained in:
parent
a4419e7abc
commit
fcf672e2b9
2 changed files with 73 additions and 23 deletions
|
@ -9,6 +9,7 @@ use bevy::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
|
use death::player_death_plugin;
|
||||||
|
|
||||||
use crate::{AppState, METER};
|
use crate::{AppState, METER};
|
||||||
|
|
||||||
|
@ -18,13 +19,18 @@ use super::{
|
||||||
PLAYER_DEPTH,
|
PLAYER_DEPTH,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod animation;
|
mod camera {
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Event)]
|
#[derive(Component)]
|
||||||
enum PlayerDeathEvent {
|
struct CameraAttached;
|
||||||
Void,
|
|
||||||
EggDied,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod movement {}
|
||||||
|
|
||||||
|
mod animation;
|
||||||
|
mod death;
|
||||||
|
|
||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
struct RespawnPointSetEvent;
|
struct RespawnPointSetEvent;
|
||||||
|
|
||||||
|
@ -62,6 +68,7 @@ pub(super) fn player_plugin(app: &mut App) {
|
||||||
)
|
)
|
||||||
.in_set(IngameSet),
|
.in_set(IngameSet),
|
||||||
)
|
)
|
||||||
|
.add_plugins(player_death_plugin)
|
||||||
.init_resource::<PlayerSpawnOneshot>();
|
.init_resource::<PlayerSpawnOneshot>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +82,13 @@ enum PlayerAnimations {
|
||||||
fn debug_player_pos(query: Query<&Transform, With<Player>>) {
|
fn debug_player_pos(query: Query<&Transform, With<Player>>) {
|
||||||
let trans = query.single();
|
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(
|
fn move_player(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
kb_input: Res<ButtonInput<KeyCode>>,
|
kb_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
@ -147,11 +161,11 @@ fn move_player(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p_transform.translation.y < -10. * player_coords.block_size {
|
// if p_transform.translation.y < -10. * player_coords.block_size {
|
||||||
commands.trigger(PlayerDeathEvent::Void);
|
// commands.trigger(death::PlayerDeathEvent::Void);
|
||||||
p_transform.translation =
|
// p_transform.translation =
|
||||||
Vec2::new(player_coords.x, player_coords.y).extend(PLAYER_DEPTH);
|
// Vec2::new(player_coords.x, player_coords.y).extend(PLAYER_DEPTH);
|
||||||
}
|
// }
|
||||||
player.last_grounded += 1;
|
player.last_grounded += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -215,24 +229,11 @@ fn add_player(
|
||||||
}),
|
}),
|
||||||
))
|
))
|
||||||
.insert(KinematicCharacterController {
|
.insert(KinematicCharacterController {
|
||||||
//translation: todo!(),
|
|
||||||
//custom_shape: todo!(),
|
|
||||||
custom_mass: Some(10000.0),
|
custom_mass: Some(10000.0),
|
||||||
//up: todo!(),
|
|
||||||
offset: CharacterLength::Absolute(0.01),
|
offset: CharacterLength::Absolute(0.01),
|
||||||
slide: true,
|
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,
|
apply_impulse_to_dynamic_bodies: true,
|
||||||
snap_to_ground: Some(CharacterLength::Absolute(0.5)),
|
snap_to_ground: Some(CharacterLength::Absolute(0.5)),
|
||||||
//filter_flags: todo!(),
|
|
||||||
//filter_groups: todo!(),
|
|
||||||
// normal_nudge_factor: 1.,
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
49
src/game/player/death.rs
Normal file
49
src/game/player/death.rs
Normal file
|
@ -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::<PlayerDeathEvent>()
|
||||||
|
.add_systems(Update, (check_void_death, respawn_player).in_set(IngameSet));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_void_death(
|
||||||
|
mut ev_death: EventWriter<PlayerDeathEvent>,
|
||||||
|
query: Query<&Transform, With<Player>>,
|
||||||
|
// 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<PlayerCoords>,
|
||||||
|
) {
|
||||||
|
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<PlayerDeathEvent>,
|
||||||
|
mut query: Query<&mut Transform, With<Player>>,
|
||||||
|
player_coords: Res<PlayerCoords>,
|
||||||
|
) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue