WIP choices

This commit is contained in:
yuni 2024-04-13 18:57:23 +02:00
parent e8302833da
commit 8df6914dba

View file

@ -40,10 +40,10 @@ 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_reply_keys.before(handle_chat_timer),
handle_chat_timer.before(handle_chat_events),
handle_new_conversations.before(handle_chat_events), handle_new_conversations.before(handle_chat_events),
handle_chat_events, handle_chat_events,
handle_reply_keys,
handle_chat_timer.before(handle_chat_events),
)); ));
app.add_event::<StartConversationEvent>(); app.add_event::<StartConversationEvent>();
app.add_event::<ChatEvent>(); app.add_event::<ChatEvent>();
@ -51,10 +51,12 @@ impl Plugin for ChatPlugin {
} }
} }
type ChatPos = Vec<usize>;
#[derive(Component)] #[derive(Component)]
pub struct Chat { pub struct Chat {
pub id: usize, pub id: usize,
pub position: Vec<usize>, pub position: ChatPos,
pub timer: f64, pub timer: f64,
pub talker: Talker, pub talker: Talker,
} }
@ -63,6 +65,7 @@ pub struct Chat {
pub struct Choice { pub struct Choice {
pub text: String, pub text: String,
pub key: usize, pub key: usize,
pub goto: ChatPos,
} }
// This is the only place where any YAML interaction should be happening. // This is the only place where any YAML interaction should be happening.
@ -131,15 +134,23 @@ impl ChatDB {
chat.position[index] += 1; chat.position[index] += 1;
while chat.position.len() > 0 { while chat.position.len() > 0 {
if let None = self.at(chat.id, &chat.position) { dbg!(self.at(chat.id, &chat.position));
chat.position.pop(); match self.at(chat.id, &chat.position) {
None => {
dbg!("Pop.");
chat.position.pop();
let index = chat.position.len() - 1;
chat.position[index] += 1;
},
Some(_) => {
break;
}
} }
let index = chat.position.len() - 1;
chat.position[index] += 1;
} }
return true; return true;
} }
// Note that this (intentionally) may result in a pointer that's out of bounds.
fn pointer_lookahead(&self, position: &mut Vec<usize>) { fn pointer_lookahead(&self, position: &mut Vec<usize>) {
let index = position.len() - 1; let index = position.len() - 1;
position[index] += 1; position[index] += 1;
@ -152,7 +163,10 @@ impl ChatDB {
let mut pointer: Option<&Value> = Some(&self.0[id]); let mut pointer: Option<&Value> = Some(&self.0[id]);
for index in position { for index in position {
if let Some(Value::Sequence(seq)) = self.0.get(id) { if let Some(Value::Sequence(seq)) = self.0.get(id) {
pointer = Some(&seq[*index]); // TODO: handle mappings
if let Some(value) = seq.get(*index) {
pointer = Some(value);
}
} else { } else {
return None; return None;
} }
@ -166,7 +180,6 @@ impl ChatDB {
if conv.is_none() { if conv.is_none() {
return; return;
} }
let conv = conv.unwrap();
// Handle next entry in the chat list // Handle next entry in the chat list
let mut is_skipping_through = false; let mut is_skipping_through = false;
@ -178,7 +191,7 @@ impl ChatDB {
else if let Some(_) = self.search_choice(self.at(chat.id, &chat.position)) { else if let Some(_) = self.search_choice(self.at(chat.id, &chat.position)) {
is_skipping_through = true; is_skipping_through = true;
} }
else if let Some(message) = conv[chat.position[0]].as_str() { else if let Some(Value::String(message)) = self.at(chat.id, &chat.position) {
event.send(ChatEvent::SpawnMessage(message.to_string())); event.send(ChatEvent::SpawnMessage(message.to_string()));
} }
@ -191,7 +204,9 @@ impl ChatDB {
break; break;
} }
if let Some(choice) = self.search_choice(self.at(chat.id, &pos)) { if let Some(choice) = self.search_choice(self.at(chat.id, &pos)) {
event.send(ChatEvent::SpawnChoice(choice, key)); let mut goto: Vec<usize> = pos.clone();
goto.push(0);
event.send(ChatEvent::SpawnChoice(choice, key, goto));
key += 1; key += 1;
} }
else { else {
@ -221,7 +236,7 @@ pub enum ChatEvent {
DespawnAllChoices, DespawnAllChoices,
DespawnAllChats, DespawnAllChats,
SpawnMessage(String), SpawnMessage(String),
SpawnChoice(String, usize), SpawnChoice(String, usize, ChatPos),
//Script(String, String, String), //Script(String, String, String),
} }
@ -313,12 +328,13 @@ pub fn handle_chat_events(
log.chat(message.into(), chat.talker.name.clone().unwrap_or(NAME_FALLBACK.to_string())); log.chat(message.into(), chat.talker.name.clone().unwrap_or(NAME_FALLBACK.to_string()));
chat.timer = now + ((message.len() as f32).max(CHAT_SPEED_MIN_LEN) * TALKER_SPEED_FACTOR * chat.talker.talking_speed / settings.chat_speed) as f64; chat.timer = now + ((message.len() as f32).max(CHAT_SPEED_MIN_LEN) * TALKER_SPEED_FACTOR * chat.talker.talking_speed / settings.chat_speed) as f64;
} }
ChatEvent::SpawnChoice(replytext, key) => { ChatEvent::SpawnChoice(replytext, key, goto) => {
commands.spawn(( commands.spawn((
world::DespawnOnPlayerDeath, world::DespawnOnPlayerDeath,
Choice { Choice {
text: replytext.into(), text: replytext.into(),
key: *key, key: *key,
goto: goto.clone(),
} }
)); ));
chat.timer = now + CHOICE_TIMER / settings.chat_speed as f64; chat.timer = now + CHOICE_TIMER / settings.chat_speed as f64;
@ -331,20 +347,22 @@ fn handle_reply_keys(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
settings: ResMut<settings::Settings>, settings: ResMut<settings::Settings>,
q_choices: Query<&Choice>, q_choices: Query<&Choice>,
mut q_chats: Query<&mut Chat>,
//mut evwriter_sendmsg: EventWriter<SendMessageEvent>, //mut evwriter_sendmsg: EventWriter<SendMessageEvent>,
mut evwriter_sfx: EventWriter<audio::PlaySfxEvent>, mut evwriter_sfx: EventWriter<audio::PlaySfxEvent>,
time: Res<Time>,
) { ) {
let mut selected_choice: usize = 0; let mut selected_choice: usize = 0;
'outer: for key in settings.get_reply_keys() { 'outer: for key in settings.get_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 {
evwriter_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); if let Ok(mut chat) = q_chats.get_single_mut() {
// evwriter_sendmsg.send(SendMessageEvent { evwriter_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
// conv_id: choice.conv_id.clone(), chat.timer = time.elapsed_seconds_f64();
// conv_label: choice.conv_label.clone(), chat.position = choice.goto.clone();
// text: choice.text.clone(), info!("GOTO {:?}", &chat.position);
// }); }
break 'outer; break 'outer;
} }
} }