@@ 169,9 169,11 @@ fn module_spawn(
);
transform.rotate_z(angle);
if part_query.iter().count() < FREE_MODULE_CAP {
+ let flags = PartFlags { attached: false };
let mut entity = commands.spawn(PartBundle {
part_type: PartType::Cargo,
transform: TransformBundle::from(transform),
+ flags
});
entity
.insert(RigidBody::Dynamic)
@@ 199,6 201,7 @@ fn module_spawn(
part: Part {
part_type: PartType::Cargo,
transform: proto_transform!(transform),
+ flags: proto_part_flags!(flags)
},
};
let buf = serde_json::to_vec(&packet).unwrap();
@@ 218,6 221,7 @@ fn on_message(
&mut Transform,
&mut Velocity,
Option<&LooseAttach>,
+ &mut PartFlags,
),
(Without<PlanetType>, Without<Player>, Without<Attach>),
>,
@@ 230,6 234,7 @@ fn on_message(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags
),
(Without<PlanetType>, Without<Player>),
>,
@@ 282,6 287,7 @@ fn on_message(
part: PartBundle {
part_type: PartType::Hearty,
transform: TransformBundle::from(transform),
+ flags: PartFlags { attached: false }
},
player: Player {
addr: *addr,
@@ 365,7 371,7 @@ fn on_message(
// tell the player where parts are
let mut parts = Vec::new();
- for (entity, part_type, transform, _, _) in &part_query {
+ for (entity, part_type, transform, _, _, flags) in &part_query {
parts.push((
entity.index(),
Part {
@@ 373,10 379,11 @@ fn on_message(
transform: proto_transform!(Transform::from_translation(
transform.translation * SCALE
)),
+ flags: proto_part_flags!(flags)
},
));
}
- for (entity, part_type, transform, _, _, _, _) in &attached_query {
+ for (entity, part_type, transform, _, _, _, _, flags) in &attached_query {
parts.push((
entity.index(),
Part {
@@ 384,6 391,7 @@ fn on_message(
transform: proto_transform!(Transform::from_translation(
transform.translation * SCALE
)),
+ flags: proto_part_flags!(flags)
},
));
}
@@ 395,6 403,7 @@ fn on_message(
transform.translation * SCALE
)
.with_rotation(transform.rotation)),
+ flags: ProtoPartFlags { attached: false }
},
));
let packet = Packet::PartPositions { parts };
@@ 506,6 515,7 @@ fn on_message(
module.2.rotation =
Quat::from_euler(EulerRot::ZYX, angle, 0., 0.);
module.3.linvel = velocity.linvel;
+ module.5.attached = true;
let joint = FixedJointBuilder::new()
.local_anchor1(vec2(0. / SCALE, -53. / SCALE));
let mut children = [None, None, None, None];
@@ 559,6 569,7 @@ fn on_message(
module.2.rotation =
Quat::from_euler(EulerRot::ZYX, angle + PI, 0., 0.);
module.3.linvel = velocity.linvel;
+ module.5.attached = true;
let joint = FixedJointBuilder::new()
.local_anchor1(vec2(0. / SCALE, 53. / SCALE));
let mut children = [None, None, None, None];
@@ 616,6 627,7 @@ fn on_message(
0.,
);
module.3.linvel = velocity.linvel;
+ module.5.attached = true;
let joint = FixedJointBuilder::new()
.local_anchor1(vec2(53. / SCALE, 0. / SCALE))
.local_basis2(std::f32::consts::PI / 2.);
@@ 678,6 690,7 @@ fn on_message(
0.,
);
module.3.linvel = velocity.linvel;
+ module.5.attached = true;
let joint = FixedJointBuilder::new()
.local_anchor1(vec2(-53. / SCALE, 0. / SCALE))
.local_basis2(-std::f32::consts::PI / 2.);
@@ 724,12 737,13 @@ fn on_message(
break;
}
} else if attached_query.contains(select) {
- let module = attached_query.get(select).unwrap();
+ let mut module = attached_query.get_mut(select).unwrap();
let parent = module.3.parent.unwrap();
commands.entity(select).remove::<ImpulseJoint>();
commands.entity(select).remove::<Attach>();
let children_attach = module.3.clone();
if *module.1 == PartType::LandingThruster {
+ module.7.attached = false;
commands.entity(select).insert(LooseAttach {
children: module.3.children,
});
@@ 737,6 751,7 @@ fn on_message(
.entity(module.3.children[2].unwrap())
.remove::<Attach>();
} else {
+ module.7.attached = false;
detach_recursive(
&mut commands,
module.3.clone(),
@@ 792,6 807,8 @@ fn on_message(
&mut attached_query,
&mut part_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
break;
}
// move module to cursor since no attach
@@ 806,7 823,7 @@ fn on_message(
}
break;
}
- for (entity, part_type, transform, _m_attach, _velocity, _, _) in
+ for (entity, part_type, transform, _m_attach, _velocity, _, _, _) in
&attached_query
{
if *part_type == PartType::LandingThrusterSuspension {
@@ 836,7 853,7 @@ fn on_message(
}
}
}
- for (entity, part_type, transform, _, _) in &part_query {
+ for (entity, part_type, transform, _, _, _) in &part_query {
if *part_type == PartType::LandingThrusterSuspension {
continue;
}
@@ 899,6 916,7 @@ fn detach_recursive(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags
),
(Without<PlanetType>, Without<Player>),
>,
@@ 906,8 924,8 @@ fn detach_recursive(
for child in attach.children {
if let Some(child) = child {
{
- let (entity, part_type, _transform, attach, _velocity, _, _) =
- attached_query.get(child).unwrap();
+ let (entity, part_type, _transform, attach, _velocity, _, _, mut flags) =
+ attached_query.get_mut(child).unwrap();
commands.entity(entity).remove::<Attach>();
if *part_type == PartType::LandingThruster {
commands.entity(entity).insert(LooseAttach {
@@ 916,21 934,26 @@ fn detach_recursive(
commands
.entity(attach.children[2].unwrap())
.remove::<Attach>();
+ flags.attached = false;
continue;
} else if *part_type == PartType::LandingThrusterSuspension {
+ flags.attached = false;
let parent = attach.parent.unwrap();
let parent_attach = attached_query.get(parent).unwrap().3;
println!("suspension {:?} {:?}", parent_attach.children, entity);
commands.entity(parent).insert(LooseAttach {
children: parent_attach.children,
});
+
} else {
+ flags.attached = false;
commands.entity(entity).remove::<ImpulseJoint>();
detach_recursive(commands, attach.clone(), attached_query);
}
}
- let (entity, _part_type, _transform, attach, _velocity, _, _) =
+ let (entity, _part_type, _transform, attach, _velocity, _, _, mut flags) =
attached_query.get_mut(child).unwrap();
+ flags.attached = false;
let parent = attach.parent.unwrap();
if attached_query.contains(parent) {
let mut parent_attach = attached_query.get_mut(parent).unwrap().3;
@@ 962,6 985,7 @@ fn attach_on_module_tree(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags
),
(Without<PlanetType>, Without<Player>),
>,
@@ 972,6 996,7 @@ fn attach_on_module_tree(
&mut Transform,
&mut Velocity,
Option<&LooseAttach>,
+ &mut PartFlags
),
(Without<PlanetType>, Without<Player>, Without<Attach>),
>,
@@ 979,7 1004,7 @@ fn attach_on_module_tree(
let mut ret = false;
for child in attach.children {
if let Some(child) = child {
- let (entity, _part_type, transform, mut attach, velocity, can_attach, loose_attach) =
+ let (entity, _part_type, transform, mut attach, velocity, can_attach, loose_attach, _) =
attached_query.get_mut(child).unwrap();
let p_pos = transform.translation;
@@ 1028,6 1053,7 @@ fn attach_on_module_tree(
children,
});
attach.children[2] = Some(module.0);
+ module.5.attached = true;
return true;
} else if attach.children[1] == None
&& attachable
@@ 1069,6 1095,7 @@ fn attach_on_module_tree(
children,
});
attach.children[1] = Some(module.0);
+ module.5.attached = true;
return true;
} else if attach.children[3] == None
&& attachable
@@ 1110,6 1137,7 @@ fn attach_on_module_tree(
children,
});
attach.children[3] = Some(module.0);
+ module.5.attached = true;
return true;
}
ret = ret
@@ 1137,7 1165,7 @@ fn convert_modules(
planet_query: Query<(Entity, &PlanetType, &Children)>,
player_query: Query<&Attach, With<Player>>,
mut attached_query: Query<
- (Entity, &mut PartType, &mut Attach, &Children, &Transform),
+ (Entity, &mut PartType, &mut Attach, &Children, &Transform, &PartFlags),
Without<Player>,
>,
mut collider_query: Query<
@@ 1199,7 1227,7 @@ fn convert_modules_recursive(
planet_type: PlanetType,
attach: Attach,
attached_query: &mut Query<
- (Entity, &mut PartType, &mut Attach, &Children, &Transform),
+ (Entity, &mut PartType, &mut Attach, &Children, &Transform, &PartFlags),
Without<Player>,
>,
collider_query: &mut Query<
@@ 1210,7 1238,7 @@ fn convert_modules_recursive(
) {
for child in attach.children {
if let Some(child) = child {
- let (module_entity, mut part_type, mut attach, children, module_transform) =
+ let (module_entity, mut part_type, mut attach, children, module_transform, part_flags) =
attached_query.get_mut(child).unwrap();
if *part_type == PartType::Cargo {
match planet_type {
@@ 1238,6 1266,7 @@ fn convert_modules_recursive(
part: Part {
part_type: PartType::Hub,
transform: proto_transform!(transform),
+ flags: proto_part_flags!(part_flags)
},
};
let buf = serde_json::to_vec(&packet).unwrap();
@@ 1260,6 1289,7 @@ fn convert_modules_recursive(
let mut suspension = commands.spawn(PartBundle {
transform: TransformBundle::from(*module_transform),
part_type: PartType::LandingThrusterSuspension,
+ flags: PartFlags { attached: false }
});
suspension
.insert(RigidBody::Dynamic)
@@ 1298,6 1328,7 @@ fn convert_modules_recursive(
part: Part {
part_type: PartType::LandingThruster,
transform: proto_transform!(transform),
+ flags: proto_part_flags!(part_flags)
},
};
let buf = serde_json::to_vec(&packet).unwrap();
@@ 1309,6 1340,7 @@ fn convert_modules_recursive(
part: Part {
part_type: PartType::LandingThrusterSuspension,
transform: proto_transform!(transform),
+ flags: proto_part_flags!(part_flags)
},
};
let buf = serde_json::to_vec(&packet).unwrap();
@@ 1343,6 1375,7 @@ fn break_modules(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags
),
(Without<PlanetType>, Without<Player>),
>,
@@ 1350,13 1383,14 @@ fn break_modules(
) {
let joints = rapier_context.entity2impulse_joint();
let mut detach_list = Vec::new();
- for (entity, part_type, _, attach, _, _, _) in &mut attached_query {
+ for (entity, part_type, _, attach, _, _, _, mut flags) in &mut attached_query {
if *part_type == PartType::LandingThrusterSuspension {
continue;
}
let handle = joints.get(&entity).unwrap();
let joint = rapier_context.impulse_joints.get(*handle).unwrap();
- if joint.impulses.magnitude() > 0.00005 {
+ if joint.impulses.magnitude() > 0.0001 {
+ flags.attached = false;
detach_list.push((entity, *part_type, attach.clone()));
}
}
@@ 1367,9 1401,12 @@ fn break_modules(
commands.entity(entity).insert(LooseAttach {
children: attach.children,
});
- commands
+ if (attach.children[2].is_some()) {
+ commands
.entity(attach.children[2].unwrap())
.remove::<Attach>();
+ }
+
} else {
detach_recursive(&mut commands, attach.clone(), &mut attached_query);
}
@@ 1516,12 1553,12 @@ fn despawn_module_tree(
fn on_position_change(
mut commands: Commands,
- part_query: Query<(Entity, &PartType, &Transform), Changed<Transform>>,
+ part_query: Query<(Entity, &PartType, &Transform, &PartFlags), Changed<Transform>>,
planet_query: Query<(Entity, &PlanetType, &Transform), Changed<Transform>>,
mut packet_send: EventWriter<ServerEvent>,
) {
let mut updated_parts = Vec::new();
- for (entity, part_type, transform) in part_query.iter() {
+ for (entity, part_type, transform, flags) in part_query.iter() {
let id = commands.entity(entity).id().index();
updated_parts.push((
id,
@@ 1531,6 1568,7 @@ fn on_position_change(
transform.translation * SCALE
)
.with_rotation(transform.rotation)),
+ flags: proto_part_flags!(flags)
},
));
}