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