From 1a32083ff657ca517125f740be12361e035ab8f7 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Mon, 1 Jan 2024 22:57:50 -0600 Subject: [PATCH] module on module attachment added --- server/src/component.rs | 2 +- server/src/main.rs | 220 ++++++++++++++++++++++++++++++---------- 2 files changed, 166 insertions(+), 56 deletions(-) diff --git a/server/src/component.rs b/server/src/component.rs index 570886a9cb2f36f667cf922a369589d18dc53c48..a0edae10f1aaa1e2cf0547fb59df1619bfc79917 100644 --- a/server/src/component.rs +++ b/server/src/component.rs @@ -43,7 +43,7 @@ pub struct Input { pub right: bool, } -#[derive(Component)] +#[derive(Component, Clone, Debug)] pub struct Player { pub addr: SocketAddr, pub username: String, diff --git a/server/src/main.rs b/server/src/main.rs index fbef3cfd2577919ecd616ea94e835789e8131e65..a46163a28058f34483e0e0e3a24b6eda511b5003 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -128,7 +128,7 @@ fn on_message( mut commands: Commands, planet_query: Query<(Entity, &PlanetType, &Transform)>, mut part_query: Query<(Entity, &PartType, &mut Transform, &mut Velocity), (Without, Without, Without)>, - mut attached_query: Query<(Entity, &PartType, &mut Transform, &Attach), (Without, Without)>, + mut attached_query: Query<(Entity, &PartType, &mut Transform, &mut Attach, &Velocity), (Without, Without)>, mut player_query: Query<(Entity, &mut Player, &Transform, &Velocity, &mut Attach), Without>, mut packet_recv: Local>, mut packet_event_send: ResMut>, @@ -245,7 +245,7 @@ fn on_message( }, )); } - for (entity, part_type, transform, _) in &attached_query { + for (entity, part_type, transform, _, _) in &attached_query { parts.push(( entity.index(), Part { @@ -342,61 +342,83 @@ fn on_message( break; }; q_player.selected = None; - let mut module = part_query.get_mut(select).unwrap(); - // attach module - let p_pos = transform.translation; - let (rel_x, rel_y) = ( - p_pos.x - x / SCALE, - p_pos.y - y / SCALE, - ); - let angle = transform.rotation.to_euler(EulerRot::ZYX).0; - let (rel_x, rel_y) = ( - rel_x * (-angle).cos() - rel_y * (-angle).sin(), - rel_x * (-angle).sin() + rel_y * (-angle).cos(), - ); - - if 15./SCALE < rel_y && rel_y < 30./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE { - module.2.translation = vec3(p_pos.x + 53./SCALE*angle.sin(), p_pos.y - 53./SCALE*angle.cos(), 0.); - module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle, 0., 0.); - module.3.linvel = velocity.linvel; - let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, -53. / SCALE)); - let mut module_entity = commands.entity(module.0); - module_entity.insert(ImpulseJoint::new(entity, joint)); - attach.children[2] = Some(module.0); - break; - } else if -30./SCALE < rel_y && rel_y < -15./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE { - module.2.translation = vec3(p_pos.x - 53./SCALE*angle.sin(), p_pos.y + 53./SCALE*angle.cos(), 0.); - module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + std::f32::consts::PI, 0., 0.); - module.3.linvel = velocity.linvel; - let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, 53. / SCALE)); - let mut module_entity = commands.entity(module.0); - module_entity.insert(ImpulseJoint::new(entity, joint)); - attach.children[0] = Some(module.0); - break; - } else if -30./SCALE < rel_x && rel_x < -15./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE { - module.2.translation = vec3(p_pos.x + 53./SCALE*angle.cos(), p_pos.y + 53./SCALE*angle.sin(), 0.); - module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + (std::f32::consts::PI/2.), 0., 0.); - module.3.linvel = velocity.linvel; - let joint = FixedJointBuilder::new() - .local_anchor1(vec2(53. / SCALE, 0. / SCALE)) - .local_basis2(std::f32::consts::PI/2.); - let mut module_entity = commands.entity(module.0); - module_entity.insert(ImpulseJoint::new(entity, joint)); - attach.children[1] = Some(module.0); - break; - } else if 15./SCALE < rel_x && rel_x < 30./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE { - module.2.translation = vec3(p_pos.x - 53./SCALE*angle.cos(), p_pos.y - 53./SCALE*angle.sin(), 0.); - module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle - (std::f32::consts::PI/2.), 0., 0.); - module.3.linvel = velocity.linvel; - let joint = FixedJointBuilder::new() - .local_anchor1(vec2(-53. / SCALE, 0. / SCALE)) - .local_basis2(-std::f32::consts::PI/2.); - let mut module_entity = commands.entity(module.0); - module_entity.insert(ImpulseJoint::new(entity, joint)); - attach.children[1] = Some(module.0); + { + let mut module = part_query.get_mut(select).unwrap(); + // attach module + let p_pos = transform.translation; + let (rel_x, rel_y) = ( + p_pos.x - x / SCALE, + p_pos.y - y / SCALE, + ); + let angle = transform.rotation.to_euler(EulerRot::ZYX).0; + let (rel_x, rel_y) = ( + rel_x * (-angle).cos() - rel_y * (-angle).sin(), + rel_x * (-angle).sin() + rel_y * (-angle).cos(), + ); + + if 15./SCALE < rel_y && rel_y < 30./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE { + module.2.translation = vec3(p_pos.x + 53./SCALE*angle.sin(), p_pos.y - 53./SCALE*angle.cos(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle, 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, -53. / SCALE)); + let mut module_entity = commands.entity(module.0); + module_entity.insert(ImpulseJoint::new(entity, joint)); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + attach.children[2] = Some(module.0); + break; + } else if -30./SCALE < rel_y && rel_y < -15./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE { + module.2.translation = vec3(p_pos.x - 53./SCALE*angle.sin(), p_pos.y + 53./SCALE*angle.cos(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + std::f32::consts::PI, 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, 53. / SCALE)); + let mut module_entity = commands.entity(module.0); + module_entity.insert(ImpulseJoint::new(entity, joint)); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + attach.children[0] = Some(module.0); + break; + } else if -30./SCALE < rel_x && rel_x < -15./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE { + module.2.translation = vec3(p_pos.x + 53./SCALE*angle.cos(), p_pos.y + 53./SCALE*angle.sin(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + (std::f32::consts::PI/2.), 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new() + .local_anchor1(vec2(53. / SCALE, 0. / SCALE)) + .local_basis2(std::f32::consts::PI/2.); + let mut module_entity = commands.entity(module.0); + module_entity.insert(ImpulseJoint::new(entity, joint)); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + attach.children[1] = Some(module.0); + break; + } else if 15./SCALE < rel_x && rel_x < 30./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE { + module.2.translation = vec3(p_pos.x - 53./SCALE*angle.cos(), p_pos.y - 53./SCALE*angle.sin(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle - (std::f32::consts::PI/2.), 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new() + .local_anchor1(vec2(-53. / SCALE, 0. / SCALE)) + .local_basis2(-std::f32::consts::PI/2.); + let mut module_entity = commands.entity(module.0); + module_entity.insert(ImpulseJoint::new(entity, joint)); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + attach.children[1] = Some(module.0); + break; + } + } + if attach_on_module_tree(x, y, &mut commands, attach.clone(), select, &mut attached_query, &mut part_query) { break; } - module.2.translation = vec3(x / SCALE, y / SCALE, 0.); + // move module to cursor since no attach + part_query.get_mut(select).unwrap().2.translation = vec3(x / SCALE, y / SCALE, 0.); break; } for (entity, part_type, transform, _) in &part_query { @@ -430,6 +452,94 @@ fn on_message( } } +fn attach_on_module_tree( + x: f32, + y: f32, + commands: &mut Commands, + attach: Attach, + select: Entity, + attached_query: &mut Query<(Entity, &PartType, &mut Transform, &mut Attach, &Velocity), (Without, Without)>, + part_query: &mut Query<(Entity, &PartType, &mut Transform, &mut Velocity), (Without, Without, Without)>, +) -> bool { + let mut ret = false; + for child in attach.children { + if let Some(child) = child { + let (entity, _part_type, transform, mut attach, velocity) = attached_query.get_mut(child).unwrap(); + + let p_pos = transform.translation; + let (rel_x, rel_y) = ( + p_pos.x - x / SCALE, + p_pos.y - y / SCALE, + ); + let angle = transform.rotation.to_euler(EulerRot::ZYX).0; + let (rel_x, rel_y) = ( + rel_x * (-angle).cos() - rel_y * (-angle).sin(), + rel_x * (-angle).sin() + rel_y * (-angle).cos(), + ); + let mut module = part_query.get_mut(select).unwrap(); + if 15./SCALE < rel_y && rel_y < 30./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE { + module.2.translation = vec3(p_pos.x + 53./SCALE*angle.sin(), p_pos.y - 53./SCALE*angle.cos(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle, 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, -53. / SCALE)); + let mut module_entity = commands.entity(module.0); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + module_entity.insert(ImpulseJoint::new(entity, joint)); + attach.children[2] = Some(module.0); + return true; + } else if -30./SCALE < rel_y && rel_y < -15./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE { + module.2.translation = vec3(p_pos.x - 53./SCALE*angle.sin(), p_pos.y + 53./SCALE*angle.cos(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + std::f32::consts::PI, 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, 53. / SCALE)); + let mut module_entity = commands.entity(module.0); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + module_entity.insert(ImpulseJoint::new(entity, joint)); + attach.children[0] = Some(module.0); + return true; + } else if -30./SCALE < rel_x && rel_x < -15./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE { + module.2.translation = vec3(p_pos.x + 53./SCALE*angle.cos(), p_pos.y + 53./SCALE*angle.sin(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + (std::f32::consts::PI/2.), 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new() + .local_anchor1(vec2(53. / SCALE, 0. / SCALE)) + .local_basis2(std::f32::consts::PI/2.); + let mut module_entity = commands.entity(module.0); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + module_entity.insert(ImpulseJoint::new(entity, joint)); + attach.children[1] = Some(module.0); + return true; + } else if 15./SCALE < rel_x && rel_x < 30./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE { + module.2.translation = vec3(p_pos.x - 53./SCALE*angle.cos(), p_pos.y - 53./SCALE*angle.sin(), 0.); + module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle - (std::f32::consts::PI/2.), 0., 0.); + module.3.linvel = velocity.linvel; + let joint = FixedJointBuilder::new() + .local_anchor1(vec2(-53. / SCALE, 0. / SCALE)) + .local_basis2(-std::f32::consts::PI/2.); + let mut module_entity = commands.entity(module.0); + module_entity.insert(Attach { + associated_player: Some(entity), + children: [None, None, None, None], + }); + module_entity.insert(ImpulseJoint::new(entity, joint)); + attach.children[1] = Some(module.0); + return true; + } + ret = ret | attach_on_module_tree(x, y, commands, attach.clone(), select, attached_query, part_query); + } + } + return ret; +} + fn on_close( player_query: Query<(Entity, &Player, &Attach)>, attached_query: Query<&Attach, With>,