~starkingdoms/starkingdoms

e861fb04547c0949e9621adeaf7cc5d6b885e20f — ghostlyzsh 1 year, 11 months ago b2f768d
rustfmt
1 files changed, 173 insertions(+), 51 deletions(-)

M server/src/main.rs
M server/src/main.rs => server/src/main.rs +173 -51
@@ 37,7 37,7 @@ const EARTH_SIZE: f32 = 1000.0;
const MOON_SIZE: f32 = EARTH_SIZE / 4.;

const EARTH_MASS: f32 = 10000.0;
const MOON_MASS: f32  = EARTH_MASS / 30.;
const MOON_MASS: f32 = EARTH_MASS / 30.;

const GRAVITY: f32 = 0.02;
const PART_HALF_SIZE: f32 = 25.0;


@@ 107,7 107,12 @@ fn spawn_planets(mut commands: Commands) {
        .insert(ReadMassProperties::default())
        .insert(RigidBody::Fixed);
}
fn module_spawn(mut commands: Commands, time: Res<Time>, mut module_timer: ResMut<ModuleTimer>, part_query: Query<&PartType, Without<Attach>>) {
fn module_spawn(
    mut commands: Commands,
    time: Res<Time>,
    mut module_timer: ResMut<ModuleTimer>,
    part_query: Query<&PartType, Without<Attach>>,
) {
    if module_timer.0.tick(time.delta()).just_finished() {
        let angle: f32 = {
            let mut rng = rand::thread_rng();


@@ 384,21 389,29 @@ fn on_message(
                                    let mut module = part_query.get_mut(select).unwrap();
                                    // attach module
                                    let p_pos = transform.translation;
                                    let (rel_x, rel_y) = (
                                        p_pos.x - x / SCALE,
                                        p_pos.y - y / SCALE,
                                    );
                                    let (rel_x, rel_y) = (p_pos.x - x / SCALE, p_pos.y - y / SCALE);
                                    let angle = transform.rotation.to_euler(EulerRot::ZYX).0;
                                    let (rel_x, rel_y) = (
                                        rel_x * (-angle).cos() - rel_y * (-angle).sin(),
                                        rel_x * (-angle).sin() + rel_y * (-angle).cos(),
                                    );

                                    if attach.children[2] == None && 15./SCALE < rel_y && rel_y < 30./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE {
                                        module.2.translation = vec3(p_pos.x + 53./SCALE*angle.sin(), p_pos.y - 53./SCALE*angle.cos(), 0.);
                                        module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle, 0., 0.);
                                    if attach.children[2] == None
                                        && 15. / SCALE < rel_y
                                        && rel_y < 30. / SCALE
                                        && -20. / SCALE < rel_x
                                        && rel_x < 20. / SCALE
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x + 53. / SCALE * angle.sin(),
                                            p_pos.y - 53. / SCALE * angle.cos(),
                                            0.,
                                        );
                                        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 joint = FixedJointBuilder::new()
                                            .local_anchor1(vec2(0. / SCALE, -53. / SCALE));
                                        let mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {


@@ 407,11 420,26 @@ fn on_message(
                                        });
                                        attach.children[2] = Some(module.0);
                                        break;
                                    } else if attach.children[0] == None && -30./SCALE < rel_y && rel_y < -15./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE {
                                        module.2.translation = vec3(p_pos.x - 53./SCALE*angle.sin(), p_pos.y + 53./SCALE*angle.cos(), 0.);
                                        module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + std::f32::consts::PI, 0., 0.);
                                    } else if attach.children[0] == None
                                        && -30. / SCALE < rel_y
                                        && rel_y < -15. / SCALE
                                        && -20. / SCALE < rel_x
                                        && rel_x < 20. / SCALE
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x - 53. / SCALE * angle.sin(),
                                            p_pos.y + 53. / SCALE * angle.cos(),
                                            0.,
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle + std::f32::consts::PI,
                                            0.,
                                            0.,
                                        );
                                        module.3.linvel = velocity.linvel;
                                        let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, 53. / SCALE));
                                        let joint = FixedJointBuilder::new()
                                            .local_anchor1(vec2(0. / SCALE, 53. / SCALE));
                                        let mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {


@@ 420,13 448,27 @@ fn on_message(
                                        });
                                        attach.children[0] = Some(module.0);
                                        break;
                                    } else if attach.children[1] == None && -30./SCALE < rel_x && rel_x < -15./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE {
                                        module.2.translation = vec3(p_pos.x + 53./SCALE*angle.cos(), p_pos.y + 53./SCALE*angle.sin(), 0.);
                                        module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + (std::f32::consts::PI/2.), 0., 0.);
                                    } else if attach.children[1] == None
                                        && -30. / SCALE < rel_x
                                        && rel_x < -15. / SCALE
                                        && -20. / SCALE < rel_y
                                        && rel_y < 20. / SCALE
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x + 53. / SCALE * angle.cos(),
                                            p_pos.y + 53. / SCALE * angle.sin(),
                                            0.,
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle + (std::f32::consts::PI / 2.),
                                            0.,
                                            0.,
                                        );
                                        module.3.linvel = velocity.linvel;
                                        let joint = FixedJointBuilder::new()
                                            .local_anchor1(vec2(53. / SCALE, 0. / SCALE))
                                            .local_basis2(std::f32::consts::PI/2.);
                                            .local_basis2(std::f32::consts::PI / 2.);
                                        let mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {


@@ 435,13 477,27 @@ fn on_message(
                                        });
                                        attach.children[1] = Some(module.0);
                                        break;
                                    } else if attach.children[3] == None && 15./SCALE < rel_x && rel_x < 30./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE {
                                        module.2.translation = vec3(p_pos.x - 53./SCALE*angle.cos(), p_pos.y - 53./SCALE*angle.sin(), 0.);
                                        module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle - (std::f32::consts::PI/2.), 0., 0.);
                                    } else if attach.children[3] == None
                                        && 15. / SCALE < rel_x
                                        && rel_x < 30. / SCALE
                                        && -20. / SCALE < rel_y
                                        && rel_y < 20. / SCALE
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x - 53. / SCALE * angle.cos(),
                                            p_pos.y - 53. / SCALE * angle.sin(),
                                            0.,
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle - (std::f32::consts::PI / 2.),
                                            0.,
                                            0.,
                                        );
                                        module.3.linvel = velocity.linvel;
                                        let joint = FixedJointBuilder::new()
                                            .local_anchor1(vec2(-53. / SCALE, 0. / SCALE))
                                            .local_basis2(-std::f32::consts::PI/2.);
                                            .local_basis2(-std::f32::consts::PI / 2.);
                                        let mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {


@@ 456,9 512,14 @@ fn on_message(
                                    let parent = module.3.parent.unwrap();
                                    commands.entity(select).remove::<ImpulseJoint>();
                                    commands.entity(select).remove::<Attach>();
                                    detach_recursive(&mut commands, module.3.clone(), &mut attached_query);
                                    detach_recursive(
                                        &mut commands,
                                        module.3.clone(),
                                        &mut attached_query,
                                    );
                                    if attached_query.contains(parent) {
                                        let mut parent_attach = attached_query.get_mut(parent).unwrap().3;
                                        let mut parent_attach =
                                            attached_query.get_mut(parent).unwrap().3;
                                        let children = parent_attach.children.clone();
                                        for (i, child) in children.iter().enumerate() {
                                            if let Some(child) = child {


@@ 468,26 529,40 @@ fn on_message(
                                            }
                                        }
                                    } else {
                                        for (i, child) in attach.children.clone().iter().enumerate() {
                                        for (i, child) in attach.children.clone().iter().enumerate()
                                        {
                                            if let Some(child) = child {
                                                if *child == select {
                                                    attach.children[i] = None;
                                                    let mut module = attached_query.get_mut(select).unwrap();
                                                    module.2.translation = vec3(x / SCALE, y / SCALE, 0.);
                                                    let mut module =
                                                        attached_query.get_mut(select).unwrap();
                                                    module.2.translation =
                                                        vec3(x / SCALE, y / SCALE, 0.);
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }
                                if attach_on_module_tree(x, y, &mut commands, attach.clone(), select, &mut attached_query, &mut part_query) {
                                if attach_on_module_tree(
                                    x,
                                    y,
                                    &mut commands,
                                    attach.clone(),
                                    select,
                                    &mut attached_query,
                                    &mut part_query,
                                ) {
                                    break;
                                }
                                // move module to cursor since no attach
                                part_query.get_mut(select).unwrap().2.translation = vec3(x / SCALE, y / SCALE, 0.);
                                part_query.get_mut(select).unwrap().2.translation =
                                    vec3(x / SCALE, y / SCALE, 0.);
                                break;
                            }
                            for (entity, part_type, transform, _m_attach, _velocity) in &attached_query {
                            for (entity, part_type, transform, _m_attach, _velocity) in
                                &attached_query
                            {
                                let pos = transform.translation;
                                let rel_x = pos.x - x / SCALE;
                                let rel_y = pos.y - y / SCALE;


@@ 552,18 627,23 @@ fn on_message(
fn detach_recursive(
    commands: &mut Commands,
    attach: Attach,
    attached_query: &mut Query<(Entity, &PartType, &mut Transform, &mut Attach, &Velocity), (Without<PlanetType>, Without<Player>)>,
    attached_query: &mut Query<
        (Entity, &PartType, &mut Transform, &mut Attach, &Velocity),
        (Without<PlanetType>, Without<Player>),
    >,
) {
    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) =
                    attached_query.get(child).unwrap();
                commands.entity(entity).remove::<ImpulseJoint>();
                commands.entity(entity).remove::<Attach>();

                detach_recursive(commands, attach.clone(), attached_query);
            }
            let (entity, _part_type, _transform, attach, _velocity) = attached_query.get_mut(child).unwrap();
            let (entity, _part_type, _transform, attach, _velocity) =
                attached_query.get_mut(child).unwrap();
            let parent = attach.parent.unwrap();
            if attached_query.contains(parent) {
                let mut parent_attach = attached_query.get_mut(parent).unwrap().3;


@@ 586,27 666,40 @@ fn attach_on_module_tree(
    commands: &mut Commands,
    attach: Attach,
    select: Entity,
    attached_query: &mut Query<(Entity, &PartType, &mut Transform, &mut Attach, &Velocity), (Without<PlanetType>, Without<Player>)>,
    part_query: &mut Query<(Entity, &PartType, &mut Transform, &mut Velocity), (Without<PlanetType>, Without<Player>, Without<Attach>)>,
    attached_query: &mut Query<
        (Entity, &PartType, &mut Transform, &mut Attach, &Velocity),
        (Without<PlanetType>, Without<Player>),
    >,
    part_query: &mut Query<
        (Entity, &PartType, &mut Transform, &mut Velocity),
        (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) = attached_query.get_mut(child).unwrap();
            let (entity, _part_type, transform, mut attach, velocity) =
                attached_query.get_mut(child).unwrap();

            let p_pos = transform.translation;
            let (rel_x, rel_y) = (
                p_pos.x - x / SCALE,
                p_pos.y - y / SCALE,
            );
            let (rel_x, rel_y) = (p_pos.x - x / SCALE, p_pos.y - y / SCALE);
            let angle = transform.rotation.to_euler(EulerRot::ZYX).0;
            let (rel_x, rel_y) = (
                rel_x * (-angle).cos() - rel_y * (-angle).sin(),
                rel_x * (-angle).sin() + rel_y * (-angle).cos(),
            );
            let mut module = part_query.get_mut(select).unwrap();
            if attach.children[2] == None && 15./SCALE < rel_y && rel_y < 30./SCALE && -20./SCALE < rel_x && rel_x < 20./SCALE {
                module.2.translation = vec3(p_pos.x + 53./SCALE*angle.sin(), p_pos.y - 53./SCALE*angle.cos(), 0.);
            if attach.children[2] == None
                && 15. / SCALE < rel_y
                && rel_y < 30. / SCALE
                && -20. / SCALE < rel_x
                && rel_x < 20. / SCALE
            {
                module.2.translation = vec3(
                    p_pos.x + 53. / SCALE * angle.sin(),
                    p_pos.y - 53. / SCALE * angle.cos(),
                    0.,
                );
                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));


@@ 618,13 711,23 @@ fn attach_on_module_tree(
                });
                attach.children[2] = Some(module.0);
                return true;
            } else if attach.children[1] == None && -30./SCALE < rel_x && rel_x < -15./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE {
                module.2.translation = vec3(p_pos.x + 53./SCALE*angle.cos(), p_pos.y + 53./SCALE*angle.sin(), 0.);
                module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle + (std::f32::consts::PI/2.), 0., 0.);
            } else if attach.children[1] == None
                && -30. / SCALE < rel_x
                && rel_x < -15. / SCALE
                && -20. / SCALE < rel_y
                && rel_y < 20. / SCALE
            {
                module.2.translation = vec3(
                    p_pos.x + 53. / SCALE * angle.cos(),
                    p_pos.y + 53. / SCALE * angle.sin(),
                    0.,
                );
                module.2.rotation =
                    Quat::from_euler(EulerRot::ZYX, angle + (std::f32::consts::PI / 2.), 0., 0.);
                module.3.linvel = velocity.linvel;
                let joint = FixedJointBuilder::new()
                    .local_anchor1(vec2(53. / SCALE, 0. / SCALE))
                    .local_basis2(std::f32::consts::PI/2.);
                    .local_basis2(std::f32::consts::PI / 2.);
                let mut module_entity = commands.entity(module.0);
                module_entity.insert(ImpulseJoint::new(entity, joint));
                module_entity.insert(Attach {


@@ 633,13 736,23 @@ fn attach_on_module_tree(
                });
                attach.children[1] = Some(module.0);
                return true;
            } else if attach.children[3] == None && 15./SCALE < rel_x && rel_x < 30./SCALE && -20./SCALE < rel_y && rel_y < 20./SCALE {
                module.2.translation = vec3(p_pos.x - 53./SCALE*angle.cos(), p_pos.y - 53./SCALE*angle.sin(), 0.);
                module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle - (std::f32::consts::PI/2.), 0., 0.);
            } else if attach.children[3] == None
                && 15. / SCALE < rel_x
                && rel_x < 30. / SCALE
                && -20. / SCALE < rel_y
                && rel_y < 20. / SCALE
            {
                module.2.translation = vec3(
                    p_pos.x - 53. / SCALE * angle.cos(),
                    p_pos.y - 53. / SCALE * angle.sin(),
                    0.,
                );
                module.2.rotation =
                    Quat::from_euler(EulerRot::ZYX, angle - (std::f32::consts::PI / 2.), 0., 0.);
                module.3.linvel = velocity.linvel;
                let joint = FixedJointBuilder::new()
                    .local_anchor1(vec2(-53. / SCALE, 0. / SCALE))
                    .local_basis2(-std::f32::consts::PI/2.);
                    .local_basis2(-std::f32::consts::PI / 2.);
                let mut module_entity = commands.entity(module.0);
                module_entity.insert(ImpulseJoint::new(entity, joint));
                module_entity.insert(Attach {


@@ 649,7 762,16 @@ fn attach_on_module_tree(
                attach.children[3] = Some(module.0);
                return true;
            }
            ret = ret | attach_on_module_tree(x, y, commands, attach.clone(), select, attached_query, part_query);
            ret = ret
                | attach_on_module_tree(
                    x,
                    y,
                    commands,
                    attach.clone(),
                    select,
                    attached_query,
                    part_query,
                );
        }
    }
    return ret;