summaryrefslogtreecommitdiff
path: root/ecs/src/extension.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-13 17:38:45 +0200
committerHampusM <hampus@hampusmat.com>2024-04-13 17:38:45 +0200
commitef7b76ff39d501028852835649f618fcbe17a003 (patch)
tree00cf4966c1bcfa0812f436a793a1711867869601 /ecs/src/extension.rs
parentf2120591b0051d4239de73960709248456e884e7 (diff)
feat(ecs): add extensions
Diffstat (limited to 'ecs/src/extension.rs')
-rw-r--r--ecs/src/extension.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/ecs/src/extension.rs b/ecs/src/extension.rs
new file mode 100644
index 0000000..fc5a345
--- /dev/null
+++ b/ecs/src/extension.rs
@@ -0,0 +1,56 @@
+use crate::component::Sequence as ComponentSequence;
+use crate::event::Event;
+use crate::sole::Sole;
+use crate::system::System;
+use crate::{SoleAlreadyExistsError, World};
+
+/// A collection of systems, entities & soles that can be added to a [`World`].
+pub trait Extension
+{
+ fn collect(self, collector: Collector<'_>);
+}
+
+/// Passed to a [`Extension`] to collects it's systems, entities & soles.
+pub struct Collector<'world>
+{
+ world: &'world mut World,
+}
+
+impl<'world> Collector<'world>
+{
+ /// Returns a new `Collector` for the given [`World`].
+ pub fn new(world: &'world mut World) -> Self
+ {
+ Self { world }
+ }
+
+ /// Adds a system to the [`World`].
+ pub fn add_system<'this, EventT, SystemImpl>(
+ &'this mut self,
+ event: EventT,
+ system: impl System<'this, SystemImpl>,
+ ) where
+ EventT: Event,
+ {
+ self.world.register_system(event, system);
+ }
+
+ /// Adds a entity to the [`World`].
+ pub fn add_entity<Comps>(&mut self, components: Comps)
+ where
+ Comps: ComponentSequence,
+ {
+ self.world.create_entity(components);
+ }
+
+ /// Adds a globally shared singleton value to the [`World`].
+ ///
+ /// # Errors
+ /// Returns `Err` if this [`Sole`] has already been added.
+ pub fn add_sole<SoleT>(&mut self, sole: SoleT) -> Result<(), SoleAlreadyExistsError>
+ where
+ SoleT: Sole,
+ {
+ self.world.add_sole(sole)
+ }
+}