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_hanabi = "0.3.0"
bevy-inspector-egui = "0.12.1" bevy-inspector-egui = "0.12.1"
bevy_rapier2d = "0.16.0" bevy_rapier2d = "0.16.0"
[profile.dev.package."*"]
opt-level = 3

View file

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