~starkingdoms/starkingdoms

c95c0de7db4cf819f1a34f0826a3a581fcace059 — ghostlyzsh 1 year, 11 months ago c1a8491
broken code
3 files changed, 123 insertions(+), 6 deletions(-)

M server/src/component.rs
M server/src/main.rs
M server/src/packet.rs
M server/src/component.rs => server/src/component.rs +3 -1
@@ 24,14 24,16 @@ pub enum PlanetType {
    Moon,
}

#[derive(Component, Clone, Copy, Serialize, Deserialize, Debug)]
#[derive(Component, Clone, Copy, PartialEq, Serialize, Deserialize, Debug)]
pub enum PartType {
    Hearty,
    Cargo,
    Hub,
}

#[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 +113 -5
@@ 72,6 72,7 @@ fn main() {
        .add_systems(FixedUpdate, on_position_change)
        .add_systems(FixedUpdate, gravity_update)
        .add_systems(FixedUpdate, player_input_update)
        .add_systems(FixedUpdate, convert_modules)
        //.insert_resource(Time::<Fixed>::from_seconds(1.0/20.0))
        .run();



@@ 95,6 96,11 @@ fn spawn_planets(mut commands: Commands) {
        .insert(Collider::ball(EARTH_SIZE / SCALE))
        .insert(AdditionalMassProperties::Mass(EARTH_MASS))
        .insert(ReadMassProperties::default())
        .with_children(|children| {
            children.spawn(Collider::ball((EARTH_SIZE + 3.) / SCALE))
                .insert(ActiveEvents::COLLISION_EVENTS)
                .insert(Sensor);
        })
        .insert(RigidBody::Fixed);
    let moon_pos = Transform::from_xyz(3000.0 / SCALE, 0.0, 0.0);
    commands


@@ 105,6 111,11 @@ fn spawn_planets(mut commands: Commands) {
        .insert(Collider::ball(MOON_SIZE / SCALE))
        .insert(AdditionalMassProperties::Mass(MOON_MASS))
        .insert(ReadMassProperties::default())
        .with_children(|children| {
            children.spawn(Collider::ball((MOON_SIZE + 3.) / SCALE))
                .insert(ActiveEvents::COLLISION_EVENTS)
                .insert(Sensor);
        })
        .insert(RigidBody::Fixed);
}
fn module_spawn(


@@ 112,6 123,7 @@ fn module_spawn(
    time: Res<Time>,
    mut module_timer: ResMut<ModuleTimer>,
    part_query: Query<&PartType, Without<Attach>>,
    mut packet_send: EventWriter<ServerEvent>,
) {
    if module_timer.0.tick(time.delta()).just_finished() {
        let angle: f32 = {


@@ 125,12 137,12 @@ fn module_spawn(
        );
        transform.rotate_z(angle);
        if part_query.iter().count() < FREE_MODULE_CAP {
            commands
            let mut entity = commands
                .spawn(PartBundle {
                    part_type: PartType::Cargo,
                    transform: TransformBundle::from(transform),
                })
                .insert(RigidBody::Dynamic)
                });
            entity.insert(RigidBody::Dynamic)
                .with_children(|children| {
                    children
                        .spawn(Collider::cuboid(18.75 / SCALE, 23.4375 / SCALE))


@@ 144,6 156,17 @@ fn module_spawn(
                .insert(ExternalImpulse::default())
                .insert(Velocity::default())
                .insert(ReadMassProperties::default());

            let packet = Packet::SpawnPart {
                id: entity.id().index(),
                part: Part {
                    part_type: PartType::Cargo,
                    transform: proto_transform!(transform),
                }
            };
            let buf = serde_json::to_vec(&packet).unwrap();

            packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf));
        }
    }
}


@@ 197,6 220,7 @@ fn on_message(
                                selected: None,
                            },
                            attach: Attach {
                                associated_player: None,
                                parent: None,
                                children: [None, None, None, None],
                            },


@@ 415,6 439,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],
                                        });


@@ 443,6 468,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],
                                        });


@@ 472,6 498,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],
                                        });


@@ 501,6 528,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],
                                        });


@@ 706,6 734,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: attach.associated_player,
                    parent: Some(entity),
                    children: [None, None, None, None],
                });


@@ 731,6 760,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: attach.associated_player,
                    parent: Some(entity),
                    children: [None, None, None, None],
                });


@@ 756,6 786,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: attach.associated_player,
                    parent: Some(entity),
                    children: [None, None, None, None],
                });


