~starkingdoms/starkingdoms

ref: dd101bc47e02b050cc85e160450af537e601d8fe starkingdoms/server/src/entity.rs -rw-r--r-- 5.3 KiB
dd101bc4 — core whoa, i just rewrote spacetime again 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
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
use std::{sync::atomic::AtomicU32, collections::HashMap, net::SocketAddr};

use nalgebra::Vector2;
use starkingdoms_protocol::planet::PlanetType;

use crate::{planet::Planet, SCALE, manager::{ClientHandlerMessage, Player, Module, AttachedModule}};

pub type EntityId = u32;
pub type Entities = HashMap<EntityId, Entity>;
static mut ENTITY_ID_COUNT: AtomicU32 = AtomicU32::new(0);
pub fn get_entity_id() -> EntityId {
    let last_entity_id = unsafe { &ENTITY_ID_COUNT };
    let id = last_entity_id.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
    if id > 4_147_483_600 { panic!("No remaining entity ids") };
    id
}

#[derive(Default)]
pub struct EntityHandler {
    pub entities: Entities,
}

impl EntityHandler {
    pub fn new() -> EntityHandler {
        EntityHandler {
            entities: Entities::new()
        }
    }
    pub fn get_planets(&self) -> Vec<Planet> {
        let mut ids = Vec::new();
        for entity in self.entities.values() {
            if let Entity::Planet(planet) = entity {
                ids.push(planet.clone());
            }
        }
        ids
    }
    pub fn get_planet(&self, planet_type: PlanetType) -> Option<Planet> {
        let mut planets = self.get_planets();
        for i in 0..planets.len() {
            if planets[i].planet_type == planet_type {
                planets.remove(i);
            }
        }
        if planets.is_empty() {
            return None;
        }
        Some(planets[0].clone())
    }

    pub fn get_players(&self) -> Vec<(SocketAddr, Player)> {
        let mut players = Vec::new();
        for entity in self.entities.values() {
            if let Entity::Player(player) = entity {
                players.push((player.addr, player.clone()));
            }
        }
        players
    }
    pub fn get_player_from_id(&self, id: EntityId) -> Option<Player> {
        if let Some(Entity::Player(player)) = self.entities.get(&id) {
            Some(player.clone())
        } else {
            None
        }
    }
    pub fn get_player_id(&self, addr: SocketAddr) -> Option<EntityId> {
        for (id, entity) in self.entities.iter() {
            if let Entity::Player(player) = entity {
                if player.addr == addr {
                    return Some(*id);
                }
            }
        }
        None
    }
    pub fn get_player(&self, addr: SocketAddr) -> Option<Player> {
        let mut players = self.get_players();
        for i in 0..players.len() {
            if players[i].0 != addr {
                players.remove(i);
            }
        }
        if players.is_empty() {
            return None;
        }
        Some(players[0].clone().1)
    }
    pub fn get_modules(&self) -> Vec<Module> {
        let mut modules = Vec::new();
        for entity in self.entities.values() {
            if let Entity::Module(module) = entity {
                modules.push(module.clone());
            }
        }
        modules
    }
    pub fn get_module_count(&self) -> u32 {
        let mut module_count = 0;
        for entity in self.entities.values() {
            if let Entity::Module(_module) = entity {
                module_count += 1;
            }
        }
        module_count
    }
    pub fn get_module_from_id(&self, id: EntityId) -> Option<Module> {
        if let Some(Entity::Module(module)) = self.entities.get(&id) {
            return Some(module.clone());
        }
        None
    }
    pub fn get_from_module(&self, p_module: &Module) -> Option<EntityId> {
        for (id, entity) in self.entities.iter() {
            if let Entity::Module(module) = entity {
                if module.handle == p_module.handle {
                    return Some(*id);
                }
            }
        }
        None
    }
    pub fn get_all_attached(&self) -> Vec<AttachedModule> {
        let mut modules = Vec::new();
        for entity in self.entities.values() {
            if let Entity::AttachedModule(module) = entity {
                modules.push(module.clone());
            }
        }
        modules
    }
    pub fn get_attached_from_id(&self, id: EntityId) -> Option<AttachedModule> {
        if let Some(Entity::AttachedModule(module)) = self.entities.get(&id) {
            return Some(module.clone());
        }
        None
    }

    pub fn gravity(&self, position: (f64, f64), mass: f64) -> (f64, f64) {
        let mut direction = Vector2::zeros();
        let planets = self.get_planets();
        for planet in planets {
            let planet_grav = planet.gravity(position, mass);
            direction.x += planet_grav.0;
            direction.y += planet_grav.1;
        }
        (direction.x, direction.y)
    }

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

        for planet in self.get_planets() {
            planets.push(starkingdoms_protocol::planet::Planet {
                planet_type: planet.planet_type.into(),
                x: planet.position.0 * SCALE,
                y: planet.position.1 * SCALE,
                radius: planet.radius, // DO NOT * SCALE. THIS VALUE IS NOT SCALED!
                special_fields: Default::default(),
            });
        }

        ClientHandlerMessage::PlanetData {
            planets
        }
    }
}

#[derive(Clone)]
pub enum Entity {
    Player(Player),
    Planet(Planet),
    Module(Module),
    AttachedModule(AttachedModule),
}