data/
level.rs

1//! Defines the [`Level`] struct and it's implementations.
2use bevy::prelude::{Bundle, Component, Name, Transform, Visibility};
3use std::borrow::Cow;
4
5/// A [`Level`] indicates a single visible subset of a map, most commonly used to represent
6/// distinct parts of the same logical project.
7///
8/// An example would be a 'project' that represents a full roadside tavern which might have a ground
9/// floor, a basement and an upper floor for lodging. Each physical floor would be represented under
10/// a [`Level`] so that all floors are grouped in the same savefile (project) "Roadside Tavern" but
11/// the levels are separated so that the user can edit/export each physical floor separately.
12#[derive(Component, Default)]
13#[component(immutable)]
14#[require(Transform::from_xyz(0.0, 0.0, 0.0), Visibility::default())]
15pub struct Level;
16
17impl Level {
18    /// Generates a new [`Bundle`] with a level to indicate the start of a hierarchy under which
19    /// a map will be set.
20    ///
21    /// # Examples
22    ///
23    /// Here's how to spawn a simple `Level` for the ground floor.
24    ///
25    /// ```
26    /// # use bevy::prelude::*;
27    /// # use data::Project;
28    /// # use data::Level;
29    /// #
30    /// # fn main() {
31    /// #   App::new()
32    /// #       .add_systems(Startup, spawn_project)
33    /// #       .run();
34    /// # }
35    /// #
36    /// # fn spawn_project(mut commands: Commands) {
37    ///     commands.spawn((
38    ///         Project::new("Roadside Inn"),
39    ///         children![
40    ///             Level::new("Ground Floor")
41    ///         ]
42    ///     ));
43    /// # }
44    /// ```
45    #[allow(clippy::new_ret_no_self)]
46    #[must_use = "Level 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), Level {})
49    }
50}