From 5ed17b2ec1a3f95349833a08850fc8d7ef5d8d4f Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Sat, 4 Apr 2026 05:43:53 -0500 Subject: [PATCH] feat: toggle drill button --- .../assets/config/parts/hearty.part.toml | 3 + .../assets/config/parts/thruster.part.toml | 25 +++++++++ crates/unified/src/client/crafting/ui.rs | 55 +++++++++++++++++-- crates/unified/src/config/part.rs | 5 ++ crates/unified/src/ecs.rs | 12 ++++ crates/unified/src/server/drill.rs | 17 ++++++ crates/unified/src/server/mod.rs | 3 + crates/unified/src/shared_plugins.rs | 4 +- 8 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 crates/unified/assets/config/parts/thruster.part.toml create mode 100644 crates/unified/src/server/drill.rs diff --git a/crates/unified/assets/config/parts/hearty.part.toml b/crates/unified/assets/config/parts/hearty.part.toml index 7947d453dcac6848eaaee112f3091b9f6cb6b606..914a62779806fcad56edce8edef787b75d3d2022 100644 --- a/crates/unified/assets/config/parts/hearty.part.toml +++ b/crates/unified/assets/config/parts/hearty.part.toml @@ -66,3 +66,6 @@ heat_cooling_constant = 1.0 [crafting] can_craft = true + +[drill] +resource_multiplier = 1.0 diff --git a/crates/unified/assets/config/parts/thruster.part.toml b/crates/unified/assets/config/parts/thruster.part.toml new file mode 100644 index 0000000000000000000000000000000000000000..ef1b19cce46dcef40cd5e30033f64b5f6860e873 --- /dev/null +++ b/crates/unified/assets/config/parts/thruster.part.toml @@ -0,0 +1,25 @@ +[part] +name = "Thruster" +sprite_connected = "textures/thruster_on.png" +sprite_disconnected = "textures/thruster_off.png" + +[physics] +width = 50 +height = 50 +mass = 100 + +[[thruster]] +id = "main" +apply_force_at_local = [ 0, 0 ] +thrust_vector = [ 0.0, -20000.0 ] + +[[joint]] +id = "Top" +target = { translation = [ 0.0, 55.0, 0.0 ], rotation = 0.0 } +snap = { translation = [ 0.0, 25.0, 0.0 ], rotation = 0.0 } + +[crafting] +can_craft = false + +[[crafting.recipe]] +inputs = { iron = 25 } diff --git a/crates/unified/src/client/crafting/ui.rs b/crates/unified/src/client/crafting/ui.rs index 900dc735ef60434150ec1c6af17a473e76078c84..2ff673133471452cd9f29f135e4e382785103d9a 100644 --- a/crates/unified/src/client/crafting/ui.rs +++ b/crates/unified/src/client/crafting/ui.rs @@ -1,15 +1,17 @@ use bevy::{input_focus::{AutoFocus, InputFocus}, ui::RelativeCursorPosition}; -use crate::{attachment::PartInShip, client::colors, ecs::{CanCraft, CraftingUi, MainCamera, Me}, prelude::*}; +use crate::{attachment::PartInShip, client::colors, ecs::{CanCraft, CraftingUi, MainCamera, Me, ToggleDrillEvent}, prelude::*}; pub fn crafting_ui_plugin(app: &mut App) { - app.add_systems(Update, close_button); + app.add_systems(Update, (close_button, drill_button)); } #[derive(Component)] struct CloseButton(Entity); // stores corresponding menu entity #[derive(Component)] -struct PreviousInteraction(Interaction); // stores corresponding menu entity +struct PreviousInteraction(Interaction); +#[derive(Component)] +struct DrillButton(Entity); // stores corresponding part pub fn open_crafting_ui( ev: On>, @@ -28,11 +30,12 @@ pub fn open_crafting_ui( }; // we have our crafting entity! // now make the ui - setup_ui(transform, commands, camera); + setup_ui(entity, transform, commands, camera); } } fn setup_ui( + parent_part: Entity, parent_transform: &Transform, mut commands: Commands, camera: Single<(Entity, &Camera, &GlobalTransform), (With, Without)>, @@ -75,9 +78,53 @@ fn setup_ui( Text::new("x"), )); }); + parent.spawn(( + Node { + ..Default::default() + }, + Button, + DrillButton(parent_part), + BackgroundColor(colors::CRUST), + PreviousInteraction(Interaction::None), + )) + .with_children(|parent| { + parent.spawn(( + Node { + ..Default::default() + }, + Text::new("Start Drill"), + )); + }); }); } +fn drill_button( + mut interaction_query: Query< + (&Interaction, &mut PreviousInteraction, &mut BackgroundColor, &DrillButton, &mut Button), + Changed, + >, + mut toggle_drill_writer: MessageWriter, +) { + for (interaction, mut previous_interaction, mut color, drill_button, mut button) in &mut interaction_query { + match *interaction { + Interaction::Pressed => { + *color = colors::SURFACE_1.into(); + } + Interaction::Hovered => { + *color = colors::SURFACE_0.into(); + if previous_interaction.0 == Interaction::Pressed { + // released + toggle_drill_writer.write(ToggleDrillEvent { drill_entity: drill_button.0 }); + } + } + Interaction::None => { + *color = colors::CRUST.into(); + } + } + previous_interaction.0 = *interaction; + } +} + fn close_button( mut commands: Commands, mut interaction_query: Query< diff --git a/crates/unified/src/config/part.rs b/crates/unified/src/config/part.rs index d9e7f4e3698d474b30a34968f1d47bf1deba1207..022d01d5607dea6b844aaf7427fe05303ed56ae0 100644 --- a/crates/unified/src/config/part.rs +++ b/crates/unified/src/config/part.rs @@ -15,6 +15,7 @@ pub struct PartConfig { pub joints: Vec, pub cooling: Option, pub crafting: Option, + pub drill: Option, } #[derive(Deserialize, TypePath, Serialize, Clone, Debug, PartialEq)] pub struct PartPartConfig { @@ -70,3 +71,7 @@ pub struct CoolingConfig { pub struct CraftingConfig { pub can_craft: bool, } +#[derive(Deserialize, TypePath, Serialize, Clone, Debug, PartialEq)] +pub struct DrillConfig { + pub resource_multiplier: f32, +} diff --git a/crates/unified/src/ecs.rs b/crates/unified/src/ecs.rs index a34849011ea98c38e600ccb9ec8b0322f98ccf1f..6ccfac24ab9c65458a18aca6af4e22404bc75aed 100644 --- a/crates/unified/src/ecs.rs +++ b/crates/unified/src/ecs.rs @@ -129,3 +129,15 @@ pub struct Radiator { pub emissivity: f32, pub surface_area: f32, } + +#[derive(Component, Serialize, Deserialize, Debug)] +#[require(Replicated)] +pub struct Drill { + pub drilling: bool, + pub resource_multiplier: f32, +} +#[derive(Message, Serialize, Deserialize, Debug, MapEntities, Clone)] +pub struct ToggleDrillEvent { + #[entities] + pub drill_entity: Entity, +} diff --git a/crates/unified/src/server/drill.rs b/crates/unified/src/server/drill.rs new file mode 100644 index 0000000000000000000000000000000000000000..eb0a8008e48e2b2cef7c5fa30c02e22cdbafc926 --- /dev/null +++ b/crates/unified/src/server/drill.rs @@ -0,0 +1,17 @@ +use crate::{ecs::{Drill, ToggleDrillEvent}, prelude::*}; + +pub fn drill_plugin(app: &mut App) { + app.add_systems(Update, toggle_drill); +} + +fn toggle_drill( + mut toggle_drill_reader: MessageReader>, + mut drills: Query<&mut Drill>, +) { + for toggle_drill_event in toggle_drill_reader.read() { + // this getting of the drill also serves to check whether or not + // the entity is a drill + let Ok(mut drill) = drills.get_mut(toggle_drill_event.drill_entity) else { return }; + drill.drilling = !drill.drilling; + } +} diff --git a/crates/unified/src/server/mod.rs b/crates/unified/src/server/mod.rs index 91a395e7daaee7bfd4280f8a3469fd7a906a4f13..84e990af336bea5654cead8bdf52a7de18729519 100644 --- a/crates/unified/src/server/mod.rs +++ b/crates/unified/src/server/mod.rs @@ -2,10 +2,12 @@ mod earth_parts; mod gravity; mod part; mod heat; +mod drill; pub mod planets; pub mod player; mod system_sets; +use crate::server::drill::drill_plugin; use crate::server::earth_parts::spawn_parts_plugin; use crate::server::gravity::newtonian_gravity_plugin; use crate::server::heat::conduction::heat_conduction_plugin; @@ -55,6 +57,7 @@ impl Plugin for ServerPlugin { .add_plugins(heat_cooling_plugin) .add_plugins(heat_radiation_plugin) .add_plugins(heat_conduction_plugin) + .add_plugins(drill_plugin) .configure_sets(Update, WorldUpdateSet.before(PlayerInputSet)); //.configure_sets(Update, PlayerInputSet.before(PhysicsSet::SyncBackend)); } diff --git a/crates/unified/src/shared_plugins.rs b/crates/unified/src/shared_plugins.rs index 1a61058601ffe508a22830daa7ffd72fe1c81cc4..c3a831470cc6892f9685ab0f86afc6bfb6722ba9 100644 --- a/crates/unified/src/shared_plugins.rs +++ b/crates/unified/src/shared_plugins.rs @@ -1,6 +1,6 @@ use crate::attachment::{Joint, JointOf, PartInShip, Peer, Ship, SnapOf, SnapOfJoint}; use crate::config::planet::{Planet, PlanetConfigCollection}; -use crate::ecs::{CanCraft, DragRequestEvent, Hi, Part, Particles, Player, PlayerStorage, Temperature}; +use crate::ecs::{CanCraft, DragRequestEvent, Drill, Hi, Part, Particles, Player, PlayerStorage, Temperature, ToggleDrillEvent}; use bevy::app::{App, PluginGroup, PluginGroupBuilder}; use bevy_common_assets::toml::TomlAssetPlugin; use crate::prelude::*; @@ -34,6 +34,7 @@ impl PluginGroup for SharedPluginGroup { pub fn register_everything(app: &mut App) { app.add_mapped_client_message::(Channel::Ordered); app.add_mapped_client_message::(Channel::Ordered); + app.add_mapped_client_message::(Channel::Ordered); app.add_mapped_server_message::(Channel::Ordered); app.replicate::(); app.replicate::(); @@ -54,6 +55,7 @@ pub fn register_everything(app: &mut App) { app.replicate::(); app.replicate::(); app.replicate::(); + app.replicate::(); } fn physics_setup_plugin(app: &mut App) {