mirror of
https://github.com/Noratrieb/spaceship.git
synced 2026-01-16 09:25:06 +01:00
forces
This commit is contained in:
parent
07f17e5e02
commit
aa1b983764
4 changed files with 198 additions and 38 deletions
35
src/forces.rs
Normal file
35
src/forces.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use std::any::TypeId;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier3d::prelude::ExternalForce;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct ExternalForceSet {
|
||||
forces: IndexMap<TypeId, ExternalForce, ahash::RandomState>,
|
||||
}
|
||||
|
||||
impl ExternalForceSet {
|
||||
pub fn get<T: 'static>(&self) -> ExternalForce {
|
||||
self.forces
|
||||
.get(&TypeId::of::<T>())
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn set<T: 'static>(&mut self, force: ExternalForce) {
|
||||
self.forces.insert(TypeId::of::<T>(), force);
|
||||
}
|
||||
|
||||
fn combine(&self) -> ExternalForce {
|
||||
self.forces
|
||||
.values()
|
||||
.fold(ExternalForce::default(), |f1, &f2| f1 + f2)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_external_forces(mut query: Query<(&mut ExternalForce, &ExternalForceSet)>) {
|
||||
for (mut force, forces) in &mut query {
|
||||
*force = forces.combine();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue