4 releases (breaking)

new 0.4.0 Dec 8, 2024
0.3.0 Jul 13, 2024
0.2.0 Feb 23, 2024
0.1.0 Nov 18, 2023

#2164 in Game dev

Download history 8/week @ 2024-08-17 10/week @ 2024-08-24 17/week @ 2024-08-31 19/week @ 2024-09-07 16/week @ 2024-09-14 23/week @ 2024-09-21 33/week @ 2024-09-28 6/week @ 2024-10-05 22/week @ 2024-10-12 7/week @ 2024-10-19 7/week @ 2024-10-26 30/week @ 2024-11-02 10/week @ 2024-11-09 21/week @ 2024-11-16 75/week @ 2024-11-23 19/week @ 2024-11-30

131 downloads per month

MIT/Apache

36KB
172 lines

bevy_trauma_shake

crates.io MIT/Apache 2.0 crates.io docs.rs

Add camera shakes to your 2d Bevy game with three lines of code.

Goals

  • Zero configuration required
  • Sensible defaults
  • Batteries included (default noise)
  • Compatible with bevy_pancam

Usage

Add the plugin:

app.add_plugins(TraumaPlugin);

Simply add a component to your camera:

commands.spawn((Camera2dBundle::default(), Shake::default()));

Make it shake:

fn shake(mut shake: Query<&mut Shake>, keys: Res<ButtonInput<KeyCode>>) {
    if keys.just_pressed(KeyCode::Space) {
        shake.single_mut().add_trauma(0.2);
    }
}

There is also a convenience system param for applying trauma to all Shakes:

fn shake(mut shake: Shakes, keys: Res<ButtonInput<KeyCode>>) {
    if keys.just_pressed(KeyCode::Space) {
        shakes.add_trauma(0.2);
    }
}

And an event, if you prefer that:

fn shake(mut trauma: EventWriter<TraumaEvent>, keys: Res<ButtonInput<KeyCode>>) {
    if keys.just_pressed(KeyCode::Space) {
        trauma.send(0.2.into());
    }
}

And even a command:

fn shake(mut commands: Commands, keys: Res<ButtonInput<KeyCode>>) {
    if keys.just_pressed(KeyCode::Space) {
        info!("Adding small trauma");
        commands.add_trauma(0.2);
    }
}

Maybe I went a little overboard and I should remove one of those ways, in any case, they can be toggled through the features: system_param, events, commands.

Optional configuration

Optionally add ShakeSettings, if you're not happy with the defaults.

    commands.spawn((
        Name::new("Camera"),
        Camera2dBundle::default(),
        Shake::default(),
        ShakeSettings {
            amplitude: 200.,
            trauma_power: 3.,
            decay_per_second: 0.3,
            frequency: 4.,
            octaves: 2,
        },
        PanCam::default(),
    ));

Bevy Version Support

The main branch targets the latest bevy release.

bevy bevy_trauma_shake
0.15 0.4, main
0.14 0.3
0.13 0.2
0.12 0.1

License

bevy_trauma_shake is dual-licensed under either

at your option.

Thanks

Contributions

PRs welcome!

Dependencies

~39–72MB
~1.5M SLoC