Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic serialization example #560

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bevy_rapier2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions bevy_rapier2d/examples/joints2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

#[allow(unused)]
fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
Expand Down
39 changes: 39 additions & 0 deletions bevy_rapier2d/examples/serialization2.rs
Original file line number Diff line number Diff line change
@@ -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::<NoUserData>::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<RapierContext>) {
#[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<AppExit>) {
exit_event.send(AppExit::Success);
}