~starkingdoms/starkingdoms

ref: 54589315a90d94a48978d94420ff0a656d81a2df starkingdoms/crates/unified/src/ecs/thruster.rs -rw-r--r-- 1.2 KiB
54589315 — core feat: thruster metadata 24 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
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};

#[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)]
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,
}