@@ -5,7 +5,7 @@ use crate::{
5
5
component:: Component ,
6
6
entity:: { Entity , EntityMap , MapEntities , MapEntitiesError } ,
7
7
system:: Resource ,
8
- world:: { unsafe_world_cell:: UnsafeWorldCell , FromWorld , World } ,
8
+ world:: { unsafe_world_cell:: UnsafeWorldCell , 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
///
56
58
/// # Safety
@@ -85,9 +87,9 @@ impl ReflectComponent {
85
87
///
86
88
/// # Panics
87
89
///
88
- /// Panics if there is no [`Component`] of the given type or the `entity` does not exist .
89
- pub fn apply ( & self , world : & mut World , entity : Entity , component : & dyn Reflect ) {
90
- ( self . 0 . apply ) ( world , entity, component) ;
90
+ /// Panics if there is no [`Component`] of the given type.
91
+ pub fn apply ( & self , entity : EntityMut , component : & dyn Reflect ) {
92
+ ( self . 0 . apply ) ( entity, component) ;
91
93
}
92
94
93
95
/// 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.
@@ -103,14 +105,19 @@ impl ReflectComponent {
103
105
///
104
106
/// # Panics
105
107
///
106
- /// Panics if there is no [`Component`] of the given type or the `entity` does not exist.
107
- pub fn remove ( & self , world : & mut World , entity : Entity ) {
108
- ( self . 0 . remove ) ( world, entity) ;
108
+ /// Panics if there is no [`Component`] of the given type.
109
+ pub fn remove ( & self , entity : EntityMut ) {
110
+ ( self . 0 . remove ) ( entity) ;
111
+ }
112
+
113
+ /// Returns whether entity contains this [`Component`]
114
+ pub fn contains ( & self , entity : EntityRef ) -> bool {
115
+ ( self . 0 . contains ) ( entity)
109
116
}
110
117
111
118
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
112
- pub fn reflect < ' a > ( & self , world : & ' a World , entity : Entity ) -> Option < & ' a dyn Reflect > {
113
- ( self . 0 . reflect ) ( world , entity)
119
+ pub fn reflect < ' a > ( & self , entity : EntityRef < ' a > ) -> Option < & ' a dyn Reflect > {
120
+ ( self . 0 . reflect ) ( entity)
114
121
}
115
122
116
123
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
@@ -181,8 +188,8 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
181
188
component. apply ( reflected_component) ;
182
189
world. entity_mut ( entity) . insert ( component) ;
183
190
} ,
184
- apply : |world , entity, reflected_component| {
185
- let mut component = world . get_mut :: < C > ( entity ) . unwrap ( ) ;
191
+ apply : |mut entity, reflected_component| {
192
+ let mut component = entity . get_mut :: < C > ( ) . unwrap ( ) ;
186
193
component. apply ( reflected_component) ;
187
194
} ,
188
195
apply_or_insert : |world, entity, reflected_component| {
@@ -194,9 +201,10 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
194
201
world. entity_mut ( entity) . insert ( component) ;
195
202
}
196
203
} ,
197
- remove : |world , entity| {
198
- world . entity_mut ( entity) . remove :: < C > ( ) ;
204
+ remove : |mut entity| {
205
+ entity. remove :: < C > ( ) ;
199
206
} ,
207
+ contains : |entity| entity. contains :: < C > ( ) ,
200
208
copy : |source_world, destination_world, source_entity, destination_entity| {
201
209
let source_component = source_world. get :: < C > ( source_entity) . unwrap ( ) ;
202
210
let mut destination_component = C :: from_world ( destination_world) ;
@@ -205,12 +213,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
205
213
. entity_mut ( destination_entity)
206
214
. insert ( destination_component) ;
207
215
} ,
208
- reflect : |world, entity| {
209
- world
210
- . get_entity ( entity) ?
211
- . get :: < C > ( )
212
- . map ( |c| c as & dyn Reflect )
213
- } ,
216
+ reflect : |entity| entity. get :: < C > ( ) . map ( |c| c as & dyn Reflect ) ,
214
217
reflect_mut : |world, entity| {
215
218
// SAFETY: reflect_mut is an unsafe function pointer used by
216
219
// 1. `reflect_unchecked_mut` which must be called with an UnsafeWorldCell with access to the the component `C` on the `entity`, and
0 commit comments