@@ 14,6 14,12 @@ pub enum PartType {
Cargo,
}
+#[derive(Component, Clone, Debug)]
+pub struct Attach {
+ pub associated_player: Option<Entity>,
+ pub children: [Option<Entity>; 4]
+}
+
#[derive(Component, Clone, Copy, Serialize, Deserialize, Debug, Default)]
pub struct Input {
pub up: bool,
@@ 45,4 51,5 @@ pub struct PartBundle {
pub struct PlayerBundle {
pub part: PartBundle,
pub player: Player,
+ pub attach: Attach,
}
@@ 1,7 1,7 @@
use std::net::Ipv4Addr;
-use crate::mathutil::{rot2d, v3_to_v2};
-use bevy::math::{vec2, vec3};
+use crate::mathutil::rot2d;
+use bevy::math::vec2;
use bevy::utils::tracing;
use bevy::{ecs::event::ManualEventReader, prelude::*};
use bevy_rapier2d::prelude::*;
@@ 122,7 122,7 @@ fn on_message(
0.0,
);
transform.rotate_z(angle);
- let id = commands
+ let entity_id = commands
.spawn(PlayerBundle {
part: PartBundle {
part_type: PartType::Hearty,
@@ 133,6 133,10 @@ fn on_message(
username: username.to_string(),
input: component::Input::default(),
},
+ attach: Attach {
+ associated_player: None,
+ children: [None, None, None, None],
+ },
})
.insert(Collider::cuboid(
PART_HALF_SIZE / SCALE,
@@ 149,9 153,41 @@ fn on_message(
})
.insert(ExternalForce::default())
.insert(ReadMassProperties::default())
+ .insert(RigidBody::Dynamic).id();
+ let id = entity_id.index();
+
+ // spawn module
+ let module_id = commands
+ .spawn(PartBundle {
+ part_type: PartType::Cargo,
+ transform: TransformBundle::from(Transform::from_xyz(1100. / SCALE, 0., 0.)),
+ })
+ //.insert(Collider::cuboid(18.75 / SCALE, 23.4375 / SCALE))
.insert(RigidBody::Dynamic)
- .id()
- .index();
+ .with_children(|children| {
+ children
+ .spawn(Collider::cuboid(18.75 / SCALE, 23.4375 / SCALE))
+ .insert(TransformBundle::from(Transform::from_xyz(
+ 0.,
+ 1.5625 / SCALE,
+ 0.,
+ )));
+ })
+ .insert(ExternalForce::default())
+ .insert(ExternalImpulse::default())
+ .insert(ReadMassProperties::default()).id();
+
+ // attachment logic
+ let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, -53. / SCALE));
+ let mut module = commands.entity(module_id);
+ module.insert(ImpulseJoint::new(entity_id, joint));
+ let mut entity = commands.entity(entity_id);
+ // temporary remove attach
+ entity.remove::<Attach>();
+ entity.insert(Attach {
+ associated_player: None,
+ children: [None, None, Some(module_id), None]
+ });
// tell this player the planets
let mut planets = Vec::new();
@@ 299,7 335,9 @@ fn on_message(
}
fn on_close(
- player_query: Query<(Entity, &Player)>,
+ player_query: Query<(Entity, &Player, &Attach)>,
+ attached_query: Query<&Attach, With<PartType>>,
+ part_query: Query<&PartType>,
mut commands: Commands,
mut packet_recv: Local<ManualEventReader<ServerEvent>>,
mut packet_send: ResMut<Events<ServerEvent>>,
@@ 307,13 345,14 @@ fn on_close(
let mut packets = Vec::new();
for packet in packet_recv.read(&packet_send) {
if let ServerEvent::Close(addr) = packet {
- for (entity, player) in &player_query {
+ for (entity, player, attach) in &player_query {
if player.addr == *addr {
- commands.entity(entity).despawn();
+ despawn_module_tree(&mut commands, attach, &attached_query, &part_query);
+ commands.entity(entity).despawn_recursive();
let packet = Packet::PlayerLeave { id: entity.index() };
let buf = serde_json::to_vec(&packet).unwrap();
- for (in_entity, player) in &player_query {
+ for (in_entity, player, _) in &player_query {
if entity != in_entity {
packets.push(ServerEvent::Send(
player.addr,
@@ 331,6 370,31 @@ fn on_close(
}
}
+fn despawn_module_tree(
+ commands: &mut Commands,
+ attach: &Attach,
+ attached_query: &Query<&Attach, With<PartType>>,
+ part_query: &Query<&PartType>,
+) {
+ for child in attach.children {
+ if let Some(child) = child {
+ commands.entity(child).despawn_recursive();
+ let attach = match attached_query.get(child) {
+ Ok(s) => s,
+ Err(_) => {
+ match part_query.get(child) {
+ Ok(_) => {
+ continue;
+ }
+ Err(e) => panic!("{}", e)
+ }
+ }
+ };
+ despawn_module_tree(commands, attach, attached_query, part_query);
+ }
+ }
+}
+
fn on_position_change(
mut commands: Commands,
part_query: Query<(Entity, &PartType, &Transform), Changed<Transform>>,