~starkingdoms/starkingdoms

ref: d2b8956323d8e82903824637cdcfb2f978dd94e9 starkingdoms/crates/unified/src/ecs/thruster.rs -rw-r--r-- 1.3 KiB
d2b89563 — core chore: please thy lord clippy 16 days ago
                                                                                
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
use std::ops::Deref;
use bevy::ecs::entity::MapEntities;
use bevy::math::Vec2;
use bevy::prelude::Bundle;
use serde::{Deserialize, Serialize};
use crate::prelude::{ChildOf, Component, Entity, Transform};
use bevy_replicon::prelude::Replicated;

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct ThrusterId(pub String);
impl ThrusterId {
    #[must_use]
    pub fn from_part_and_thruster_id(part: impl AsRef<str>, thruster: impl AsRef<str>) -> Self {
        Self(format!("{}:{}", part.as_ref(), thruster.as_ref()))
    }
}

#[derive(Component, Serialize, Deserialize, MapEntities)]
#[relationship_target(relationship = ThrusterOfPart, linked_spawn)]
pub struct PartThrusters(#[entities] Vec<Entity>);
impl Deref for PartThrusters {
    type Target = Vec<Entity>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Component, Serialize, Deserialize, MapEntities)]
#[relationship(relationship_target = PartThrusters)]
pub struct ThrusterOfPart(#[entities] pub Entity);

#[derive(Component, Serialize, Deserialize)]
#[require(Replicated)]
pub struct Thruster {
    pub id: ThrusterId,
    pub thrust_vector: Vec2
}

#[derive(Bundle)]
pub struct ThrusterBundle {
    pub thruster: Thruster,
    pub transform: Transform,
    pub child_of: ChildOf,
    pub thruster_of_part: ThrusterOfPart,
}