Collision added
This commit is contained in:
+13
-4
@@ -1,9 +1,11 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::collision::{Collider, HasCollided};
|
||||||
|
|
||||||
const FALL_SPEED: f32 = 300.0; // Falling speed
|
const FALL_SPEED: f32 = 300.0; // Falling speed
|
||||||
const DIVE_SPEED: f32 = 700.0; // Falling speed sped up
|
const DIVE_SPEED: f32 = 700.0; // Falling speed sped up
|
||||||
const HORIZONTAL_SPEED: f32 = 300.0; // Lateral movement speed
|
const HORIZONTAL_SPEED: f32 = 300.0; // Lateral movement speed
|
||||||
const START_POSITION: (f32, f32, f32) = (0.0, 0.0, 1.0);
|
const START_POSITION: Vec3 = Vec3::new(0.0, 0.0, 2.0); // Start position
|
||||||
|
|
||||||
// The player
|
// The player
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
@@ -17,9 +19,12 @@ pub struct Velocity(Vec2);
|
|||||||
fn spawn_player(mut commands: Commands) {
|
fn spawn_player(mut commands: Commands) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Player,
|
Player,
|
||||||
|
Collider {
|
||||||
|
size: Vec2::new(20.0, 40.0),
|
||||||
|
},
|
||||||
Velocity(Vec2::ZERO),
|
Velocity(Vec2::ZERO),
|
||||||
Sprite::from_color(Color::srgb(1.0, 1.0, 1.0), Vec2::new(20.0, 40.0)),
|
Sprite::from_color(Color::srgb(1.0, 1.0, 1.0), Vec2::new(20.0, 40.0)),
|
||||||
Transform::from_xyz(START_POSITION.0, START_POSITION.1, START_POSITION.2),
|
Transform::from_xyz(START_POSITION.x, START_POSITION.y, START_POSITION.z),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +57,7 @@ pub(crate) fn move_player(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Position update (no smoothness in velocity)
|
||||||
transform.translation += velocity.0.extend(0.0) * time.delta_secs();
|
transform.translation += velocity.0.extend(0.0) * time.delta_secs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +65,15 @@ pub(crate) fn move_player(
|
|||||||
fn reset_player(
|
fn reset_player(
|
||||||
mut player: Single<(&mut Transform, &mut Sprite), With<Player>>,
|
mut player: Single<(&mut Transform, &mut Sprite), With<Player>>,
|
||||||
input: Res<ButtonInput<KeyCode>>,
|
input: Res<ButtonInput<KeyCode>>,
|
||||||
|
mut has_collided: ResMut<HasCollided>,
|
||||||
) {
|
) {
|
||||||
let (mut transform, mut sprite) = player.into_inner();
|
let (mut transform, mut sprite) = player.into_inner();
|
||||||
|
|
||||||
|
// R key for reset
|
||||||
if input.just_pressed(KeyCode::KeyR) {
|
if input.just_pressed(KeyCode::KeyR) {
|
||||||
transform.translation = Vec3::new(START_POSITION.0, START_POSITION.1, START_POSITION.2);
|
transform.translation = Vec3::new(START_POSITION.x, START_POSITION.y, START_POSITION.z);
|
||||||
sprite.color = Color::WHITE
|
sprite.color = Color::WHITE;
|
||||||
|
has_collided.0 = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user