bevy_mod_component_mirror

Crates.iobevy_mod_component_mirror
lib.rsbevy_mod_component_mirror
version0.11.0
sourcesrc
created_at2023-01-29 20:19:10.466504
updated_at2023-11-10 09:27:40.648967
descriptionA bevy plugin to mirror components
homepage
repositoryhttps://github.com/devildahu/bevy_mod_component_mirror
max_upload_size
id771177
size43,318
Nicola Papale (nicopap)

documentation

README

Bevy Component Mirrors

Bevy tracking Latest version MIT/Apache 2.0 Documentation

A third party crate to mirror Component values.

By default, it also provides a set of bevy Components mirroring the values of bevy_rapier Components. (Currently only bevy_rapier3d, PRs welcome!) Since some of bevy_rapier Components do not implement Reflect, they may be harder to work with. This crate is especially useful with bevy-inspector-egui, it will allow you to edit rapier values at run time, so you don't have to restart your game repetitively to find the right physics parameters.

Usage

  1. Add this crate as a dependency to your Cargo.toml.
[dependencies]
bevy_mod_component_mirror = "0.11.0"
  1. Add RapierMirrorsPlugins to your app
use bevy_mod_component_mirror::RapierMirrorsPlugins;

# fn main() {
# let mut app = bevy::prelude::App::new();
app
  // Notice  v the plural
  .add_plugins(RapierMirrorsPlugins);
# }

That's it! Now every Entity with the following rapier (3d) components will automatically have an equivalent XyzMirror component that automatically syncs its value with it.

  • ImpulseJoint
  • Collider (some collider shape are not implemented yet!)
  • ColliderMassProperties
  • AdditionalMassProperties

Implement your own mirrors

If you wish to mirror other components, you need to do the following:

  • Create a Component (eg: ForeignMirror)
  • Implement the Mirror trait for that component.
  • Implement From<&'a Foreign> for ForeignMirror
  • Add MirrorPlugin::<Foreign, ForeignMirror>::new() to your app
use bevy_mod_component_mirror::{Mirror, MirrorPlugin};
use bevy::prelude::*;

# mod foreign_crate {
#   use super::*;
#   #[derive(Component)]
#   pub struct Foreign; impl Foreign {
#   pub fn set_length(&mut self, value: f32) {}
#   pub fn length(&self) -> f32 { 0.0 }
# }}
use foreign_crate::Foreign;

// Component: required because you want it to be a component
// Reflect: this let `MirrorPlugin` register the `Mirror` type itself
#[derive(Component, Reflect)]
pub struct ForeignMirror {
  inner: f32,
}

// Foreign → ForeignMirror
impl<'a> From<&'a Foreign> for ForeignMirror {
  fn from(value: &'a Foreign) -> Self {
    ForeignMirror {
      inner: value.length(),
    }
  }
}
// ForeignMirror → Foreign
impl Mirror<Foreign> for ForeignMirror {
  fn apply(&self, value: &mut Foreign) {
    value.set_length(self.inner);
  }
}

fn main() {
  let mut app = App::new();
  app.add_plugins(MirrorPlugin::<Foreign, ForeignMirror>::new());
}

Features

If you don't need the definitions for the rapier components but still wish to use the mirror plugin, you can disable the rapier components with:

[dependencies]
bevy_mod_component_mirror = { version = "0.11.0", default-features = false }

Version matrix

bevy bevy_rapier3d bevy_mod_component_mirror
0.12 0.23.0 0.11.0
0.11 0.22.0 0.10.0
0.10 0.21.0 0.9
0.9 0.20.0 0.7

Change log

  • 0.11: BREAKING: Bump to bevy 0.12 & rapier 0.23
  • 0.10: BREAKING: Bump to bevy 0.11 & rapier 0.22 (Thanks Naomijub on GitHub, See #3)
  • 0.9: Fix a compilation error which source is currently unknown

Development

Consider moving .git/hooks/pre-commit.sample to .git/hooks/pre-commit.sample, and adding the following lines before the last exec one:

if ! make pre-hook ; then
  echo Error: Some pre-commit checks did not pass!
  exit 1
fi

License

Copyright © 2022 Nicola Papale

This software is licensed under either MIT or Apache 2.0 at your leisure. See licenses directory for details.

Commit count: 15

cargo fmt