~starkingdoms/starkingdoms

0f5e9b77ea7f7245f41e52f5f88c68e9ab63fadc — ghostlyzsh 1 year, 11 months ago 3d862de
module refactor attachment portion done
2 files changed, 142 insertions(+), 395 deletions(-)

M server/src/main.rs
M starkingdoms-client/src/rendering.ts
M server/src/main.rs => server/src/main.rs +139 -388
@@ 520,7 520,7 @@ fn on_message(
                } => {
                    let x = x / CLIENT_SCALE;
                    let y = y / CLIENT_SCALE;
                    for (entity, mut q_player, transform, velocity, mut attach) in &mut player_query
                    for (entity, mut q_player, _transform, _velocity, mut attach) in &mut player_query
                    {
                        if q_player.addr == *from {
                            if released {


@@ 530,253 530,7 @@ fn on_message(
                                    break;
                                };
                                q_player.selected = None;
                                if part_query.contains(select) {
                                    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, p_pos.y - y);
                                    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].is_none()
                                        && 0.3 < rel_y
                                        && rel_y < 0.6
                                        && -0.4 < rel_x
                                        && rel_x < 0.4
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x + 1.06 * angle.sin(),
                                            p_pos.y - 1.06 * angle.cos(),
                                            0.,
                                        );
                                        module.2.rotation =
                                            Quat::from_euler(EulerRot::ZYX, angle, 0., 0.);
                                        module.3.linvel = velocity.linvel;
                                        module.5.attached = true;
                                        let joint =
                                            FixedJointBuilder::new().local_anchor1(vec2(0., -1.06));
                                        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: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });
                                            }
                                            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: Some(entity),
                                            parent: Some(entity),
                                            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 + 1.06 * angle.sin(),
                                                p_pos.y - 1.06 * angle.cos(),
                                                0.,
                                            );
                                            transform.rotation =
                                                Quat::from_euler(EulerRot::ZYX, angle, 0., 0.);
                                        }
                                        break;
                                    } else if attach.children[0].is_none()
                                        && -0.6 < rel_y
                                        && rel_y < -0.3
                                        && -0.4 < rel_x
                                        && rel_x < 0.4
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x - 1.06 * angle.sin(),
                                            p_pos.y + 1.06 * angle.cos(),
                                            0.,
                                        );
                                        module.2.rotation =
                                            Quat::from_euler(EulerRot::ZYX, angle + PI, 0., 0.);
                                        module.3.linvel = velocity.linvel;
                                        module.5.attached = true;
                                        let joint =
                                            FixedJointBuilder::new().local_anchor1(vec2(0., 1.06));
                                        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: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });
                                            }
                                            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: Some(entity),
                                            parent: Some(entity),
                                            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 - 1.06 * angle.sin(),
                                                p_pos.y + 1.06 * angle.cos(),
                                                0.,
                                            );
                                            transform.rotation =
                                                Quat::from_euler(EulerRot::ZYX, angle + PI, 0., 0.);
                                        }
                                        break;
                                    } else if attach.children[1].is_none()
                                        && -0.6 < rel_x
                                        && rel_x < -0.3
                                        && -0.4 < rel_y
                                        && rel_y < 0.4
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x + 1.06 * angle.cos(),
                                            p_pos.y + 1.06 * angle.sin(),
                                            0.,
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle + (PI / 2.),
                                            0.,
                                            0.,
                                        );
                                        module.3.linvel = velocity.linvel;
                                        module.5.attached = true;
                                        let joint = FixedJointBuilder::new()
                                            .local_anchor1(vec2(1.06, 0.))
                                            .local_basis2(std::f32::consts::PI / 2.);
                                        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: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });
                                            }
                                            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: Some(entity),
                                            parent: Some(entity),
                                            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 + 1.06 * angle.cos(),
                                                p_pos.y + 1.06 * angle.sin(),
                                                0.,
                                            );
                                            transform.rotation = Quat::from_euler(
                                                EulerRot::ZYX,
                                                angle + (PI / 2.),
                                                0.,
                                                0.,
                                            );
                                        }
                                        break;
                                    } else if attach.children[3].is_none()
                                        && 0.3 < rel_x
                                        && rel_x < 0.6
                                        && -0.4 < rel_y
                                        && rel_y < 0.4
                                    {
                                        module.2.translation = vec3(
                                            p_pos.x - 1.06 * angle.cos(),
                                            p_pos.y - 1.06 * angle.sin(),
                                            0.,
                                        );
                                        module.2.rotation = Quat::from_euler(
                                            EulerRot::ZYX,
                                            angle - (PI / 2.),
                                            0.,
                                            0.,
                                        );
                                        module.3.linvel = velocity.linvel;
                                        module.5.attached = true;
                                        let joint = FixedJointBuilder::new()
                                            .local_anchor1(vec2(-1.06, 0.))
                                            .local_basis2(-std::f32::consts::PI / 2.);
                                        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: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });
                                            }
                                            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: Some(entity),
                                            parent: Some(entity),
                                            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 - 1.06 * angle.cos(),
                                                p_pos.y - 1.06 * angle.sin(),
                                                0.,
                                            );
                                            transform.rotation = Quat::from_euler(
                                                EulerRot::ZYX,
                                                angle - (PI / 2.),
                                                0.,
                                                0.,
                                            );
                                        }
                                        break;
                                    }
                                } else if attached_query.contains(select) {
                                if attached_query.contains(select) {
                                    let mut module = attached_query.get_mut(select).unwrap();
                                    let parent = module.3.parent.unwrap();
                                    commands.entity(select).remove::<ImpulseJoint>();


@@ 840,10 594,11 @@ fn on_message(
                                    x,
                                    y,
                                    &mut commands,
                                    attach.clone(),
                                    entity,
                                    select,
                                    &mut attached_query,
                                    &mut part_query,
                                    &mut player_query,
                                ) {
                                    let mut part = part_query.get_mut(select).unwrap();
                                    part.5.attached = true; // all of this code is cursed. what the hell is it actually doing


@@ 1249,7 1004,7 @@ fn attach_on_module_tree(
    x: f32,
    y: f32,
    commands: &mut Commands,
    attach: Attach,
    this: Entity,
    select: Entity,
    attached_query: &mut Query<
        (


@@ 1275,159 1030,155 @@ fn attach_on_module_tree(
        ),
        (Without<PlanetType>, Without<Player>, Without<Attach>),
    >,
    player_query: &mut Query<
        (Entity, &mut Player, &Transform, &Velocity, &mut Attach),
        Without<PlanetType>,
    >,
) -> bool {
    let mut ret = false;
    for child in attach.children.iter().flatten() {
        let (entity, _part_type, transform, mut attach, velocity, can_attach, loose_attach, _) =
            attached_query.get_mut(*child).unwrap();

        let p_pos = transform.translation;
        let (rel_x, rel_y) = (p_pos.x - x, p_pos.y - y);
        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();
        let attachable = can_attach.is_some();
        if attach.children[2].is_none()
            && attachable
            && 0.3 < rel_y
            && rel_y < 0.6
            && -0.4 < rel_x
            && rel_x < 0.4
        {
            module.2.translation = vec3(
                p_pos.x + 1.06 * angle.sin(),
                p_pos.y - 1.06 * 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., -1.06));
            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);
            module_entity.insert(ImpulseJoint::new(entity, joint));
            module_entity.insert(Attach {
                associated_player: attach.associated_player,
                parent: Some(entity),
                children,
            });
            attach.children[2] = Some(module.0);
            module.5.attached = true;
            return true;
        } else if attach.children[1].is_none()
            && attachable
            && -0.6 < rel_x
            && rel_x < -0.3
            && -0.4 < rel_y
            && rel_y < 0.4
        {
            module.2.translation = vec3(
                p_pos.x + 1.06 * angle.cos(),
                p_pos.y + 1.06 * 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(1.06, 0.))
                .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>();
                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);
            module_entity.insert(ImpulseJoint::new(entity, joint));
            module_entity.insert(Attach {
                associated_player: attach.associated_player,
                parent: Some(entity),
                children,
            });
            attach.children[1] = Some(module.0);
            module.5.attached = true;
            return true;
        } else if attach.children[3].is_none()
            && attachable
            && 0.3 < rel_x
            && rel_x < 0.6
            && -0.4 < rel_y
            && rel_y < 0.4
        {
            module.2.translation = vec3(
                p_pos.x - 1.06 * angle.cos(),
                p_pos.y - 1.06 * 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(-1.06, 0.))
                .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>();
                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);
            module_entity.insert(ImpulseJoint::new(entity, joint));
            module_entity.insert(Attach {
                associated_player: attach.associated_player,
                parent: Some(entity),
                children,
            });
            attach.children[3] = Some(module.0);
            module.5.attached = true;
            return true;
        }
        ret |= if *module.1 != PartType::LandingThruster {
    let attach = if attached_query.contains(this) {
        attached_query.get(this).unwrap().3
    } else {
        player_query.get(this).unwrap().4
    };
    for this in attach.clone().children.iter().flatten() {
        let module = part_query.get(select).unwrap();
        let part_type = module.1.clone();
        ret |= if part_type != PartType::LandingThruster {
            attach_on_module_tree(
                x,
                y,
                commands,
                attach.clone(),
                *this,
                select,
                attached_query,
                part_query,
                player_query,
            )
        } else {
            false
        };
    }
    let is_player = player_query.contains(this);
    let (entity, transform, mut attach, velocity, can_attach) =
        if attached_query.contains(this) {
            let (entity, _, transform, attach, velocity, can_attach, _, _) = attached_query.get_mut(this).unwrap();
            (entity, *transform, attach, velocity, can_attach)
        }
        else {
            let (entity, _, transform, velocity, attach) = player_query.get_mut(this).unwrap();
            (entity, *transform, attach, velocity, Some(&CanAttach(15)))
        };

    let p_pos = transform.translation;
    let (rel_x, rel_y) = (p_pos.x - x, p_pos.y - y);
    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();

    let attachable = match can_attach {
        Some(s) => s.0,
        None => return false
    };
    let mut attachment_slot = 5;
    let mut offset = Vec2::ZERO;
    let mut angle_offset = 0.;

    if attach.children[2].is_none()
        && attachable & 4 != 0
        && 0.3 < rel_y
        && rel_y < 0.6
        && -0.4 < rel_x
        && rel_x < 0.4
    {
        attachment_slot = 2;
        offset = Vec2::new(0., -1.06);
        angle_offset = 0.;
    } else if attach.children[0].is_none()
        && -0.6 < rel_y
        && rel_y < -0.3
        && -0.4 < rel_x
        && rel_x < 0.4
    {
        attachment_slot = 0;
        offset = Vec2::new(0., 1.06);
        angle_offset = PI;
    } else if attach.children[1].is_none()
        && -0.6 < rel_x
        && rel_x < -0.3
        && -0.4 < rel_y
        && rel_y < 0.4
    {
        attachment_slot = 1;
        offset = Vec2::new(1.06, 0.);
        angle_offset = PI / 2.;
    } else if attach.children[3].is_none()
        && 0.3 < rel_x
        && rel_x < 0.6
        && -0.4 < rel_y
        && rel_y < 0.4
    {
        attachment_slot = 3;
        offset = Vec2::new(-1.06, 0.);
        angle_offset = -PI / 2.;
    }
    let new_transform = Transform::from_xyz(
        p_pos.x + offset.x * angle.cos() - offset.y * angle.sin(),
        p_pos.y + offset.x * angle.sin() + offset.y * angle.cos(),
        0.
    )
    .with_rotation(Quat::from_euler(
        EulerRot::ZYX,
        angle+angle_offset,
        0.,
        0.,
    ));
    if attachment_slot != 5 {
        let associated_player = if is_player {
            Some(entity)
        } else {
            attach.associated_player
        };
        *module.2 = new_transform;
        module.3.linvel = velocity.linvel;
        let joint = FixedJointBuilder::new().local_anchor1(offset).local_basis2(angle_offset);
        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,
                        parent: Some(entity),
                        children: [None, None, None, None],
                    });
            }
            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,
            parent: Some(entity),
            children,
        });
        attach.children[attachment_slot] = Some(module.0);
        module.5.attached = true;
        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 = new_transform;
        }
    }

    ret
}


