2024-03-17 22:49:50 +00:00
|
|
|
use bevy::prelude::*;
|
2024-03-19 00:24:27 +00:00
|
|
|
use crate::{nature, settings, actor, audio, hud};
|
|
|
|
|
|
|
|
const MIN_INTERACT_DISTANCE: f32 = 30.0;
|
2024-03-17 22:49:50 +00:00
|
|
|
|
|
|
|
pub struct ActorPlugin;
|
|
|
|
impl Plugin for ActorPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.add_systems(FixedUpdate, update);
|
2024-03-19 00:24:27 +00:00
|
|
|
app.add_systems(Update, (
|
|
|
|
handle_conversations,
|
|
|
|
handle_input,
|
|
|
|
));
|
|
|
|
app.add_event::<StartConversationEvent>();
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:24:27 +00:00
|
|
|
#[derive(Event)]
|
|
|
|
pub struct StartConversationEvent {
|
|
|
|
pub conv: String
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:49:50 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Actor {
|
|
|
|
pub hp: f32,
|
|
|
|
pub m: f32, // mass
|
|
|
|
pub v: Vec3, // velocity
|
|
|
|
// TODO: rotation
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Actor {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
hp: 100.0,
|
|
|
|
m: 100.0,
|
|
|
|
v: Vec3::ZERO,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:24:27 +00:00
|
|
|
#[derive(Component)] pub struct Player;
|
|
|
|
#[derive(Component)] pub struct PlayerInConversation;
|
|
|
|
#[derive(Component)] pub struct InConversationWithPlayer;
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Talker {
|
|
|
|
pub conv: String,
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:49:50 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct LifeForm {
|
|
|
|
pub adrenaline: f32,
|
|
|
|
pub adrenaline_baseline: f32,
|
|
|
|
pub adrenaline_jolt: f32,
|
|
|
|
}
|
|
|
|
impl Default for LifeForm { fn default() -> Self { Self {
|
|
|
|
adrenaline: 0.3,
|
|
|
|
adrenaline_baseline: 0.3,
|
|
|
|
adrenaline_jolt: 0.0,
|
|
|
|
}}}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Suit {
|
|
|
|
pub oxygen: f32,
|
|
|
|
pub power: f32,
|
|
|
|
pub oxygen_max: f32,
|
|
|
|
pub power_max: f32,
|
|
|
|
}
|
|
|
|
impl Default for Suit { fn default() -> Self { SUIT_SIMPLE } }
|
|
|
|
|
|
|
|
const SUIT_SIMPLE: Suit = Suit {
|
|
|
|
power: 1e5,
|
|
|
|
power_max: 1e5,
|
2024-03-18 22:53:52 +00:00
|
|
|
oxygen: nature::OXY_D,
|
|
|
|
oxygen_max: nature::OXY_D,
|
2024-03-17 22:49:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn update(
|
|
|
|
time: Res<Time>,
|
|
|
|
mut query: Query<(&mut LifeForm, &mut Suit)>,
|
|
|
|
) {
|
|
|
|
let d = time.delta_seconds();
|
|
|
|
for (mut lifeform, mut suit) in query.iter_mut() {
|
|
|
|
if lifeform.adrenaline_jolt.abs() > 1e-3 {
|
|
|
|
lifeform.adrenaline_jolt *= 0.99;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lifeform.adrenaline_jolt = 0.0
|
|
|
|
}
|
|
|
|
lifeform.adrenaline = (lifeform.adrenaline - 0.0001 + lifeform.adrenaline_jolt * 0.01).clamp(0.0, 1.0);
|
2024-03-18 22:53:52 +00:00
|
|
|
suit.oxygen = (suit.oxygen - nature::OXY_S*d).clamp(0.0, 1.0);
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:24:27 +00:00
|
|
|
pub fn handle_input(
|
|
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
|
|
|
settings: ResMut<settings::Settings>,
|
|
|
|
query: Query<(&Talker, &Transform)>,
|
|
|
|
player: Query<&Transform, With<actor::Player>>,
|
|
|
|
mut ew_conv: EventWriter<StartConversationEvent>,
|
2024-03-19 02:18:16 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
2024-03-19 00:24:27 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
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()});
|
2024-03-19 02:18:16 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::IncomingChatMessage));
|
2024-03-19 00:24:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_conversations(
|
|
|
|
mut er_conv: EventReader<StartConversationEvent>,
|
|
|
|
mut log: ResMut<hud::Log>,
|
|
|
|
) {
|
|
|
|
for my_event in er_conv.read() {
|
2024-03-19 02:18:16 +00:00
|
|
|
log.chat(my_event.conv.clone(), "Alien".to_string());
|
2024-03-19 00:24:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:49:50 +00:00
|
|
|
|
|
|
|
//pub enum SuitSystemHandler {
|
|
|
|
// Heat,
|
|
|
|
// None,
|
|
|
|
//}
|
|
|
|
//#[derive(Component)]
|
|
|
|
//pub struct SuitSystem {
|
|
|
|
// pub name: String,
|
|
|
|
// pub active: bool,
|
|
|
|
// pub power: f32,
|
|
|
|
// pub handler: SuitSystemHandler,
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//impl Default for SuitSystem {
|
|
|
|
// fn default() -> Self {
|
|
|
|
// Self {
|
|
|
|
// name: "Untitled".to_string(),
|
|
|
|
// active: true,
|
|
|
|
// power: 0.0,
|
|
|
|
// handler: SuitSystemHandler::None,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//pub fn setup(
|
|
|
|
// mut commands: Commands,
|
|
|
|
// settings: Res<settings::Settings>,
|
|
|
|
//) {
|
|
|
|
// commands.spawn((
|
|
|
|
// Player,
|
|
|
|
// SuitSystem {
|
|
|
|
// name: "HUD".to_string(),
|
|
|
|
// active: settings.hud_active,
|
|
|
|
// power: -0.05,
|
|
|
|
// ..default()
|
|
|
|
// },
|
|
|
|
// SuitSystem {
|
|
|
|
// name: "Heater".to_string(),
|
|
|
|
// handler: SuitSystemHandler::Heat,
|
|
|
|
// ..default()
|
|
|
|
// }
|
|
|
|
// ));
|
|
|
|
//}
|