single-user service manager
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206use crate::apogee_core_v1::apogee_condition_v1;use crate::ecs;use bevy_ecs::prelude::*;use std::collections::HashSet;use std::sync;
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]pub struct Condition;
#[derive(Component)]pub struct Name(pub sync::Arc<str>);
#[derive(Component)]pub struct Fulfilled;
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]pub struct Immutable(pub bool);
#[derive(Component, Default)]pub struct Objects(pub Vec<apogee_condition_v1::ApogeeConditionV1>);
#[derive(Bundle)]pub struct Bundle { pub marker: Condition, pub name: Name, pub immutable: Immutable, pub objects: Objects,}
pub fn spawn( commands: &mut Commands, conditions: &mut ecs::ConditionRegistry, managers: &ecs::Managers, name: sync::Arc<str>, immutable: bool,) -> Entity { let entity = commands .spawn(Bundle { marker: Condition, name: Name(sync::Arc::clone(&name)), immutable: Immutable(immutable), objects: Objects::default(), }) .id(); conditions.by_name.insert(sync::Arc::clone(&name), entity); managers.condition_added(&name); entity}
pub fn resolve( commands: &mut Commands, conditions: &mut ecs::ConditionRegistry, managers: &ecs::Managers, name: sync::Arc<str>, immutable: bool,) -> Entity { if let Some(entity) = conditions.by_name.get(&name).copied() { if immutable { commands.entity(entity).insert(Immutable(true)); } entity } else { spawn(commands, conditions, managers, name, immutable) }}
pub fn sync_objects( mut removed_fulfilled: RemovedComponents<Fulfilled>, changed_fulfilled: Query<Entity, Changed<Fulfilled>>, mut conditions: Query< ( Entity, Ref<Name>, Ref<Immutable>, Has<Fulfilled>, &mut Objects, ), With<Condition>, >,) { let mut fulfillment_changed = removed_fulfilled.read().collect::<HashSet<_>>(); fulfillment_changed.extend(changed_fulfilled.iter());
for (entity, name, immutable, fulfilled, mut objects) in &mut conditions { objects .bypass_change_detection() .0 .retain(|object| object.try_client().is_some()); if objects.is_changed() { for object in &objects.0 { object.send_name(&name.0); object.send_fulfilled(u32::from(fulfilled)); object.send_can_modify(u32::from(!immutable.0)); object.send_done(); } continue; }
for object in &objects.0 { if name.is_changed() { object.send_name(&name.0); } if fulfillment_changed.contains(&entity) { object.send_fulfilled(u32::from(fulfilled)); } if immutable.is_changed() { object.send_can_modify(u32::from(!immutable.0)); } object.send_done(); } }}
impl hyprwire::Dispatch<apogee_condition_v1::ApogeeConditionV1> for crate::Apogee { fn event( &mut self, object: &apogee_condition_v1::ApogeeConditionV1, event: <apogee_condition_v1::ApogeeConditionV1 as hyprwire::Object>::Event<'_>, ) { let Some(condition) = self .world .resource_mut::<ecs::ConditionRegistry>() .by_object .get(object) .copied() else { return; };
match event { apogee_condition_v1::Event::Start => { self.world.trigger(Start { entity: condition, object: object.clone(), }); } apogee_condition_v1::Event::Stop => { self.world.trigger(Stop { entity: condition, object: object.clone(), }); } } }}
#[derive(Event)]pub struct Stop { entity: Entity, object: apogee_condition_v1::ApogeeConditionV1,}
pub fn handle_stop( stop: On<Stop>, mut commands: Commands, conditions: Query<&Immutable, With<Condition>>,) { let Ok(immutable) = conditions.get(stop.entity) else { stop.object.send_error( crate::apogee_core_v1::StartError::NotFound, "condition was not found", ); return; }; if immutable.0 { stop.object.send_error( crate::apogee_core_v1::StartError::Immutable, "condition cannot be modified manually", ); return; }
commands.entity(stop.entity).remove::<Fulfilled>(); stop.object.send_ok();}
#[derive(Event)]pub struct Start { entity: Entity, object: apogee_condition_v1::ApogeeConditionV1,}
pub fn handle_start( start: On<Start>, mut commands: Commands, conditions: Query<&Immutable, With<Condition>>,) { let Ok(immutable) = conditions.get(start.entity) else { start.object.send_error( crate::apogee_core_v1::StartError::NotFound, "condition was not found", ); return; }; if immutable.0 { start.object.send_error( crate::apogee_core_v1::StartError::Immutable, "condition cannot be modified manually", ); return; }
commands.entity(start.entity).insert(Fulfilled); start.object.send_ok();}