@@ 137,6 137,15 @@ impl EntityHandler {
}
modules
}
+ pub fn get_all_attached_id(&self) -> Vec<(EntityId, AttachedModule)> {
+ let mut modules = Vec::new();
+ for (id, entity) in self.entities.clone() {
+ if let Entity::AttachedModule(module) = entity {
+ modules.push((id, module.clone()));
+ }
+ }
+ modules
+ }
pub fn get_attached_from_id(&self, id: EntityId) -> Option<AttachedModule> {
if let Some(Entity::AttachedModule(module)) = self.entities.get(&id) {
return Some(module.clone());
@@ 1,16 1,16 @@
use crate::entity::EntityHandler;
-use crate::module::Module;
+use crate::module::{Module, AttachedModule};
use crate::orbit::constants::{GAME_ORBITS_ENABLED, MARS_APHELION, MARS_ORBIT_TIME, MARS_PERIHELION, MOON_APOAPSIS, MOON_ORBIT_TIME, MOON_PERIAPSIS};
use crate::orbit::orbit::{calculate_point_on_orbit, calculate_world_position_of_orbit};
use crate::{
entity::{get_entity_id, Entity},
manager::{ClientHandlerMessage, ClientManager, PhysicsData},
- planet::{Planets},
+ planet::Planets,
SCALE,
};
use async_std::sync::RwLock;
use async_std::task::sleep;
-use log::{warn};
+use log::warn;
use nalgebra::{point, vector};
use protobuf::SpecialFields;
use rand::Rng;
@@ 95,7 95,7 @@ pub async fn timer_main(
);
// update mars
- let mars_id = planets.get_planet_id(PlanetType::Mars).ok_or("moon does not exist")?;
+ let mars_id = planets.get_planet_id(PlanetType::Mars).ok_or("mars does not exist")?;
if let Entity::Planet(mars) = map
.get_mut(&mars_id)
.ok_or("mars does not exist")? {
@@ 226,11 226,37 @@ pub async fn timer_main(
module_body.mass(),
);
module_body.apply_impulse(vector![grav_force.0, grav_force.1], true);
+
+ for child in module.children.clone() {
+ if let Some(child) = child {
+ let joint = physics_data.impulse_joint_set
+ .get(child.connection).ok_or("module joint does not exist")?;
+ if joint.impulses.magnitude() > 0.00006 {
+ let module: Option<AttachedModule>;
+ if let Some(Entity::AttachedModule(p_module)) =
+ entities.entities.get_mut(&child.child)
+ {
+ module = Some(p_module.clone());
+ } else {
+ warn!("attempted to detach nonexistent module");
+ continue;
+ }
+ let player_id = module.as_ref().ok_or("cannot detach module that doesn't exist")?.player_id;
+ AttachedModule::detach(
+ &mut physics_data,
+ &mut entities,
+ player_id,
+ &module.ok_or("cannot detach module that doesn't exist")?,
+ );
+ }
+ }
+ }
}
}
{
- for (player_id, player) in &entities.read().await.get_players() {
+ let mut entities = entities.write().await;
+ for (player_id, player) in &entities.get_players() {
let player_handle = player.handle;
let player_body = physics_data
.rigid_body_set
@@ 238,8 264,7 @@ pub async fn timer_main(
.ok_or("player body does not exist")?;
player_body.reset_forces(true);
player_body.reset_torques(true);
- let planets = entities.read().await;
- let grav_force = planets.gravity(
+ let grav_force = entities.gravity(
(player_body.translation().x, player_body.translation().y),
player_body.mass(),
);
@@ 330,6 355,34 @@ pub async fn timer_main(
username,
special_fields: SpecialFields::default(),
});
+
+ for child in player.children.clone() {
+ if let Some(child) = child {
+ let joint = physics_data.impulse_joint_set
+ .get(child.connection).ok_or("module joint does not exist")?;
+ // displays impulse on joint * 10000 so its visible, use to tune breaking
+ // force
+ //debug!("impulse: {}", joint.impulses.magnitude() * 10000.);
+ if joint.impulses.magnitude() > 0.00006 {
+ let module: Option<AttachedModule>;
+ if let Some(Entity::AttachedModule(p_module)) =
+ entities.entities.get_mut(&child.child)
+ {
+ module = Some(p_module.clone());
+ } else {
+ warn!("attempted to detach nonexistent module");
+ continue;
+ }
+ let player_id = module.as_ref().ok_or("cannot detach module that doesn't exist")?.player_id;
+ AttachedModule::detach(
+ &mut physics_data,
+ &mut entities,
+ player_id,
+ &module.ok_or("cannot detach module that doesn't exist")?,
+ );
+ }
+ }
+ }
}
}