outfly/src/chat.rs

144 lines
4 KiB
Rust

extern crate yaml_rust;
use bevy::prelude::*;
use yaml_rust::{Yaml, YamlLoader};
use crate::{audio, world};
pub const CHATS: &[&str] = &[
include_str!("chats/serenity.yaml"),
include_str!("chats/startrans.yaml"),
];
pub const TOKEN_CHAT: &str = "chat";
pub struct ChatPlugin;
impl Plugin for ChatPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, load_chats);
app.add_systems(Update, (
handle_new_conversations,
handle_chat_events.after(handle_new_conversations),
));
app.add_event::<StartConversationEvent>();
app.add_event::<ChatEvent>();
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;
#[derive(Resource)]
pub struct ChatDB(Vec<Yaml>);
impl ChatDB {
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() {
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);
}
}
}
if let Some(result) = found {
return Ok(result);
}
return Err(format!("No chat with the conversation ID `{id}` was found."));
}
pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {
event.send(ChatEvent::DespawnAllChoices);
chat.position += 1;
}
}
#[derive(Component)]
#[derive(Clone)]
pub struct Talker {
pub conv_id: String,
pub name: Option<String>,
pub pronoun: Option<String>,
}
#[derive(Event)]
pub struct StartConversationEvent {
pub talker: Talker,
}
#[derive(Event)]
pub enum ChatEvent {
DespawnAllChoices,
//ShowMessage(String),
//SpawnChoice(String),
//Script(String, String, String),
}
pub fn load_chats(mut chatdb: ResMut<ChatDB>) {
for chat_yaml in CHATS {
if let Ok(mut yaml_data) = YamlLoader::load_from_str(chat_yaml) {
chatdb.0.append(&mut yaml_data);
} else {
error!("Could not load chat definitions. Validate files in `src/chats/` path.");
}
}
}
pub fn handle_new_conversations(
mut commands: Commands,
mut er_conv: EventReader<StartConversationEvent>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
mut ew_chatevent: EventWriter<ChatEvent>,
chatdb: Res<ChatDB>,
q_chats: Query<&Chat>,
time: Res<Time>,
) {
for event in er_conv.read() {
if !q_chats.is_empty() {
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
return;
}
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,
));
}
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();
}
}
}
}
}