data/
project.rs

1//! Defines the [`Project`] struct and it's implementations.
2use bevy::prelude::{Bundle, Component, Name, Transform, Visibility};
3use std::borrow::Cow;
4
5/// Top level component in the hierarchy used by `DungeonRS` to identify components that relate to the
6/// map the user is editing. In short, only components under this [`Project`] component will be considered
7/// when saving/loading a map or exporting.
8///
9/// This allows editing tools to spawn additional components (like Gizmo's, temporary stamps, ...)
10/// without having them accidentally included in any export or persistence operations.
11///
12/// This distinction also allows a (minor) performance increase since queries can be run on a subset
13/// of the ECS hierarchy rather than all components available.
14#[derive(Component)]
15#[component(immutable)]
16#[require(Transform::from_xyz(0.0, 0.0, 0.0), Visibility::default())]
17pub struct Project;
18
19impl Project {
20    /// Generates a new [`Bundle`] with a project to indicate the start of a hierarchy under which
21    /// the map (often referred to as 'project', hence the name) will be set.
22    ///
23    /// # Arguments
24    /// * `name` - The human-friendly name of the project, entirely unrelated to the filename. This
25    ///   is mostly used in the user interface.
26    ///
27    /// # Examples
28    ///
29    /// Here's how to spawn a simple `Project` named "Roadside Inn"
30    ///
31    /// ```
32    /// # use bevy::prelude::*;
33    /// # use data::Project;
34    /// #
35    /// # fn main() {
36    /// #   App::new()
37    /// #       .add_systems(Startup, spawn_project)
38    /// #       .run();
39    /// # }
40    /// #
41    /// # fn spawn_project(mut commands: Commands) {
42    ///     commands.spawn(Project::new("Roadside Inn"));
43    /// # }
44    /// ```
45    #[allow(clippy::new_ret_no_self)]
46    #[must_use = "Project won't be added to the world unless spawned"]
47    pub fn new(name: impl Into<Cow<'static, str>>) -> impl Bundle {
48        (Name::new(name), Project {})
49    }
50}