~starkingdoms/starkingdoms

db4a4e62c2b97ef76cce6b80d9e2a68b1e861dc1 — ghostlyzsh 1 year, 11 months ago d2ee73e
can now move loose landing thrusters properly
2 files changed, 95 insertions(+), 19 deletions(-)

M server/src/component.rs
M server/src/main.rs
M server/src/component.rs => server/src/component.rs +5 -0
@@ 41,6 41,11 @@ pub struct Attach {
    pub children: [Option<Entity>; 4],
}

#[derive(Component, Clone, Debug)]
pub struct LooseAttach {
    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


M server/src/main.rs => server/src/main.rs +90 -19
@@ 195,11 195,19 @@ fn on_message(
    mut commands: Commands,
    planet_query: Query<(Entity, &PlanetType, &Transform)>,
    mut part_query: Query<
        (Entity, &PartType, &mut Transform, &mut Velocity),
        (Entity, &PartType, &mut Transform, &mut Velocity, Option<&LooseAttach>),
        (Without<PlanetType>, Without<Player>, Without<Attach>),
    >,
    mut attached_query: Query<
        (Entity, &PartType, &mut Transform, &mut Attach, &Velocity, Option<&CanAttach>),
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Attach,
            &Velocity,
            Option<&CanAttach>,
            Option<&LooseAttach>,
        ),
        (Without<PlanetType>, Without<Player>),
    >,
    mut player_query: Query<


@@ 314,7 322,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, _, _) in &part_query {
                        parts.push((
                            entity.index(),
                            Part {


@@ 325,7 333,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 {


@@ 561,6 569,10 @@ fn on_message(
                                    let parent = module.3.parent.unwrap();
                                    commands.entity(select).remove::<ImpulseJoint>();
                                    commands.entity(select).remove::<Attach>();
                                    if *module.1 == PartType::LandingThruster {
                                        commands.entity(entity).insert(LooseAttach { children: attach.children });
                                    }
                                    let children_attach = module.3.clone();
                                    detach_recursive(
                                        &mut commands,
                                        module.3.clone(),


@@ 591,6 603,12 @@ fn on_message(
                                                        attached_query.get_mut(select).unwrap();
                                                    module.2.translation =
                                                        vec3(x / SCALE, y / SCALE, 0.);
                                                    if *module.1 == PartType::LandingThruster {
                                                        let sub_entity = children_attach.children[2].unwrap();
                                                        let mut suspension = attached_query.get_mut(sub_entity).unwrap();
                                                        suspension.2.translation =
                                                            vec3(x / SCALE, y / SCALE, 0.);
                                                    }
                                                }
                                            }
                                        }


@@ 609,13 627,24 @@ fn on_message(
                                    break;
                                }
                                // move module to cursor since no attach
                                part_query.get_mut(select).unwrap().2.translation =
                                let mut part = part_query.get_mut(select).unwrap();
                                part.2.translation =
                                    vec3(x / SCALE, y / SCALE, 0.);
                                if *part.1 == PartType::LandingThruster {
                                    if let Some(loose_attach) = part.4 {
                                        let sub_entity = loose_attach.children[2].unwrap();
                                        let mut part = part_query.get_mut(sub_entity).unwrap();
                                        part.2.translation = vec3(x / SCALE, y / SCALE, 0.);
                                    }
                                }
                                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 {
                                    continue;
                                }
                                let pos = transform.translation;
                                let rel_x = pos.x - x / SCALE;
                                let rel_y = pos.y - y / SCALE;


@@ 640,7 669,10 @@ 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;
                                }
                                let pos = transform.translation;
                                let rel_x = pos.x - x / SCALE;
                                let rel_y = pos.y - y / SCALE;


@@ 681,21 713,37 @@ fn detach_recursive(
    commands: &mut Commands,
    attach: Attach,
    attached_query: &mut Query<
        (Entity, &PartType, &mut Transform, &mut Attach, &Velocity, Option<&CanAttach>),
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Attach,
            &Velocity,
            Option<&CanAttach>,
            Option<&LooseAttach>,
        ),
        (Without<PlanetType>, Without<Player>),
    >,
) {
    for child in attach.children {
        if let Some(child) = child {
            {
                let (entity, _part_type, _transform, attach, _velocity, _) =
                let (entity, part_type, _transform, attach, _velocity, _, _) =
                    attached_query.get(child).unwrap();
                commands.entity(entity).remove::<ImpulseJoint>();
                commands.entity(entity).remove::<Attach>();

                detach_recursive(commands, attach.clone(), attached_query);
                if *part_type == PartType::LandingThruster {
                    commands.entity(entity).remove::<ImpulseJoint>();
                    commands.entity(entity).insert(LooseAttach { children: attach.children });
                } else if *part_type == PartType::LandingThrusterSuspension {
                    let parent = attach.parent.unwrap();
                    let parent_attach = attached_query.get(parent).unwrap().3;
                    commands.entity(parent).insert(LooseAttach { children: parent_attach.children });
                } else {
                    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, _, _) =
                attached_query.get_mut(child).unwrap();
            let parent = attach.parent.unwrap();
            if attached_query.contains(parent) {


@@ 720,18 768,26 @@ fn attach_on_module_tree(
    attach: Attach,
    select: Entity,
    attached_query: &mut Query<
        (Entity, &PartType, &mut Transform, &mut Attach, &Velocity, Option<&CanAttach>),
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Attach,
            &Velocity,
            Option<&CanAttach>,
            Option<&LooseAttach>,
        ),
        (Without<PlanetType>, Without<Player>),
    >,
    part_query: &mut Query<
        (Entity, &PartType, &mut Transform, &mut Velocity),
        (Entity, &PartType, &mut Transform, &mut Velocity, Option<&LooseAttach>),
        (Without<PlanetType>, Without<Player>, Without<Attach>),
    >,
) -> bool {
    let mut ret = false;
    for child in attach.children {
        if let Some(child) = child {
            let (entity, _part_type, transform, mut attach, velocity, can_attach) =
            let (entity, _part_type, transform, mut attach, velocity, can_attach, loose_attach) =
                attached_query.get_mut(child).unwrap();

            let p_pos = transform.translation;


@@ 758,12 814,17 @@ fn attach_on_module_tree(
                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 children = [None, None, None, None];
                if let Some(loose_attach) = loose_attach {
                    commands.entity(entity).remove::<LooseAttach>();
                    children = loose_attach.children;
                }
                let mut module_entity = commands.entity(module.0);
                module_entity.insert(ImpulseJoint::new(entity, joint));
                module_entity.insert(Attach {
                    associated_player: attach.associated_player,
                    parent: Some(entity),
                    children: [None, None, None, None],
                    children,
                });
                attach.children[2] = Some(module.0);
                return true;


@@ 785,12 846,17 @@ fn attach_on_module_tree(
                let joint = FixedJointBuilder::new()
                    .local_anchor1(vec2(53. / SCALE, 0. / SCALE))
                    .local_basis2(std::f32::consts::PI / 2.);
                let mut children = [None, None, None, None];
                if let Some(loose_attach) = loose_attach {
                    commands.entity(entity).remove::<LooseAttach>();
                    children = loose_attach.children;
                }
                let mut module_entity = commands.entity(module.0);
                module_entity.insert(ImpulseJoint::new(entity, joint));
                module_entity.insert(Attach {
                    associated_player: attach.associated_player,
                    parent: Some(entity),
                    children: [None, None, None, None],
                    children,
                });
                attach.children[1] = Some(module.0);
                return true;


@@ 812,12 878,17 @@ fn attach_on_module_tree(
                let joint = FixedJointBuilder::new()
                    .local_anchor1(vec2(-53. / SCALE, 0. / SCALE))
                    .local_basis2(-std::f32::consts::PI / 2.);
                let mut children = [None, None, None, None];
                if let Some(loose_attach) = loose_attach {
                    commands.entity(entity).remove::<LooseAttach>();
                    children = loose_attach.children;
                }
                let mut module_entity = commands.entity(module.0);
                module_entity.insert(ImpulseJoint::new(entity, joint));
                module_entity.insert(Attach {
                    associated_player: attach.associated_player,
                    parent: Some(entity),
                    children: [None, None, None, None],
                    children,
                });
                attach.children[3] = Some(module.0);
                return true;