crates/bevy_immediate_core/src/immediate/ctx.rs
原文件:crates/bevy_immediate_core/src/immediate/ctx.rs。
阅读快照 以下内容来自 2026-11-17 导入时的本地源码,后续不会自动同步。编辑、编译和运行请回到原项目;本页 Markdown 不是可直接编译的源文件。
use bevy_ecs::{
entity::Entity,
hierarchy::ChildOf,
query::With,
system::{Commands, Query, Res, ResMut, SystemChangeTick},
};
use crate::{
CapSet, Imm, ImmCapAccessRequestsResource, ImmCapQueryParam, ImmId,
capabilities::ImmCapResourcesParam,
immediate::{
Current, CurrentEntity, ImmMarker, cached_hash::CachedHash,
entity_mapping::ImmediateModeEntityMapping, upkeep::ImmediateModeStateResource,
},
};
/// Immediate mode ctx
#[derive(bevy_ecs::system::SystemParam)]
pub struct ImmCtx<'w, 's, Caps: CapSet> {
/// Access data from entities for components that were requested by capabilities
///
/// Filtered with [`Without<IsResource>`] so it does not conflict with
/// [`ImmCapResourcesParam`](crate::capabilities::ImmCapResourcesParam).
///
/// In case of collision. Use [`super::ImmQuery`] or
/// [`bevy_ecs::prelude::Without<ImmMarker<()>>`] (replace () with your used `Cap``)
pub cap_entities: ImmCapQueryParam<'w, 's, Caps>,
/// Access requested resources that were requested by capabilities
/// (resource entities filtered with [`With<IsResource>`](bevy_ecs::resource::IsResource))
pub cap_resources: ImmCapResourcesParam<'w, 's, Caps>,
/// World commands
pub commands: Commands<'w, 's>,
/// System execution ticks
///
/// Useful for state change detection
pub system_change_tick: SystemChangeTick,
pub(super) access_requests: Res<'w, ImmCapAccessRequestsResource<Caps>>,
pub(super) state: Res<'w, ImmediateModeStateResource<Caps>>,
pub(super) mapping: Res<'w, ImmediateModeEntityMapping<Caps>>,
pub(super) entity_query: Query<'w, 's, ImmEntityQuery<Caps>, (With<ImmMarker<Caps>>, ())>,
pub(super) cached_hash: ResMut<'w, CachedHash<Caps>>,
#[cfg(feature = "hotpatching")]
pub(super) hotpatching: Res<'w, super::hotpatching::HotpatchingCounter>,
}
impl<'w, 's, Caps> ImmCtx<'w, 's, Caps>
where
Caps: CapSet,
{
/// Initialize entity hierarchy managed by immediate mode
///
/// When `hotpatching` feature is enabled. Will combine id with last time when hotpatching was triggered
pub fn build_immediate_root<T: std::hash::Hash>(self, root_id: T) -> Imm<'w, 's, Caps> {
let id = ImmId::new(root_id);
#[cfg(feature = "hotpatching")]
let id = ImmId::new((id, self.hotpatching.hotpatch()));
Imm {
ctx: self,
current: Current {
id,
entity: None,
auto_id_idx: 0,
id_pref: ImmId::new(49382395483011234u64),
},
}
}
/// Initialize entity hierarchy managed by immediate mode starting from given **existing** entity
///
/// When `hotpatching` feature is enabled. Will combine id with last time when hotpatching was triggered
pub fn build_immediate_from<T: std::hash::Hash>(
self,
root_id: T,
entity: Entity,
) -> Imm<'w, 's, Caps> {
let id = ImmId::new(root_id);
#[cfg(feature = "hotpatching")]
let id = ImmId::new((id, self.hotpatching.hotpatch()));
Imm {
ctx: self,
current: Current {
id,
entity: Some(CurrentEntity {
entity,
will_be_spawned: false,
}),
auto_id_idx: 0,
id_pref: ImmId::new(49382395483011234u64),
},
}
}
/// Access commands
pub fn commands_mut(&mut self) -> &mut Commands<'w, 's> {
&mut self.commands
}
}
#[derive(bevy_ecs::query::QueryData)]
#[query_data(mutable)]
pub(super) struct ImmEntityQuery<Marker: Send + Sync + 'static> {
pub(super) tracker: &'static mut ImmMarker<Marker>,
pub(super) child_of: Option<&'static ChildOf>,
}