~starkingdoms/starkingdoms

ref: 9f2fc987783eb8bccb423c5326d4c82de719356c starkingdoms/crates/client/src/networking/mod.rs -rw-r--r-- 9.4 KiB
9f2fc987 — ghostly_zsh shut up cargo.lock 8 months 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
201
202
203
204
use std::collections::HashMap;

use bevy_ecs::{entity::Entity, event::Events, query::{QuerySingleError, With}, world::World};
use nalgebra::{Rotation2, Scale2, Scale3, Translation3};
use starkingdoms_common::{packet::Packet, PartType, PlanetType};

use crate::components::{Camera, Part, Planet, Player, RecvPacket, SendPacket, ServerId, SpriteBundle, Texture, Transform};

#[cfg(target_arch = "wasm32")]
#[path = "ws_wasm.rs"]
pub mod ws;
#[cfg(not(target_arch = "wasm32"))]
#[path = "ws_native.rs"]
pub mod ws;

fn texture_name(part_type: PartType, attached: bool) -> String {
    use PartType::*;
    if attached {
        match part_type {
            Placeholder => panic!("AHHHH PLACEHOLDER PANIC"),
            Hearty => "hearty.svg",
            Cargo => "cargo_on.svg",
            Hub => "hub_on.svg",
            LandingThruster => "landingthruster_on.svg",
            LandingThrusterSuspension => "landingleg.svg",
        }.to_string()
    } else {
        match part_type {
            Placeholder => panic!("AHHHH PLACEHOLDER PANIC"),
            Hearty => "hearty.svg",
            Cargo => "cargo_off.svg",
            Hub => "hub_off.svg",
            LandingThruster => "landingthruster_off.svg",
            LandingThrusterSuspension => "landingleg.svg",
        }.to_string()
    }
}

