~starkingdoms/starkingdoms

1a32083ff657ca517125f740be12361e035ab8f7 — ghostlyzsh 1 year, 11 months ago 20f0072
module on module attachment added
2 files changed, 166 insertions(+), 56 deletions(-)

M server/src/component.rs
M server/src/main.rs
M server/src/component.rs => server/src/component.rs +1 -1
@@ 43,7 43,7 @@ pub struct Input {
    pub right: bool,
}

#[derive(Component)]
#[derive(Component, Clone, Debug)]
pub struct Player {
    pub addr: SocketAddr,
    pub username: String,

M server/src/main.rs => server/src/main.rs +165 -55
@@ 128,7 128,7 @@ fn on_message(
    mut commands: Commands,
    planet_query: Query<(Entity, &PlanetType, &Transform)>,
    mut part_query: Query<(Entity, &PartType, &mut Transform, &mut Velocity), (Without<PlanetType>, Without<Player>, Without<Attach>)>,
    mut attached_query: Query<(Entity, &PartType, &mut Transform, &Attach), (Without<PlanetType>, Without<Player>)>,
    mut attached_query: Query<(Entity, &PartType, &mut Transform, &mut Attach, &Velocity), (Without<PlanetType>, Without<Player>)>,
    mut player_query: Query<(Entity, &mut Player, &Transform, &Velocity, &mut Attach), Without<PlanetType>>,
    mut packet_recv: Local<ManualEventReader<ServerEvent>>,
    mut packet_event_send: ResMut<Events<ServerEvent>>,


@@ 245,7 245,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 {


@@ 342,61 342,83 @@ fn on_message(
                                    break;
                                };
                                q_player.selected = None;
                                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 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 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 mut module_entity = commands.entity(module.0);
                                    module_entity.insert(ImpulseJoint::new(entity, joint));
                                    attach.children[2] = Some(module.0);
                                    break;
                                } else if -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 mut module_entity = commands.entity(module.0);
                                    module_entity.insert(ImpulseJoint::new(entity, joint));
                                    attach.children[0] = Some(module.0);
                                    break;
                                } else if -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.);
                                    let mut module_entity = commands.entity(module.0);
                                    module_entity.insert(ImpulseJoint::new(entity, joint));
                                    attach.children[1] = Some(module.0);
                                    break;
                                } else if 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.);
                                    let mut module_entity = commands.entity(module.0);
                                    module_entity.insert(ImpulseJoint::new(entity, joint));
                                    attach.children[1] = Some(module.0);
                                {
                                    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 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 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 mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {
                                            associated_player: Some(entity),
                                            children: [None, None, None, None],
                                        });
                                        attach.children[2] = Some(module.0);
                                        break;
                                    } else if -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 mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {
                                            associated_player: Some(entity),
                                            children: [None, None, None, None],
                                        });
                                        attach.children[0] = Some(module.0);
                                        break;
                                    } else if -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.);
                                        let mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {
                                            associated_player: Some(entity),
                                            children: [None, None, None, None],
                                        });
                                        attach.children[1] = Some(module.0);
                                        break;
                                    } else if 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.);
                                        let mut module_entity = commands.entity(module.0);
                                        module_entity.insert(ImpulseJoint::new(entity, joint));
                                        module_entity.insert(Attach {
                                            associated_player: Some(entity),
                                            children: [None, None, None, None],
                                        });
                                        attach.children[1] = Some(module.0);
                                        break;
                                    }
                                }
                                if attach_on_module_tree(x, y, &mut commands, attach.clone(), select, &mut attached_query, &mut part_query) {
                                    break;
                                }
                                module.2.translation = vec3(x / SCALE, y / SCALE, 0.);
                                // move module to cursor since no attach
                                part_query.get_mut(select).unwrap().2.translation = vec3(x / SCALE, y / SCALE, 0.);
                                break;
                            }
                            for (entity, part_type, transform, _) in &part_query {


@@ 430,6 452,94 @@ fn on_message(
    }
}

fn attach_on_module_tree(
    x: f32,
    y: f32,
    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>)>,
) -> 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 p_pos = transform.translation;
            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 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 mut module_entity = commands.entity(module.0);
                module_entity.insert(Attach {
                    associated_player: Some(entity),
                    children: [None, None, None, None],
                });
                module_entity.insert(ImpulseJoint::new(entity, joint));
                attach.children[2] = Some(module.0);
                return true;
            } else if -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 mut module_entity = commands.entity(module.0);
                module_entity.insert(Attach {
                    associated_player: Some(entity),
                    children: [None, None, None, None],
                });
                module_entity.insert(ImpulseJoint::new(entity, joint));
                attach.children[0] = Some(module.0);
                return true;
            } else if -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.);
                let mut module_entity = commands.entity(module.0);
                module_entity.insert(Attach {
                    associated_player: Some(entity),
                    children: [None, None, None, None],
                });
                module_entity.insert(ImpulseJoint::new(entity, joint));
                attach.children[1] = Some(module.0);
                return true;
            } else if 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.);
                let mut module_entity = commands.entity(module.0);
                module_entity.insert(Attach {
                    associated_player: Some(entity),
                    children: [None, None, None, None],
                });
                module_entity.insert(ImpulseJoint::new(entity, joint));
                attach.children[1] = Some(module.0);
                return true;
            }
            ret = ret | attach_on_module_tree(x, y, commands, attach.clone(), select, attached_query, part_query);
        }
    }
    return ret;
}

fn on_close(
    player_query: Query<(Entity, &Player, &Attach)>,
    attached_query: Query<&Attach, With<PartType>>,