use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use crate::state::Game;
pub const PIXELS_PER_METER: f32 = 16.0;
pub const GRAVITY: Vec2 = Vec2::ZERO;
#[allow(clippy::module_name_repetitions)]
pub struct PhysicsPlugin;
impl Plugin for PhysicsPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(
PIXELS_PER_METER,
))
.insert_resource(RapierConfiguration {
gravity: GRAVITY,
..default()
});
app.add_systems(Update, display_events.run_if(in_state(Game::Playing)));
#[cfg(debug_assertions)]
{
app.add_plugins(RapierDebugRenderPlugin {
enabled: false,
..default()
});
app.add_systems(Update, toggle_debug_rendering);
}
}
}
#[allow(clippy::needless_pass_by_value)]
fn toggle_debug_rendering(
mut debug_render_context: ResMut<DebugRenderContext>,
keyboard_input: Res<Input<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::F3) {
debug_render_context.enabled = !debug_render_context.enabled;
}
}
fn display_events(
mut collision_events: EventReader<CollisionEvent>,
mut contact_force_events: EventReader<ContactForceEvent>,
) {
for collision_event in collision_events.read() {
info!("Received collision event: {:?}", collision_event);
}
for contact_force_event in contact_force_events.read() {
info!("Received contact force event: {:?}", contact_force_event);
}
}