@@ 30,6 30,8 @@ pub enum PartType {
Hearty,
Cargo,
Hub,
+ LandingThruster,
+ LandingThrusterSuspension,
}
#[derive(Component, Clone, Debug)]
@@ 39,6 41,9 @@ pub struct Attach {
pub children: [Option<Entity>; 4],
}
+#[derive(Component, Clone, Copy, PartialEq, Debug)]
+pub struct CanAttach(pub u8); // each bit means a slot able to attach to
+
#[derive(Component, Clone, Copy, Serialize, Deserialize, Debug, Default)]
pub struct Input {
pub up: bool,
@@ 833,11 833,12 @@ fn attach_on_module_tree(
}
fn convert_modules(
+ mut commands: Commands,
rapier_context: Res<RapierContext>,
planet_query: Query<(Entity, &PlanetType, &Children)>,
player_query: Query<&Attach, With<Player>>,
- mut attached_query: Query<(&mut PartType, &Attach, &Children), Without<Player>>,
- mut collider_query: Query<(&mut Collider, &mut Transform, &Parent)>,
+ mut attached_query: Query<(Entity, &mut PartType, &mut Attach, &Children, &Transform), Without<Player>>,
+ mut collider_query: Query<(&mut Collider, &mut Transform, &Parent), (Without<Player>, Without<Attach>)>,
mut packet_send: EventWriter<ServerEvent>,
) {
for (_planet_entity, planet_type, children) in &planet_query {
@@ 856,7 857,7 @@ fn convert_modules(
attached_query
.get(other)
.unwrap()
- .1
+ .2
.associated_player
.unwrap()
} else if collider_query.contains(other) {
@@ 865,7 866,7 @@ fn convert_modules(
attached_query
.get(parent)
.unwrap()
- .1
+ .2
.associated_player
.unwrap()
} else {
@@ 876,6 877,7 @@ fn convert_modules(
};
let player_attach = player_query.get(player_entity).unwrap();
convert_modules_recursive(
+ &mut commands,
*planet_type,
player_attach.clone(),
&mut attached_query,
@@ 888,15 890,16 @@ fn convert_modules(
}
fn convert_modules_recursive(
+ commands: &mut Commands,
planet_type: PlanetType,
attach: Attach,
- attached_query: &mut Query<(&mut PartType, &Attach, &Children), Without<Player>>,
- collider_query: &mut Query<(&mut Collider, &mut Transform, &Parent)>,
+ attached_query: &mut Query<(Entity, &mut PartType, &mut Attach, &Children, &Transform), Without<Player>>,
+ collider_query: &mut Query<(&mut Collider, &mut Transform, &Parent), (Without<Player>, Without<Attach>)>,
packet_send: &mut EventWriter<ServerEvent>,
) {
for child in attach.children {
if let Some(child) = child {
- let (mut part_type, attach, children) = attached_query.get_mut(child).unwrap();
+ let (module_entity, mut part_type, mut attach, children, module_transform) = attached_query.get_mut(child).unwrap();
if *part_type == PartType::Cargo {
match planet_type {
PlanetType::Mars => {
@@ 906,6 909,8 @@ fn convert_modules_recursive(
*collider =
Collider::cuboid(PART_HALF_SIZE / SCALE, PART_HALF_SIZE / SCALE);
*transform = Transform::from_xyz(0., 0., 0.);
+ commands.get_entity(module_entity).unwrap().remove::<CanAttach>();
+ commands.get_entity(module_entity).unwrap().insert(CanAttach(15));
let packet = Packet::DespawnPart { id: child.index() };
let buf = serde_json::to_vec(&packet).unwrap();
packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf.clone()));
@@ 921,16 926,84 @@ fn convert_modules_recursive(
packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf));
}
+ PlanetType::Moon => {
+ *part_type = PartType::LandingThruster;
+ let (mut collider, mut transform, _) =
+ collider_query.get_mut(*children.first().unwrap()).unwrap();
+ *collider =
+ Collider::cuboid(PART_HALF_SIZE / SCALE, 18.75 / SCALE);
+ *transform = Transform::from_xyz(0., 6.25 / SCALE, 0.);
+ //commands.get_entity(module_entity).unwrap().remove::<CanAttach>();
+ let joint = PrismaticJointBuilder::new(Vec2::new(0., 1.))
+ .local_anchor1(Vec2::new(0., 0.))
+ .local_anchor2(Vec2::new(0., 0.))
+ .motor_position(0., 32., 6.)
+ .build();
+ let mut suspension = commands.spawn(PartBundle {
+ transform: TransformBundle::from(*module_transform),
+ part_type: PartType::LandingThrusterSuspension,
+ });
+ suspension.insert(RigidBody::Dynamic)
+ .with_children(|children| {
+ children
+ .spawn(Collider::cuboid(PART_HALF_SIZE / SCALE, 1. / SCALE))
+ .insert(TransformBundle::from(Transform::from_xyz(
+ 0.,
+ -24. / SCALE,
+ 0.,
+ )));
+ })
+ .insert(ImpulseJoint::new(module_entity, joint))
+ .insert(ExternalForce::default())
+ .insert(ExternalImpulse::default())
+ .insert(Velocity::default())
+ .insert(ReadMassProperties::default())
+ .insert(Attach {
+ associated_player: attach.associated_player,
+ parent: Some(module_entity),
+ children: [None, None, None, None],
+ });
+ attach.children[2] = Some(suspension.id());
+
+
+ let packet = Packet::DespawnPart { id: child.index() };
+ let buf = serde_json::to_vec(&packet).unwrap();
+ packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf.clone()));
+
+ let packet = Packet::SpawnPart {
+ id: child.index(),
+ part: Part {
+ part_type: PartType::LandingThruster,
+ transform: proto_transform!(transform),
+ },
+ };
+ let buf = serde_json::to_vec(&packet).unwrap();
+
+ packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf));
+
+ let packet = Packet::SpawnPart {
+ id: suspension.id().index(),
+ part: Part {
+ part_type: PartType::LandingThrusterSuspension,
+ transform: proto_transform!(transform)
+ }
+ };
+ let buf = serde_json::to_vec(&packet).unwrap();
+ packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf));
+ }
_ => {}
}
}
- convert_modules_recursive(
- planet_type,
- attach.clone(),
- attached_query,
- collider_query,
- packet_send,
- );
+ if *part_type != PartType::LandingThruster {
+ convert_modules_recursive(
+ commands,
+ planet_type,
+ attach.clone(),
+ attached_query,
+ collider_query,
+ packet_send,
+ );
+ }
}
}
}