~starkingdoms/starkingdoms

f185e5b890e0f16a8fa967f63ecf8985fc988752 — ghostlyzsh 1 year, 11 months ago 543fdff
detaching modules added
2 files changed, 94 insertions(+), 10 deletions(-)

M server/src/component.rs
M server/src/main.rs
M server/src/component.rs => server/src/component.rs +1 -1
@@ 31,7 31,7 @@ pub enum PartType {

#[derive(Component, Clone, Debug)]
pub struct Attach {
    pub associated_player: Option<Entity>,
    pub parent: Option<Entity>,
    pub children: [Option<Entity>; 4],
}


M server/src/main.rs => server/src/main.rs +93 -9
@@ 171,7 171,7 @@ fn on_message(
                                selected: None,
                            },
                            attach: Attach {
                                associated_player: None,
                                parent: None,
                                children: [None, None, None, None],
                            },
                        })


@@ 358,7 358,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;


@@ 380,7 380,7 @@ fn on_message(
                                        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: [None, None, None, None],
                                        });
                                        attach.children[2] = Some(module.0);


@@ 393,7 393,7 @@ fn on_message(
                                        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: [None, None, None, None],
                                        });
                                        attach.children[0] = Some(module.0);


@@ 408,7 408,7 @@ fn on_message(
                                        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: [None, None, None, None],
                                        });
                                        attach.children[1] = Some(module.0);


@@ 423,12 423,40 @@ fn on_message(
                                        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: [None, None, None, None],
                                        });
                                        attach.children[3] = Some(module.0);
                                        break;
                                    }
                                } else if attached_query.contains(select) {
                                    let module = attached_query.get(select).unwrap();
                                    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);
                                    if attached_query.contains(parent) {
                                        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 {
                                                if *child == entity {
                                                    parent_attach.children[i] = None;
                                                }
                                            }
                                        }
                                    } else {
                                        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.);
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }
                                if attach_on_module_tree(x, y, &mut commands, attach.clone(), select, &mut attached_query, &mut part_query) {
                                    break;


@@ 437,6 465,31 @@ fn on_message(
                                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 {
                                let pos = transform.translation;
                                let rel_x = pos.x - x / SCALE;
                                let rel_y = pos.y - y / SCALE;
                                let angle = -transform.rotation.z;
                                let x = rel_x * angle.cos() - rel_y * angle.sin();
                                let y = rel_x * angle.sin() + rel_y * angle.cos();
                                let mut bound =
                                    [-25. / SCALE, 25. / SCALE, -25. / SCALE, 25. / SCALE]; // left, right, top, bottom
                                if let PartType::Cargo = part_type {
                                    bound = [
                                        -18.75 / SCALE,
                                        18.75 / SCALE,
                                        -25. / SCALE,
                                        21.875 / SCALE,
                                    ];
                                }

                                if bound[0] < x && x < bound[1] {
                                    if bound[2] < y && y < bound[3] {
                                        q_player.selected = Some(entity);
                                        break;
                                    }
                                }
                            }
                            for (entity, part_type, transform, _) in &part_query {
                                let pos = transform.translation;
                                let rel_x = pos.x - x / SCALE;


@@ 474,6 527,37 @@ 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>)>,
) {
    for child in attach.children {
        if let Some(child) = child {
            {
                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 parent = attach.parent.unwrap();
            if attached_query.contains(parent) {
                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 {
                        if *child == entity {
                            parent_attach.children[i] = None;
                        }
                    }
                }
            }
        }
    }
}

fn attach_on_module_tree(
    x: f32,
    y: f32,


@@ 507,7 591,7 @@ fn attach_on_module_tree(
                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: [None, None, None, None],
                });
                attach.children[2] = Some(module.0);


@@ 522,7 606,7 @@ fn attach_on_module_tree(
                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: [None, None, None, None],
                });
                attach.children[1] = Some(module.0);


@@ 537,7 621,7 @@ fn attach_on_module_tree(
                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: [None, None, None, None],
                });
                attach.children[3] = Some(module.0);