implement showing all first-level messages of a chat

This commit is contained in:
yuni 2024-04-13 01:21:38 +02:00
parent b41891ba3b
commit eff6306a93
3 changed files with 47 additions and 5 deletions

View file

@ -9,14 +9,17 @@ pub const CHATS: &[&str] = &[
]; ];
pub const TOKEN_CHAT: &str = "chat"; pub const TOKEN_CHAT: &str = "chat";
pub const TALKER_SPEED_FACTOR: f32 = 1.0 / 17.0;
pub const CHAT_SPEED_MIN_LEN: f32 = 40.0;
pub struct ChatPlugin; pub struct ChatPlugin;
impl Plugin for ChatPlugin { impl Plugin for ChatPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, load_chats); app.add_systems(Startup, load_chats);
app.add_systems(Update, ( app.add_systems(Update, (
handle_new_conversations, handle_new_conversations.before(handle_chat_events),
handle_chat_events.after(handle_new_conversations), handle_chat_events,
handle_chat_timer.before(handle_chat_events),
)); ));
app.add_event::<StartConversationEvent>(); app.add_event::<StartConversationEvent>();
app.add_event::<ChatEvent>(); app.add_event::<ChatEvent>();
@ -29,6 +32,7 @@ pub struct Chat {
pub id: usize, pub id: usize,
pub position: usize, pub position: usize,
pub timer: f64, pub timer: f64,
pub talker: Talker,
} }
#[derive(Component)] #[derive(Component)]
@ -66,9 +70,17 @@ impl ChatDB {
pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) { pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {
event.send(ChatEvent::DespawnAllChoices); event.send(ChatEvent::DespawnAllChoices);
let conv = &self.0[chat.id]; let conv = &self.0[chat.id].as_vec();
if conv.is_none() {
return;
}
let conv = conv.unwrap();
chat.position += 1; chat.position += 1;
dbg!(chat.position); if chat.position >= conv.len() {
event.send(ChatEvent::DisplayMessage("Disconnected.".to_string()));
event.send(ChatEvent::DespawnAllChats);
return;
}
if let Some(message) = conv[chat.position].as_str() { if let Some(message) = conv[chat.position].as_str() {
event.send(ChatEvent::DisplayMessage(message.to_string())); event.send(ChatEvent::DisplayMessage(message.to_string()));
} }
@ -81,6 +93,7 @@ pub struct Talker {
pub conv_id: String, pub conv_id: String,
pub name: Option<String>, pub name: Option<String>,
pub pronoun: Option<String>, pub pronoun: Option<String>,
pub talking_speed: f32,
} }
#[derive(Event)] #[derive(Event)]
@ -91,6 +104,7 @@ pub struct StartConversationEvent {
#[derive(Event)] #[derive(Event)]
pub enum ChatEvent { pub enum ChatEvent {
DespawnAllChoices, DespawnAllChoices,
DespawnAllChats,
DisplayMessage(String), DisplayMessage(String),
//SpawnChoice(String), //SpawnChoice(String),
//Script(String, String, String), //Script(String, String, String),
@ -125,6 +139,7 @@ pub fn handle_new_conversations(
id: chat_id, id: chat_id,
position: 0, position: 0,
timer: time.elapsed_seconds_f64(), timer: time.elapsed_seconds_f64(),
talker: event.talker.clone(),
}; };
chatdb.advance_chat(&mut chat, &mut ew_chatevent); chatdb.advance_chat(&mut chat, &mut ew_chatevent);
commands.spawn(( commands.spawn((
@ -139,21 +154,48 @@ pub fn handle_new_conversations(
} }
} }
pub fn handle_chat_timer(
time: Res<Time>,
chatdb: Res<ChatDB>,
mut q_chats: Query<&mut Chat>,
mut ew_chatevent: EventWriter<ChatEvent>,
) {
let now = time.elapsed_seconds_f64();
for mut chat in &mut q_chats {
if now >= chat.timer {
chatdb.advance_chat(&mut chat, &mut ew_chatevent);
}
}
}
pub fn handle_chat_events( pub fn handle_chat_events(
mut commands: Commands, mut commands: Commands,
mut er_chatevent: EventReader<ChatEvent>, mut er_chatevent: EventReader<ChatEvent>,
mut log: ResMut<hud::Log>, mut log: ResMut<hud::Log>,
q_choices: Query<Entity, With<Choice>>, q_choices: Query<Entity, With<Choice>>,
mut q_chats: Query<(Entity, &mut Chat)>,
time: Res<Time>,
) { ) {
for event in er_chatevent.read() { for event in er_chatevent.read() {
let now = time.elapsed_seconds_f64();
let chat_maybe = q_chats.get_single_mut();
if chat_maybe.is_err() {
return;
}
let (chat_entity, mut chat) = chat_maybe.unwrap();
match event { match event {
ChatEvent::DespawnAllChoices => { ChatEvent::DespawnAllChoices => {
for entity in &q_choices { for entity in &q_choices {
commands.entity(entity).despawn(); commands.entity(entity).despawn();
} }
} }
ChatEvent::DespawnAllChats => {
commands.entity(chat_entity).despawn();
}
ChatEvent::DisplayMessage(message) => { ChatEvent::DisplayMessage(message) => {
log.chat(message.into(), "Someone".to_string()); log.chat(message.into(), "Someone".to_string());
chat.timer = now + ((message.len() as f32).max(CHAT_SPEED_MIN_LEN) * TALKER_SPEED_FACTOR * chat.talker.talking_speed) as f64;
} }
} }
} }

View file

@ -17,7 +17,6 @@
- chat: Icarus - chat: Icarus
- TEST MESSAGE
- if talkcount > 0: - if talkcount > 0:
- "Oh, hi again! How are you?" - "Oh, hi again! How are you?"
- goto: howru - goto: howru

View file

@ -695,6 +695,7 @@ fn spawn_entities(
conv_id: state.chat.clone(), conv_id: state.chat.clone(),
name: state.name.clone(), name: state.name.clone(),
pronoun: Some(state.pronoun.clone()), pronoun: Some(state.pronoun.clone()),
talking_speed: 1.0,
}); });
} }
if state.is_vehicle { if state.is_vehicle {