~starkingdoms/starkingdoms

f9c5c52fa8e3f78da3ebbeb971b4ff1c8ed7bc99 — ghostlyzsh 1 year, 11 months ago a80c003
landing thrusters physically in the game
M server/src/component.rs => server/src/component.rs +5 -0
@@ 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,

M server/src/main.rs => server/src/main.rs +87 -14
@@ 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,
                );
            }
        }
    }
}

M starkingdoms-client/src/hub.ts => starkingdoms-client/src/hub.ts +0 -12
@@ 201,18 201,6 @@ export async function hub_connect(
          let id = p.parts[i][0];
          let new_part = p.parts[i][1];

          if (global.parts_map.has(id)) {
            let old_part = global.parts_map.get(id)!;
            let dx = new_part.transform.x - old_part.transform.x;
            let dy = new_part.transform.y - old_part.transform.y;
            let drot = new_part.transform.rot - old_part.transform.rot;
            const CUTOFF_XY = 0.01;
            const CUTOFF_ROT = 0.01;
            if (dx < CUTOFF_XY && dy < CUTOFF_XY && drot < CUTOFF_ROT) {
              continue; // this packet is under the cutoff, we don't care about it
            }
          }

          global.parts_map.set(id, new_part);
          if (id === global.me?.part_id) {
            document.getElementById("pos-val-x")!.innerText = Math.round(

M starkingdoms-client/src/protocol.ts => starkingdoms-client/src/protocol.ts +2 -0
@@ 12,6 12,8 @@ export enum PartType {
  Hearty = "Hearty",
  Cargo = "Cargo",
  Hub = "Hub",
  LandingThruster = "LandingThruster",
  LandingThrusterSuspension = "LandingThrusterSuspension",
}
export enum ButtonType {
  Left = "Left",

M starkingdoms-client/src/textures.ts => starkingdoms-client/src/textures.ts +6 -0
@@ 5,6 5,8 @@ import tex_mars from "./assets/mars.svg";
import tex_hearty from "./assets/hearty.svg";
import tex_cargo_off from "./assets/cargo_off.svg";
import tex_hub_off from "./assets/hub_off.svg";
import tex_landing_thruster from "./assets/landingthruster_off.svg";
import tex_landing_thruster_suspension from "./assets/landingleg.svg";
import tex_missing from "./assets/missing.svg";

export function planet_texture_url(type: PlanetType): string {


@@ 25,6 27,10 @@ export function part_texture_url(type: PartType): string {
    return tex_cargo_off;
  } else if (type == PartType.Hub) {
    return tex_hub_off;
  } else if (type == PartType.LandingThruster) {
    return tex_landing_thruster;
  } else if (type == PartType.LandingThrusterSuspension) {
    return tex_landing_thruster_suspension;
  }
  return tex_missing;
}