From f9c5c52fa8e3f78da3ebbeb971b4ff1c8ed7bc99 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Thu, 4 Jan 2024 23:42:57 -0600 Subject: [PATCH] landing thrusters physically in the game --- server/src/component.rs | 5 ++ server/src/main.rs | 101 ++++++++++++++++++++++++---- starkingdoms-client/src/hub.ts | 12 ---- starkingdoms-client/src/protocol.ts | 2 + starkingdoms-client/src/textures.ts | 6 ++ 5 files changed, 100 insertions(+), 26 deletions(-) diff --git a/server/src/component.rs b/server/src/component.rs index 6980baf18edbb84e0324b4a4b81e2c525947522d..20aa2185f76798ce714ddee993680d6af6504ea4 100644 --- a/server/src/component.rs +++ b/server/src/component.rs @@ -30,6 +30,8 @@ pub enum PartType { Hearty, Cargo, Hub, + LandingThruster, + LandingThrusterSuspension, } #[derive(Component, Clone, Debug)] @@ -39,6 +41,9 @@ pub struct Attach { pub children: [Option; 4], } +#[derive(Component, Clone, Copy, PartialEq, Debug)] +pub struct CanAttach(pub u8); // each bit means a slot able to attach to + #[derive(Component, Clone, Copy, Serialize, Deserialize, Debug, Default)] pub struct Input { pub up: bool, diff --git a/server/src/main.rs b/server/src/main.rs index e42f3355d09535b221c742c1f3a3cdc7ce686b6f..a968923529b5ed2148cf14be5aa10e4aa1c76327 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -833,11 +833,12 @@ fn attach_on_module_tree( } fn convert_modules( + mut commands: Commands, rapier_context: Res, planet_query: Query<(Entity, &PlanetType, &Children)>, player_query: Query<&Attach, With>, - mut attached_query: Query<(&mut PartType, &Attach, &Children), Without>, - mut collider_query: Query<(&mut Collider, &mut Transform, &Parent)>, + mut attached_query: Query<(Entity, &mut PartType, &mut Attach, &Children, &Transform), Without>, + mut collider_query: Query<(&mut Collider, &mut Transform, &Parent), (Without, Without)>, mut packet_send: EventWriter, ) { for (_planet_entity, planet_type, children) in &planet_query { @@ -856,7 +857,7 @@ fn convert_modules( attached_query .get(other) .unwrap() - .1 + .2 .associated_player .unwrap() } else if collider_query.contains(other) { @@ -865,7 +866,7 @@ fn convert_modules( attached_query .get(parent) .unwrap() - .1 + .2 .associated_player .unwrap() } else { @@ -876,6 +877,7 @@ fn convert_modules( }; let player_attach = player_query.get(player_entity).unwrap(); convert_modules_recursive( + &mut commands, *planet_type, player_attach.clone(), &mut attached_query, @@ -888,15 +890,16 @@ fn convert_modules( } fn convert_modules_recursive( + commands: &mut Commands, planet_type: PlanetType, attach: Attach, - attached_query: &mut Query<(&mut PartType, &Attach, &Children), Without>, - collider_query: &mut Query<(&mut Collider, &mut Transform, &Parent)>, + attached_query: &mut Query<(Entity, &mut PartType, &mut Attach, &Children, &Transform), Without>, + collider_query: &mut Query<(&mut Collider, &mut Transform, &Parent), (Without, Without)>, packet_send: &mut EventWriter, ) { for child in attach.children { if let Some(child) = child { - let (mut part_type, attach, children) = attached_query.get_mut(child).unwrap(); + let (module_entity, mut part_type, mut attach, children, module_transform) = attached_query.get_mut(child).unwrap(); if *part_type == PartType::Cargo { match planet_type { PlanetType::Mars => { @@ -906,6 +909,8 @@ fn convert_modules_recursive( *collider = Collider::cuboid(PART_HALF_SIZE / SCALE, PART_HALF_SIZE / SCALE); *transform = Transform::from_xyz(0., 0., 0.); + commands.get_entity(module_entity).unwrap().remove::(); + commands.get_entity(module_entity).unwrap().insert(CanAttach(15)); let packet = Packet::DespawnPart { id: child.index() }; let buf = serde_json::to_vec(&packet).unwrap(); packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf.clone())); @@ -921,16 +926,84 @@ fn convert_modules_recursive( packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf)); } + PlanetType::Moon => { + *part_type = PartType::LandingThruster; + let (mut collider, mut transform, _) = + collider_query.get_mut(*children.first().unwrap()).unwrap(); + *collider = + Collider::cuboid(PART_HALF_SIZE / SCALE, 18.75 / SCALE); + *transform = Transform::from_xyz(0., 6.25 / SCALE, 0.); + //commands.get_entity(module_entity).unwrap().remove::(); + let joint = PrismaticJointBuilder::new(Vec2::new(0., 1.)) + .local_anchor1(Vec2::new(0., 0.)) + .local_anchor2(Vec2::new(0., 0.)) + .motor_position(0., 32., 6.) + .build(); + let mut suspension = commands.spawn(PartBundle { + transform: TransformBundle::from(*module_transform), + part_type: PartType::LandingThrusterSuspension, + }); + suspension.insert(RigidBody::Dynamic) + .with_children(|children| { + children + .spawn(Collider::cuboid(PART_HALF_SIZE / SCALE, 1. / SCALE)) + .insert(TransformBundle::from(Transform::from_xyz( + 0., + -24. / SCALE, + 0., + ))); + }) + .insert(ImpulseJoint::new(module_entity, joint)) + .insert(ExternalForce::default()) + .insert(ExternalImpulse::default()) + .insert(Velocity::default()) + .insert(ReadMassProperties::default()) + .insert(Attach { + associated_player: attach.associated_player, + parent: Some(module_entity), + children: [None, None, None, None], + }); + attach.children[2] = Some(suspension.id()); + + + let packet = Packet::DespawnPart { id: child.index() }; + let buf = serde_json::to_vec(&packet).unwrap(); + packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf.clone())); + + let packet = Packet::SpawnPart { + id: child.index(), + part: Part { + part_type: PartType::LandingThruster, + transform: proto_transform!(transform), + }, + }; + let buf = serde_json::to_vec(&packet).unwrap(); + + packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf)); + + let packet = Packet::SpawnPart { + id: suspension.id().index(), + part: Part { + part_type: PartType::LandingThrusterSuspension, + transform: proto_transform!(transform) + } + }; + let buf = serde_json::to_vec(&packet).unwrap(); + packet_send.send(ServerEvent::Broadcast(MessageType::Text, buf)); + } _ => {} } } - convert_modules_recursive( - planet_type, - attach.clone(), - attached_query, - collider_query, - packet_send, - ); + if *part_type != PartType::LandingThruster { + convert_modules_recursive( + commands, + planet_type, + attach.clone(), + attached_query, + collider_query, + packet_send, + ); + } } } } diff --git a/starkingdoms-client/src/hub.ts b/starkingdoms-client/src/hub.ts index 3a088e920cd94d5509235c74acc816b9b3943456..218287814823968d5572c68ae205ef02e585cc1d 100644 --- a/starkingdoms-client/src/hub.ts +++ b/starkingdoms-client/src/hub.ts @@ -201,18 +201,6 @@ export async function hub_connect( let id = p.parts[i][0]; let new_part = p.parts[i][1]; - if (global.parts_map.has(id)) { - let old_part = global.parts_map.get(id)!; - let dx = new_part.transform.x - old_part.transform.x; - let dy = new_part.transform.y - old_part.transform.y; - let drot = new_part.transform.rot - old_part.transform.rot; - const CUTOFF_XY = 0.01; - const CUTOFF_ROT = 0.01; - if (dx < CUTOFF_XY && dy < CUTOFF_XY && drot < CUTOFF_ROT) { - continue; // this packet is under the cutoff, we don't care about it - } - } - global.parts_map.set(id, new_part); if (id === global.me?.part_id) { document.getElementById("pos-val-x")!.innerText = Math.round( diff --git a/starkingdoms-client/src/protocol.ts b/starkingdoms-client/src/protocol.ts index 93fb5e53408e3fd76bfba87fc13975448933c644..d9241931a47dd6b42301c8bc47c5e11b935b7dbd 100644 --- a/starkingdoms-client/src/protocol.ts +++ b/starkingdoms-client/src/protocol.ts @@ -12,6 +12,8 @@ export enum PartType { Hearty = "Hearty", Cargo = "Cargo", Hub = "Hub", + LandingThruster = "LandingThruster", + LandingThrusterSuspension = "LandingThrusterSuspension", } export enum ButtonType { Left = "Left", diff --git a/starkingdoms-client/src/textures.ts b/starkingdoms-client/src/textures.ts index a796dc9009f0275a91610659100cb056af0b6afb..9938e55c7f55c145e12b248289804ae68c796159 100644 --- a/starkingdoms-client/src/textures.ts +++ b/starkingdoms-client/src/textures.ts @@ -5,6 +5,8 @@ import tex_mars from "./assets/mars.svg"; import tex_hearty from "./assets/hearty.svg"; import tex_cargo_off from "./assets/cargo_off.svg"; import tex_hub_off from "./assets/hub_off.svg"; +import tex_landing_thruster from "./assets/landingthruster_off.svg"; +import tex_landing_thruster_suspension from "./assets/landingleg.svg"; import tex_missing from "./assets/missing.svg"; export function planet_texture_url(type: PlanetType): string { @@ -25,6 +27,10 @@ export function part_texture_url(type: PartType): string { return tex_cargo_off; } else if (type == PartType.Hub) { return tex_hub_off; + } else if (type == PartType.LandingThruster) { + return tex_landing_thruster; + } else if (type == PartType.LandingThrusterSuspension) { + return tex_landing_thruster_suspension; } return tex_missing; }