From ed12e3eded2abbae198187d50d9e70515cd3876f Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Thu, 29 Jun 2023 17:04:58 -0500 Subject: [PATCH] orbits fixed --- client/src/index.ts | 1 + server/src/entity.rs | 11 ++++++ server/src/timer.rs | 94 ++++++++++++++++++++++++-------------------- 3 files changed, 63 insertions(+), 43 deletions(-) diff --git a/client/src/index.ts b/client/src/index.ts index 1d84f2f5dd57cc4f95f14051bad9f4cf56644e67..3795a8c15fc53edffe7755233712d0bac977c7f0 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -349,6 +349,7 @@ async function client_main(server: string, username: string, texture_quality: st global.context.moveTo(global.me!.x - global.me!.x, global.me!.y - global.me!.y); global.context.lineTo(planet.x - global.me!.x, planet.y - global.me!.y); global.context.stroke(); + console.log("moon: " + planet.x + ", " + planet.y); document.getElementById("pos-moon")!.innerText = `Relative to Moon: ${Math.trunc(global.me!.x - planet.x)}, ${Math.trunc(global.me!.y - planet.y)}` } diff --git a/server/src/entity.rs b/server/src/entity.rs index 7178c2f36523f99a248184af001e190bcf50697b..3ec683bc9b903f05f8f2e9980faf7c830fee0cbf 100644 --- a/server/src/entity.rs +++ b/server/src/entity.rs @@ -1,5 +1,6 @@ use std::{collections::HashMap, net::SocketAddr, sync::atomic::AtomicU32}; +use log::debug; use nalgebra::Vector2; use protobuf::SpecialFields; use starkingdoms_protocol::planet::PlanetType; @@ -44,6 +45,16 @@ impl EntityHandler { pub fn get_planet(&self, planet_type: PlanetType) -> Option { self.get_planets().iter().find(|u| u.planet_type == planet_type).cloned() } + pub fn get_planet_id(&self, planet_type: PlanetType) -> Option { + for (id, entity) in &self.entities { + if let Entity::Planet(planet) = entity { + if planet.planet_type == planet_type { + return Some(*id); + } + } + } + None + } pub fn get_players(&self) -> Vec<(SocketAddr, Player)> { let mut players = Vec::new(); diff --git a/server/src/timer.rs b/server/src/timer.rs index edf0be53237e42c2e6def84b965470143c477b78..d2758dbab6628bbc3d122f1ad78ec03c138af36e 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -10,7 +10,7 @@ use crate::{ }; use async_std::sync::RwLock; use async_std::task::sleep; -use log::warn; +use log::{warn, debug}; use nalgebra::{point, vector}; use protobuf::SpecialFields; use rand::Rng; @@ -64,51 +64,59 @@ pub async fn timer_main( // IT MAY ALWAYS BE TRUE // THATS FINE if GAME_ORBITS_ENABLED { - let planets = entities.write().await; + let mut planets = entities.write().await; + let mut map = planets.entities.clone(); // update earth (nothing changes, yet) - let earth = planets - .get_planet(PlanetType::Earth) - .ok_or("earth does not exist")?; - let new_earth_position = vector![earth.position.0, earth.position.1]; - - // update moon - let moon: &mut Planet = &mut planets - .get_planet(PlanetType::Moon) - .ok_or("moon does not exist")?; - let new_moon_position = calculate_world_position_of_orbit( - calculate_point_on_orbit(MOON_PERIAPSIS, MOON_APOAPSIS, time / MOON_ORBIT_TIME), - new_earth_position, - ); - let moon_body = physics_data - .rigid_body_set - .get_mut(moon.body_handle) - .ok_or("moon does not exist")?; - moon_body.set_next_kinematic_position(new_moon_position.into()); - moon.position = ( - moon_body.translation()[0] / SCALE, - moon_body.translation()[1] / SCALE, - ); - - // update mars - let mars: &mut Planet = &mut planets - .get_planet(PlanetType::Mars) - .ok_or("mars does not exist")?; - let new_mars_position = calculate_world_position_of_orbit( - calculate_point_on_orbit(MARS_PERIHELION, MARS_APHELION, time / MARS_ORBIT_TIME), - new_moon_position, - ); - let mars_body = physics_data - .rigid_body_set - .get_mut(mars.body_handle) - .ok_or("mars does not exist")?; - mars_body.set_next_kinematic_position(new_mars_position.into()); - mars.position = ( - mars_body.translation()[0] / SCALE, - mars_body.translation()[1] / SCALE, - ); - + let earth_id = planets.get_planet_id(PlanetType::Earth).ok_or("earth does not exist")?; + if let Entity::Planet(earth) = map + .get(&earth_id) + .ok_or("earth does not exist")? { + let new_earth_position = vector![earth.position.0, earth.position.1]; + + // update moon + let moon_id = planets.get_planet_id(PlanetType::Moon).ok_or("moon does not exist")?; + if let Entity::Planet(moon) = map + .get_mut(&moon_id) + .ok_or("moon does not exist")? { + let new_moon_position = calculate_world_position_of_orbit( + calculate_point_on_orbit(MOON_PERIAPSIS, MOON_APOAPSIS, time / MOON_ORBIT_TIME), + new_earth_position, + ); + let new_moon_position = new_moon_position / SCALE; + let moon_body = physics_data + .rigid_body_set + .get_mut(moon.body_handle) + .ok_or("moon does not exist")?; + moon_body.set_next_kinematic_translation(new_moon_position.into()); + moon.position = ( + moon_body.translation()[0], + moon_body.translation()[1], + ); + // update mars + let mars_id = planets.get_planet_id(PlanetType::Mars).ok_or("moon does not exist")?; + if let Entity::Planet(mars) = map + .get_mut(&mars_id) + .ok_or("mars does not exist")? { + let new_mars_position = calculate_world_position_of_orbit( + calculate_point_on_orbit(MARS_PERIHELION, MARS_APHELION, time / MARS_ORBIT_TIME), + new_moon_position, + ); + let new_mars_position = new_mars_position/SCALE; + let mars_body = physics_data + .rigid_body_set + .get_mut(mars.body_handle) + .ok_or("mars does not exist")?; + mars_body.set_next_kinematic_translation(new_mars_position.into()); + mars.position = ( + mars_body.translation()[0], + mars_body.translation()[1], + ); + } + } + } + planets.entities = map; } physics_data.tick(&mut pipeline);