pub fn process_packets(
    world: &mut World,
    send_packet_events: &mut Events<SendPacket>,
    recv_packet_events: &mut Events<RecvPacket>,
    planet_types: &mut HashMap<PlanetType, (Entity, u32)>,
    amount: &mut u32,
    max: &mut u32,
) {
    use Packet::*;
    let mut recv_cursor = recv_packet_events.get_cursor();
    for recv in recv_cursor.read(&recv_packet_events) {
        match &recv.0 {
            LoginResponse { id } => {
                let mut player_query = world.query_filtered::<Entity, With<Player>>();
                let entity = player_query.single(world);
                world.entity_mut(entity).insert(ServerId(*id));
            }
            PlayerList { players } => { // existing players
                // username sync, eventually
                for player in players {
                    world.spawn((Transform {
                        translation: Translation3::new(0.0, 0.0, 0.0),
                        rotation: Rotation2::new(0.0),
                        scale: Scale3::new(25.0, 25.0, 1.0),
                    }, Texture {
                        name: "hearty.svg".to_string(),
                    }, ServerId(player.0), Part(false)));
                }
            }
            SpawnPlayer { id, username } => {
                // username sync, eventually
                world.spawn((Transform {
                    translation: Translation3::new(0.0, 0.0, 0.0),
                    rotation: Rotation2::new(0.0),
                    scale: Scale3::new(25.0, 25.0, 1.0),
                }, Texture {
                    name: "hearty.svg".to_string(),
                }, ServerId(*id), Part(false)));
            }
            SpawnPart { id, part } => {
                world.spawn((Transform {
                    translation: Translation3::new(part.transform.x, part.transform.y, 0.0),
                    rotation: Rotation2::new(part.transform.rot),
                    scale: Scale3::new(25.0, 25.0, 1.0),
                }, Texture {
                    name: texture_name(part.part_type, part.flags.attached),
                }, ServerId(*id), Part(part.flags.attached)));
            }
            PartPositions { parts } => {
                for (id, part) in parts {
                    if part.part_type == PartType::Hearty {
                        let mut player_query = world.query_filtered::<&ServerId, With<Player>>();
                        let server_id = match player_query.get_single(world) {
                            Ok(player) => player,
                            Err(e) => match e {
                                QuerySingleError::NoEntities(s) => {
                                    continue;
                                }
                                QuerySingleError::MultipleEntities(s) => {
                                    panic!("There should never multiple players marked as players");
                                }
                            }
                        };
                        if server_id.0 == *id {
                            let mut camera = world.resource_mut::<Camera>();
                            camera.x = -part.transform.x;
                            camera.y = -part.transform.y;
                        }
                    }
                    let mut part_query = world.query::<(Entity, &ServerId, &mut Transform, &mut Texture, &mut Part)>();
                    for (entity, server_id, mut transform, mut texture, mut bevy_part) in part_query.iter_mut(world) {
                        if server_id.0 == *id {
                            transform.translation.x = part.transform.x;
                            transform.translation.y = part.transform.y;
                            transform.rotation = Rotation2::new(part.transform.rot);

                            if part.flags.attached && !bevy_part.0 {
                                texture.name = texture_name(part.part_type, part.flags.attached);
                                bevy_part.0 = true;
                            } else if !part.flags.attached && bevy_part.0 {
                                texture.name = texture_name(part.part_type, part.flags.attached);
                                bevy_part.0 = false;
                            }
                        }
                    }
                }
            }
            PlanetPositions { planets } => {
                for (server_id, planet) in planets {
                    let mut planet_query = world.query_filtered::<&mut Transform, With<Planet>>();
                    if !planet_types.contains_key(&planet.planet_type) {
                        let entity = world.spawn(SpriteBundle {
                            transform: Transform {
                                translation: Translation3::new(planet.transform.x, planet.transform.y, 0.0),
                                rotation: Rotation2::new(planet.transform.rot),
                                scale: Scale3::new(planet.radius, planet.radius, 1.0),
                            },
                            texture: Texture {
                                name: match planet.planet_type {
                                    /*PlanetType::Sun => "sun.svg",
                                    PlanetType::Mercury => "mercury.svg",
                                    PlanetType::Venus => "venus.svg",
                                    PlanetType::Earth => "earth.svg",
                                    PlanetType::Moon => "moon.svg",
                                    PlanetType::Mars => "mars.svg",
                                    PlanetType::Jupiter => "jupiter.svg",
                                    PlanetType::Saturn => "saturn.svg",
                                    PlanetType::Uranus => "uranus.svg",
                                    PlanetType::Neptune => "neptune.svg",
                                    PlanetType::Pluto => "pluto.svg",*/
                                    PlanetType::Sun => "sun.svg",
                                    PlanetType::Mercury => "moon.svg",
                                    PlanetType::Venus => "venus.svg",
                                    PlanetType::Earth => "earth.svg",
                                    PlanetType::Moon => "moon.svg",
                                    PlanetType::Mars => "mars.svg",
                                    PlanetType::Jupiter => "jupiter.svg",
                                    PlanetType::Saturn => "sun.svg",
                                    PlanetType::Uranus => "venus.svg",
                                    PlanetType::Neptune => "mars.svg",
                                    PlanetType::Pluto => "earth.svg",
                                }.to_string()
                            },
                        });
                        planet_types.insert(planet.planet_type, (entity.id(), *server_id));
                    }
                }
            }
            EnergyUpdate { amount: m_amount, max: m_max } => {
                *amount = *m_amount;
                *max = *m_max;
            }
            PlayerLeave { id } => {
                let mut part_query = world.query_filtered::<(Entity, &ServerId), With<Part>>();
                let mut entity_to_remove = None;
                for (entity, server_id) in part_query.iter_mut(world) {
                    if server_id.0 == *id {
                        entity_to_remove = Some(entity);
                    }
                }
                match entity_to_remove {
                    Some(entity) => {
                        world.despawn(entity);
                    }
                    None => {}
                }
            }
            DespawnPart { id } => {
                let mut part_query = world.query_filtered::<(Entity, &ServerId), With<Part>>();
                let mut entity_to_remove = None;
                for (entity, server_id) in part_query.iter_mut(world) {
                    if server_id.0 == *id {
                        entity_to_remove = Some(entity);
                    }
                }
                match entity_to_remove {
                    Some(entity) => {
                        world.despawn(entity);
                    }
                    None => {}
                }
            }
            _ => {}
        }
    }
}