@@ 777,6 808,76 @@ fn attach_on_module_tree(
    return ret;
}

fn convert_modules(
    rapier_context: Res<RapierContext>,
    planet_query: Query<(Entity, &PlanetType, &Children)>,
    player_query: Query<&Attach, With<Player>>,
    mut attached_query: Query<(&mut PartType, &Attach, &Children), Without<Player>>,
    mut collider_query: Query<(&mut Collider, &mut Transform)>,
    mut packet_send: EventWriter<ServerEvent>,
) {
    for (_planet_entity, planet_type, children) in &planet_query {
        for (entity1, entity2, intersecting) in rapier_context.intersections_with(*children.first().unwrap()) {
            if intersecting {
                let other = if *children.first().unwrap() == entity1 {
                    entity2
                } else {
                    entity1
                };
                let player_entity = if player_query.contains(other) {
                    other
                } else if attached_query.contains(other) {
                    attached_query.get(other).unwrap().1.associated_player.unwrap()
                } else { continue };
                let player_attach = player_query.get(player_entity).unwrap();
                convert_modules_recursive(*planet_type, player_attach.clone(), &mut attached_query, &mut collider_query, &mut packet_send);
            }
        }
    }
}

fn convert_modules_recursive(
    planet_type: PlanetType,
    attach: Attach,
    attached_query: &mut Query<(&mut PartType, &Attach, &Children), Without<Player>>,
    collider_query: &mut Query<(&mut Collider, &mut Transform)>,
    packet_send: &mut EventWriter<ServerEvent>,
) {
    for child in attach.children {
        if let Some(child) = child {
            let (mut part_type, attach, children) = attached_query.get_mut(child).unwrap();
            if *part_type == PartType::Cargo {
                match planet_type {
                    PlanetType::Moon => {
                        *part_type = PartType::Hub;
                        let (mut collider, mut transform) = collider_query.get_mut(*children.first().unwrap()).unwrap();
                        *collider = Collider::cuboid(PART_HALF_SIZE / SCALE, PART_HALF_SIZE / SCALE);
                        *transform = Transform::from_xyz(0., 0., 0.);
                        let packet = Packet::DespawnPart {
                            id: child.index(),
                        };
                        let buf = serde_json::to_vec(&packet).unwrap();
                        packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf.clone()));

                        let packet = Packet::SpawnPart {
                            id: child.index(),
                            part: Part {
                                part_type: PartType::Hub,
                                transform: proto_transform!(transform),
                            }
                        };
                        let buf = serde_json::to_vec(&packet).unwrap();

                        packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf));
                    }
                    _ => {}
                }
            }
            convert_modules_recursive(planet_type, attach.clone(), attached_query, collider_query, packet_send);
        }
    }
}

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


@@ 790,7 891,7 @@ fn on_close(
        if let ServerEvent::Close(addr) = packet {
            for (entity, player, attach) in &player_query {
                if player.addr == *addr {
                    despawn_module_tree(&mut commands, attach, &attached_query, &part_query);
                    despawn_module_tree(&mut commands, attach, &attached_query, &part_query, &mut packets);
                    commands.entity(entity).despawn_recursive();

                    let packet = Packet::PlayerLeave { id: entity.index() };


@@ 818,10 919,17 @@ fn despawn_module_tree(
    attach: &Attach,
    attached_query: &Query<&Attach, With<PartType>>,
    part_query: &Query<&PartType>,
    packets: &mut Vec<ServerEvent>,
) {
    for child in attach.children {
        if let Some(child) = child {
            commands.entity(child).despawn_recursive();
            let packet = Packet::DespawnPart {
                id: child.index(),
            };
            let buf = serde_json::to_vec(&packet).unwrap();
            packets.push(ServerEvent::Broadcast(MessageType::Text, buf.clone()));

            let attach = match attached_query.get(child) {
                Ok(s) => s,
                Err(_) => match part_query.get(child) {


@@ 831,7 939,7 @@ fn despawn_module_tree(
                    Err(e) => panic!("{}", e),
                },
            };
            despawn_module_tree(commands, attach, attached_query, part_query);
            despawn_module_tree(commands, attach, attached_query, part_query, packets);
        }
    }
}

M server/src/packet.rs => server/src/packet.rs +7 -0
@@ 97,6 97,13 @@ pub enum Packet {
    PartPositions {
        parts: Vec<(u32, Part)>,
    },
    SpawnPart {
        id: u32,
        part: Part,
    },
    DespawnPart {
        id: u32,
    },
    PlayerLeave {
        id: u32,
    },