use velocity for movement

This commit is contained in:
Nixon 2022-08-23 00:56:11 +08:00
parent 55278858cb
commit 683bfa2c99
2 changed files with 60 additions and 18 deletions

View file

@ -11,3 +11,6 @@ bevy_fundsp = "0.1.0"
bevy_hanabi = "0.3.0"
bevy-inspector-egui = "0.12.1"
bevy_rapier2d = "0.16.0"
[profile.dev.package."*"]
opt-level = 3

View file

@ -184,6 +184,9 @@ fn spawn_character(
.insert(RigidBody::Dynamic)
.insert(Collider::cuboid(32., 32.))
.insert(ExternalForce::default())
.insert(Velocity::default())
.insert(GravityScale(10.0))
.insert(LockedAxes::ROTATION_LOCKED)
.insert(Friction::new(1.0))
.insert(Damping {
linear_damping: 0.5,
@ -264,6 +267,7 @@ fn keyboard_input_system(
current_level: Res<CurrentLevel>,
mut characters: Query<(
&CharacterId,
&mut Velocity,
&mut ExternalImpulse,
&mut ExternalForce,
&Children,
@ -281,11 +285,21 @@ fn keyboard_input_system(
audio.play(dsp_assets.graph(&sine_wave));
let selected = if let Some(selected_character_id) = &mut selected_character_id.0 {
if let Some((_character_id, _impulse, _force, children)) = characters
.iter_mut()
.find(|(character_id, _impulse, _force, _children)| {
*character_id == selected_character_id
}) {
if let Some((
_character_id,
_velocity,
_impulse,
_force,
children
)) = characters.iter_mut().find(|(
character_id,
_velocity,
_impulse,
_force,
_children
)| { *character_id == selected_character_id }) {
effect
.get_mut(children[0])
.unwrap()
@ -304,9 +318,20 @@ fn keyboard_input_system(
selected_character_id.0 = Some(CharacterId(0));
CharacterId(0)
};
if let Some((_character_id, _impulse, _force, children)) = characters
.iter_mut()
.find(|(character_id, _impulse, _force, _children)| **character_id == selected)
if let Some((
_character_id,
_velocity,
_impulse,
_force,
children
)) = characters.iter_mut().find(|(
character_id,
_velocity,
_impulse,
_force,
_children
)| **character_id == selected)
{
effect
.get_mut(children[0])
@ -316,17 +341,31 @@ fn keyboard_input_system(
.set_active(true);
}
}
let right_pressed: bool = keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D);
let left_pressed: bool = keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A);
if let Some(selected_character_id) = &selected_character_id.0 {
if let Some((_character_id, mut impulse, mut force, _children)) = characters
.iter_mut()
.find(|(character_id, _impulse, _force, _children)| {
*character_id == selected_character_id
}) {
force.force.x = 200.
* (keyboard_input.pressed(KeyCode::Right) as i8
- keyboard_input.pressed(KeyCode::Left) as i8) as f32;
impulse.impulse.y =
keyboard_input.just_pressed(KeyCode::Space) as i8 as f32 * 100.;
if let Some((
_character_id,
mut velocity,
mut impulse,
mut force,
_children
)) = characters.iter_mut().find(|(
character_id,
_velocity,
_impulse,
_force,
_children
)| { *character_id == selected_character_id }) {
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
if keyboard_input.just_pressed(KeyCode::Space) {
velocity.linvel.y = 500.;
}
}
}
}