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::Cargo = 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::Cargo = 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;
}*/
}
}
}
}