~starkingdoms/starkingdoms

6fca7a488e3c678ed19e333f01494052b622acfb — ghostlyzsh 2 years ago 1e40dcd + 03ef6f7
attachment partially working, weird stuff though
3 files changed, 58 insertions(+), 20 deletions(-)

M server/src/handler.rs
M server/src/manager.rs
M server/src/timer.rs
M server/src/handler.rs => server/src/handler.rs +6 -6
@@ 203,6 203,7 @@ pub async fn handle_client(
                                    ])
                                    .rotation(angle + PI / 2.)
                                    .build();
                                debug!("rotation: {}", player_body.rotation().angle());
                                let player_collider: Collider =
                                    ColliderBuilder::cuboid(25.0 / SCALE, 25.0 / SCALE)
                                        .mass_properties(MassProperties::new(


@@ 270,6 271,10 @@ pub async fn handle_client(
                                e_write_handle
                                    .entities
                                    .insert(player_id, Entity::Player(player));

                                data_handle.rigid_body_set = rigid_body_set;
                                data_handle.collider_set = collider_set;

                                AttachedModule::attach_new(
                                    &mut data_handle,
                                    &mut e_write_handle,


@@ 277,7 282,6 @@ pub async fn handle_client(
                                    player_id,
                                    ModuleTemplate {
                                        translation: vector![0.0, 50.0],
                                        heading: 0.0,
                                        mass_properties: MassProperties::new(
                                            point![0.0, 0.0],
                                            120.0,


@@ 285,12 289,8 @@ pub async fn handle_client(
                                        ),
                                        module_type: ModuleType::Cargo,
                                    },
                                    0,
                                    angle,
                                    1,
                                );
                                data_handle.rigid_body_set = rigid_body_set;
                                data_handle.collider_set = collider_set;
                                debug!("running");
                            }
                        }
                        MessageC2S::Goodbye(pkt) => {

M server/src/manager.rs => server/src/manager.rs +49 -11
@@ 1,6 1,7 @@
use async_std::channel::Sender;
use async_std::sync::RwLock;
use nalgebra::point;
use log::debug;
use nalgebra::{point, vector};
use rapier2d_f64::na::Vector2;
use rapier2d_f64::prelude::{
    BroadPhase, CCDSolver, ColliderBuilder, ColliderSet, FixedJointBuilder, ImpulseJointHandle,


@@ 11,6 12,7 @@ use rapier2d_f64::prelude::{
use starkingdoms_protocol::api::APISavedPlayerData;
use starkingdoms_protocol::module::ModuleType;
use std::collections::HashMap;
use std::f64::consts::PI;
use std::net::SocketAddr;
use std::sync::Arc;



@@ 62,7 64,6 @@ pub struct Module {
#[derive(Clone)]
pub struct ModuleTemplate {
    pub translation: Vector2<Real>,
    pub heading: f64,
    pub mass_properties: MassProperties,
    pub module_type: ModuleType,
}


@@ 168,7 169,6 @@ impl AttachedModule {
        player_id: EntityId,
        module: ModuleTemplate,
        attachment_slot: usize,
        rotation: f64,
    ) {
        let mut entity_map = entities.entities.clone();



@@ 184,25 184,63 @@ impl AttachedModule {
                panic!("unexpected parent");
            }
        };

        let parent_body = data.rigid_body_set.get(parent_handle).expect("Parent body does not exist");
        let parent_pos = vector![parent_body.translation().x, parent_body.translation().y];

        let (anchor, rotation) = match attachment_slot {
            0 => {
                (point![
                    0. / SCALE,
                    53. / SCALE
                ], PI)
            }
            1 => {
                (point![
                    -103. / SCALE,
                    0. / SCALE
                ], PI/2.)
            }
            2 => {
                (point![
                    0. / SCALE,
                    -53. / SCALE
                ], 0.)
            }
            3 => {
                (point![
                    53. / SCALE,
                    0. / SCALE
                ], -PI/2.)
            }
            _ => {
                (point![
                    0. / SCALE,
                    53. / SCALE
                ], 0.)
            }
        };
        debug!("anchor: {}", anchor);

        let module_pos = parent_pos + vector![anchor.x * -rotation.sin(), anchor.y * rotation.cos()];

        // create attachment module
        let module_collider = ColliderBuilder::cuboid(25.0 / SCALE, 25.0 / SCALE)
            .mass_properties(module.mass_properties)
            .build();
        let module_body = RigidBodyBuilder::fixed()
            .translation(module.translation)
            .rotation(module.heading)
        let module_body = RigidBodyBuilder::dynamic()
            .translation(module_pos)
            .rotation(parent_body.rotation().angle() + rotation + 1.)
            .build();
        debug!("angle: {}", parent_body.rotation().angle());
        let attached_handle = data.rigid_body_set.insert(module_body);
        data.collider_set.insert_with_parent(
            module_collider,
            attached_handle,
            &mut data.rigid_body_set,
        );
        let anchor = point![
            -0. / SCALE * rotation.cos() + 100. / SCALE * rotation.sin(),
            -0. / SCALE * rotation.sin() - 100. / SCALE * rotation.cos()
        ];
        let attach_joint = PrismaticJointBuilder::new(Vector2::x_axis())

        let attach_joint = FixedJointBuilder::new()
            .local_anchor1(anchor)
            .local_anchor2(point![0.0, 0.0 / SCALE])
            //.local_frame2(Isometry::rotation(rotation))

M server/src/timer.rs => server/src/timer.rs +3 -3
@@ 302,21 302,21 @@ pub async fn timer_main(
                            .iter()
                            .map(|m| {
                                let module = m.to_module();
                                info!("{:?}", module);
                                //info!("{:?}", module);
                                module
                            })
                            .collect(),
                    );
                    modules.iter().for_each(|module| {
                        if attached_handles.contains(&module.handle) {
                            info!(
                            /*info!(
                                "{:?}",
                                physics_data
                                    .rigid_body_set
                                    .get(module.handle)
                                    .unwrap()
                                    .translation()
                            );
                            );*/
                        }
                    });
                    let protocol_modules: Vec<starkingdoms_protocol::module::Module> = modules