@@ -5,7 +5,7 @@ use crate::{
5
5
component:: Component ,
6
6
entity:: { Entity , EntityMap , MapEntities , MapEntitiesError } ,
7
7
system:: Resource ,
8
- world:: { FromWorld , World } ,
8
+ world:: { EntityMut , EntityRef , FromWorld , World } ,
9
9
} ;
10
10
use bevy_reflect:: {
11
11
impl_from_reflect_value, impl_reflect_value, FromType , Reflect , ReflectDeserialize ,
@@ -44,13 +44,15 @@ pub struct ReflectComponentFns {
44
44
/// Function pointer implementing [`ReflectComponent::insert()`].
45
45
pub insert : fn ( & mut World , Entity , & dyn Reflect ) ,
46
46
/// Function pointer implementing [`ReflectComponent::apply()`].
47
- pub apply : fn ( & mut World , Entity , & dyn Reflect ) ,
47
+ pub apply : fn ( EntityMut , & dyn Reflect ) ,
48
48
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
49
49
pub apply_or_insert : fn ( & mut World , Entity , & dyn Reflect ) ,
50
50
/// Function pointer implementing [`ReflectComponent::remove()`].
51
- pub remove : fn ( & mut World , Entity ) ,
51
+ pub remove : fn ( EntityMut ) ,
52
+ /// Function pointer implementing [`ReflectComponent::contains()`].
53
+ pub contains : fn ( EntityRef ) -> bool ,
52
54
/// Function pointer implementing [`ReflectComponent::reflect()`].
53
- pub reflect : fn ( & World , Entity ) -> Option < & dyn Reflect > ,
55
+ pub reflect : fn ( EntityRef ) -> Option < & dyn Reflect > ,
54
56
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
55
57
pub reflect_mut : unsafe fn ( & World , Entity ) -> Option < Mut < dyn Reflect > > ,
56
58
/// Function pointer implementing [`ReflectComponent::copy()`].
@@ -82,9 +84,9 @@ impl ReflectComponent {
82
84
///
83
85
/// # Panics
84
86
///
85
- /// Panics if there is no [`Component`] of the given type or the `entity` does not exist .
86
- pub fn apply ( & self , world : & mut World , entity : Entity , component : & dyn Reflect ) {
87
- ( self . 0 . apply ) ( world , entity, component) ;
87
+ /// Panics if there is no [`Component`] of the given type.
88
+ pub fn apply ( & self , entity : EntityMut , component : & dyn Reflect ) {
89
+ ( self . 0 . apply ) ( entity, component) ;
88
90
}
89
91
90
92
/// Uses reflection to set the value of this [`Component`] type in the entity to the given value or insert a new one if it does not exist.
@@ -100,14 +102,19 @@ impl ReflectComponent {
100
102
///
101
103
/// # Panics
102
104
///
103
- /// Panics if there is no [`Component`] of the given type or the `entity` does not exist.
104
- pub fn remove ( & self , world : & mut World , entity : Entity ) {
105
- ( self . 0 . remove ) ( world, entity) ;
105
+ /// Panics if there is no [`Component`] of the given type.
106
+ pub fn remove ( & self , entity : EntityMut ) {
107
+ ( self . 0 . remove ) ( entity) ;
108
+ }
109
+
110
+ /// Returns whether entity contains this [`Component`]
111
+ pub fn contains ( & self , entity : EntityRef ) -> bool {
112
+ ( self . 0 . contains ) ( entity)
106
113
}
107
114
108
115
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
109
- pub fn reflect < ' a > ( & self , world : & ' a World , entity : Entity ) -> Option < & ' a dyn Reflect > {
110
- ( self . 0 . reflect ) ( world , entity)
116
+ pub fn reflect < ' a > ( & self , entity : EntityRef < ' a > ) -> Option < & ' a dyn Reflect > {
117
+ ( self . 0 . reflect ) ( entity)
111
118
}
112
119
113
120
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
@@ -178,8 +185,8 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
178
185
component. apply ( reflected_component) ;
179
186
world. entity_mut ( entity) . insert ( component) ;
180
187
} ,
181
- apply : |world , entity, reflected_component| {
182
- let mut component = world . get_mut :: < C > ( entity ) . unwrap ( ) ;
188
+ apply : |mut entity, reflected_component| {
189
+ let mut component = entity . get_mut :: < C > ( ) . unwrap ( ) ;
183
190
component. apply ( reflected_component) ;
184
191
} ,
185
192
apply_or_insert : |world, entity, reflected_component| {
@@ -191,9 +198,10 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
191
198
world. entity_mut ( entity) . insert ( component) ;
192
199
}
193
200
} ,
194
- remove : |world , entity| {
195
- world . entity_mut ( entity) . remove :: < C > ( ) ;
201
+ remove : |mut entity| {
202
+ entity. remove :: < C > ( ) ;
196
203
} ,
204
+ contains : |entity| entity. contains :: < C > ( ) ,
197
205
copy : |source_world, destination_world, source_entity, destination_entity| {
198
206
let source_component = source_world. get :: < C > ( source_entity) . unwrap ( ) ;
199
207
let mut destination_component = C :: from_world ( destination_world) ;
@@ -202,12 +210,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
202
210
. entity_mut ( destination_entity)
203
211
. insert ( destination_component) ;
204
212
} ,
205
- reflect : |world, entity| {
206
- world
207
- . get_entity ( entity) ?
208
- . get :: < C > ( )
209
- . map ( |c| c as & dyn Reflect )
210
- } ,
213
+ reflect : |entity| entity. get :: < C > ( ) . map ( |c| c as & dyn Reflect ) ,
211
214
reflect_mut : |world, entity| {
212
215
// SAFETY: reflect_mut is an unsafe function pointer used by `reflect_unchecked_mut` which promises to never
213
216
// produce aliasing mutable references, and reflect_mut, which has mutable world access
0 commit comments