~starkingdoms/starkingdoms

ref: 39d56abc48b6f649328ab5ea7c0adeace42c3b35 starkingdoms/crates/unified/src/server/planets.rs -rw-r--r-- 8.3 KiB
39d56abc — core feat: pain, suffering, and despair 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::{config::planet::{Planet, PlanetBundle, PlanetConfigCollection}, ecs::PlanetSensor, world_config::WorldConfigResource};
use bevy::{asset::Handle, math::DVec3};
use crate::prelude::*;
use bevy_replicon::prelude::Replicated;
use crate::config::planet::{PlanetSpring, PlanetSpringJoint};

pub fn planets_plugin(app: &mut App) {
    app.init_resource::<PlanetConfigResource>()
        .add_systems(Startup, start_loading_planets)
        .add_systems(Update, update_planets);
}

#[derive(Resource, Default)]
pub struct PlanetConfigResource {
    handle: Option<Handle<PlanetConfigCollection>>,
}

fn start_loading_planets(assets: Res<AssetServer>, mut planets: ResMut<PlanetConfigResource>) {
    planets.handle = Some(assets.load("config/planets.pc.toml"));
}

pub fn update_planets(
    mut commands: Commands,
    mut ev_config: MessageReader<AssetEvent<PlanetConfigCollection>>,
    assets: ResMut<Assets<PlanetConfigCollection>>,
    planets: ResMut<PlanetConfigResource>,
    mut q_planets: Query<(
        Entity,
        &mut Planet,
        &mut Transform,
        &mut Mass,
    )>,
    mut planet_joint_springs: Query<(&PlanetSpringJoint, &mut FixedJoint)>,
    world_config: Res<WorldConfigResource>,
) {
    let Some(ref world_config) = world_config.config else {
        return;
    };
    let Some(handle) = planets.handle.as_ref() else {
        return;
    };

    let waiting_for_asset_id = handle.id();

    for ev in ev_config.read() {
        match ev {
            AssetEvent::Added { id } => {
                if *id == waiting_for_asset_id {
                    debug!("planet config loaded - creating planets");
                    let planet_config = assets.get(*id).unwrap();
                    for planet in &planet_config.planets {
                        let planet_position = vec3(planet.default_transform[0], planet.default_transform[1], planet.default_transform[2]);
                        let mut planet_entity = commands
                            .spawn((PlanetBundle {
                                planet: planet.clone(),
                                transform: Transform::from_xyz(
                                    planet.default_transform[0],
                                    planet.default_transform[1],
                                    planet.default_transform[2],
                                ),
                                collider: Collider::circle(planet.radius),
                                mass: Mass(planet.mass)
                            },
                            SleepingDisabled
                        ));
                        planet_entity.with_child((
                            Collider::circle(planet.radius+2.0),
                            Sensor,
                            PlanetSensor(planet.name.clone()),
                            CollisionEventsEnabled,
                        ));
                        let planet_entity_id = planet_entity.id();
                        if let Some(orbit) = &planet.orbit {
                            let Some(parent_planet) = planet_config.planets.iter().find(|u| orbit.orbiting == u.name) else { continue };
                            let parent_planet_position = vec3(parent_planet.default_transform[0],
                                parent_planet.default_transform[1], parent_planet.default_transform[2]);
                            let r = (planet_position - parent_planet_position).as_dvec3();
                            let g = world_config.world.gravity * parent_planet.mass as f64
                                / r.length_squared();
                            // tangential velocity
                            let v = (g*r.length() as f64).sqrt();
                            let v_dir = DVec3::Z.cross(r).truncate().normalize();
                            let v = v_dir * v;
                            planet_entity.insert(LinearVelocity(v));

                            let spring = commands.spawn((
                                PlanetSpring {
                                    name: planet.name.clone()
                                },
                                Transform::from_xyz(
                                    planet.default_transform[0],
                                    planet.default_transform[1],
                                    planet.default_transform[2],
                                ),
                                RigidBody::Kinematic,
                            )).id();
                            commands.spawn((
                                PlanetSpringJoint {
                                    name: planet.name.clone()
                                },
                                FixedJoint::new(planet_entity_id, spring).with_point_compliance(planet_config.orbit.planet_spring_compliance),
                                JointDamping {
                                    linear: planet_config.orbit.planet_spring_damping,
                                    angular: 1.0,
                                },
                            ));
                        }

                        trace!(?planet, "new planet spawned");
                    }
                }
            }
            AssetEvent::Modified { id } => {
                if *id == waiting_for_asset_id {
                    trace!("planet config modified - reloading planets");
                    let planet_config = assets.get(*id).unwrap();

                    info!("updating compliance on all planet spring joints");
                    planet_joint_springs.iter_mut().for_each(|mut u| u.1.point_compliance = planet_config.orbit.planet_spring_compliance);

                    for planet in &planet_config.planets {
                        let existing_planet = q_planets
                            .iter_mut()
                            .find(|(_, p, _, _)| p.name == planet.name);

                        if let Some((existing, mut e_planet, mut e_transform, mut e_mass)) =
                            existing_planet
                        {
                            commands
                                .entity(existing)
                                .remove::<Collider>()
                                .insert(Collider::circle(planet.radius));
                            *e_planet = planet.clone();
                            *e_mass = Mass(planet.mass);
                            trace!(?planet, "planet hot-reloaded");
                        } else {
                            let planet_entity = commands
                                .spawn(PlanetBundle {
                                    planet: planet.clone(),
                                    transform: Transform::from_xyz(
                                        planet.default_transform[0],
                                        planet.default_transform[1],
                                        planet.default_transform[2],
                                    ),
                                    collider: Collider::circle(planet.radius),
                                    mass: Mass(
                                        planet.mass,
                                    ),
                                }).id();

                            if planet.orbit.is_some() {
                                let spring =commands.spawn((
                                    PlanetSpring {
                                        name: planet.name.clone()
                                    },
                                    Transform::from_xyz(
                                        planet.default_transform[0],
                                        planet.default_transform[1],
                                        planet.default_transform[2],
                                    )
                                )).id();
                                commands.spawn((
                                    PlanetSpringJoint {
                                        name: planet.name.clone()
                                    },
                                    FixedJoint::new(planet_entity, spring).with_point_compliance(planet_config.orbit.planet_spring_compliance)
                                ));
                            }
                            trace!(?planet, "new planet spawned");
                        }
                    }
                }
            }
            _ => {}
        }
    }
}