~starkingdoms/starkingdoms

ref: 93b350fc44934524b95c9f80884d921c53fe7981 starkingdoms/crates/server/src/player/player_mouse_input.rs -rw-r--r-- 6.3 KiB
93b350fc — ghostly_zsh chassis change merge 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
205
use bevy::{math::vec3, prelude::*};
use bevy_rapier2d::prelude::*;

use crate::{
    crafting::components::IsMining,
    module::component::{Attach, CanAttach, LooseAttach, PartFlags, PartType},
    planet::PlanetType,
    ws::{PacketMessageConvert, WsEvent},
};
use starkingdoms_common::{
    packet::{ButtonType, Packet},
    PartType as c_PartType,
};

use super::component::Player;

pub fn attach_or_detach(
    select: Entity,
    attached_query: &mut Query<
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Attach,
            &Velocity,
            Option<&CanAttach>,
            Option<&LooseAttach>,
            &mut PartFlags,
        ),
        (Without<PlanetType>, Without<Player>),
    >,
    player_query: &mut Query<
        (
            Entity,
            &mut Player,
            &Transform,
            &Velocity,
            &mut Attach,
            &mut PartFlags,
        ),
        Without<PlanetType>,
    >,
    part_query: &mut Query<
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Velocity,
            Option<&LooseAttach>,
            &mut PartFlags,
        ),
        (Without<PlanetType>, Without<Player>, Without<Attach>),
    >,
    commands: &mut Commands,
    x: f32,
    y: f32,
    entity: Entity,
) {
    if attached_query.contains(select) {
        let module = attached_query.get(select).unwrap();
        let attach = module.3.clone();
        let lost_energy_capacity =
            crate::module::detach_recursive(commands, module.0, attached_query, player_query);
        let mut module = attached_query.get_mut(select).unwrap();
        module.2.translation = vec3(x, y, 0.);
        if *module.1 == c_PartType::LandingThruster.into() {
            let sub_entity = attach.children[2].unwrap();
            let mut suspension = attached_query.get_mut(sub_entity).unwrap();
            suspension.2.translation = vec3(x, y, 0.);
        }
        let mut player = player_query.get_mut(entity).unwrap().1;
        player.energy_capacity -= lost_energy_capacity;
        player.energy = std::cmp::min(player.energy, player.energy_capacity);
        return;
    }
    if crate::module::attach_on_module_tree(
        x,
        y,
        commands,
        entity,
        select,
        entity,
        attached_query,
        part_query,
        player_query,
    ) {
        let mut part = part_query.get_mut(select).unwrap();
        part.5.attached = true; // all of this code is cursed. what the hell is it actually doing
        return;
    }
    // move module to cursor since no attach
    let mut part = part_query.get_mut(select).unwrap();
    part.2.translation = vec3(x, y, 0.);
    if *part.1 == c_PartType::LandingThruster.into() {
        if let Some(loose_attach) = part.4 {
            let sub_entity = loose_attach.children[2].unwrap();
            let mut part = part_query.get_mut(sub_entity).unwrap();
            part.2.translation = vec3(x, y, 0.);
        }
    }
}

pub fn mouse_picking(
    attached_query: &Query<
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Attach,
            &Velocity,
            Option<&CanAttach>,
            Option<&LooseAttach>,
            &mut PartFlags,
        ),
        (Without<PlanetType>, Without<Player>),
    >,
    part_query: &Query<
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Velocity,
            Option<&LooseAttach>,
            &mut PartFlags,
        ),
        (Without<PlanetType>, Without<Player>, Without<Attach>),
    >,
    player: (Entity, &Transform),
    mining_query: &mut Query<&mut IsMining>,
    q_player: &mut Player,
    x: f32,
    y: f32,
    button: &ButtonType,
    entity: Entity,
    send_events: &mut Vec<WsEvent>,
) {
    for (m_entity, part_type, transform, m_attach, _velocity, _, _, _) in attached_query.iter() {
        if *part_type == c_PartType::LandingThrusterSuspension.into() {
            continue;
        }
        let pos = transform.translation;
        let rel_x = pos.x - x;
        let rel_y = pos.y - y;
        let angle = -transform.rotation.z;
        let x = rel_x * angle.cos() - rel_y * angle.sin();
        let y = rel_x * angle.sin() + rel_y * angle.cos();
        let mut bound = [-0.5, 0.5, -0.5, 0.5]; // left, right, top, bottom
        if let c_PartType::Chassis = part_type.0 {
            bound = [-0.375, 0.375, -0.5, 0.4375];
        }

        if bound[0] < x
            && x < bound[1]
            && bound[2] < y
            && y < bound[3]
            && m_attach.associated_player.unwrap() == entity
        {
            q_player.selected = Some(m_entity);
            break;
        }
    }
    for (entity, part_type, transform, _, _, _) in part_query.iter() {
        if *part_type == c_PartType::LandingThrusterSuspension.into() {
            continue;
        }
        let pos = transform.translation;
        let rel_x = pos.x - x;
        let rel_y = pos.y - y;
        let angle = -transform.rotation.z;
        let x = rel_x * angle.cos() - rel_y * angle.sin();
        let y = rel_x * angle.sin() + rel_y * angle.cos();
        let mut bound = [-0.5, 0.5, -0.5, 0.5]; // left, right, top, bottom
        if let c_PartType::Chassis = part_type.0 {
            bound = [-0.375, 0.375, -0.5, 0.4375];
        }

        if bound[0] < x && x < bound[1] && bound[2] < y && y < bound[3] {
            q_player.selected = Some(entity);
            break;
        }
    }
    {
        let transform = player.1;
        let pos = transform.translation;
        let rel_x = pos.x - x;
        let rel_y = pos.y - y;
        let angle = -transform.rotation.z;
        let x = rel_x * angle.cos() - rel_y * angle.sin();
        let y = rel_x * angle.sin() + rel_y * angle.cos();
        let bound = [-0.5, 0.5, -0.5, 0.5]; // left, right, top, bottom

        if bound[0] < x && x < bound[1] && bound[2] < y && y < bound[3] {
            if *button == ButtonType::Right {
                send_events.push(WsEvent::Send {
                    to: q_player.addr,
                    message: Packet::OpenCraftingUi { id: entity.index() }.into_message(),
                });
                // toggle mining
                /*if let Ok(mut is_mining) = mining_query.get_mut(entity) {
                    is_mining.0 = !is_mining.0;
                }*/
            }
        }
    }
}