~starkingdoms/starkingdoms

2b6612f60801525694dd4292ae90d0e90928e9b7 — ghostlyzsh 2 years ago b8af3c9 + 29b0c98
i heard one time that other module types are good to be accessible
5 files changed, 204 insertions(+), 207 deletions(-)

M client/src/index.ts
M client/src/particle.ts
M server/src/entity.rs
M server/src/planet.rs
M server/src/timer.rs
M client/src/index.ts => client/src/index.ts +34 -157
@@ 517,11 517,36 @@ async function client_main(server: string, username: string, texture_quality: st
        const thruster_end_a = 0;
        const thruster_start_size = 4;
        const thruster_end_size = 50;
        const thruster_final_rotation = 90;

        function createThrusterParticle(x: number, y: number, vel_x: number, vel_y: number) {
            createParticle({
                x: global.me!.x + calculateRotated(x, y, global.me!.rotation)[0],
                y: global.me!.y + calculateRotated(x, y, global.me!.rotation)[1],
                lifetime: 500,
                timer: 0,
                startSize: thruster_start_size,
                finalSize: thruster_end_size,
                startRotation: 0,
                finalRotation: thruster_final_rotation,
                startOpacity: thruster_start_a,
                endOpacity: thruster_end_a,
                startR: thruster_r,
                startG: thruster_g,
                startB: thruster_b,
                endR: thruster_r,
                endG: thruster_g,
                endB: thruster_b,
                velocity_x: calculateRotated(vel_x, vel_y, global.me!.rotation)[0],
                velocity_y: calculateRotated(vel_x, vel_y, global.me!.rotation)[1]
            });
        }

        if (global.me !== null) {
            console.log(thruster_counter);
            thruster_counter += 1;
            if (thruster_counter > 5) {
            //thruster_counter = 1; // uncomment this line to disable particle limits
            if (thruster_counter > 3) {
                console.log("resetting counter");
                thruster_counter = 0;
            } else if (thruster_counter == 1) {


@@ 529,177 554,29 @@ async function client_main(server: string, username: string, texture_quality: st
                if (global.keys.up) {
                    // two backward thrusters
                    // this one is blue
                    createParticle({
                        x: global.me.x + calculateRotated(-25, 25, global.me.rotation)[0],
                        y: global.me.y + calculateRotated(-25, 25, global.me.rotation)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });

                    // two backward thrusters
                    // this one is pink
                    createParticle({
                        x: global.me.x + calculateRotated(25, -25, global.me.rotation + Math.PI/2)[0],
                        y: global.me.y + calculateRotated(25, -25, global.me.rotation + Math.PI/2)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });
                    createThrusterParticle(-25, 25, 0, 0.5);
                    createThrusterParticle(25, 25, 0, 0.5);
                }

                if (global.keys.down) {
                    // two backward thrusters
                    // this one is blue
                    createParticle({
                        x: global.me.x + calculateRotated(25, -25, global.me.rotation)[0],
                        y: global.me.y + calculateRotated(25, -25, global.me.rotation)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });

                    // two backward thrusters
                    // this one is pink
                    createParticle({
                        x: global.me.x + calculateRotated(-25, 25, global.me.rotation + Math.PI/2)[0],
                        y: global.me.y + calculateRotated(-25, 25, global.me.rotation + Math.PI/2)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });
                    createThrusterParticle(25, -25, 0, -0.5);
                    createThrusterParticle(-25, -25, 0, -0.5);
                }

                if (global.keys.left) {
                    // two backward thrusters
                    // this one is blue
                    createParticle({
                        x: global.me.x + calculateRotated(25, 25, global.me.rotation)[0],
                        y: global.me.y + calculateRotated(25, 25, global.me.rotation)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });

                    // two backward thrusters
                    // this one is pink
                    createParticle({
                        x: global.me.x + calculateRotated(-25, 25, global.me.rotation + Math.PI/2)[0],
                        y: global.me.y + calculateRotated(-25, 25, global.me.rotation + Math.PI/2)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });
                    createThrusterParticle(25, 25, 0, 0.5);
                    createThrusterParticle(-25, -25, 0, -0.5);
                }

                if (global.keys.right) {
                    // two backward thrusters
                    // this one is blue
                    createParticle({
                        x: global.me.x + calculateRotated(-25, 25, global.me.rotation)[0],
                        y: global.me.y + calculateRotated(-25, 25, global.me.rotation)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });

                    // two backward thrusters
                    // this one is pink
                    createParticle({
                        x: global.me.x + calculateRotated(-25, -25, global.me.rotation + Math.PI/2)[0],
                        y: global.me.y + calculateRotated(-25, -25, global.me.rotation + Math.PI/2)[1],
                        lifetime: 500,
                        timer: 0,
                        startSize: thruster_start_size,
                        finalSize: thruster_end_size,
                        startRotation: 0,
                        finalRotation: 180,
                        startOpacity: thruster_start_a,
                        endOpacity: thruster_end_a,
                        startR: thruster_r,
                        startG: thruster_g,
                        startB: thruster_b,
                        endR: thruster_r,
                        endG: thruster_g,
                        endB: thruster_b
                    });
                    createThrusterParticle(-25, 25, 0,0.5);
                    createThrusterParticle(25, -25, 0, -0.5);
                }
            }


M client/src/particle.ts => client/src/particle.ts +7 -1
@@ 16,7 16,9 @@ export interface Particle {
    startG: number,
    endG: number,
    startB: number,
    endB: number
    endB: number,
    velocity_x: number,
    velocity_y: number
}

let particles: Particle[] = [];


@@ 61,6 63,10 @@ export function tickParticles(delta: number) {
    let keptParticles = []
    for (let i = 0; i < particles.length; i++) {
        particles[i].timer += delta;

        particles[i].x += particles[i].velocity_x * delta;
        particles[i].y += particles[i].velocity_y * delta;

        if (!(particles[i].timer > particles[i].lifetime)) {
            keptParticles.push(particles[i]);
        } else {

M server/src/entity.rs => server/src/entity.rs +33 -0
@@ 1,6 1,7 @@
use std::{collections::HashMap, net::SocketAddr, sync::atomic::AtomicU32};
use nalgebra::Vector2;
use protobuf::SpecialFields;
use rapier2d_f64::prelude::RigidBodyHandle;
use starkingdoms_protocol::planet::PlanetType;

use crate::{


@@ 31,6 32,33 @@ impl EntityHandler {
            entities: Entities::new(),
        }
    }
    pub fn get_entity_from_handle(&self, handle: RigidBodyHandle) -> Option<Entity> {
        for (_, entity) in &self.entities {
            match entity.clone() {
                Entity::Player(p) => {
                    if p.handle == handle {
                        return Some(entity.clone());
                    }
                }
                Entity::Module(p) => {
                    if p.handle == handle {
                        return Some(entity.clone());
                    }
                }
                Entity::AttachedModule(p) => {
                    if p.handle == handle {
                        return Some(entity.clone());
                    }
                }
                Entity::Planet(p) => {
                    if p.body_handle == handle {
                        return Some(entity.clone());
                    }
                }
            }
        }
        None
    }
    pub fn get_planets(&self) -> Vec<Planet> {
        let mut ids = Vec::new();
        for entity in self.entities.values() {


@@ 70,6 98,11 @@ impl EntityHandler {
            None
        }
    }
    pub fn get_player_from_handle(&self, handle: RigidBodyHandle) -> Option<Player> {
        let players = self.get_players();
        let player = players.iter().find(|p| p.1.handle == handle);
        player.map(|p| p.1.clone())
    }
    pub fn get_player_id(&self, addr: SocketAddr) -> Option<EntityId> {
        for (id, entity) in &self.entities {
            if let Entity::Player(player) = entity {

M server/src/planet.rs => server/src/planet.rs +6 -2
@@ 1,7 1,7 @@
use nalgebra::{vector, Vector2};
use protobuf::SpecialFields;
use rapier2d_f64::prelude::{
    ColliderBuilder, ColliderSet, RigidBodyBuilder, RigidBodyHandle, RigidBodySet,
    ColliderBuilder, ColliderSet, RigidBodyBuilder, RigidBodyHandle, RigidBodySet, ActiveEvents,
};
use starkingdoms_protocol::planet::PlanetType;
use std::collections::HashMap;


@@ 57,13 57,17 @@ impl Planets {
        collider_set: &mut ColliderSet,
    ) -> (EntityId, Entity) {
        let collider = ColliderBuilder::ball(radius / SCALE).build();
        let sensor = ColliderBuilder::ball((radius + 1.) / SCALE)
            .sensor(true)
            .active_events(ActiveEvents::COLLISION_EVENTS)
            .build();
        let body = RigidBodyBuilder::kinematic_position_based()
            .translation(vector![position.0 / SCALE, position.1 / SCALE])
            .dominance_group(127)
            .additional_mass(0.0);
        let body_handle = rigid_body_set.insert(body);

        collider_set.insert_with_parent(collider, body_handle, rigid_body_set);
        collider_set.insert_with_parent(sensor, body_handle, rigid_body_set);

        let entity_id = get_entity_id();
        (

M server/src/timer.rs => server/src/timer.rs +124 -47
@@ 10,11 10,11 @@ use crate::{
};
use async_std::sync::RwLock;
use async_std::task::sleep;
use log::warn;
use log::{warn, debug};
use nalgebra::{point, vector};
use protobuf::SpecialFields;
use rand::Rng;
use rapier2d_f64::prelude::{ColliderBuilder, MassProperties, PhysicsPipeline, RigidBodyBuilder};
use rapier2d_f64::prelude::{ColliderBuilder, MassProperties, PhysicsPipeline, RigidBodyBuilder, SharedShape};
use starkingdoms_protocol::{module::ModuleType, planet::PlanetType, player::Player};
use std::error::Error;
use std::{f64::consts::PI, sync::Arc, time::Duration};


@@ 168,51 168,6 @@ pub async fn timer_main(
                    .insert(get_entity_id(), Entity::Module(module));
            }
            let mut entities = entities.write().await;
            for module in &mut entities.get_modules() {
                let module_handle = module.handle;
                let module_body = physics_data
                    .rigid_body_set
                    .get_mut(module_handle)
                    .ok_or("module does not exist")?;
                module_body.reset_forces(true);
                module_body.reset_torques(true);
                let grav_force = entities.gravity(
                    (module_body.translation().x, module_body.translation().y),
                    module_body.mass(),
                );
                module_body.apply_impulse(vector![grav_force.0, grav_force.1], true);
                let id = entities
                    .get_from_module(module)
                    .ok_or("module entity does not exist")?;
                if let Entity::Module(p_module) = entities
                    .entities
                    .get_mut(&id)
                    .ok_or("module does not exist")?
                {
                    p_module.lifetime += 5. / 1000.;
                }
                if module.lifetime > 80. {
                    let mut rigid_body_set = physics_data.rigid_body_set.clone();
                    let mut island_manager = physics_data.island_manager.clone();
                    let mut collider_set = physics_data.collider_set.clone();
                    let mut impulse_joint_set = physics_data.impulse_joint_set.clone();
                    let mut multibody_joint_set = physics_data.multibody_joint_set.clone();
                    rigid_body_set.remove(
                        module.handle,
                        &mut island_manager,
                        &mut collider_set,
                        &mut impulse_joint_set,
                        &mut multibody_joint_set,
                        true,
                    );
                    physics_data.rigid_body_set = rigid_body_set;
                    physics_data.collider_set = collider_set;
                    physics_data.island_manager = island_manager;
                    physics_data.impulse_joint_set = impulse_joint_set;
                    physics_data.multibody_joint_set = multibody_joint_set;
                    entities.entities.remove(&id);
                }
            }
            for module in &mut entities.get_all_attached() {
                let module_handle = module.handle;
                let module_body = physics_data


@@ 385,6 340,128 @@ pub async fn timer_main(
                }
            }
        }
        {
            let mut entities = entities.write().await;
            for planet in &entities.get_planets() {
                let body = physics_data.rigid_body_set.get(planet.body_handle).ok_or("planet body not found")?;
                let collider = body.colliders()[1];
                let narrow = physics_data.narrow_phase.clone();
                for (collider1, collider2, intersecting) in narrow.intersections_with(collider) {
                    if intersecting {
                        if collider1 == collider {
                            let collider_handle = match physics_data.collider_set.get_mut(collider2).ok_or("body doesn't exist collider2") {
                                Ok(c) => c,
                                Err(s) => { warn!("{}", s); continue; }
                            };
                            let body_handle = collider_handle.parent().ok_or("collider not attached to body")?;
                            let entity = entities.get_entity_from_handle(body_handle).ok_or("entity body doesn't exist")?;
                            match entity {
                                Entity::Player(player) => {
                                    let tree = player.search_modules(&entities);
                                    for module in tree {
                                        if module.module_type == ModuleType::Cargo {
                                            let module_id = entities.get_id_from_attached(&module)
                                                .ok_or("module doesn't exist")?;
                                            let module_handle =physics_data.rigid_body_set.get(module.handle)
                                                .ok_or("attached module body does not exist")?.colliders()[0];
                                            let module_collider = physics_data.collider_set.get_mut(module_handle)
                                                .ok_or("attached module collider does not exist")?;
                                            if let Entity::AttachedModule(attached) = entities.entities.get_mut(&module_id)
                                                .ok_or("module doesn't exist")? {
                                                match planet.planet_type {
                                                    PlanetType::Mars => {
                                                        attached.module_type = ModuleType::Hub;
                                                        module_collider.set_shape(SharedShape::cuboid(25./SCALE, 25./SCALE));
                                                        module_collider.set_translation_wrt_parent(vector![0., 0.]);
                                                    }
                                                    _ => {}
                                                };
                                            }
                                        }
                                    }
                                }
                                Entity::AttachedModule(module) => {
                                    let tree = entities.get_player_from_id(module.player_id).ok_or("player not found on attached")?
                                        .search_modules(&entities);
                                    for module in tree {
                                        if module.module_type == ModuleType::Cargo {
                                            let module_id = entities.get_id_from_attached(&module)
                                                .ok_or("module doesn't exist")?;
                                            let module_handle =physics_data.rigid_body_set.get(module.handle)
                                                .ok_or("attached module body does not exist")?.colliders()[0];
                                            let module_collider = physics_data.collider_set.get_mut(module_handle)
                                                .ok_or("attached module collider does not exist")?;
                                            if let Entity::AttachedModule(attached) = entities.entities.get_mut(&module_id)
                                                .ok_or("module doesn't exist")? {
                                                match planet.planet_type {
                                                    PlanetType::Mars => {
                                                        attached.module_type = ModuleType::Hub;
                                                        module_collider.set_shape(SharedShape::cuboid(25./SCALE, 25./SCALE));
                                                        module_collider.set_translation_wrt_parent(vector![0., 0.]);
                                                    }
                                                    _ => {}
                                                };
                                            }
                                        }
                                    }
                                }
                                _ => {}
                            }
                        } else {
                            let player_handle = physics_data.collider_set.get(collider1).ok_or("player body doesn't exist")?;
                            let body_handle = player_handle.parent().ok_or("player collider not attached to body")?;
                            let player = entities.get_player_from_handle(body_handle).ok_or("player doesn't exist")?;
                            let tree = player.search_modules(&entities);
                        }
                    }
                }
            }
            for module in &mut entities.get_modules() {
                let module_handle = module.handle;
                let module_body = physics_data
                    .rigid_body_set
                    .get_mut(module_handle)
                    .ok_or("module does not exist")?;
                module_body.reset_forces(true);
                module_body.reset_torques(true);
                let grav_force = entities.gravity(
                    (module_body.translation().x, module_body.translation().y),
                    module_body.mass(),
                );
                module_body.apply_impulse(vector![grav_force.0, grav_force.1], true);
                let id = entities
                    .get_from_module(module)
                    .ok_or("module entity does not exist")?;
                if let Entity::Module(p_module) = entities
                    .entities
                    .get_mut(&id)
                    .ok_or("module does not exist")?
                {
                    p_module.lifetime += 5. / 1000.;
                }
                if module.lifetime > 80. {
                    let mut rigid_body_set = physics_data.rigid_body_set.clone();
                    let mut island_manager = physics_data.island_manager.clone();
                    let mut collider_set = physics_data.collider_set.clone();
                    let mut impulse_joint_set = physics_data.impulse_joint_set.clone();
                    let mut multibody_joint_set = physics_data.multibody_joint_set.clone();
                    rigid_body_set.remove(
                        module.handle,
                        &mut island_manager,
                        &mut collider_set,
                        &mut impulse_joint_set,
                        &mut multibody_joint_set,
                        true,
                    );
                    physics_data.rigid_body_set = rigid_body_set;
                    physics_data.collider_set = collider_set;
                    physics_data.island_manager = island_manager;
                    physics_data.impulse_joint_set = impulse_joint_set;
                    physics_data.multibody_joint_set = multibody_joint_set;
                    entities.entities.remove(&id);
                }
            }
        }

        let mut to_remove = vec![];