diff options
Diffstat (limited to 'engine-ecs')
| -rw-r--r-- | engine-ecs/src/actions.rs | 155 | ||||
| -rw-r--r-- | engine-ecs/src/lib.rs | 33 |
2 files changed, 184 insertions, 4 deletions
diff --git a/engine-ecs/src/actions.rs b/engine-ecs/src/actions.rs index 411a5d5..8387acc 100644 --- a/engine-ecs/src/actions.rs +++ b/engine-ecs/src/actions.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use crate::component::{ + Component, IntoParts, Parts as ComponentParts, Sequence as ComponentSequence, @@ -173,6 +174,154 @@ impl Actions<'_> self.remove_components(entity_uid, Ids::uids()); } + /// Queues up setting the target of a entity's pair at the end of the current tick. + /// If the pair does not exist in the entity, it is still added to the entity with the + /// new target. + #[tracing::instrument( + skip_all, + fields(entity=%entity_id, pair=%pair.id(), new_target=%new_target_id) + )] + pub fn set_pair_target( + &mut self, + entity_id: Uid, + pair: Pair<Uid, Uid>, + new_target_id: Uid, + ) + { + debug_assert!(!entity_id.is_pair()); + debug_assert!(!new_target_id.is_pair()); + + if let Some(world) = self.world { + if world.get_entity(entity_id).is_none() { + tracing::error!("Entity does not exist"); + return; + } + } + + let pair_id = pair.id(); + + self.action_queue.push(Action::SetPair { + entity_id: entity_id, + pair_id: pair_id, + new_pair: Pair::builder() + .relation_id(pair_id.relation()) + .target_id(new_target_id) + .build() + .into_parts(), + }); + } + + /// Queues up setting the target of a entity's pair at the end of the current tick. + /// If the pair does not exist in the entity, it is still added to the entity with the + /// new target. + #[tracing::instrument( + skip_all, + fields(entity=%entity_id, pair=%pair.id(), new_target=%NewTarget::id()) + )] + pub fn set_pair_target_and_data<NewTarget>( + &mut self, + entity_id: Uid, + pair: Pair<Uid, Uid>, + new_target: NewTarget, + ) where + NewTarget: Component, + { + debug_assert!(!entity_id.is_pair()); + + if let Some(world) = self.world { + if world.get_entity(entity_id).is_none() { + tracing::error!("Entity does not exist"); + return; + } + } + + let pair_id = pair.id(); + + self.action_queue.push(Action::SetPair { + entity_id: entity_id, + pair_id: pair_id, + new_pair: Pair::builder() + .relation_id(pair_id.relation()) + .target_as_data(new_target) + .build() + .into_parts(), + }); + } + + /// Queues up setting the relation of a entity's pair at the end of the current tick. + /// If the pair does not exist in the entity, it is still added to the entity with the + /// new relation. + #[tracing::instrument( + skip_all, + fields(entity=%entity_id, pair=%pair.id(), new_relation=%new_relation_id) + )] + pub fn set_pair_relation( + &mut self, + entity_id: Uid, + pair: Pair<Uid, Uid>, + new_relation_id: Uid, + ) + { + debug_assert!(!entity_id.is_pair()); + debug_assert!(!new_relation_id.is_pair()); + + if let Some(world) = self.world { + if world.get_entity(entity_id).is_none() { + tracing::error!("Entity does not exist"); + return; + } + } + + let pair_id = pair.id(); + + self.action_queue.push(Action::SetPair { + entity_id: entity_id, + pair_id: pair_id, + new_pair: Pair::builder() + .relation_id(new_relation_id) + .target_id(pair_id.target()) + .build() + .into_parts(), + }); + } + + /// Queues up setting the relation of a entity's pair at the end of the current tick. + /// If the pair does not exist in the entity, it is still added to the entity with the + /// new relation. + #[tracing::instrument( + skip_all, + fields(entity=%entity_id, pair=%pair.id(), new_relation=%NewRelation::id()) + )] + pub fn set_pair_relation_and_data<NewRelation>( + &mut self, + entity_id: Uid, + pair: Pair<Uid, Uid>, + new_relation: NewRelation, + ) where + NewRelation: Component, + { + debug_assert!(!entity_id.is_pair()); + + if let Some(world) = self.world { + if world.get_entity(entity_id).is_none() { + tracing::error!("Entity does not exist"); + return; + } + } + + let pair_id = pair.id(); + + self.action_queue.push(Action::SetPair { + entity_id: entity_id, + pair_id: pair_id, + new_pair: Pair::builder() + .relation_as_data(new_relation) + .target_id(pair_id.target()) + .build() + .into_parts(), + }); + } + /// Stops the [`World`]. The world will finish the current tick and that tick will be /// the last. pub fn stop(&mut self) @@ -202,5 +351,11 @@ pub(crate) enum Action Despawn(Uid), AddComponents(Uid, Vec<ComponentParts>), RemoveComponents(Uid, Vec<Uid>), + SetPair + { + entity_id: Uid, + pair_id: Uid, + new_pair: ComponentParts, + }, Stop, } diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index 97316f9..039661a 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -285,9 +285,7 @@ impl World extension.collect(extension_collector); } - pub fn query<'this, TermsT>( - &'this self, - ) -> Query<'this, TermsT> + pub fn query<'this, TermsT>(&'this self) -> Query<'this, TermsT> where TermsT: QueryTermTuple<'this>, { @@ -609,6 +607,29 @@ impl World &mut self.data.component_storage, ); } + Action::SetPair { entity_id, pair_id, new_pair } => { + if self.get_entity(entity_id).is_none() { + tracing::error!( + entity = %entity_id, + pair = %pair_id, + new_pair = %new_pair.id, + "Cannot set pair of a non-existant entity" + ); + continue; + }; + + let _ = self + .data + .component_storage + .remove_entity_component(entity_id, pair_id); + + Self::add_entity_components( + entity_id, + [new_pair], + &mut self.data.component_storage, + &EventSubmitter::new(&self.data.new_events), + ); + } Action::Stop => { self.stop.store(true, Ordering::Relaxed); } @@ -712,7 +733,11 @@ impl World if let Err(err) = component_storage.remove_entity_component(entity_uid, component_id) { - tracing::error!("Failed to remove component to entity: {err}"); + tracing::error!( + entity=%entity_uid, + component=%component_id, + "Failed to remove component from entity: {err}" + ); } } } |
