~starkingdoms/starkingdoms

5acaa78ad8f7c7f00ea23df78c5ab09c96796cbd — ghostlyzsh 1 year, 11 months ago 4d45eca
landing thruster manipulation should finally work
1 files changed, 200 insertions(+), 53 deletions(-)

M server/src/main.rs
M server/src/main.rs => server/src/main.rs +200 -53
@@ 23,8 23,8 @@ use bevy::math::{vec2, vec3};
use bevy::{ecs::event::ManualEventReader, prelude::*};
use bevy_rapier2d::prelude::*;
use bevy_twite::{twite::frame::MessageType, ServerEvent, TwiteServerConfig, TwiteServerPlugin};
use component::*;
use component::Input;
use component::*;
use packet::*;
use rand::Rng;



@@ 76,8 76,7 @@ fn main() {
        .add_systems(Update, on_message)
        .add_systems(Update, on_close)
        .add_systems(FixedUpdate, on_position_change)
        .add_systems(FixedUpdate, gravity_update)
        .add_systems(FixedUpdate, player_input_update)
        .add_systems(FixedUpdate, (gravity_update, player_input_update).chain())
        .add_systems(FixedUpdate, convert_modules)
        //.insert_resource(Time::<Fixed>::from_seconds(1.0/20.0))
        .run();


@@ 478,6 477,13 @@ fn on_message(
                                        let mut children = [None, None, None, None];
                                        if let Some(loose_attach) = module.4 {
                                            commands.entity(entity).remove::<LooseAttach>();
                                            if *module.1 == PartType::LandingThruster {
                                                commands.entity(loose_attach.children[2].unwrap()).insert(Attach {
                                                    associated_player: attach.associated_player,
                                                    parent: Some(entity),
                                                    children: [None, None, None, None]
                                                });
                                            }
                                            children = loose_attach.children;
                                        }
                                        let mut module_entity = commands.entity(module.0);


@@ 488,6 494,17 @@ fn on_message(
                                            children,
                                        });
                                        attach.children[2] = Some(module.0);
                                        if *module.1 == PartType::LandingThruster {
                                            let loose_attach = module.4.unwrap().clone();
                                            let mut transform = part_query.get_mut(loose_attach.children[2].unwrap()).unwrap().2;
                                            transform.translation = vec3(
                                                p_pos.x + 53. / SCALE * angle.sin(),
                                                p_pos.y - 53. / SCALE * angle.cos(),
                                                0.,
                                            );
                                            transform.rotation =
                                                Quat::from_euler(EulerRot::ZYX, angle, 0., 0.);
                                        }
                                        break;
                                    } else if attach.children[0] == None
                                        && -30. / SCALE < rel_y


@@ 502,7 519,7 @@ fn on_message(
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle + std::f32::consts::PI,
                                            angle + PI,
                                            0.,
                                            0.,
                                        );


@@ 512,6 529,13 @@ fn on_message(
                                        let mut children = [None, None, None, None];
                                        if let Some(loose_attach) = module.4 {
                                            commands.entity(entity).remove::<LooseAttach>();
                                            if *module.1 == PartType::LandingThruster {
                                                commands.entity(loose_attach.children[2].unwrap()).insert(Attach {
                                                    associated_player: attach.associated_player,
                                                    parent: Some(entity),
                                                    children: [None, None, None, None]
                                                });
                                            }
                                            children = loose_attach.children;
                                        }
                                        let mut module_entity = commands.entity(module.0);


@@ 522,6 546,17 @@ fn on_message(
                                            children,
                                        });
                                        attach.children[0] = Some(module.0);
                                        if *module.1 == PartType::LandingThruster {
                                            let loose_attach = module.4.unwrap().clone();
                                            let mut transform = part_query.get_mut(loose_attach.children[2].unwrap()).unwrap().2;
                                            transform.translation = vec3(
                                                p_pos.x - 53. / SCALE * angle.sin(),
                                                p_pos.y + 53. / SCALE * angle.cos(),
                                                0.,
                                            );
                                            transform.rotation =
                                                Quat::from_euler(EulerRot::ZYX, angle + PI, 0., 0.);
                                        }
                                        break;
                                    } else if attach.children[1] == None
                                        && -30. / SCALE < rel_x


