Crate bevy_trauma_shake

Source
Expand description

§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((Camera2d, Shake::default()));

Make it shake:

fn shake(mut shake: Single<&mut Shake>, keys: Res<ButtonInput<KeyCode>>) {
    if keys.just_pressed(KeyCode::Space) {
        shake.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.

bevybevy_trauma_shake
0.160.5, main
0.150.4
0.140.3
0.130.2
0.120.1

§License

bevy_trauma_shake is dual-licensed under either

at your option.

§Thanks

§Contributions

PRs welcome!

Modules§

prelude
Everything you need to use the plugin

Structs§

Shake
Makes the entity shake according to applied trauma.
ShakeSettings
These can generally be left unchanged.
Shakes
Convenience SystemParam for adding trauma to all Shakes at the same time.
TraumaEvent
Event for adding trauma to all shakes
TraumaPlugin
Plugin for adding trauma-based shakes to 2d cameras (or other things).

Traits§

TraumaCommands
Extension trait for Command, adding commands for easily applying trauma fire-and-forget-style.