2023-02-02 01:59:33 +00:00
|
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "DialogueNPC.h"
|
|
|
|
|
#include "Blueprint/UserWidget.h"
|
|
|
|
|
#include "Components/TextBlock.h"
|
2023-02-02 02:39:35 +00:00
|
|
|
|
#include "GameFramework/Character.h"
|
2023-02-02 01:59:33 +00:00
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UDialogueNPC::NextDialogue()
|
|
|
|
|
{
|
|
|
|
|
//Dialogue Skip
|
2023-02-09 03:12:27 +00:00
|
|
|
|
if (CurrentDialogue.Len() < CurrentDialoguePath[DialogueIndex].Len())
|
2023-02-02 01:59:33 +00:00
|
|
|
|
{
|
2023-02-09 03:12:27 +00:00
|
|
|
|
CurrentDialogue = CurrentDialoguePath[DialogueIndex];
|
2023-02-02 01:59:33 +00:00
|
|
|
|
DialogueText->SetText(FText::FromString(CurrentDialogue));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DialogueIndex++;
|
2023-02-09 03:12:27 +00:00
|
|
|
|
if (DialogueIndex >= CurrentDialoguePath.Num())
|
2023-02-02 01:59:33 +00:00
|
|
|
|
{
|
|
|
|
|
EndDialogue();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CurrentDialogue = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UDialogueNPC::NextCharacter()
|
|
|
|
|
{
|
2023-02-09 03:12:27 +00:00
|
|
|
|
if (DialogueIndex >= CurrentDialoguePath.Num()) return;
|
2023-02-02 02:39:35 +00:00
|
|
|
|
|
2023-02-09 03:12:27 +00:00
|
|
|
|
if (CurrentDialogue.Len() < CurrentDialoguePath[DialogueIndex].Len())
|
2023-02-02 01:59:33 +00:00
|
|
|
|
{
|
2023-02-09 03:12:27 +00:00
|
|
|
|
CurrentDialogue.AppendChar(CurrentDialoguePath[DialogueIndex][CurrentDialogue.Len()]);
|
2023-02-02 01:59:33 +00:00
|
|
|
|
DialogueText->SetText(FText::FromString(CurrentDialogue));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called every frame
|
|
|
|
|
void UDialogueNPC::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
|
|
|
|
{
|
|
|
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UDialogueNPC::StartDialogue()
|
|
|
|
|
{
|
2023-02-02 02:39:35 +00:00
|
|
|
|
//Disable Character Movement
|
|
|
|
|
if (ACharacter* PlayerCharacter = Cast<ACharacter>(GetWorld()->GetFirstPlayerController()->GetPawn()))
|
|
|
|
|
{
|
|
|
|
|
PlayerCharacter->DisableInput(GetWorld()->GetFirstPlayerController());
|
|
|
|
|
}
|
2023-02-02 01:59:33 +00:00
|
|
|
|
//Set to UI Mode Only
|
|
|
|
|
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
|
|
|
|
|
PlayerController->SetInputMode(FInputModeUIOnly());
|
|
|
|
|
PlayerController->bShowMouseCursor = true;
|
2023-02-02 02:39:35 +00:00
|
|
|
|
|
2023-02-02 01:59:33 +00:00
|
|
|
|
DialogueWidgetInstance->AddToViewport();
|
2023-02-06 03:28:53 +00:00
|
|
|
|
DialogueIndex = 1;
|
2023-02-02 01:59:33 +00:00
|
|
|
|
NPCNameText->SetText(FText::FromString(NPCName));
|
|
|
|
|
CurrentDialogue = "";
|
2023-02-09 03:12:27 +00:00
|
|
|
|
CurrentDialoguePath = DialoguePaths[0].Dialogue;
|
2023-02-06 01:13:17 +00:00
|
|
|
|
GetWorld()->GetTimerManager().SetTimer(TextAnimationTimerHandle, this, &UDialogueNPC::NextCharacter, TextAnimationSpeed, true);
|
2023-02-02 01:59:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UDialogueNPC::EndDialogue()
|
|
|
|
|
{
|
|
|
|
|
TextAnimationTimerHandle.Invalidate();
|
|
|
|
|
DialogueWidgetInstance->RemoveFromParent();
|
|
|
|
|
|
2023-02-02 02:39:35 +00:00
|
|
|
|
//Enable Character Movement
|
|
|
|
|
if (ACharacter* PlayerCharacter = Cast<ACharacter>(GetWorld()->GetFirstPlayerController()->GetPawn()))
|
|
|
|
|
{
|
|
|
|
|
PlayerCharacter->EnableInput(GetWorld()->GetFirstPlayerController());
|
|
|
|
|
}
|
2023-02-02 01:59:33 +00:00
|
|
|
|
//Reset UI Mode
|
|
|
|
|
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
|
|
|
|
|
PlayerController->SetInputMode(FInputModeGameOnly());
|
|
|
|
|
PlayerController->bShowMouseCursor = false;
|
|
|
|
|
}
|
2023-02-06 03:28:53 +00:00
|
|
|
|
|
2023-02-09 17:01:03 +00:00
|
|
|
|
FDialoguePath UDialogueNPC::CreateRootDialoguePath()
|
2023-02-06 03:28:53 +00:00
|
|
|
|
{
|
2023-02-09 17:01:03 +00:00
|
|
|
|
return FDialoguePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FDialoguePath UDialogueNPC::CreateDialoguePath(FDialoguePath ParentDialoguePath)
|
|
|
|
|
{
|
|
|
|
|
return FDialoguePath("#" + FString::FromInt(DialoguePaths.Num()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UDialogueNPC::GotoDialoguePath(FString PathId)
|
|
|
|
|
{
|
|
|
|
|
for each (FDialoguePath DialogPath in DialoguePaths)
|
|
|
|
|
{
|
|
|
|
|
if (DialogPath.Dialogue[0] == PathId)
|
|
|
|
|
{
|
|
|
|
|
CurrentDialoguePath = DialogPath.Dialogue;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FDialoguePath UDialogueNPC::AddDialogue(FText TextInput, FDialoguePath DialoguePath)
|
|
|
|
|
{
|
|
|
|
|
if (TextInput.IsEmpty()) return DialoguePath;
|
|
|
|
|
DialoguePath.Dialogue.Add(TextInput.ToString());
|
|
|
|
|
return DialoguePath;
|
2023-02-06 03:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UDialogueNPC::GetFinalDialogue(TArray<FString> DialogueArray)
|
|
|
|
|
{
|
2023-02-09 03:12:27 +00:00
|
|
|
|
DialoguePaths.Add(FDialoguePath(DialogueArray));
|
2023-02-06 03:28:53 +00:00
|
|
|
|
}
|