Skip to content

Commit 240a427

Browse files
authored
Merge branch 'main' into better-validate-warnings
2 parents 5179627 + bd20382 commit 240a427

4 files changed

Lines changed: 49 additions & 11 deletions

File tree

crates/bevy_ecs/src/reflect/component.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
use super::from_reflect_with_fallback;
6161
use crate::{
6262
change_detection::Mut,
63-
component::Component,
63+
component::{Component, ComponentId},
6464
entity::Entity,
6565
world::{
6666
unsafe_world_cell::UnsafeEntityCell, EntityMut, EntityWorldMut, FilteredEntityMut,
@@ -119,6 +119,8 @@ pub struct ReflectComponentFns {
119119
pub reflect_unchecked_mut: unsafe fn(UnsafeEntityCell<'_>) -> Option<Mut<'_, dyn Reflect>>,
120120
/// Function pointer implementing [`ReflectComponent::copy()`].
121121
pub copy: fn(&World, &mut World, Entity, Entity, &TypeRegistry),
122+
/// Function pointer implementing [`ReflectComponent::register_component()`].
123+
pub register_component: fn(&mut World) -> ComponentId,
122124
}
123125

124126
impl ReflectComponentFns {
@@ -220,6 +222,11 @@ impl ReflectComponent {
220222
);
221223
}
222224

225+
/// Register the type of this [`Component`] in [`World`], returning its [`ComponentId`].
226+
pub fn register_component(&self, world: &mut World) -> ComponentId {
227+
(self.0.register_component)(world)
228+
}
229+
223230
/// Create a custom implementation of [`ReflectComponent`].
224231
///
225232
/// This is an advanced feature,
@@ -303,6 +310,9 @@ impl<C: Component + Reflect + TypePath> FromType<C> for ReflectComponent {
303310
let c = unsafe { entity.get_mut::<C>() };
304311
c.map(|c| c.map_unchanged(|value| value as &mut dyn Reflect))
305312
},
313+
register_component: |world: &mut World| -> ComponentId {
314+
world.register_component::<C>()
315+
},
306316
})
307317
}
308318
}

crates/bevy_ecs/src/reflect/resource.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use crate::{
88
change_detection::Mut,
9+
component::ComponentId,
910
system::Resource,
1011
world::{unsafe_world_cell::UnsafeWorldCell, World},
1112
};
@@ -59,6 +60,8 @@ pub struct ReflectResourceFns {
5960
pub reflect_unchecked_mut: unsafe fn(UnsafeWorldCell<'_>) -> Option<Mut<'_, dyn Reflect>>,
6061
/// Function pointer implementing [`ReflectResource::copy()`].
6162
pub copy: fn(&World, &mut World, &TypeRegistry),
63+
/// Function pointer implementing [`ReflectResource::register_resource()`].
64+
pub register_resource: fn(&mut World) -> ComponentId,
6265
}
6366

6467
impl ReflectResourceFns {
@@ -145,6 +148,11 @@ impl ReflectResource {
145148
(self.0.copy)(source_world, destination_world, registry);
146149
}
147150

151+
/// Register the type of this [`Resource`] in [`World`], returning the [`ComponentId`]
152+
pub fn register_resource(&self, world: &mut World) -> ComponentId {
153+
(self.0.register_resource)(world)
154+
}
155+
148156
/// Create a custom implementation of [`ReflectResource`].
149157
///
150158
/// This is an advanced feature,
@@ -217,6 +225,10 @@ impl<R: Resource + FromReflect + TypePath> FromType<R> for ReflectResource {
217225
from_reflect_with_fallback::<R>(source_resource, destination_world, registry);
218226
destination_world.insert_resource(destination_resource);
219227
},
228+
229+
register_resource: |world: &mut World| -> ComponentId {
230+
world.register_resource::<R>()
231+
},
220232
})
221233
}
222234
}

crates/bevy_ecs/src/world/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,25 @@ impl World {
334334
self.components.component_id::<T>()
335335
}
336336

