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

View file

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

View file

@ -213,6 +213,7 @@ pub enum Avatar {
Armor,
}
#[derive(Clone)]
pub enum LogLevel {
Achievement,
Always,
@ -224,7 +225,18 @@ pub enum LogLevel {
//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,
sender: String,
level: LogLevel,
@ -310,6 +322,20 @@ impl Log {
pub fn clear(&mut self) {
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(
@ -1016,19 +1042,8 @@ fn update_hud(
// Display the last chat line as "subtitles"
if !q_chat.is_empty() {
let messages: Vec<&Message> = log
.logs
.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();
if let Some(message) = log.get_subtitle_line() {
node_currentline.sections[0].value = message.format();
} else {
node_currentline.sections[0].value = "".to_string();
}