~starkingdoms/starkingdoms

ed12e3eded2abbae198187d50d9e70515cd3876f — ghostlyzsh 2 years ago 1233a5f
orbits fixed
3 files changed, 63 insertions(+), 43 deletions(-)

M client/src/index.ts
M server/src/entity.rs
M server/src/timer.rs
M client/src/index.ts => client/src/index.ts +1 -0
@@ 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)}`
                }

M server/src/entity.rs => server/src/entity.rs +11 -0
@@ 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<Planet> {
        self.get_planets().iter().find(|u| u.planet_type == planet_type).cloned()
    }
    pub fn get_planet_id(&self, planet_type: PlanetType) -> Option<EntityId> {
        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();

M server/src/timer.rs => server/src/timer.rs +51 -43
@@ 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);