@@ 536,7 571,7 @@ fn on_message(
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle + (std::f32::consts::PI / 2.),
                                            angle + (PI / 2.),
                                            0.,
                                            0.,
                                        );


@@ 547,6 582,13 @@ fn on_message(
                                        let mut children = [None, None, None, None];
                                        if let Some(loose_attach) = module.4 {
                                            commands.entity(entity).remove::<LooseAttach>();
                                            if *module.1 == PartType::LandingThruster {
                                                commands.entity(loose_attach.children[2].unwrap()).insert(Attach {
                                                    associated_player: attach.associated_player,
                                                    parent: Some(entity),
                                                    children: [None, None, None, None]
                                                });
                                            }
                                            children = loose_attach.children;
                                        }
                                        let mut module_entity = commands.entity(module.0);


@@ 557,6 599,17 @@ fn on_message(
                                            children,
                                        });
                                        attach.children[1] = Some(module.0);
                                        if *module.1 == PartType::LandingThruster {
                                            let loose_attach = module.4.unwrap().clone();
                                            let mut transform = part_query.get_mut(loose_attach.children[2].unwrap()).unwrap().2;
                                            transform.translation = vec3(
                                                p_pos.x + 53. / SCALE * angle.cos(),
                                                p_pos.y + 53. / SCALE * angle.sin(),
                                                0.,
                                            );
                                            transform.rotation =
                                                Quat::from_euler(EulerRot::ZYX, angle + (PI / 2.), 0., 0.);
                                        }
                                        break;
                                    } else if attach.children[3] == None
                                        && 15. / SCALE < rel_x


@@ 571,7 624,7 @@ fn on_message(
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle - (std::f32::consts::PI / 2.),
                                            angle - (PI / 2.),
                                            0.,
                                            0.,
                                        );


