add game::JupiterPos resource

This commit is contained in:
yuni 2024-06-11 00:58:56 +02:00
parent 5817944a79
commit 77c1bd1e6a
2 changed files with 15 additions and 17 deletions

View file

@ -555,18 +555,12 @@ fn handle_wants_maxvelocity(
&WantsMaxVelocity,
Option<&OrbitsJupiter>,
)>,
id2pos: Res<game::Id2Pos>,
jupiter_pos: Res<game::JupiterPos>,
) {
let dt = time.delta_seconds();
let jupiter_pos: DVec3 = if let Some(jupiter_pos) = id2pos.0.get(cmd::ID_JUPITER) {
*jupiter_pos
} else {
warn!("Could not determine Jupiter's position");
DVec3::ZERO
};
for (pos, mut v, engine, maxv, orbits_jupiter) in &mut query {
let target_velocity = if orbits_jupiter.is_some() {
let relative_pos = pos.0 - jupiter_pos;
let relative_pos = pos.0 - jupiter_pos.0;
nature::orbital_velocity(relative_pos, nature::JUPITER_MASS)
} else {
DVec3::ZERO
@ -672,21 +666,15 @@ fn handle_gravity(
time: Res<Time>,
mut timer: ResMut<GravityUpdateTimer>,
mut q_pos: Query<(&Position, &mut LinearVelocity), With<OrbitsJupiter>>,
id2pos: Res<game::Id2Pos>,
jupiter_pos: Res<game::JupiterPos>,
) {
if !timer.0.tick(time.delta()).just_finished() {
return;
}
let pos_jupiter: DVec3 = if let Some(pos_jupiter) = id2pos.0.get(cmd::ID_JUPITER) {
*pos_jupiter
} else {
return;
};
// this assumes prograde orbits for every object
for (pos, mut v) in &mut q_pos {
let relative_pos = pos.0 - pos_jupiter;
let relative_pos = pos.0 - jupiter_pos.0;
let accel = nature::gravitational_acceleration(relative_pos, nature::JUPITER_MASS);
v.0 += accel;
}

View file

@ -32,6 +32,7 @@ impl Plugin for GamePlugin {
);
app.add_systems(Update, check_achievements);
app.insert_resource(Id2Pos(HashMap::new()));
app.insert_resource(JupiterPos(DVec3::ZERO));
app.insert_resource(var::AchievementTracker::default());
app.insert_resource(var::Settings::default());
app.insert_resource(var::GameVars::default());
@ -50,6 +51,8 @@ pub struct PlayerDiesEvent(pub actor::DamageType);
#[derive(Resource)]
pub struct Id2Pos(pub HashMap<String, DVec3>);
#[derive(Resource)]
pub struct JupiterPos(pub DVec3);
#[derive(Resource)]
pub struct AchievementCheckTimer(pub Timer);
#[derive(Event)]
@ -351,10 +354,17 @@ fn handle_cheats(
}
}
fn update_id2pos(mut id2pos: ResMut<Id2Pos>, q_id: Query<(&Position, &actor::Identifier)>) {
fn update_id2pos(
mut id2pos: ResMut<Id2Pos>,
mut jupiterpos: ResMut<JupiterPos>,
q_id: Query<(&Position, &actor::Identifier)>,
) {
id2pos.0.clear();
for (pos, id) in &q_id {
id2pos.0.insert(id.0.clone(), pos.0);
if id.0 == "jupiter" {
jupiterpos.0 = pos.0;
}
}
}