@@ 170,9 170,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)
@@ 200,6 202,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();
@@ 219,6 222,7 @@ fn on_message(
&mut Transform,
&mut Velocity,
Option<&LooseAttach>,
+ &mut PartFlags,
),
(Without<PlanetType>, Without<Player>, Without<Attach>),
>,
@@ 231,6 235,7 @@ fn on_message(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags,
),
(Without<PlanetType>, Without<Player>),
>,
@@ 283,6 288,7 @@ fn on_message(
part: PartBundle {
part_type: PartType::Hearty,
transform: TransformBundle::from(transform),
+ flags: PartFlags { attached: false },
},
player: Player {
addr: *addr,
@@ 366,7 372,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 {
@@ 374,10 380,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 {
@@ 385,6 392,7 @@ fn on_message(
transform: proto_transform!(Transform::from_translation(
transform.translation * SCALE
)),
+ flags: proto_part_flags!(flags),
},
));
}
@@ 396,6 404,7 @@ fn on_message(
transform.translation * SCALE
)
.with_rotation(transform.rotation)),
+ flags: ProtoPartFlags { attached: false },
},
));
let packet = Packet::PartPositions { parts };
@@ 507,6 516,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];
@@ 560,6 570,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];
@@ 617,6 628,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.);
@@ 679,6 691,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.);
@@ 725,12 738,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,
});
@@ 738,6 752,7 @@ fn on_message(
.entity(module.3.children[2].unwrap())
.remove::<Attach>();
} else {
+ module.7.attached = false;
detach_recursive(
&mut commands,
module.3.clone(),
@@ 793,6 808,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
@@ 807,7 824,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 {
@@ 837,7 854,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;
}
@@ 939,6 956,7 @@ fn detach_recursive(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags,
),
(Without<PlanetType>, Without<Player>),
>,
@@ 946,8 964,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 {
@@ 956,8 974,10 @@ 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);
@@ 965,12 985,14 @@ fn detach_recursive(
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;
@@ 1002,6 1024,7 @@ fn attach_on_module_tree(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags,
),
(Without<PlanetType>, Without<Player>),
>,
@@ 1012,6 1035,7 @@ fn attach_on_module_tree(
&mut Transform,
&mut Velocity,
Option<&LooseAttach>,
+ &mut PartFlags,
),
(Without<PlanetType>, Without<Player>, Without<Attach>),
>,
@@ 1019,7 1043,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;
@@ 1068,6 1092,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
@@ 1109,6 1134,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
@@ 1150,6 1176,7 @@ fn attach_on_module_tree(
children,
});
attach.children[3] = Some(module.0);
+ module.5.attached = true;
return true;
}
ret = ret
@@ 1177,7 1204,14 @@ 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<
@@ 1239,7 1273,14 @@ 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<
@@ 1250,7 1291,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 {
@@ 1278,6 1319,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();
@@ 1300,6 1342,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)
@@ 1338,6 1381,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();
@@ 1349,6 1393,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();
@@ 1383,6 1428,7 @@ fn break_modules(
&Velocity,
Option<&CanAttach>,
Option<&LooseAttach>,
+ &mut PartFlags,
),
(Without<PlanetType>, Without<Player>),
>,
@@ 1390,13 1436,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()));
}
}
@@ 1407,9 1454,11 @@ fn break_modules(
commands.entity(entity).insert(LooseAttach {
children: attach.children,
});
- commands
- .entity(attach.children[2].unwrap())
- .remove::<Attach>();
+ if (attach.children[2].is_some()) {
+ commands
+ .entity(attach.children[2].unwrap())
+ .remove::<Attach>();
+ }
} else {
detach_recursive(&mut commands, attach.clone(), &mut attached_query);
}
@@ 1556,12 1605,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,
@@ 1571,6 1620,7 @@ fn on_position_change(
transform.translation * SCALE
)
.with_rotation(transform.rotation)),
+ flags: proto_part_flags!(flags),
},
));
}