nightshade 0.57.0

A cross-platform data-oriented game engine.
Documentation
//! How physics joins whole-world save and load. Restored components carry no
//! rapier handles, so the simulation restarts from them: the resource resets to
//! a fresh world keeping only the settings a game chose.

use crate::ecs::snapshot::hooks::SnapshotCapability;
use crate::ecs::world::World;
use crate::plugins::physics::resources::PhysicsWorld;

/// Registers this plugin's stake in a world snapshot. Called from the plugin's
/// `build`.
pub fn register_snapshot_capability(world: &mut World) {
    crate::ecs::snapshot::hooks::register_snapshot_capability(
        world,
        SnapshotCapability {
            key: "physics",
            member_world: |world| {
                world
                    .ecs
                    .resource::<PhysicsWorld>()
                    .and_then(|physics| physics.component_world)
            },
            registry: crate::plugins::physics::schema::register_physics_components,
            restore: restore_physics,
        },
    );
}

fn restore_physics(world: &mut World, member_world: Option<usize>) {
    if world.ecs.resource::<PhysicsWorld>().is_none() {
        return;
    }
    let physics = world.plugin_resource_mut::<PhysicsWorld>();
    let gravity = physics.gravity;
    let fixed_timestep = physics.fixed_timestep;
    let max_substeps = physics.max_substeps;
    let enabled = physics.enabled;
    let debug_draw = physics.debug_draw;
    *physics = PhysicsWorld::default();
    physics.gravity = gravity;
    physics.fixed_timestep = fixed_timestep;
    physics.max_substeps = max_substeps;
    physics.enabled = enabled;
    physics.debug_draw = debug_draw;
    physics.component_world = member_world;
}