Switch Observer and Trigger to StaticBundle
This commit is contained in:
parent
07f6d1df90
commit
ddd69c56c4
@ -9,6 +9,7 @@ use alloc::{
|
||||
};
|
||||
pub use bevy_derive::AppLabel;
|
||||
use bevy_ecs::{
|
||||
bundle::StaticBundle,
|
||||
component::RequiredComponentsError,
|
||||
error::{DefaultErrorHandler, ErrorHandler},
|
||||
event::{event_update_system, EventCursor},
|
||||
@ -1340,7 +1341,7 @@ impl App {
|
||||
/// }
|
||||
/// });
|
||||
/// ```
|
||||
pub fn add_observer<E: Event, B: Bundle, M>(
|
||||
pub fn add_observer<E: Event, B: StaticBundle, M>(
|
||||
&mut self,
|
||||
observer: impl IntoObserverSystem<E, B, M>,
|
||||
) -> &mut Self {
|
||||
|
@ -138,6 +138,7 @@ use variadics_please::all_tuples;
|
||||
|
||||
use crate::{
|
||||
archetype::ArchetypeFlags,
|
||||
bundle::StaticBundle,
|
||||
change_detection::MaybeLocation,
|
||||
component::ComponentId,
|
||||
entity::EntityHashMap,
|
||||
@ -159,7 +160,7 @@ use smallvec::SmallVec;
|
||||
/// [`Event`] data itself. If it was triggered for a specific [`Entity`], it includes that as well. It also
|
||||
/// contains event propagation information. See [`On::propagate`] for more information.
|
||||
///
|
||||
/// The generic `B: Bundle` is used to modify the further specialize the events that this observer is interested in.
|
||||
/// The generic `B: StaticBundle` is used to modify the further specialize the events that this observer is interested in.
|
||||
/// The entity involved *does not* have to have these components, but the observer will only be
|
||||
/// triggered if the event matches the components in `B`.
|
||||
///
|
||||
@ -169,7 +170,7 @@ use smallvec::SmallVec;
|
||||
/// Providing multiple components in this bundle will cause this event to be triggered by any
|
||||
/// matching component in the bundle,
|
||||
/// [rather than requiring all of them to be present](https://github.com/bevyengine/bevy/issues/15325).
|
||||
pub struct On<'w, E, B: Bundle = ()> {
|
||||
pub struct On<'w, E, B: StaticBundle = ()> {
|
||||
event: &'w mut E,
|
||||
propagate: &'w mut bool,
|
||||
trigger: ObserverTrigger,
|
||||
@ -180,7 +181,7 @@ pub struct On<'w, E, B: Bundle = ()> {
|
||||
#[deprecated(since = "0.17.0", note = "Renamed to `On`.")]
|
||||
pub type Trigger<'w, E, B = ()> = On<'w, E, B>;
|
||||
|
||||
impl<'w, E, B: Bundle> On<'w, E, B> {
|
||||
impl<'w, E, B: StaticBundle> On<'w, E, B> {
|
||||
/// Creates a new instance of [`On`] for the given event and observer information.
|
||||
pub fn new(event: &'w mut E, propagate: &'w mut bool, trigger: ObserverTrigger) -> Self {
|
||||
Self {
|
||||
@ -250,7 +251,7 @@ impl<'w, E, B: Bundle> On<'w, E, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'w, E: EntityEvent, B: Bundle> On<'w, E, B> {
|
||||
impl<'w, E: EntityEvent, B: StaticBundle> On<'w, E, B> {
|
||||
/// Returns the [`Entity`] that was targeted by the `event` that triggered this observer.
|
||||
///
|
||||
/// Note that if event propagation is enabled, this may not be the same as the original target of the event,
|
||||
@ -294,7 +295,7 @@ impl<'w, E: EntityEvent, B: Bundle> On<'w, E, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'w, E: Debug, B: Bundle> Debug for On<'w, E, B> {
|
||||
impl<'w, E: Debug, B: StaticBundle> Debug for On<'w, E, B> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("On")
|
||||
.field("event", &self.event)
|
||||
@ -305,7 +306,7 @@ impl<'w, E: Debug, B: Bundle> Debug for On<'w, E, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'w, E, B: Bundle> Deref for On<'w, E, B> {
|
||||
impl<'w, E, B: StaticBundle> Deref for On<'w, E, B> {
|
||||
type Target = E;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@ -313,7 +314,7 @@ impl<'w, E, B: Bundle> Deref for On<'w, E, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'w, E, B: Bundle> DerefMut for On<'w, E, B> {
|
||||
impl<'w, E, B: StaticBundle> DerefMut for On<'w, E, B> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.event
|
||||
}
|
||||
@ -767,7 +768,7 @@ impl World {
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the given system is an exclusive system.
|
||||
pub fn add_observer<E: Event, B: Bundle, M>(
|
||||
pub fn add_observer<E: Event, B: StaticBundle, M>(
|
||||
&mut self,
|
||||
system: impl IntoObserverSystem<E, B, M>,
|
||||
) -> EntityWorldMut {
|
||||
|
@ -3,6 +3,7 @@ use bevy_utils::prelude::DebugName;
|
||||
use core::any::Any;
|
||||
|
||||
use crate::{
|
||||
bundle::StaticBundle,
|
||||
component::{ComponentId, Mutable, StorageType},
|
||||
error::{ErrorContext, ErrorHandler},
|
||||
lifecycle::{ComponentHook, HookContext},
|
||||
@ -209,7 +210,7 @@ impl Observer {
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the given system is an exclusive system.
|
||||
pub fn new<E: Event, B: Bundle, M, I: IntoObserverSystem<E, B, M>>(system: I) -> Self {
|
||||
pub fn new<E: Event, B: StaticBundle, M, I: IntoObserverSystem<E, B, M>>(system: I) -> Self {
|
||||
let system = Box::new(IntoObserverSystem::into_system(system));
|
||||
assert!(
|
||||
!system.is_exclusive(),
|
||||
@ -336,7 +337,7 @@ impl Component for Observer {
|
||||
}
|
||||
}
|
||||
|
||||
fn observer_system_runner<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
|
||||
fn observer_system_runner<E: Event, B: StaticBundle, S: ObserverSystem<E, B>>(
|
||||
mut world: DeferredWorld,
|
||||
observer_trigger: ObserverTrigger,
|
||||
ptr: PtrMut,
|
||||
@ -438,7 +439,7 @@ impl<T: Any + System> AnyNamedSystem for T {
|
||||
/// The type parameters of this function _must_ match those used to create the [`Observer`].
|
||||
/// As such, it is recommended to only use this function within the [`Observer::new`] method to
|
||||
/// ensure type parameters match.
|
||||
fn hook_on_add<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
|
||||
fn hook_on_add<E: Event, B: StaticBundle, S: ObserverSystem<E, B>>(
|
||||
mut world: DeferredWorld<'_>,
|
||||
HookContext { entity, .. }: HookContext,
|
||||
) {
|
||||
|
@ -8,7 +8,7 @@ use alloc::vec::Vec;
|
||||
use log::info;
|
||||
|
||||
use crate::{
|
||||
bundle::{Bundle, InsertMode},
|
||||
bundle::{Bundle, InsertMode, StaticBundle},
|
||||
change_detection::MaybeLocation,
|
||||
component::{Component, ComponentId, ComponentInfo},
|
||||
entity::{Entity, EntityClonerBuilder},
|
||||
@ -218,7 +218,7 @@ pub fn despawn() -> impl EntityCommand {
|
||||
/// An [`EntityCommand`] that creates an [`Observer`](crate::observer::Observer)
|
||||
/// listening for events of type `E` targeting an entity
|
||||
#[track_caller]
|
||||
pub fn observe<E: EntityEvent, B: Bundle, M>(
|
||||
pub fn observe<E: EntityEvent, B: StaticBundle, M>(
|
||||
observer: impl IntoObserverSystem<E, B, M>,
|
||||
) -> impl EntityCommand {
|
||||
let caller = MaybeLocation::caller();
|
||||
|
@ -15,7 +15,7 @@ use core::marker::PhantomData;
|
||||
|
||||
use crate::{
|
||||
self as bevy_ecs,
|
||||
bundle::{Bundle, InsertMode, NoBundleEffect},
|
||||
bundle::{Bundle, InsertMode, NoBundleEffect, StaticBundle},
|
||||
change_detection::{MaybeLocation, Mut},
|
||||
component::{Component, ComponentId, Mutable},
|
||||
entity::{Entities, Entity, EntityClonerBuilder, EntityDoesNotExistError},
|
||||
@ -1114,7 +1114,7 @@ impl<'w, 's> Commands<'w, 's> {
|
||||
/// Panics if the given system is an exclusive system.
|
||||
///
|
||||
/// [`On`]: crate::observer::On
|
||||
pub fn add_observer<E: Event, B: Bundle, M>(
|
||||
pub fn add_observer<E: Event, B: StaticBundle, M>(
|
||||
&mut self,
|
||||
observer: impl IntoObserverSystem<E, B, M>,
|
||||
) -> EntityCommands {
|
||||
@ -1968,7 +1968,7 @@ impl<'a> EntityCommands<'a> {
|
||||
}
|
||||
|
||||
/// Creates an [`Observer`] listening for events of type `E` targeting this entity.
|
||||
pub fn observe<E: EntityEvent, B: Bundle, M>(
|
||||
pub fn observe<E: EntityEvent, B: StaticBundle, M>(
|
||||
&mut self,
|
||||
observer: impl IntoObserverSystem<E, B, M>,
|
||||
) -> &mut Self {
|
||||
|
@ -2,7 +2,7 @@ use core::ops::{Deref, DerefMut};
|
||||
|
||||
use variadics_please::all_tuples;
|
||||
|
||||
use crate::{bundle::Bundle, prelude::On, system::System};
|
||||
use crate::{bundle::StaticBundle, prelude::On, system::System};
|
||||
|
||||
/// Trait for types that can be used as input to [`System`]s.
|
||||
///
|
||||
@ -222,7 +222,7 @@ impl<'i, T: ?Sized> DerefMut for InMut<'i, T> {
|
||||
/// Used for [`ObserverSystem`]s.
|
||||
///
|
||||
/// [`ObserverSystem`]: crate::system::ObserverSystem
|
||||
impl<E: 'static, B: Bundle> SystemInput for On<'_, E, B> {
|
||||
impl<E: 'static, B: StaticBundle> SystemInput for On<'_, E, B> {
|
||||
type Param<'i> = On<'i, E, B>;
|
||||
type Inner<'i> = On<'i, E, B>;
|
||||
|
||||
|
@ -3,10 +3,11 @@ use bevy_utils::prelude::DebugName;
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use crate::{
|
||||
bundle::StaticBundle,
|
||||
component::{CheckChangeTicks, ComponentId, Tick},
|
||||
error::Result,
|
||||
never::Never,
|
||||
prelude::{Bundle, On},
|
||||
prelude::On,
|
||||
query::FilteredAccessSet,
|
||||
schedule::{Fallible, Infallible},
|
||||
system::{input::SystemIn, System},
|
||||
@ -16,12 +17,12 @@ use crate::{
|
||||
use super::{IntoSystem, SystemParamValidationError};
|
||||
|
||||
/// Implemented for [`System`]s that have [`On`] as the first argument.
|
||||
pub trait ObserverSystem<E: 'static, B: Bundle, Out = Result>:
|
||||
pub trait ObserverSystem<E: 'static, B: StaticBundle, Out = Result>:
|
||||
System<In = On<'static, E, B>, Out = Out> + Send + 'static
|
||||
{
|
||||
}
|
||||
|
||||
impl<E: 'static, B: Bundle, Out, T> ObserverSystem<E, B, Out> for T where
|
||||
impl<E: 'static, B: StaticBundle, Out, T> ObserverSystem<E, B, Out> for T where
|
||||
T: System<In = On<'static, E, B>, Out = Out> + Send + 'static
|
||||
{
|
||||
}
|
||||
@ -38,7 +39,7 @@ impl<E: 'static, B: Bundle, Out, T> ObserverSystem<E, B, Out> for T where
|
||||
label = "the trait `IntoObserverSystem` is not implemented",
|
||||
note = "for function `ObserverSystem`s, ensure the first argument is `On<T>` and any subsequent ones are `SystemParam`"
|
||||
)]
|
||||
pub trait IntoObserverSystem<E: 'static, B: Bundle, M, Out = Result>: Send + 'static {
|
||||
pub trait IntoObserverSystem<E: 'static, B: StaticBundle, M, Out = Result>: Send + 'static {
|
||||
/// The type of [`System`] that this instance converts into.
|
||||
type System: ObserverSystem<E, B, Out>;
|
||||
|
||||
@ -51,7 +52,7 @@ where
|
||||
S: IntoSystem<On<'static, E, B>, Out, M> + Send + 'static,
|
||||
S::System: ObserverSystem<E, B, Out>,
|
||||
E: 'static,
|
||||
B: Bundle,
|
||||
B: StaticBundle,
|
||||
{
|
||||
type System = S::System;
|
||||
|
||||
@ -65,7 +66,7 @@ where
|
||||
S: IntoSystem<On<'static, E, B>, (), M> + Send + 'static,
|
||||
S::System: ObserverSystem<E, B, ()>,
|
||||
E: Send + Sync + 'static,
|
||||
B: Bundle,
|
||||
B: StaticBundle,
|
||||
{
|
||||
type System = InfallibleObserverWrapper<E, B, S::System, ()>;
|
||||
|
||||
@ -77,7 +78,7 @@ impl<E, B, M, S> IntoObserverSystem<E, B, (Never, M), Result> for S
|
||||
where
|
||||
S: IntoSystem<On<'static, E, B>, Never, M> + Send + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
B: Bundle,
|
||||
B: StaticBundle,
|
||||
{
|
||||
type System = InfallibleObserverWrapper<E, B, S::System, Never>;
|
||||
|
||||
@ -106,7 +107,7 @@ impl<E, B, S, Out> System for InfallibleObserverWrapper<E, B, S, Out>
|
||||
where
|
||||
S: ObserverSystem<E, B, Out>,
|
||||
E: Send + Sync + 'static,
|
||||
B: Bundle,
|
||||
B: StaticBundle,
|
||||
Out: Send + Sync + 'static,
|
||||
{
|
||||
type In = On<'static, E, B>;
|
||||
|
@ -2649,14 +2649,14 @@ impl<'w> EntityWorldMut<'w> {
|
||||
///
|
||||
/// Panics if the given system is an exclusive system.
|
||||
#[track_caller]
|
||||
pub fn observe<E: EntityEvent, B: Bundle, M>(
|
||||
pub fn observe<E: EntityEvent, B: StaticBundle, M>(
|
||||
&mut self,
|
||||
observer: impl IntoObserverSystem<E, B, M>,
|
||||
) -> &mut Self {
|
||||
self.observe_with_caller(observer, MaybeLocation::caller())
|
||||
}
|
||||
|
||||
pub(crate) fn observe_with_caller<E: EntityEvent, B: Bundle, M>(
|
||||
pub(crate) fn observe_with_caller<E: EntityEvent, B: StaticBundle, M>(
|
||||
&mut self,
|
||||
observer: impl IntoObserverSystem<E, B, M>,
|
||||
caller: MaybeLocation,
|
||||
|
Loading…
Reference in New Issue
Block a user