MCPcopy Create free account
hub / github.com/CleanCut/rusty_engine / logic

Function logic

examples/mouse_state.rs:45–83  ·  view source on GitHub ↗
(engine: &mut Engine, _: &mut GameState)

Source from the content-addressed store, hash-verified

43}
44
45fn logic(engine: &mut Engine, _: &mut GameState) {
46 if let Some(sprite) = engine.sprites.get_mut("Race Car") {
47 // Use the latest state of the mouse buttons to rotate the sprite
48 let mut rotation_amount = 0.0;
49 if engine.mouse_state.pressed(MouseButton::Left) {
50 rotation_amount += ROTATION_SPEED * engine.delta_f32;
51 }
52 if engine.mouse_state.pressed(MouseButton::Right) {
53 rotation_amount -= ROTATION_SPEED * engine.delta_f32;
54 }
55 sprite.rotation += rotation_amount;
56
57 // Use the latest state of the mouse wheel to scale the sprite
58 if let Some(location) = engine.mouse_state.location() {
59 sprite.translation = location
60 }
61
62 // Honestly, this is probably the one "state" thing that you should ignore in favor of
63 // processing each event instead (see the mouse_events example), since you can then handle
64 // fast spins of the wheel. But here is how to use the mouse wheel state sort of like a
65 // button. `wheel_direction` will be `1.0`, `0.0`, or `-1.0` depending on what's going on
66 // with the mouse wheel.
67 let wheel_direction = engine.mouse_state.wheel().y;
68 sprite.scale *= 1.0 + (wheel_direction * 0.1);
69 sprite.scale = sprite.scale.clamp(0.1, 4.0);
70 }
71
72 // Offset the move indicator from the move indicator origin to visually represent the relative
73 // mouse motion for the frame
74 if let Some(sprite) = engine.sprites.get_mut("move indicator") {
75 let motion = engine.mouse_state.motion();
76 // There seems to be a Bevy 0.6 bug where every other frame we don't receive any mouse
77 // motion events, so ignore those frames.
78 // TODO: Follow up on this bug in upstream Bevy
79 if motion != Vec2::ZERO {
80 sprite.translation = motion + Vec2::from(ORIGIN_LOCATION);
81 }
82 }
83}

Callers

nothing calls this directly

Calls 4

locationMethod · 0.80
wheelMethod · 0.80
motionMethod · 0.80
pressedMethod · 0.45

Tested by

no test coverage detected