From 3fc3aad82364b66e8b7667afc979c05855a03dd9 Mon Sep 17 00:00:00 2001 From: Jan Hohenheim Date: Wed, 1 Mar 2023 19:45:28 +0100 Subject: [PATCH] Fix dialog choices being triggered all at once --- src/player_control/actions.rs | 54 +++++++++++++++++++++++++-------- src/world_interaction/dialog.rs | 12 +++++--- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/player_control/actions.rs b/src/player_control/actions.rs index f402df11..341a97b7 100644 --- a/src/player_control/actions.rs +++ b/src/player_control/actions.rs @@ -44,7 +44,7 @@ impl Plugin for ActionsPlugin { } } -#[derive(Debug, Clone, Actionlike, Reflect, FromReflect, Default)] +#[derive(Debug, Clone, Copy, Actionlike, Reflect, FromReflect, Default)] pub enum PlayerAction { #[default] Move, @@ -52,7 +52,37 @@ pub enum PlayerAction { Jump, Interact, SpeedUpDialog, - NumberedChoice(u16), + NumberedChoice1, + NumberedChoice2, + NumberedChoice3, + NumberedChoice4, + NumberedChoice5, + NumberedChoice6, + NumberedChoice7, + NumberedChoice8, + NumberedChoice9, + NumberedChoice0, +} + +impl PlayerAction { + pub fn numbered_choice(index: u8) -> Self { + match index { + 0 => PlayerAction::NumberedChoice0, + 1 => PlayerAction::NumberedChoice1, + 2 => PlayerAction::NumberedChoice2, + 3 => PlayerAction::NumberedChoice3, + 4 => PlayerAction::NumberedChoice4, + 5 => PlayerAction::NumberedChoice5, + 6 => PlayerAction::NumberedChoice6, + 7 => PlayerAction::NumberedChoice7, + 8 => PlayerAction::NumberedChoice8, + 9 => PlayerAction::NumberedChoice9, + _ => panic!( + "Numbered choice index out of range: got {}, expected 0-9", + index + ), + } + } } #[derive(Debug, Clone, Actionlike, Reflect, FromReflect, Default)] @@ -75,16 +105,16 @@ pub fn create_player_action_input_manager_bundle() -> InputManagerBundle { let text = create_choice_rich_text(0, "Continue"); - if ui.button(text).clicked() || actions.just_pressed(PlayerAction::NumberedChoice(1)) { + if ui.button(text).clicked() || actions.just_pressed(PlayerAction::numbered_choice(1)) { current_dialog.current_page = next_page_id; *elapsed_time = 0.0; } @@ -196,9 +196,13 @@ fn present_choices( .enumerate() { let text = create_choice_rich_text(index, &choice.text); - if ui.button(text).clicked() - || actions.just_pressed(PlayerAction::NumberedChoice(index as u16 + 1)) + if ui.button(&text).clicked() + || actions.just_pressed(PlayerAction::numbered_choice(index as u8 + 1)) { + info!("Picked choice: {:?}", choice_id); + info!("Text: {:?}", text); + info!("Choice: {:?}", choice); + info!("Index: {:?}", index); picked_choice = Some((choice_id.clone(), choice.clone())); } } @@ -225,7 +229,7 @@ fn present_choices( } NextPage::Exit => { let text = create_choice_rich_text(0, "Exit"); - if ui.button(text).clicked() || actions.just_pressed(PlayerAction::NumberedChoice(1)) { + if ui.button(text).clicked() || actions.just_pressed(PlayerAction::numbered_choice(1)) { commands.remove_resource::(); actions_frozen.unfreeze(); }