fix ending of chats

This commit is contained in:
yuni 2024-10-26 02:19:27 +02:00
parent 092feccc84
commit b2f28c36f1
3 changed files with 69 additions and 27 deletions

View file

@ -108,6 +108,7 @@ pub struct Chat {
pub internal_id: usize, pub internal_id: usize,
pub position: ChatPos, pub position: ChatPos,
pub timer: f64, pub timer: f64,
pub message_open: bool,
pub talker: Talker, pub talker: Talker,
} }
@ -476,6 +477,7 @@ impl ChatDB {
let mut processed_a_choice = false; let mut processed_a_choice = false;
match current_item { match current_item {
Some(Value::String(message)) => { Some(Value::String(message)) => {
chat.message_open = true;
event.send(ChatEvent::SpawnMessage( event.send(ChatEvent::SpawnMessage(
message.to_string(), message.to_string(),
hud::LogLevel::Chat, hud::LogLevel::Chat,
@ -523,23 +525,35 @@ impl ChatDB {
let key = key.as_str(); let key = key.as_str();
match (key, value) { match (key, value) {
(Some(TOKEN_MSG), Value::String(message)) => { (Some(TOKEN_MSG), Value::String(message)) => {
let loglevel = hud::LogLevel::Chat;
if loglevel.is_subtitle() {
chat.message_open = true;
}
event.send(ChatEvent::SpawnMessage( event.send(ChatEvent::SpawnMessage(
message.to_string(), message.to_string(),
hud::LogLevel::Chat, loglevel,
sound.clone(), sound.clone(),
)); ));
} }
(Some(TOKEN_SYSTEM), Value::String(message)) => { (Some(TOKEN_SYSTEM), Value::String(message)) => {
let loglevel = hud::LogLevel::Info;
if loglevel.is_subtitle() {
chat.message_open = true;
}
event.send(ChatEvent::SpawnMessage( event.send(ChatEvent::SpawnMessage(
message.to_string(), message.to_string(),
hud::LogLevel::Info, loglevel,
sound.clone(), sound.clone(),
)); ));
} }
(Some(TOKEN_WARN), Value::String(message)) => { (Some(TOKEN_WARN), Value::String(message)) => {
let loglevel = hud::LogLevel::Warning;
if loglevel.is_subtitle() {
chat.message_open = true;
}
event.send(ChatEvent::SpawnMessage( event.send(ChatEvent::SpawnMessage(
message.to_string(), message.to_string(),
hud::LogLevel::Warning, loglevel,
sound.clone(), sound.clone(),
)); ));
} }
@ -578,11 +592,7 @@ impl ChatDB {
} }
None => { None => {
if chat.position.len() == 0 { if chat.position.len() == 0 {
event.send(ChatEvent::SpawnMessage( // Here it used to spawn the "Disconnected" message.
"Disconnected.".to_string(),
hud::LogLevel::Info,
DEFAULT_SOUND.to_string(),
));
event.send(ChatEvent::DespawnAllChats); event.send(ChatEvent::DespawnAllChats);
} }
} }
@ -639,8 +649,7 @@ impl ChatDB {
// Add a "Continue" choice to any conversation line that doesn't define any choices // Add a "Continue" choice to any conversation line that doesn't define any choices
let choices_available = key > 0; let choices_available = key > 0;
let at_exit = chat.position.is_empty(); if chat.message_open && !choices_available {
if !choices_available && !at_exit {
let goto: Vec<usize> = chat.position.clone(); let goto: Vec<usize> = chat.position.clone();
event.send(ChatEvent::SpawnChoice( event.send(ChatEvent::SpawnChoice(
String::from(TEXT_CONTINUE), String::from(TEXT_CONTINUE),
@ -688,6 +697,7 @@ pub fn handle_new_conversations(
let mut chat = Chat { let mut chat = Chat {
internal_id: chat_id, internal_id: chat_id,
position: vec![0], position: vec![0],
message_open: false,
timer: time.elapsed_seconds_f64(), timer: time.elapsed_seconds_f64(),
talker: event.talker.clone(), talker: event.talker.clone(),
}; };
@ -745,6 +755,12 @@ pub fn handle_chat_events(
} }
ChatEvent::DespawnAllChats => { ChatEvent::DespawnAllChats => {
commands.entity(chat_entity).despawn(); commands.entity(chat_entity).despawn();
// also despawn all choices (keep in sync with DespawnAllChoices handler)
for entity in &q_choices {
commands.entity(entity).despawn();
}
choice_key = 0;
} }
ChatEvent::SpawnMessage(message, level, sound) => { ChatEvent::SpawnMessage(message, level, sound) => {
match level { match level {
@ -801,6 +817,8 @@ pub fn handle_chat_events(
choice_key += 1; choice_key += 1;
if ENABLE_CHOICE_TIMER && !nowait { if ENABLE_CHOICE_TIMER && !nowait {
chat.timer = now + CHOICE_TIMER / settings.chat_speed as f64; chat.timer = now + CHOICE_TIMER / settings.chat_speed as f64;
} else {
chat.timer = f64::INFINITY;
} }
} }
ChatEvent::RunScript(script) => { ChatEvent::RunScript(script) => {
@ -830,7 +848,8 @@ fn handle_reply_keys(
settings: Res<var::Settings>, settings: Res<var::Settings>,
q_choices: Query<&Choice>, q_choices: Query<&Choice>,
mut q_chats: Query<&mut Chat>, mut q_chats: Query<&mut Chat>,
mut evwriter_sfx: EventWriter<audio::PlaySfxEvent>, mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
mut ew_chat: EventWriter<ChatEvent>,
time: Res<Time>, time: Res<Time>,
) { ) {
let mut selected_choice: usize = 0; let mut selected_choice: usize = 0;
@ -838,10 +857,18 @@ fn handle_reply_keys(
if keyboard_input.just_pressed(key) { if keyboard_input.just_pressed(key) {
for choice in &q_choices { for choice in &q_choices {
if choice.key == selected_choice { if choice.key == selected_choice {
if choice.goto.is_empty() {
// Conversation ended. No need to advance the chat.
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
ew_chat.send(ChatEvent::DespawnAllChats);
break 'outer;
}
if let Ok(mut chat) = q_chats.get_single_mut() { if let Ok(mut chat) = q_chats.get_single_mut() {
evwriter_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); // Advance the chat.
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
chat.timer = time.elapsed_seconds_f64(); chat.timer = time.elapsed_seconds_f64();
chat.position = choice.goto.clone(); chat.position = choice.goto.clone();
chat.message_open = false;
} }
break 'outer; break 'outer;
} }

View file

@ -470,8 +470,8 @@
- You age slower, can fly further, time dilation from special relativity. - You age slower, can fly further, time dilation from special relativity.
- label: skiprelativityintro - label: skiprelativityintro
- Given the right speed, you age so slowly that you can literally go anywhere you want. - Given the right speed, you age so slowly that you can literally go anywhere you want.
- Any *time* you want.
- set: timetravel - set: timetravel
- Any *time* you want.
- Time travel! Only into the future though, of course.: - Time travel! Only into the future though, of course.:
- Exactly. - Exactly.
- goto: continuemonologue - goto: continuemonologue

View file

@ -213,6 +213,7 @@ pub enum Avatar {
Armor, Armor,
} }
#[derive(Clone)]
pub enum LogLevel { pub enum LogLevel {
Achievement, Achievement,
Always, Always,
@ -224,7 +225,18 @@ pub enum LogLevel {
//Ping, //Ping,
} }
struct Message { impl LogLevel {
pub fn is_subtitle(&self) -> bool {
match self {
LogLevel::Chat => true,
LogLevel::Info => true,
_ => false,
}
}
}
#[derive(Clone)]
pub struct Message {
text: String, text: String,
sender: String, sender: String,
level: LogLevel, level: LogLevel,
@ -310,6 +322,20 @@ impl Log {
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.logs.clear(); self.logs.clear();
} }
pub fn get_subtitle_line(&self) -> Option<Message> {
let messages: Vec<&Message> = self
.logs
.iter()
.filter(|msg: &&Message| msg.level.is_subtitle())
.rev()
.take(1)
.collect();
if messages.len() > 0 {
return Some(messages[0].clone());
}
return None;
}
} }
pub fn setup( pub fn setup(
@ -1016,19 +1042,8 @@ fn update_hud(
// Display the last chat line as "subtitles" // Display the last chat line as "subtitles"
if !q_chat.is_empty() { if !q_chat.is_empty() {
let messages: Vec<&Message> = log if let Some(message) = log.get_subtitle_line() {
.logs node_currentline.sections[0].value = message.format();
.iter()
.filter(|msg: &&Message| match msg.level {
LogLevel::Chat => true,
LogLevel::Info => true,
_ => false,
})
.rev()
.take(1)
.collect();
if messages.len() > 0 {
node_currentline.sections[0].value = messages[0].format();
} else { } else {
node_currentline.sections[0].value = "".to_string(); node_currentline.sections[0].value = "".to_string();
} }