@@ 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);
}
}
@@ 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![];