summaryrefslogtreecommitdiff
path: root/engine-ecs/src/actions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine-ecs/src/actions.rs')
-rw-r--r--engine-ecs/src/actions.rs155
1 files changed, 155 insertions, 0 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,
}