the alien says "Hello World" if you talk to it

This commit is contained in:
yuni 2024-03-19 01:24:27 +01:00
parent 39aa5b04b3
commit f6b067533c
4 changed files with 89 additions and 12 deletions

View file

@ -1,13 +1,25 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::nature; use crate::{nature, settings, actor, audio, hud};
const MIN_INTERACT_DISTANCE: f32 = 30.0;
pub struct ActorPlugin; pub struct ActorPlugin;
impl Plugin for ActorPlugin { impl Plugin for ActorPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(FixedUpdate, update); app.add_systems(FixedUpdate, update);
app.add_systems(Update, (
handle_conversations,
handle_input,
));
app.add_event::<StartConversationEvent>();
} }
} }
#[derive(Event)]
pub struct StartConversationEvent {
pub conv: String
}
#[derive(Component)] #[derive(Component)]
pub struct Actor { pub struct Actor {
pub hp: f32, 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)] #[derive(Component)]
pub struct LifeForm { pub struct LifeForm {
pub adrenaline: f32, pub adrenaline: f32,
@ -71,8 +92,39 @@ pub fn update(
} }
} }
#[derive(Component)] pub fn handle_input(
pub struct Player; keyboard_input: Res<ButtonInput<KeyCode>>,
settings: ResMut<settings::Settings>,
query: Query<(&Talker, &Transform)>,
player: Query<&Transform, With<actor::Player>>,
mut ew_conv: EventWriter<StartConversationEvent>,
mut ew_click: EventWriter<audio::AudioClickEvent>,
)
{
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<StartConversationEvent>,
mut log: ResMut<hud::Log>,
) {
for my_event in er_conv.read() {
log.add(my_event.conv.clone());
break;
}
}
//pub enum SuitSystemHandler { //pub enum SuitSystemHandler {
// Heat, // Heat,

View file

@ -21,8 +21,8 @@ impl Plugin for HudPlugin {
} }
} }
#[derive(Component)] #[derive(Component)] struct GaugesText;
struct GaugesText; #[derive(Component)] struct ChatText;
#[derive(Resource)] #[derive(Resource)]
struct FPSUpdateTimer(Timer); struct FPSUpdateTimer(Timer);
@ -33,7 +33,7 @@ struct Message {
} }
#[derive(Resource)] #[derive(Resource)]
struct Log { pub struct Log {
logs: VecDeque<Message>, logs: VecDeque<Message>,
} }
@ -192,6 +192,28 @@ fn setup(
bundle_fps, bundle_fps,
GaugesText, 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( fn update(

View file

@ -21,6 +21,7 @@ pub struct Settings {
pub key_down: KeyCode, pub key_down: KeyCode,
pub key_run: KeyCode, pub key_run: KeyCode,
pub key_stop: KeyCode, pub key_stop: KeyCode,
pub key_interact: KeyCode,
} }
impl Default for Settings { impl Default for Settings {
@ -45,6 +46,7 @@ impl Default for Settings {
key_down: KeyCode::ControlLeft, key_down: KeyCode::ControlLeft,
key_run: KeyCode::KeyR, key_run: KeyCode::KeyR,
key_stop: KeyCode::Space, key_stop: KeyCode::Space,
key_interact: KeyCode::KeyE,
} }
} }
} }

View file

@ -73,10 +73,7 @@ pub fn setup(
actor::Suit { actor::Suit {
oxygen: nature::OXY_M, oxygen: nature::OXY_M,
..default() ..default()
} },
));
commands.spawn((
Camera3dBundle { Camera3dBundle {
camera: Camera { camera: Camera {
hdr: true, // HDR is required for bloom hdr: true, // HDR is required for bloom
@ -193,7 +190,10 @@ pub fn setup(
} }
// Add alien // Add alien
commands.spawn(SceneBundle { commands.spawn((
actor::Actor::default(),
actor::Talker { conv: "Hello World!".to_string() },
SceneBundle {
transform: Transform { transform: Transform {
translation: Vec3::new( translation: Vec3::new(
0.0, 0.0,
@ -205,7 +205,8 @@ pub fn setup(
}, },
scene: asset_server.load(ASSET_ASTRONAUT), scene: asset_server.load(ASSET_ASTRONAUT),
..default() ..default()
}); },
));
// Space is DARK // Space is DARK
ambient_light.brightness = 0.0; ambient_light.brightness = 0.0;