summaryrefslogtreecommitdiff
path: root/engine-ecs/src/phase.rs
blob: cccf978897dde507ed0d8002e61b5d6640125f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#[cfg(debug_assertions)]
use crate::entity::Name as EntityName;
use crate::{Query, pair::{ChildOf, Pair, Wildcard}, query::term::With};
use crate::{declare_entity, Component, World};

#[derive(Debug, Default, Clone, Copy, Component)]
pub struct Phase;

declare_entity! {
/// Initialization phase that only occurs the first tick, before [`START`].
/// Should not be used for game logic
pub INIT: (Phase,);

/// Phase that only occurs the first tick, after [`INIT`].
pub START: (Phase,);

/// Phase that occurs every tick, before [`UPDATE`].
pub PRE_UPDATE: (Phase,);

/// Phase that occurs every tick, after [`PRE_UPDATE`].
pub UPDATE: (Phase,);

/// Phase that occurs every tick, after [`UPDATE`].
pub POST_UPDATE: (Phase,);
}

pub(crate) fn spawn_standard_phases(world: &mut World)
{
    world.spawn_declared_entity(&INIT);
    world.spawn_declared_entity(&START);
    world.spawn_declared_entity(&PRE_UPDATE);
    world.spawn_declared_entity(&UPDATE);
    world.spawn_declared_entity(&POST_UPDATE);
}

#[derive(Debug, Component)]
pub(crate) struct HasSystem;


#[cfg(debug_assertions)]
pub(crate) fn check_parents(phase_query: Query<(Pair<ChildOf, Wildcard>, Option<&EntityName>), (With<Phase>,)>)
{
    for (child_of, ent_name) in &phase_query {
        let Some(parent_ent) = child_of.get_target_ent() else {
            tracing::warn!(
                phase_entity=ent_name
                    .as_ref()
                    .map(|ent_name| ent_name.name.as_ref())
                    .unwrap_or("<unnamed>"),
                "Parent entity of phase entity does not exist"
            );
            continue;
        };

        if !parent_ent.has_component(Phase::id()) {
            let parent_ent_name = parent_ent.get::<EntityName>();

            tracing::warn!(
                phase_entity=ent_name
                    .as_ref()
                    .map(|ent_name| ent_name.name.as_ref())
                    .unwrap_or("<unnamed>"),
                parent_entity=parent_ent_name
                    .as_ref()
                    .map(|parent_ent_name| parent_ent_name.name.as_ref())
                    .unwrap_or("<unnamed>"),
                "Parent entity of phase entity is not a phase (missing Phase component)"
            );
        }
    }
}