From f6b067533cc129c77d067f29cd7e9374d9f357ff Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 19 Mar 2024 01:24:27 +0100 Subject: [PATCH] the alien says "Hello World" if you talk to it --- src/actor.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++--- src/hud.rs | 28 +++++++++++++++++++++--- src/settings.rs | 2 ++ src/world.rs | 13 ++++++----- 4 files changed, 89 insertions(+), 12 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index aee46d2..a2db27d 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -1,13 +1,25 @@ use bevy::prelude::*; -use crate::nature; +use crate::{nature, settings, actor, audio, hud}; + +const MIN_INTERACT_DISTANCE: f32 = 30.0; pub struct ActorPlugin; impl Plugin for ActorPlugin { fn build(&self, app: &mut App) { app.add_systems(FixedUpdate, update); + app.add_systems(Update, ( + handle_conversations, + handle_input, + )); + app.add_event::(); } } +#[derive(Event)] +pub struct StartConversationEvent { + pub conv: String +} + #[derive(Component)] pub struct Actor { pub hp: f32, @@ -26,6 +38,15 @@ impl Default for Actor { } } +#[derive(Component)] pub struct Player; +#[derive(Component)] pub struct PlayerInConversation; +#[derive(Component)] pub struct InConversationWithPlayer; + +#[derive(Component)] +pub struct Talker { + pub conv: String, +} + #[derive(Component)] pub struct LifeForm { pub adrenaline: f32, @@ -71,8 +92,39 @@ pub fn update( } } -#[derive(Component)] -pub struct Player; +pub fn handle_input( + keyboard_input: Res>, + settings: ResMut, + query: Query<(&Talker, &Transform)>, + player: Query<&Transform, With>, + mut ew_conv: EventWriter, + mut ew_click: EventWriter, +) +{ + if keyboard_input.just_pressed(settings.key_interact) { + let mindist = MIN_INTERACT_DISTANCE * MIN_INTERACT_DISTANCE; + if let Ok(player) = player.get_single() { + for (talker, transform) in &query { + if transform.translation.distance_squared(player.translation) <= mindist { + ew_conv.send(StartConversationEvent{conv: talker.conv.clone()}); + ew_click.send(audio::AudioClickEvent()); + break; + } + } + } + } +} + +pub fn handle_conversations( + mut er_conv: EventReader, + mut log: ResMut, +) { + for my_event in er_conv.read() { + log.add(my_event.conv.clone()); + break; + } +} + //pub enum SuitSystemHandler { // Heat, diff --git a/src/hud.rs b/src/hud.rs index 147adb8..e9dcea7 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -21,8 +21,8 @@ impl Plugin for HudPlugin { } } -#[derive(Component)] -struct GaugesText; +#[derive(Component)] struct GaugesText; +#[derive(Component)] struct ChatText; #[derive(Resource)] struct FPSUpdateTimer(Timer); @@ -33,7 +33,7 @@ struct Message { } #[derive(Resource)] -struct Log { +pub struct Log { logs: VecDeque, } @@ -192,6 +192,28 @@ fn setup( bundle_fps, GaugesText, )); + + // Add Chat Box +// let bundle_chatbox = TextBundle::from_sections([ +// TextSection::new( +// "Hello :D", +// TextStyle { +// font: asset_server.load(FONT), +// font_size: settings.font_size_hud, +// color: Color::GRAY, +// ..default() +// } +// ), +// ]).with_style(Style { +// position_type: PositionType::Absolute, +// bottom: Val::VMin(40.0), +// left: Val::VMin(30.0), +// ..default() +// }); +// commands.spawn(( +// bundle_chatbox, +// ChatText, +// )); } fn update( diff --git a/src/settings.rs b/src/settings.rs index 57027e3..bd8d756 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -21,6 +21,7 @@ pub struct Settings { pub key_down: KeyCode, pub key_run: KeyCode, pub key_stop: KeyCode, + pub key_interact: KeyCode, } impl Default for Settings { @@ -45,6 +46,7 @@ impl Default for Settings { key_down: KeyCode::ControlLeft, key_run: KeyCode::KeyR, key_stop: KeyCode::Space, + key_interact: KeyCode::KeyE, } } } diff --git a/src/world.rs b/src/world.rs index 2a43ca1..d8d179c 100644 --- a/src/world.rs +++ b/src/world.rs @@ -73,10 +73,7 @@ pub fn setup( actor::Suit { oxygen: nature::OXY_M, ..default() - } - )); - - commands.spawn(( + }, Camera3dBundle { camera: Camera { hdr: true, // HDR is required for bloom @@ -193,7 +190,10 @@ pub fn setup( } // Add alien - commands.spawn(SceneBundle { + commands.spawn(( + actor::Actor::default(), + actor::Talker { conv: "Hello World!".to_string() }, + SceneBundle { transform: Transform { translation: Vec3::new( 0.0, @@ -205,7 +205,8 @@ pub fn setup( }, scene: asset_server.load(ASSET_ASTRONAUT), ..default() - }); + }, + )); // Space is DARK ambient_light.brightness = 0.0;