AzureAbyss/Source/the_twilight_abyss/Dialogue/DialogueNPC.cpp

259 lines
10 KiB
C++
Raw Normal View History

// Fill out your copyright notice in the Description page of Project Settings.
#include "DialogueNPC.h"
#include "Blueprint/UserWidget.h"
#include "Components/TextBlock.h"
#include "GameFramework/Character.h"
#include "Kismet/KismetStringLibrary.h"
2023-05-04 05:31:59 +00:00
#include "Misc/OutputDeviceNull.h"
#include "the_twilight_abyss/PlayerTemp/TempCharacter.h"
#include "the_twilight_abyss/Quest/QuestSystem.h"
// Sets default values for this component's properties
UDialogueNPC::UDialogueNPC()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
static ConstructorHelpers::FClassFinder<UUserWidget> DialogueWidgetClass(TEXT("/Game/Dialogue/TextPrompt"));
DialogueWidget = DialogueWidgetClass.Class;
}
// Called when the game starts
void UDialogueNPC::BeginPlay()
{
Super::BeginPlay();
DialogueWidgetInstance = CreateWidget<UUserWidget>(GetWorld(), DialogueWidget);
NPCNameText = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Name"));
DialogueText = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Dialogue"));
NextButton = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Next"));
NextButton->OnClicked.AddDynamic(this, &UDialogueNPC::NextDialogue);
2023-03-13 04:49:09 +00:00
NPCPortraitImage = Cast<UImage>(DialogueWidgetInstance->GetWidgetFromName("Image_Portrait"));
if (IsValid(NPCPortrait)) NPCPortraitImage->SetBrushFromTexture(NPCPortrait);
Choice1Button = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Choice1"));
Choice1Button->OnClicked.AddDynamic(this, &UDialogueNPC::Choice1);
Choice2Button = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Choice2"));
Choice2Button->OnClicked.AddDynamic(this, &UDialogueNPC::Choice2);
Choice3Button = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Choice3"));
Choice3Button->OnClicked.AddDynamic(this, &UDialogueNPC::Choice3);
Choice1Text = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Choice1"));
Choice2Text = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Choice2"));
Choice3Text = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Choice3"));
NextArrow = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("NextArrow"));
}
void UDialogueNPC::NextDialogue()
{
if (Choice1Button->GetVisibility() == ESlateVisibility::Visible) return;
//Dialogue Skip
2023-03-13 04:49:09 +00:00
if (CurrentDialogue.Len() < CurrentDialogueStringPath[DialogueIndex].Len())
{
2023-03-13 04:49:09 +00:00
CurrentDialogue = CurrentDialogueStringPath[DialogueIndex];
DialogueText->SetText(FText::FromString(CurrentDialogue));
NextArrow->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
return;
}
if (CurrentDialogueStringPath[FMath::Clamp(DialogueIndex + 1, 0, CurrentDialogueStringPath.Num() - 1)].Mid(0, 2) == "##")
{
Cast<UQuestSystem>(GetWorld()->GetFirstPlayerController()->GetPawn()->GetComponentByClass(UQuestSystem::StaticClass()))->AddQuest(Quests[UKismetStringLibrary::Conv_StringToInt(CurrentDialogueStringPath[DialogueIndex].RightChop(3))]);
DialogueIndex++;
}
else if (CurrentDialogueStringPath[FMath::Clamp(DialogueIndex + 1, 0, CurrentDialogueStringPath.Num() - 1)].Mid(0, 2) == "@@")
{
FOutputDeviceNull AR;
const FString Command = BlueprintFunctions[UKismetStringLibrary::Conv_StringToInt(CurrentDialogueStringPath[DialogueIndex].RightChop(3))];;
GetOwner()->CallFunctionByNameWithArguments(*Command, AR, nullptr, true);
if (Command == "OpenShop") bResetUserControls = false;
DialogueIndex++;
}
else if (CurrentDialogueStringPath[FMath::Clamp(DialogueIndex + 1, 0, CurrentDialogueStringPath.Num() - 1)].Mid(0, 2) == "$$")
{
Cast<UInventoryComponent>(GetWorld()->GetFirstPlayerController()->GetPawn()->GetComponentByClass(UInventoryComponent::StaticClass()))->AddItem(ItemsToGive[UKismetStringLibrary::Conv_StringToInt(CurrentDialogueStringPath[DialogueIndex].RightChop(3))]);
DialogueIndex++;
}
else if (CurrentDialogueStringPath[FMath::Clamp(DialogueIndex + 1, 0, CurrentDialogueStringPath.Num() - 1)].Mid(0, 2) == "%%")
{
Cast<UQuestSystem>(GetWorld()->GetFirstPlayerController()->GetPawn()->GetComponentByClass(UQuestSystem::StaticClass()))->AddQuestFlag(QuestFlags[UKismetStringLibrary::Conv_StringToInt(CurrentDialogueStringPath[DialogueIndex].RightChop(3))], true);
DialogueIndex++;
}
DialogueIndex++;
NextArrow->SetVisibility(ESlateVisibility::Hidden);
2023-03-13 04:49:09 +00:00
if (DialogueIndex >= CurrentDialogueStringPath.Num())
{
2023-03-13 04:49:09 +00:00
if (CurrentDialoguePath->Choices.IsEmpty())
{
EndDialogue();
return;
}
GetWorld()->GetTimerManager().PauseTimer(TextAnimationTimerHandle);
DialogueText->SetText(FText::FromString(""));
Choice1Button->SetVisibility(ESlateVisibility::Visible);
if (Choice2Text->GetText().ToString() != "") Choice2Button->SetVisibility(ESlateVisibility::Visible);
2023-03-13 04:49:09 +00:00
if (Choice3Text->GetText().ToString() != "") Choice3Button->SetVisibility(ESlateVisibility::Visible);
NextArrow->SetVisibility(ESlateVisibility::Hidden);
return;
}
CurrentDialogue = "";
}
void UDialogueNPC::NextCharacter()
{
2023-03-13 04:49:09 +00:00
if (DialogueIndex >= CurrentDialogueStringPath.Num()) return;
2023-03-13 04:49:09 +00:00
if (CurrentDialogue.Len() < CurrentDialogueStringPath[DialogueIndex].Len())
{
2023-03-13 04:49:09 +00:00
CurrentDialogue.AppendChar(CurrentDialogueStringPath[DialogueIndex][CurrentDialogue.Len()]);
DialogueText->SetText(FText::FromString(CurrentDialogue));
}
else NextArrow->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
}
void UDialogueNPC::ResetDialogueUI()
{
DialogueIndex = 0;
CurrentDialogue = "";
CurrentDialogueStringPath = CurrentDialoguePath->Dialogue;
GetWorld()->GetTimerManager().UnPauseTimer(TextAnimationTimerHandle);
Choice1Button->SetVisibility(ESlateVisibility::Hidden);
Choice2Button->SetVisibility(ESlateVisibility::Hidden);
Choice3Button->SetVisibility(ESlateVisibility::Hidden);
NextArrow->SetVisibility(ESlateVisibility::Hidden);
}
// Called every frame
void UDialogueNPC::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
void UDialogueNPC::StartDialogue()
{
Quests.Empty();
2023-05-04 05:31:59 +00:00
FOutputDeviceNull AR;
const FString Command = FString::Printf(TEXT("SetRootDialoguePath"));
GetOwner()->CallFunctionByNameWithArguments(*Command, AR, nullptr, true);
2023-03-13 04:49:09 +00:00
if (IsValid(RootDialoguePath)) CurrentDialogueStringPath = RootDialoguePath->Dialogue;
if (CurrentDialogueStringPath.IsEmpty())
{
UE_LOG(LogTemp, Warning, TEXT("Dialogue Path is Empty"));
return;
}
//Set to UI Mode Only
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
PlayerController->SetIgnoreMoveInput(true);
PlayerController->SetIgnoreLookInput(true);
PlayerController->SetInputMode(FInputModeGameAndUI());
PlayerController->bShowMouseCursor = true;
2023-03-13 04:49:09 +00:00
Choice1Button->SetVisibility(ESlateVisibility::Hidden);
Choice2Button->SetVisibility(ESlateVisibility::Hidden);
Choice3Button->SetVisibility(ESlateVisibility::Hidden);
NextArrow->SetVisibility(ESlateVisibility::Hidden);
2023-03-13 04:49:09 +00:00
DialogueText->SetText(FText::FromString(""));
DialogueWidgetInstance->AddToViewport();
NPCNameText->SetText(FText::FromString(NPCName));
2023-03-13 04:49:09 +00:00
DialogueIndex = 0;
CurrentDialogue = "";
2023-03-13 04:49:09 +00:00
CurrentDialoguePath = RootDialoguePath;
CurrentDialogueStringPath = RootDialoguePath->Dialogue;
GetWorld()->GetTimerManager().SetTimer(TextAnimationTimerHandle, this, &UDialogueNPC::NextCharacter, TextAnimationSpeed, true);
}
void UDialogueNPC::EndDialogue()
{
TextAnimationTimerHandle.Invalidate();
DialogueWidgetInstance->RemoveFromParent();
if (bResetUserControls)
{
//Reset UI Mode
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
PlayerController->SetIgnoreMoveInput(false);
PlayerController->SetIgnoreLookInput(false);
PlayerController->SetInputMode(FInputModeGameOnly());
PlayerController->bShowMouseCursor = false;
}
}
UDialoguePath* UDialogueNPC::CreateRootDialoguePath(const bool ResetUserControls)
{
bResetUserControls = ResetUserControls;
2023-05-04 05:31:59 +00:00
return NewObject<UDialoguePath>();
}
2023-03-13 04:49:09 +00:00
UDialoguePath* UDialogueNPC::AddDialogue(UDialoguePath* DialoguePath, FText TextInput)
{
2023-03-13 04:49:09 +00:00
if (TextInput.IsEmpty()) return DialoguePath;
DialoguePath->Dialogue.Add(TextInput.ToString());
return DialoguePath;
}
2023-03-13 04:49:09 +00:00
void UDialogueNPC::AddChoices(UDialoguePath* ParentPath, FText ChoiceText1, FText ChoiceText2, FText ChoiceText3, UDialoguePath*& Out_ChoicePath1, UDialoguePath*& Out_ChoicePath2, UDialoguePath*& Out_ChoicePath3)
{
2023-03-13 04:49:09 +00:00
ParentPath->Choices.Add(NewObject<UDialoguePath>());
ParentPath->Choices.Add(NewObject<UDialoguePath>());
ParentPath->Choices.Add(NewObject<UDialoguePath>());
Out_ChoicePath1 = ParentPath->Choices[0];
Out_ChoicePath2 = ParentPath->Choices[1];
Out_ChoicePath3 = ParentPath->Choices[2];
Choice1Text->SetText(ChoiceText1);
Choice2Text->SetText(ChoiceText2);
Choice3Text->SetText(ChoiceText3);
}
UDialoguePath* UDialogueNPC::AddQuest(UDialoguePath* DialoguePath, UQuest* Quest)
{
DialoguePath->Dialogue.Add(FText::FromString("## " + Quests.Num()).ToString());
Quests.Add(Quests.Num(), Quest);
return DialoguePath;
}
UDialoguePath* UDialogueNPC::CallBlueprintFunction(UDialoguePath* DialoguePath, const FString FunctionName)
{
DialoguePath->Dialogue.Add(FText::FromString("@@ " + FString::FromInt(BlueprintFunctions.Num())).ToString());
BlueprintFunctions.Add(BlueprintFunctions.Num(), FunctionName);
return DialoguePath;
}
UDialoguePath* UDialogueNPC::AddItem(UDialoguePath* DialoguePath, const int ItemIndex)
{
DialoguePath->Dialogue.Add(FText::FromString("$$ " + FString::FromInt(ItemIndexes.Num())).ToString());
ItemIndexes.Add(ItemIndexes.Num(), ItemIndex);
return DialoguePath;
}
UDialoguePath* UDialogueNPC::AddQuestFlag(UDialoguePath* DialoguePath, FString Flag)
{
DialoguePath->Dialogue.Add(FText::FromString("%% " + FString::FromInt(QuestFlags.Num())).ToString());
QuestFlags.Add(QuestFlags.Num(), Flag);
return DialoguePath;
}
2023-03-13 04:49:09 +00:00
void UDialogueNPC::Choice1()
{
2023-03-13 04:49:09 +00:00
CurrentDialoguePath = CurrentDialoguePath->Choices[0];
ResetDialogueUI();
}
2023-03-13 04:49:09 +00:00
void UDialogueNPC::Choice2()
{
2023-03-13 04:49:09 +00:00
CurrentDialoguePath = CurrentDialoguePath->Choices[1];
ResetDialogueUI();
}
2023-03-13 04:49:09 +00:00
void UDialogueNPC::Choice3()
{
2023-03-13 04:49:09 +00:00
CurrentDialoguePath = CurrentDialoguePath->Choices[2];
ResetDialogueUI();
}