outfly/src/chat.rs

152 lines
4.2 KiB
Rust
Raw Normal View History

2024-04-12 19:26:23 +00:00
extern crate yaml_rust;
use bevy::prelude::*;
2024-04-12 21:03:46 +00:00
use yaml_rust::{Yaml, YamlLoader};
use crate::{audio, world};
2024-04-12 19:34:55 +00:00
pub const CHATS: &[&str] = &[
include_str!("chats/serenity.yaml"),
include_str!("chats/startrans.yaml"),
];
2024-04-12 21:03:46 +00:00
pub const TOKEN_CHAT: &str = "chat";
pub struct ChatPlugin;
impl Plugin for ChatPlugin {
fn build(&self, app: &mut App) {
2024-04-12 19:26:23 +00:00
app.add_systems(Startup, load_chats);
2024-04-12 21:03:46 +00:00
app.add_systems(Update, (
handle_new_conversations,
handle_chat_events.after(handle_new_conversations),
2024-04-12 21:03:46 +00:00
));
app.add_event::<StartConversationEvent>();
app.add_event::<ChatEvent>();
2024-04-12 19:34:55 +00:00
app.insert_resource(ChatDB(Vec::new()));
}
}
#[derive(Component)]
pub struct Chat {
pub id: u32,
pub position: u32,
pub timer: f64,
}
#[derive(Component)]
pub struct Choice;
2024-04-12 22:11:32 +00:00
// This is the only place where any YAML interaction should be happening.
2024-04-12 21:03:46 +00:00
#[derive(Resource)]
pub struct ChatDB(Vec<Yaml>);
impl ChatDB {
2024-04-12 22:11:32 +00:00
pub fn load_from_str(&mut self, yaml_string: &str) -> Result<(), ()> {
if let Ok(mut yaml_data) = YamlLoader::load_from_str(yaml_string) {
self.0.append(&mut yaml_data);
return Ok(());
}
return Err(());
}
2024-04-12 21:03:46 +00:00
pub fn get_chat_by_id(&self, id: &String) -> Result<u32, String> {
let mut found: Option<u32> = None;
for (index, object_yaml) in self.0.iter().enumerate() {
2024-04-12 21:18:07 +00:00
if let Some(chat_id) = object_yaml[0][TOKEN_CHAT].as_str() {
if chat_id == id {
if found.is_some() {
return Err("Found multiple chats with the same id!".to_string());
}
found = Some(index as u32);
}
2024-04-12 21:03:46 +00:00
}
}
if let Some(result) = found {
return Ok(result);
}
return Err(format!("No chat with the conversation ID `{id}` was found."));
}
2024-04-12 22:11:32 +00:00
pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {
event.send(ChatEvent::DespawnAllChoices);
chat.position += 1;
}
2024-04-12 21:03:46 +00:00
}
#[derive(Component)]
#[derive(Clone)]
pub struct Talker {
pub conv_id: String,
2024-04-12 21:03:46 +00:00
pub name: Option<String>,
pub pronoun: Option<String>,
}
#[derive(Event)]
pub struct StartConversationEvent {
pub talker: Talker,
}
2024-04-12 19:26:23 +00:00
#[derive(Event)]
pub enum ChatEvent {
DespawnAllChoices,
//ShowMessage(String),
//SpawnChoice(String),
//Script(String, String, String),
}
2024-04-12 19:34:55 +00:00
pub fn load_chats(mut chatdb: ResMut<ChatDB>) {
for chat_yaml in CHATS {
2024-04-12 22:11:32 +00:00
if chatdb.load_from_str(chat_yaml).is_err() {
2024-04-12 19:34:55 +00:00
error!("Could not load chat definitions. Validate files in `src/chats/` path.");
}
2024-04-12 19:26:23 +00:00
}
}
2024-04-12 21:03:46 +00:00
pub fn handle_new_conversations(
mut commands: Commands,
2024-04-12 21:03:46 +00:00
mut er_conv: EventReader<StartConversationEvent>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
mut ew_chatevent: EventWriter<ChatEvent>,
2024-04-12 21:03:46 +00:00
chatdb: Res<ChatDB>,
q_chats: Query<&Chat>,
time: Res<Time>,
2024-04-12 21:03:46 +00:00
) {
for event in er_conv.read() {
if !q_chats.is_empty() {
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
return;
}
2024-04-12 21:03:46 +00:00
match (*chatdb).get_chat_by_id(&event.talker.conv_id) {
Ok(chat_id) => {
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
let mut chat = Chat {
id: chat_id,
position: 1, // not 0, since the first item is always the chat name
timer: time.elapsed_seconds_f64(),
};
chatdb.advance_chat(&mut chat, &mut ew_chatevent);
commands.spawn((
chat,
world::DespawnOnPlayerDeath,
));
2024-04-12 21:03:46 +00:00
}
Err(error) => {
error!("Error while looking for chat ID: {error}");
}
}
}
}
pub fn handle_chat_events(
mut commands: Commands,
mut er_chatevent: EventReader<ChatEvent>,
q_choices: Query<Entity, With<Choice>>,
) {
for event in er_chatevent.read() {
match event {
ChatEvent::DespawnAllChoices => {
for entity in &q_choices {
commands.entity(entity).despawn();
}
}
}
}
}