@@ 582,6 635,13 @@ fn on_message(
                                        let mut children = [None, None, None, None];
                                        if let Some(loose_attach) = module.4 {
                                            commands.entity(entity).remove::<LooseAttach>();
                                            if *module.1 == PartType::LandingThruster {
                                                commands.entity(loose_attach.children[2].unwrap()).insert(Attach {
                                                    associated_player: attach.associated_player,
                                                    parent: Some(entity),
                                                    children: [None, None, None, None]
                                                });
                                            }
                                            children = loose_attach.children;
                                        }
                                        let mut module_entity = commands.entity(module.0);


@@ 592,6 652,17 @@ fn on_message(
                                            children,
                                        });
                                        attach.children[3] = Some(module.0);
                                        if *module.1 == PartType::LandingThruster {
                                            let loose_attach = module.4.unwrap().clone();
                                            let mut transform = part_query.get_mut(loose_attach.children[2].unwrap()).unwrap().2;
                                            transform.translation = vec3(
                                                p_pos.x - 53. / SCALE * angle.cos(),
                                                p_pos.y - 53. / SCALE * angle.sin(),
                                                0.,
                                            );
                                            transform.rotation =
                                                Quat::from_euler(EulerRot::ZYX, angle - (PI / 2.), 0., 0.);
                                        }
                                        break;
                                    }
                                } else if attached_query.contains(select) {


@@ 599,17 670,19 @@ fn on_message(
                                    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 {
                                        commands.entity(entity).insert(LooseAttach {
                                            children: attach.children,
                                        commands.entity(select).insert(LooseAttach {
                                            children: module.3.children,
                                        });
                                        commands.entity(module.3.children[2].unwrap()).remove::<Attach>();
                                    } else {
                                        detach_recursive(
                                            &mut commands,
                                            module.3.clone(),
                                            &mut attached_query,
                                        );
                                    }
                                    let children_attach = module.3.clone();
                                    detach_recursive(
                                        &mut commands,
                                        module.3.clone(),
                                        &mut attached_query,
                                    );
                                    if attached_query.contains(parent) {
                                        {
                                            let mut parent_attach =


@@ 766,13 839,15 @@ fn detach_recursive(
                    attached_query.get(child).unwrap();
                commands.entity(entity).remove::<Attach>();
                if *part_type == PartType::LandingThruster {
                    commands.entity(entity).remove::<ImpulseJoint>();
                    commands.entity(entity).insert(LooseAttach {
                        children: attach.children,
                    });
                    commands.entity(attach.children[2].unwrap()).remove::<Attach>();
                    continue;
                } else if *part_type == PartType::LandingThrusterSuspension {
                    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,
                    });


@@ 861,6 936,13 @@ fn attach_on_module_tree(
                let mut children = [None, None, None, None];
                if let Some(loose_attach) = loose_attach {
                    commands.entity(entity).remove::<LooseAttach>();
                    if *module.1 == PartType::LandingThruster {
                        commands.entity(loose_attach.children[2].unwrap()).insert(Attach {
                            associated_player: attach.associated_player,
                            parent: Some(entity),
                            children: [None, None, None, None]
                        });
                    }
                    children = loose_attach.children;
                }
                let mut module_entity = commands.entity(module.0);


@@ 893,6 975,13 @@ fn attach_on_module_tree(
                let mut children = [None, None, None, None];
                if let Some(loose_attach) = loose_attach {
                    commands.entity(entity).remove::<LooseAttach>();
                    if *module.1 == PartType::LandingThruster {
                        commands.entity(loose_attach.children[2].unwrap()).insert(Attach {
                            associated_player: attach.associated_player,
                            parent: Some(entity),
                            children: [None, None, None, None]
                        });
                    }
                    children = loose_attach.children;
                }
                let mut module_entity = commands.entity(module.0);


@@ 925,6 1014,13 @@ fn attach_on_module_tree(
                let mut children = [None, None, None, None];
                if let Some(loose_attach) = loose_attach {
                    commands.entity(entity).remove::<LooseAttach>();
                    if *module.1 == PartType::LandingThruster {
                        commands.entity(loose_attach.children[2].unwrap()).insert(Attach {
                            associated_player: attach.associated_player,
                            parent: Some(entity),
                            children: [None, None, None, None]
                        });
                    }
                    children = loose_attach.children;
                }
                let mut module_entity = commands.entity(module.0);


@@ 938,15 1034,17 @@ fn attach_on_module_tree(
                return true;
            }
            ret = ret
                | attach_on_module_tree(
                    x,
                    y,
                    commands,
                    attach.clone(),
                    select,
                    attached_query,
                    part_query,
                );
                | if *module.1 != PartType::LandingThruster {
                    attach_on_module_tree(
                        x,
                        y,
                        commands,
                        attach.clone(),
                        select,
                        attached_query,
                        part_query,
                    )
                } else { false };
        }
    }
    return ret;


@@ 1274,8 1372,17 @@ fn on_position_change(
}

fn player_input_update(
    mut player_and_body_query: Query<(Entity, &mut Player, &Attach, &mut ExternalForce, &Transform)>,
    mut attached_query: Query<(&Attach, &PartType, &mut ExternalForce, &Transform), Without<Player>>
    mut player_and_body_query: Query<(
        Entity,
        &mut Player,
        &Attach,
        &mut ExternalForce,
        &Transform,
    )>,
    mut attached_query: Query<
        (&Attach, &PartType, &mut ExternalForce, &Transform),
        Without<Player>,
    >,
) {
    for (_, player, attach, mut forces, transform) in &mut player_and_body_query {
        //forces.torque = 0.0;


@@ 1345,14 1452,22 @@ fn player_input_update(
                forces.torque += thruster_force.torque;
            }
        }
        search_thrusters(player.input, attach.clone(), *transform, &mut attached_query);
        search_thrusters(
            player.input,
            attach.clone(),
            *transform,
            &mut attached_query,
        );
    }
}
fn search_thrusters(
    input: Input,
    attach: Attach,
    p_transform: Transform,
    attached_query: &mut Query<(&Attach, &PartType, &mut ExternalForce, &Transform), Without<Player>>
    attached_query: &mut Query<
        (&Attach, &PartType, &mut ExternalForce, &Transform),
        Without<Player>,
    >,
) {
    let p_angle = p_transform.rotation.to_euler(EulerRot::ZYX).0;
    for child in attach.children {


@@ 1362,8 1477,12 @@ fn search_thrusters(
            let relative_angle = (p_angle - angle).abs();
            let relative_pos = transform.translation - p_transform.translation;
            let relative_pos = Vec2::new(
                relative_pos.x.mul_add((-p_angle).cos(), -relative_pos.y * (-p_angle).sin()),
                relative_pos.x.mul_add((-p_angle).sin(), relative_pos.y * (-p_angle).cos()),
                relative_pos
                    .x
                    .mul_add((-p_angle).cos(), -relative_pos.y * (-p_angle).sin()),
                relative_pos
                    .x
                    .mul_add((-p_angle).sin(), relative_pos.y * (-p_angle).cos()),
            );

            let mut force_mult = 0.;


@@ 1371,10 1490,12 @@ fn search_thrusters(
                force_mult = LANDING_THRUSTER_FORCE;
            }
            if input.up {
                if 3.*PI/4. < relative_angle && relative_angle < 5.*PI/4. {
                if 3. * PI / 4. < relative_angle && relative_angle < 5. * PI / 4. {
                    let thruster_force = ExternalForce::at_point(
                        Vec2::new(-force_mult / SCALE * angle.sin(),
                                   force_mult / SCALE * angle.cos()),
                        Vec2::new(
                            -force_mult / SCALE * angle.sin(),
                            force_mult / SCALE * angle.cos(),
                        ),
                        transform.translation.xy(),
                        transform.translation.xy(),
                    );


@@ 1383,10 1504,14 @@ fn search_thrusters(
                }
            }
            if input.down {
                if (0. < relative_angle && relative_angle < PI/4.)||(7.*PI/4. < relative_angle && relative_angle < 2.*PI) {
                if (0. < relative_angle && relative_angle < PI / 4.)
                    || (7. * PI / 4. < relative_angle && relative_angle < 2. * PI)
                {
                    let thruster_force = ExternalForce::at_point(
                        Vec2::new(-force_mult / SCALE * angle.sin(),
                                   force_mult / SCALE * angle.cos()),
                        Vec2::new(
                            -force_mult / SCALE * angle.sin(),
                            force_mult / SCALE * angle.cos(),
                        ),
                        transform.translation.xy(),
                        transform.translation.xy(),
                    );


@@ 1395,11 1520,13 @@ fn search_thrusters(
                }
            }
            if input.left {
                if 3.*PI/4. < relative_angle && relative_angle < 5.*PI/4. {
                if 3. * PI / 4. < relative_angle && relative_angle < 5. * PI / 4. {
                    if relative_pos.x > 24. / SCALE {
                        let thruster_force = ExternalForce::at_point(
                            Vec2::new(-force_mult / SCALE * angle.sin(),
                                       force_mult / SCALE * angle.cos()),
                            Vec2::new(
                                -force_mult / SCALE * angle.sin(),
                                force_mult / SCALE * angle.cos(),
                            ),
                            transform.translation.xy(),
                            transform.translation.xy(),
                        );


@@ 1407,11 1534,15 @@ fn search_thrusters(
                        force.torque += thruster_force.torque;
                    }
                }
                if (0. < relative_angle && relative_angle < PI/4.)||(7.*PI/4. < relative_angle && relative_angle < 2.*PI) {
                if (0. < relative_angle && relative_angle < PI / 4.)
                    || (7. * PI / 4. < relative_angle && relative_angle < 2. * PI)
                {
                    if relative_pos.x < -24. / SCALE {
                        let thruster_force = ExternalForce::at_point(
                            Vec2::new(-force_mult / SCALE * angle.sin(),
                                       force_mult / SCALE * angle.cos()),
                            Vec2::new(
                                -force_mult / SCALE * angle.sin(),
                                force_mult / SCALE * angle.cos(),
                            ),
                            transform.translation.xy(),
                            transform.translation.xy(),
                        );


@@ 1419,11 1550,13 @@ fn search_thrusters(
                        force.torque += thruster_force.torque;
                    }
                }
                if PI/4. < relative_angle && relative_angle < 3.*PI/4. {
                if PI / 4. < relative_angle && relative_angle < 3. * PI / 4. {
                    if relative_pos.y < -24. / SCALE {
                        let thruster_force = ExternalForce::at_point(
                            Vec2::new(-force_mult / SCALE * angle.sin(),
                                       force_mult / SCALE * angle.cos()),
                            Vec2::new(
                                -force_mult / SCALE * angle.sin(),
                                force_mult / SCALE * angle.cos(),
                            ),
                            transform.translation.xy(),
                            transform.translation.xy(),
                        );


@@ 1432,8 1565,10 @@ fn search_thrusters(
                    }
                    if -24. / SCALE < relative_pos.y && relative_pos.y < 24. / SCALE {
                        let thruster_force = ExternalForce::at_point(
                            Vec2::new(-force_mult / SCALE * angle.sin(),
                                       force_mult / SCALE * angle.cos()),
                            Vec2::new(
                                -force_mult / SCALE * angle.sin(),
                                force_mult / SCALE * angle.cos(),
                            ),
                            transform.translation.xy(),
                            transform.translation.xy(),
                        );


@@ 1441,11 1576,13 @@ fn search_thrusters(
                        force.torque += thruster_force.torque;
                    }
                }
                if 5.*PI/4. < relative_angle && relative_angle < 7.*PI/4. {
                if 5. * PI / 4. < relative_angle && relative_angle < 7. * PI / 4. {
                    if relative_pos.y > 24. / SCALE {
                        let thruster_force = ExternalForce::at_point(
                            Vec2::new(-force_mult / SCALE * angle.sin(),
                                       force_mult / SCALE * angle.cos()),
                            Vec2::new(
                                -force_mult / SCALE * angle.sin(),
                                force_mult / SCALE * angle.cos(),
                            ),
                            transform.translation.xy(),
                            transform.translation.xy(),
                        );


@@ 1454,8 1591,10 @@ fn search_thrusters(
                    }
                    if -24. / SCALE < relative_pos.y && relative_pos.y < 24. / SCALE {
                        let thruster_force = ExternalForce::at_point(
                            Vec2::new(-force_mult / SCALE * angle.sin(),
                                       force_mult / SCALE * angle.cos()),
                            Vec2::new(
                                -force_mult / SCALE * angle.sin(),
                                force_mult / SCALE * angle.cos(),
                            ),
                            transform.translation.xy(),
                            transform.translation.xy(),
                        );


@@ 1473,7 1612,15 @@ fn search_thrusters(
}

fn gravity_update(
    mut part_query: Query<(&Transform, &ReadMassProperties, &mut ExternalForce, &mut ExternalImpulse), With<PartType>>,
    mut part_query: Query<
        (
            &Transform,
            &ReadMassProperties,
            &mut ExternalForce,
            &mut ExternalImpulse,
        ),
        With<PartType>,
    >,
    planet_query: Query<(&Transform, &ReadMassProperties), With<PlanetType>>,
) {
    for (part_transform, part_mp, mut forces, mut impulses) in &mut part_query {