use bevy::ecs::system::SystemParam;
use bevy::prelude::*;
use std::ops::{Deref, DerefMut};
use super::super::{DefaultRapierContext, RapierContext, RapierContextEntityLink};
#[derive(SystemParam)]
pub struct ReadDefaultRapierContext<'w, 's, T: Component = DefaultRapierContext> {
rapier_context: Query<'w, 's, &'static RapierContext, With<T>>,
}
impl<'w, 's, T: Component> ReadDefaultRapierContext<'w, 's, T> {
pub fn single(&'_ self) -> &RapierContext {
self.rapier_context.single()
}
}
impl<'w, 's> Deref for ReadDefaultRapierContext<'w, 's> {
type Target = RapierContext;
fn deref(&self) -> &Self::Target {
self.rapier_context.single()
}
}
#[derive(SystemParam)]
pub struct WriteDefaultRapierContext<'w, 's, T: Component = DefaultRapierContext> {
rapier_context: Query<'w, 's, &'static mut RapierContext, With<T>>,
}
impl<'w, 's, T: Component> Deref for WriteDefaultRapierContext<'w, 's, T> {
type Target = RapierContext;
fn deref(&self) -> &Self::Target {
self.rapier_context.single()
}
}
impl<'w, 's> DerefMut for WriteDefaultRapierContext<'w, 's> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.rapier_context.single_mut().into_inner()
}
}
#[derive(SystemParam)]
pub struct RapierContextAccess<'w, 's> {
pub rapier_context: Query<'w, 's, &'static RapierContext>,
}
impl<'w, 's> RapierContextAccess<'w, 's> {
pub fn context(&self, link: &RapierContextEntityLink) -> &'_ RapierContext {
self.try_context(link)
.expect("RapierContextEntityLink.0 refers to an entity without RapierContext.")
}
pub fn try_context(&self, link: &RapierContextEntityLink) -> Option<&'_ RapierContext> {
self.rapier_context.get(link.0).ok()
}
}
impl<'w, 's> Deref for RapierContextAccess<'w, 's> {
type Target = RapierContext;
fn deref(&self) -> &Self::Target {
self.rapier_context.single()
}
}
#[derive(SystemParam)]
pub struct WriteRapierContext<'w, 's> {
pub rapier_context: Query<'w, 's, &'static mut RapierContext>,
}
impl<'w, 's> WriteRapierContext<'w, 's> {
pub fn context(&mut self, link: &RapierContextEntityLink) -> Mut<RapierContext> {
self.try_context(link)
.expect("RapierContextEntityLink.0 refers to an entity without RapierContext.")
}
pub fn try_context(&mut self, link: &RapierContextEntityLink) -> Option<Mut<RapierContext>> {
self.rapier_context.get_mut(link.0).ok()
}
pub fn try_context_from_entity(
&mut self,
rapier_context_entity: Entity,
) -> Option<Mut<RapierContext>> {
self.rapier_context.get_mut(rapier_context_entity).ok()
}
}