diff --git a/bevy_rapier2d/Cargo.toml b/bevy_rapier2d/Cargo.toml index b15d611b..f6d72339 100644 --- a/bevy_rapier2d/Cargo.toml +++ b/bevy_rapier2d/Cargo.toml @@ -64,6 +64,7 @@ approx = "0.5.1" glam = { version = "0.27", features = ["approx"] } bevy-inspector-egui = "0.25.1" bevy_egui = "0.28.0" +serde_json = "1.0" [package.metadata.docs.rs] # Enable all the features when building the docs on docs.rs diff --git a/bevy_rapier2d/examples/joints2.rs b/bevy_rapier2d/examples/joints2.rs index e30352ca..07c10b0f 100644 --- a/bevy_rapier2d/examples/joints2.rs +++ b/bevy_rapier2d/examples/joints2.rs @@ -1,6 +1,7 @@ use bevy::prelude::*; use bevy_rapier2d::prelude::*; +#[allow(unused)] fn main() { App::new() .insert_resource(ClearColor(Color::srgb( diff --git a/bevy_rapier2d/examples/serialization2.rs b/bevy_rapier2d/examples/serialization2.rs new file mode 100644 index 00000000..8beffc54 --- /dev/null +++ b/bevy_rapier2d/examples/serialization2.rs @@ -0,0 +1,39 @@ +//! Example for RapierContext serialization, run with `--features serde-serialize`. + +use bevy::prelude::*; +use bevy::MinimalPlugins; +use bevy_rapier2d::prelude::*; + +/// Note: This will end up in duplication for testbed, but that's more simple. +mod joints2; + +fn main() { + App::new() + .insert_resource(ClearColor(Color::srgb( + 0xF9 as f32 / 255.0, + 0xF9 as f32 / 255.0, + 0xFF as f32 / 255.0, + ))) + .add_plugins(( + MinimalPlugins, + RapierPhysicsPlugin::::pixels_per_meter(100.0), + )) + .add_systems(Startup, joints2::setup_physics) + .add_systems(PostUpdate, print_physics) + .add_systems(Last, quit) + .run(); +} + +pub fn print_physics(_context: Res) { + #[cfg(feature = "serde-serialize")] + println!( + "{}", + serde_json::to_string_pretty(&(*_context)).expect("Unable to serialize `RapierContext`") + ); + #[cfg(not(feature = "serde-serialize"))] + panic!("Example 'serialization' should be run with '--features serde-serialize'."); +} + +fn quit(mut exit_event: EventWriter) { + exit_event.send(AppExit::Success); +}