~starkingdoms/starkingdoms

ref: 222e757f3909b4807d3e2b42f818cdb6d739e62e starkingdoms/server/src/entity.rs -rw-r--r-- 6.2 KiB
222e757f — core Merge remote-tracking branch 'origin/master' 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::{collections::HashMap, net::SocketAddr, sync::atomic::AtomicU32};
use nalgebra::Vector2;
use protobuf::SpecialFields;
use starkingdoms_protocol::planet::PlanetType;

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

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);
    assert!(id <= 4_147_483_600, "No remaining entity ids");
    id
}

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

impl EntityHandler {
    pub fn new() -> Self {
        Self {
            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> {
        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();
        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 {
            if let Entity::Player(player) = entity {
                if player.addr == addr {
                    return Some(*id);
                }
            }
        }
        None
    }
    pub fn get_player(&self, addr: SocketAddr) -> Option<Player> {
        let players = self.get_players();
        let player = players.iter().find(|p| p.0 == addr);
        player.map(|p| p.1.clone())
    }
    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_modules_id(&self) -> Vec<(EntityId, Module)> {
        let mut modules = Vec::new();
        for (id, entity) in self.entities.clone() {
            if let Entity::Module(module) = entity {
                modules.push((id, 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 {
            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_all_attached_id(&self) -> Vec<(EntityId, AttachedModule)> {
        let mut modules = Vec::new();
        for (id, entity) in self.entities.clone() {
            if let Entity::AttachedModule(module) = entity {
                modules.push((id, 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 get_id_from_attached(&self, p_module: &AttachedModule) -> Option<EntityId> {
        for (id, entity) in &self.entities {
            if let Entity::AttachedModule(module) = entity {
                if module.handle == p_module.handle {
                    return Some(*id);
                }
            }
        }
        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 * SCALE, // DO NOT * SCALE. THIS VALUE IS NOT SCALED! YES IT IS
                special_fields: SpecialFields::default(),
            });
        }

        ClientHandlerMessage::PlanetData { planets }
    }
}

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