~starkingdoms/starkingdoms

ref: f963857665e23f741bba435f733b47f05cda237f starkingdoms/server/src/planet.rs -rw-r--r-- 2.0 KiB
f9638576TerraMaster85 new rendering paradigm & de-fuck rotation 2 years 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
use rapier2d_f64::prelude::{RigidBodyHandle, RigidBodySet, ColliderBuilder, RigidBodyBuilder, ColliderSet};
use starkingdoms_protocol::{PlanetType, ProtocolPlanet};

use crate::{SCALE, manager::ClientHandlerMessage};

#[derive(Clone)]
pub struct Planet {
    pub planet_type: PlanetType,
    pub body_handle: RigidBodyHandle,
    pub position: (f64, f64),
    pub radius: f64,
    pub mass: f64
}

#[derive(Default, Clone)]
pub struct Planets {
    pub planets: Vec<Planet>,
}

impl Planets {
    pub fn make_planet(planets: &mut Vec<Planet>, planet_type: PlanetType, mass: f64, radius: f64, position: (f64, f64), rigid_body_set: &mut RigidBodySet, collider_set: &mut ColliderSet) {
        let collider = ColliderBuilder::ball(1000.0 / SCALE)
            .build();
        let body = RigidBodyBuilder::fixed()
            .additional_mass(0.0);
        let body_handle = rigid_body_set.insert(body);

        collider_set.insert_with_parent(collider, body_handle, rigid_body_set);

        planets.push(Planet {
            planet_type,
            body_handle,
            position,
            radius,
            mass,
        });
    }

    pub fn new(rigid_body_set: &mut RigidBodySet, collider_set: &mut ColliderSet) -> Planets {
        let mut planets = Vec::new();
        
        Planets::make_planet(
            &mut planets,
            PlanetType::Earth,
            2000.0,
            1000.0,
            (0.0, 0.0),
            rigid_body_set,
            collider_set,
        );

        Planets { planets }
    }

    pub fn to_protocol(&self) -> ClientHandlerMessage {
        let mut planets = vec![];

        for planet in self.planets.clone() {
            planets.push(ProtocolPlanet {
                planet_type: planet.planet_type,
                x: planet.position.0 * SCALE,
                y: planet.position.1 * SCALE,
                radius: planet.radius,
            });
        }

        ClientHandlerMessage::PlanetData {
            planets
        }
    }
}