start implementing ChatDB.advance_chat()
This commit is contained in:
parent
ca709080eb
commit
2aaecdc113
44
src/chat.rs
44
src/chat.rs
|
@ -16,8 +16,10 @@ impl Plugin for ChatPlugin {
|
|||
app.add_systems(Startup, load_chats);
|
||||
app.add_systems(Update, (
|
||||
handle_new_conversations,
|
||||
handle_chat_events.after(handle_new_conversations),
|
||||
));
|
||||
app.add_event::<StartConversationEvent>();
|
||||
app.add_event::<ChatEvent>();
|
||||
app.insert_resource(ChatDB(Vec::new()));
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +31,9 @@ pub struct Chat {
|
|||
pub timer: f64,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Choice;
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct ChatDB(Vec<Yaml>);
|
||||
impl ChatDB {
|
||||
|
@ -49,6 +54,10 @@ impl ChatDB {
|
|||
}
|
||||
return Err(format!("No chat with the conversation ID `{id}` was found."));
|
||||
}
|
||||
pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {
|
||||
event.send(ChatEvent::DespawnAllChoices);
|
||||
chat.position += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
|
@ -64,6 +73,14 @@ pub struct StartConversationEvent {
|
|||
pub talker: Talker,
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
pub enum ChatEvent {
|
||||
DespawnAllChoices,
|
||||
//ShowMessage(String),
|
||||
//SpawnChoice(String),
|
||||
//Script(String, String, String),
|
||||
}
|
||||
|
||||
pub fn load_chats(mut chatdb: ResMut<ChatDB>) {
|
||||
for chat_yaml in CHATS {
|
||||
if let Ok(mut yaml_data) = YamlLoader::load_from_str(chat_yaml) {
|
||||
|
@ -78,24 +95,27 @@ pub fn handle_new_conversations(
|
|||
mut commands: Commands,
|
||||
mut er_conv: EventReader<StartConversationEvent>,
|
||||
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
||||
mut ew_chatevent: EventWriter<ChatEvent>,
|
||||
chatdb: Res<ChatDB>,
|
||||
q_chats: Query<&Chat>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for event in er_conv.read() {
|
||||
if !q_chats.is_empty() {
|
||||
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
|
||||
return;
|
||||
}
|
||||
for event in er_conv.read() {
|
||||
match (*chatdb).get_chat_by_id(&event.talker.conv_id) {
|
||||
Ok(chat_id) => {
|
||||
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
|
||||
commands.spawn((
|
||||
Chat {
|
||||
let mut chat = Chat {
|
||||
id: chat_id,
|
||||
position: 1, // not 0, since the first item is always the chat name
|
||||
timer: time.elapsed_seconds_f64(),
|
||||
},
|
||||
};
|
||||
chatdb.advance_chat(&mut chat, &mut ew_chatevent);
|
||||
commands.spawn((
|
||||
chat,
|
||||
world::DespawnOnPlayerDeath,
|
||||
));
|
||||
}
|
||||
|
@ -105,3 +125,19 @@ pub fn handle_new_conversations(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_chat_events(
|
||||
mut commands: Commands,
|
||||
mut er_chatevent: EventReader<ChatEvent>,
|
||||
q_choices: Query<Entity, With<Choice>>,
|
||||
) {
|
||||
for event in er_chatevent.read() {
|
||||
match event {
|
||||
ChatEvent::DespawnAllChoices => {
|
||||
for entity in &q_choices {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue