implement specifying custom chat sounds per message with "sound" token

This commit is contained in:
yuni 2024-04-13 23:35:59 +02:00
parent 4b4be21ade
commit 39a74582fc

View file

@ -23,6 +23,7 @@ pub const TOKEN_SCRIPT: &str = "script";
pub const TOKEN_SOUND: &str = "sound";
pub const TOKEN_GOTO_EXIT: &str = "EXIT";
pub const DEFAULT_SOUND: &str = "chat";
pub const MAX_BRANCH_DEPTH: usize = 64;
pub const CHOICE_TIMER: f64 = 40.0 * settings::DEFAULT_CHAT_SPEED as f64;
@ -100,7 +101,7 @@ pub struct ChatScriptEvent(String);
pub enum ChatEvent {
DespawnAllChoices,
DespawnAllChats,
SpawnMessage(String, hud::LogLevel),
SpawnMessage(String, hud::LogLevel, String),
SpawnChoice(String, usize, ChatPos),
RunScript(String),
Sleep(f64),
@ -297,31 +298,60 @@ impl ChatDB {
let mut add_choices = true;
match current_item {
Some(Value::String(message)) => {
event.send(ChatEvent::SpawnMessage(message.to_string(), hud::LogLevel::Chat));
event.send(ChatEvent::SpawnMessage(message.to_string(),
hud::LogLevel::Chat, DEFAULT_SOUND.to_string()));
}
Some(Value::Mapping(map)) => {
if let Some(_) = self.search_choice(Some(&Value::Mapping(map.clone()))) {
add_choices = false;
}
for (key, value) in map {
let mut sound = "chat".to_string();
// We're going through the list of keys/values multiple times
// to ensure that dependencies for certain commands are available
// First pass
for (key, value) in &map {
let key = key.as_str();
match (key, value) {
(Some(TOKEN_SET), _) => {}
(Some(TOKEN_IF), _) => {}
(Some(TOKEN_SOUND), Value::String(sound_name)) => {
sound = sound_name.clone();
}
_ => {}
}
}
// Second pass
for (key, value) in &map {
let key = key.as_str();
match (key, value) {
(Some(TOKEN_CHAT), _) => {}
(Some(TOKEN_LABEL), _) => {}
(Some(TOKEN_MSG), Value::String(message)) => {
event.send(ChatEvent::SpawnMessage(
message.to_string(), hud::LogLevel::Chat));
message.to_string(), hud::LogLevel::Chat, sound.clone()));
}
(Some(TOKEN_SYSTEM), Value::String(message)) => {
event.send(ChatEvent::SpawnMessage(
message.to_string(), hud::LogLevel::Info));
message.to_string(), hud::LogLevel::Info, sound.clone()));
}
(Some(TOKEN_WARN), Value::String(message)) => {
event.send(ChatEvent::SpawnMessage(
message.to_string(), hud::LogLevel::Warning));
message.to_string(), hud::LogLevel::Warning, sound.clone()));
}
_ => {}
}
}
// Third pass
for (key, value) in &map {
let key = key.as_str();
match (key, value) {
(Some(TOKEN_SLEEP), Value::Number(time)) => {
if let Some(time_f64) = time.as_f64() {
event.send(ChatEvent::Sleep(time_f64));
}
}
(Some(TOKEN_SET), _) => {}
(Some(TOKEN_IF), _) => {}
(Some(TOKEN_GOTO), Value::String(label)) => {
match self.search_label(chat.id, &label) {
Some(pos) => {
@ -333,22 +363,16 @@ impl ChatDB {
}
}
(Some(TOKEN_SCRIPT), Value::String(script)) => {
event.send(ChatEvent::RunScript(script));
}
(Some(TOKEN_SLEEP), Value::Number(time)) => {
if let Some(time_f64) = time.as_f64() {
event.send(ChatEvent::Sleep(time_f64));
}
}
_ => {
event.send(ChatEvent::RunScript(script.clone()));
}
_ => {}
}
}
}
None => {
if chat.position.len() == 0 {
event.send(ChatEvent::SpawnMessage(
"Disconnected.".to_string(), hud::LogLevel::Info));
event.send(ChatEvent::SpawnMessage("Disconnected.".to_string(),
hud::LogLevel::Info, DEFAULT_SOUND.to_string()));
event.send(ChatEvent::DespawnAllChats);
}
}
@ -471,7 +495,7 @@ pub fn handle_chat_events(
ChatEvent::DespawnAllChats => {
commands.entity(chat_entity).despawn();
}
ChatEvent::SpawnMessage(message, level) => {
ChatEvent::SpawnMessage(message, level, sound) => {
match level {
hud::LogLevel::Chat => {
log.chat(message.into(), chat.talker.name.clone().unwrap_or("".to_string()));
@ -485,7 +509,7 @@ pub fn handle_chat_events(
}
chat.timer = now + ((message.len() as f32).max(CHAT_SPEED_MIN_LEN) * TALKER_SPEED_FACTOR * chat.talker.talking_speed / settings.chat_speed) as f64;
let sfx = audio::str2sfx("chat");
let sfx = audio::str2sfx(sound);
ew_sfx.send(audio::PlaySfxEvent(sfx));
}
ChatEvent::SpawnChoice(replytext, key, goto) => {