Skip to content

Commit

Permalink
chore(behaviors): simplify move_on_orbit
Browse files Browse the repository at this point in the history
`MouseMotion` events not seem to work as expected. Code will be simplified and explanatory comments removed.
  • Loading branch information
codaishin committed Jul 16, 2024
1 parent 80a891a commit e439a3b
Showing 1 changed file with 1 addition and 46 deletions.
47 changes: 1 addition & 46 deletions src/plugins/behaviors/src/systems/move_on_orbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ pub(crate) fn move_on_orbit<TOrbitComponent: Orbit + Component>(
mut query: Query<(&TOrbitComponent, &mut Transform)>,
) {
if !mouse.pressed(MouseButton::Right) {
// It seems odd to do this, but after moving this into a plugin we observe
// events to persist more than 2 frames, so we clear manually.
// This seems to be at odds with the documentation at https://docs.rs/bevy/0.12.1/bevy/prelude/struct.Events.html
// but in line with comments in https://github.com/bevyengine/bevy/issues/10860.
mouse_motion.clear();
return;
}

Expand Down Expand Up @@ -57,10 +52,7 @@ mod tests {
let input = ButtonInput::<MouseButton>::default();

app.add_systems(Update, move_on_orbit::<_Orbit>);
// Not using `add_event`, because we don't want events to be cleared between frames
// so we can simulate the observed behavior of events not being cleared when running
// the plugin within our game.
app.init_resource::<Events<MouseMotion>>();
app.add_event::<MouseMotion>();
app.insert_resource(input);

app
Expand Down Expand Up @@ -112,41 +104,4 @@ mod tests {

app.update();
}

#[test]
fn disregard_events_that_happened_when_not_right_mouse_pressed() {
let mut app = setup_app();
let mut orbit = _Orbit::new();
let agent = Transform::from_translation(Vec3::ZERO);
let angels = Vec2::new(3., 4.);
let discard_angles = Vec2::new(100., 200.);

orbit
.mock
.expect_orbit()
.with(eq(agent), eq(angels))
.times(1)
.return_const(());

app.world_mut().spawn((orbit, agent));

app.update();

app.world_mut()
.resource_mut::<Events<MouseMotion>>()
.send(MouseMotion {
delta: discard_angles,
});

app.update();

app.world_mut()
.resource_mut::<Events<MouseMotion>>()
.send(MouseMotion { delta: angels });
app.world_mut()
.resource_mut::<ButtonInput<MouseButton>>()
.press(MouseButton::Right);

app.update();
}
}

0 comments on commit e439a3b

Please sign in to comment.