M starkingdoms-client/src/rendering.ts => starkingdoms-client/src/rendering.ts +3 -7
@@ 1,9 1,6 @@
import * as PIXI from "pixi.js";
import { global, player } from "./globals.ts";
import {
  part_texture_url,
  planet_texture_url,
} from "./textures.ts";
import { part_texture_url, planet_texture_url } from "./textures.ts";
import tex_starfield from "./assets/starfield_dim.svg";
import tex_starfield_transp from "./assets/starfield_transp.svg";



@@ 65,7 62,6 @@ export function startRender() {
  global.rendering!.starfield_near.sprite.height = window.innerHeight + 768;
  global.rendering!.starfield_near.sprite.width = window.innerWidth + 768;


  app.ticker.add(() => {
    global.rendering!.app.stage.x =
      -player()?.transform.x! + window.innerWidth / 2;


@@ 76,9 72,9 @@ export function startRender() {

    // Main starfield
    global.rendering!.starfield.off_x =
      (global.parts_map.get(global.me?.part_id).transform.x * 3 / 4) % 384;
      ((global.parts_map.get(global.me?.part_id).transform.x * 3) / 4) % 384;
    global.rendering!.starfield.off_y =
      (global.parts_map.get(global.me?.part_id).transform.y * 3 / 4) % 384;
      ((global.parts_map.get(global.me?.part_id).transform.y * 3) / 4) % 384;
    global.rendering!.starfield.sprite.x =
      Math.floor(global.parts_map.get(global.me?.part_id).transform.x / 384) *
        384 -