bevy_proto_backend/proto/prototypical.rs
1use std::fmt::Debug;
2use std::hash::Hash;
3
4use bevy::asset::Asset;
5
6use crate::children::{Children, PrototypicalChild};
7use crate::deps::Dependencies;
8use crate::path::ProtoPath;
9use crate::schematics::Schematics;
10use crate::templates::Templates;
11
12/// The trait used to define a prototype.
13///
14/// Prototypes are containers for [`Schematics`] that can be spawned in at runtime.
15/// They may also inherit from other prototypes (where the inherited prototypes are known
16/// as [`Templates`]) and contain a hierarchy of [`Children`].
17pub trait Prototypical: Asset + Sized {
18 /// The type used to identify this prototype.
19 ///
20 /// This can be used to control how a prototype is accessed.
21 /// For example, if we wanted to split prototypes into "packages",
22 /// then we could require a package identifier along with the
23 /// prototype name in order to access the prototype:
24 ///
25 /// ```
26 /// # use std::fmt::{Display, Formatter};
27 /// #[derive(Clone, Debug, PartialEq, Eq, Hash)]
28 /// struct MyProtoId {
29 /// package: String,
30 /// name: String,
31 /// }
32 ///
33 /// impl Display for MyProtoId {
34 /// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35 /// write!(f, "{}::{}", self.package, self.name)
36 /// }
37 /// }
38 /// ```
39 type Id: Clone + Debug + Eq + Hash + Send + Sync + ToString;
40 /// The type of the child this prototype uses.
41 ///
42 /// This is used to configure how a child is processed.
43 type Child: PrototypicalChild<Self>;
44
45 /// The ID of this prototype.
46 fn id(&self) -> &Self::Id;
47 /// The path to this prototype's asset file.
48 fn path(&self) -> &ProtoPath;
49 /// Whether or not this prototype requires an entity to be spawned.
50 ///
51 /// Defaults to `true`.
52 fn requires_entity(&self) -> bool {
53 true
54 }
55 /// An immutable reference to the collection of [`Schematics`] contained in this prototype.
56 fn schematics(&self) -> &Schematics;
57 /// A mutable reference to the collection of [`Schematics`] contained in this prototype.
58 fn schematics_mut(&mut self) -> &mut Schematics;
59 /// An immutable reference to the collection of [`Templates`] inherited by this prototype, if any.
60 fn templates(&self) -> Option<&Templates>;
61 /// A mutable reference to the collection of [`Templates`] inherited by this prototype, if any.
62 fn templates_mut(&mut self) -> Option<&mut Templates>;
63 /// An immutable reference to the collection of [`Dependencies`] used by this prototype.
64 fn dependencies(&self) -> &Dependencies;
65 /// A mutable reference to the collection of [`Dependencies`] used by this prototype.
66 fn dependencies_mut(&mut self) -> &mut Dependencies;
67 /// An immutable reference to the collection of [`Children`] contained in this prototype, if any.
68 fn children(&self) -> Option<&Children<Self>>;
69 /// A mutable reference to the collection of [`Children`] contained in this prototype, if any.
70 fn children_mut(&mut self) -> Option<&mut Children<Self>>;
71}