337+
/// Registers a new [`Resource`] type and returns the [`ComponentId`] created for it.
338+
///
339+
/// Note that the resource doesn't have a value in world, it's only registered.
340+
/// if you want to insert the [`Resource`] in the [`World`], use [`World::init_resource`] or [`World::insert_resource`] instead.
341+
pub fn register_resource<R: Resource>(&mut self) -> ComponentId {
342+
self.components.register_resource::<R>()
343+
}
344+
345+
/// Returns the [`ComponentId`] of the given [`Resource`] type `T`.
346+
///
347+
/// The returned `ComponentId` is specific to the `World` instance
348+
/// it was retrieved from and should not be used with another `World` instance.
349+
///
350+
/// Returns [`None`] if the `Resource` type has not yet been initialized within
351+
/// the `World` using [`World::register_resource`], [`World::init_resource`] or [`World::insert_resource`].
352+
pub fn resource_id<T: Resource>(&self) -> Option<ComponentId> {
353+
self.components.get_resource_id(TypeId::of::<T>())
354+
}
355+
337356
/// Retrieves an [`EntityRef`] that exposes read-only operations for the given `entity`.
338357
/// This will panic if the `entity` does not exist. Use [`World::get_entity`] if you want
339358
/// to check for entity existence instead of implicitly panic-ing.

crates/bevy_gltf/src/loader.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ use crate::{
44
};
55

66
use alloc::collections::VecDeque;
7-
use bevy_animation::prelude::{
8-
Keyframes, MorphWeightsKeyframes, RotationKeyframes, ScaleKeyframes, TranslationKeyframes,
9-
};
107
use bevy_asset::{
118
io::Reader, AssetLoadError, AssetLoader, Handle, LoadContext, ReadAssetBytesError,
129
};
@@ -65,7 +62,7 @@ use std::{
6562
use thiserror::Error;
6663
#[cfg(feature = "bevy_animation")]
6764
use {
68-
bevy_animation::{AnimationTarget, AnimationTargetId},
65+
bevy_animation::{prelude::*, AnimationTarget, AnimationTargetId},
6966
smallvec::SmallVec,
7067
};
7168

@@ -277,7 +274,7 @@ async fn load_gltf<'a, 'b, 'c>(
277274
let mut named_animations = HashMap::default();
278275
let mut animation_roots = HashSet::default();
279276
for animation in gltf.animations() {
280-
let mut animation_clip = bevy_animation::AnimationClip::default();
277+
let mut animation_clip = AnimationClip::default();
281278
for channel in animation.channels() {
282279
let interpolation = match channel.sampler().interpolation() {
283280
gltf::animation::Interpolation::Linear => Interpolation::Linear,
@@ -330,7 +327,7 @@ async fn load_gltf<'a, 'b, 'c>(
330327
animation_roots.insert(*root_index);
331328
animation_clip.add_curve_to_target(
332329
AnimationTargetId::from_names(path.iter()),
333-
bevy_animation::VariableCurve {
330+
VariableCurve {
334331
keyframe_timestamps,
335332
keyframes,
336333
interpolation,
@@ -738,7 +735,7 @@ async fn load_gltf<'a, 'b, 'c>(
738735
if animation_roots.contains(&node.index()) {
739736
world
740737
.entity_mut(*node_index_to_entity_map.get(&node.index()).unwrap())
741-
.insert(bevy_animation::AnimationPlayer::default());
738+
.insert(AnimationPlayer::default());
742739
}
743740
}
744741
}
@@ -2468,7 +2465,7 @@ mod test {
24682465
{
24692466
"inverseBindMatrices": 0,
24702467
"joints": [1, 2]
2471-
}
2468+
}
24722469
],
24732470
"buffers": [
24742471
{
@@ -2480,15 +2477,15 @@ mod test {
24802477
{
24812478
"buffer": 0,
24822479
"byteLength": 128
2483-
}
2480+
}
24842481
],
24852482
"accessors": [
24862483
{
24872484
"bufferView" : 0,
24882485
"componentType" : 5126,
24892486
"count" : 2,
24902487
"type" : "MAT4"
2491-
}
2488+
}
24922489
],
24932490
"scene": 0,
24942491
"scenes": [{ "nodes": [0] }]

0 commit comments

Comments
 (0)