implement delays between messages

This commit is contained in:
yuni 2024-03-19 06:01:17 +01:00
parent bac0b59733
commit 029a53c115
2 changed files with 28 additions and 9 deletions

View file

@ -7,7 +7,7 @@
id: "hialien", id: "hialien",
name: "Icarus", name: "Icarus",
label: "INIT", label: "INIT",
delay: 0.0, delay: 3.0,
sound: "ping", sound: "ping",
reply: "Requesting permission to communicate...", reply: "Requesting permission to communicate...",
goto: "requested", goto: "requested",
@ -20,7 +20,7 @@
id: "hialien", id: "hialien",
name: "Icarus", name: "Icarus",
label: "requested", label: "requested",
delay: 1.0, delay: 5.0,
sound: "chat", sound: "chat",
reply: "Oh hey there, didn't even notice you! Was playing some VR game! What's up?", reply: "Oh hey there, didn't even notice you! Was playing some VR game! What's up?",
goto: "reply1", goto: "reply1",
@ -33,9 +33,22 @@
id: "hialien", id: "hialien",
name: "Icarus", name: "Icarus",
label: "reply1", label: "reply1",
delay: 3.0, delay: 1.0,
sound: "chat", sound: "chat",
reply: "Not so chatty, huh? That's ok. See you around.", reply: "Not so chatty, huh? That's ok. See you around.",
goto: "disco",
),
},
),
4294967299: (
components: {
"outfly::actor::ChatBranch": (
id: "hialien",
name: "Icarus",
label: "disco",
delay: 0.0,
sound: "ping",
reply: "Disconnected.",
goto: "EXIT", goto: "EXIT",
), ),
}, },

View file

@ -53,7 +53,7 @@ pub struct ChatBranch {
pub id: String, pub id: String,
pub name: String, pub name: String,
pub label: String, pub label: String,
pub delay: f32, pub delay: f64,
pub sound: String, pub sound: String,
pub reply: String, pub reply: String,
pub goto: String, pub goto: String,
@ -72,7 +72,7 @@ pub struct ChatChoice {
pub struct Chat { pub struct Chat {
pub id: String, pub id: String,
pub label: String, pub label: String,
pub timer: f32, pub timer: f64,
} }
#[derive(Component)] #[derive(Component)]
@ -182,16 +182,14 @@ pub fn handle_input(
pub fn handle_new_conversations( pub fn handle_new_conversations(
mut commands: Commands, mut commands: Commands,
mut er_conv: EventReader<StartConversationEvent>, mut er_conv: EventReader<StartConversationEvent>,
mut log: ResMut<hud::Log>, time: Res<Time>,
) { ) {
for _my_event in er_conv.read() { for _my_event in er_conv.read() {
log.info("Establishing connection with Alien".to_string());
commands.spawn(Chat { commands.spawn(Chat {
id: "hialien".to_string(), id: "hialien".to_string(),
label: "INIT".to_string(), label: "INIT".to_string(),
timer: 0.0, timer: time.elapsed_seconds_f64(),
}); });
//log.chat(my_event.conv.clone(), "Alien".to_string());
break; break;
} }
} }
@ -201,15 +199,20 @@ pub fn handle_conversations(
mut log: ResMut<hud::Log>, mut log: ResMut<hud::Log>,
mut q_conv: Query<(Entity, &mut Chat)>, mut q_conv: Query<(Entity, &mut Chat)>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>, mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
time: Res<Time>,
chat_branches: Query<&ChatBranch>, // TODO: use Table for faster iteration? chat_branches: Query<&ChatBranch>, // TODO: use Table for faster iteration?
//chat_choices: Query<&ChatChoice>, //chat_choices: Query<&ChatChoice>,
) { ) {
let now = time.elapsed_seconds_f64();
for (entity, mut chat) in &mut q_conv { for (entity, mut chat) in &mut q_conv {
if chat.label == "EXIT" { if chat.label == "EXIT" {
debug!("Despawning chat."); debug!("Despawning chat.");
commands.entity(entity).despawn(); commands.entity(entity).despawn();
continue; continue;
} }
if now < chat.timer {
continue;
}
let branches: Vec<&ChatBranch> = chat_branches.iter() let branches: Vec<&ChatBranch> = chat_branches.iter()
.filter(|branch| branch.id == chat.id && branch.label == chat.label) .filter(|branch| branch.id == chat.id && branch.label == chat.label)
@ -228,6 +231,9 @@ pub fn handle_conversations(
let sfx = audio::str2sfx(branch.sound.as_str()); let sfx = audio::str2sfx(branch.sound.as_str());
ew_sfx.send(audio::PlaySfxEvent(sfx)); ew_sfx.send(audio::PlaySfxEvent(sfx));
} }
info!("<chat.timer={:.2}, branch.delay={:.2}, epoch={:.2}", chat.timer, branch.delay, now);
chat.timer = now + branch.delay;
info!(">chat.timer={:.2}, branch.delay={:.2}, epoch={:.2}", chat.timer, branch.delay, now);
} }
} }