reset modifs
This commit is contained in:
+35
-7
@@ -1,10 +1,12 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::collision::check_collision_with_player;
|
||||
use crate::collision::{Collider, HasCollided};
|
||||
use crate::obstacle::RegenerateObstacles;
|
||||
|
||||
const FALL_SPEED: f32 = 300.0; // Falling speed
|
||||
const DIVE_SPEED: f32 = 700.0; // Falling speed sped up
|
||||
const HORIZONTAL_SPEED: f32 = 300.0; // Lateral movement speed
|
||||
const FALL_SPEED: f32 = 600.0; // Falling speed
|
||||
const DIVE_SPEED: f32 = 1000.0; // Falling speed sped up
|
||||
const HORIZONTAL_SPEED: f32 = 350.0; // Lateral movement speed
|
||||
pub const START_POSITION: Vec3 = Vec3::new(0.0, 0.0, 2.0); // Start position
|
||||
|
||||
// The player
|
||||
@@ -15,6 +17,10 @@ pub struct Player;
|
||||
#[derive(Component)]
|
||||
pub struct Velocity(Vec2);
|
||||
|
||||
// Falling bool
|
||||
#[derive(Resource, Default)]
|
||||
pub struct FallStarted(pub bool);
|
||||
|
||||
// Spawn entity player
|
||||
fn spawn_player(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
@@ -33,12 +39,24 @@ pub(crate) fn move_player(
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mut player: Query<(&mut Velocity, &mut Transform), With<Player>>,
|
||||
mut fall_started: ResMut<FallStarted>,
|
||||
) {
|
||||
// If matching entity
|
||||
let Ok((mut velocity, mut transform)) = player.single_mut() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if !fall_started.0
|
||||
&& (input.just_pressed(KeyCode::KeyA)
|
||||
|| input.just_pressed(KeyCode::KeyD)
|
||||
|| input.just_pressed(KeyCode::KeyS))
|
||||
{
|
||||
fall_started.0 = true;
|
||||
} else if !fall_started.0 {
|
||||
// Null speed
|
||||
return;
|
||||
}
|
||||
|
||||
// Change then horizontal direction regardless the input
|
||||
let direction = if input.pressed(KeyCode::KeyA) {
|
||||
-1.0
|
||||
@@ -62,18 +80,22 @@ pub(crate) fn move_player(
|
||||
}
|
||||
|
||||
// Reset player position
|
||||
fn reset_player(
|
||||
pub fn reset_player(
|
||||
mut player: Single<(&mut Transform, &mut Sprite), With<Player>>,
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
mut has_collided: ResMut<HasCollided>,
|
||||
mut fall_started: ResMut<FallStarted>,
|
||||
mut regenerate_obstacles: ResMut<RegenerateObstacles>,
|
||||
) {
|
||||
let (mut transform, mut sprite) = player.into_inner();
|
||||
|
||||
// R key for reset
|
||||
if input.just_pressed(KeyCode::KeyR) {
|
||||
// R key for reset or player has collider
|
||||
if input.just_pressed(KeyCode::KeyR) || has_collided.0 {
|
||||
transform.translation = Vec3::new(START_POSITION.x, START_POSITION.y, START_POSITION.z);
|
||||
sprite.color = Color::WHITE;
|
||||
has_collided.0 = false;
|
||||
fall_started.0 = false;
|
||||
regenerate_obstacles.0 = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +104,13 @@ pub struct PlayerPlugin;
|
||||
impl Plugin for PlayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn_player)
|
||||
.init_resource::<FallStarted>()
|
||||
.add_systems(Update, move_player)
|
||||
.add_systems(Update, reset_player.after(move_player));
|
||||
.add_systems(
|
||||
Update,
|
||||
reset_player
|
||||
.after(move_player)
|
||||
.after(check_collision_with_player),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user