AzureAbyss/Source/the_twilight_abyss/Dialogue/DialogueNPC.cpp

109 lines
3.3 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"
// 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()
{
//log bruh
UE_LOG(LogTemp, Warning, TEXT("Next Dialogue"));
//Dialogue Skip
if (CurrentDialogue.Len() < Dialogue[DialogueIndex].Len())
{
CurrentDialogue = Dialogue[DialogueIndex];
DialogueText->SetText(FText::FromString(CurrentDialogue));
return;
}
DialogueIndex++;
if (DialogueIndex >= Dialogue.Num())
{
EndDialogue();
return;
}
CurrentDialogue = "";
}
void UDialogueNPC::NextCharacter()
{
if (DialogueIndex >= Dialogue.Num()) return;
if (CurrentDialogue.Len() < Dialogue[DialogueIndex].Len())
{
CurrentDialogue.AppendChar(Dialogue[DialogueIndex][CurrentDialogue.Len()]);
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()
{
//Disable Character Movement
if (ACharacter* PlayerCharacter = Cast<ACharacter>(GetWorld()->GetFirstPlayerController()->GetPawn()))
{
PlayerCharacter->DisableInput(GetWorld()->GetFirstPlayerController());
}
//Set to UI Mode Only
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
PlayerController->SetInputMode(FInputModeUIOnly());
PlayerController->bShowMouseCursor = true;
DialogueWidgetInstance->AddToViewport();
DialogueIndex = 0;
NPCNameText->SetText(FText::FromString(NPCName));
CurrentDialogue = "";
GetWorld()->GetTimerManager().SetTimer(TextAnimationTimerHandle, this, &UDialogueNPC::NextCharacter, 0.05f, true);
}
void UDialogueNPC::EndDialogue()
{
TextAnimationTimerHandle.Invalidate();
DialogueWidgetInstance->RemoveFromParent();
//Enable Character Movement
if (ACharacter* PlayerCharacter = Cast<ACharacter>(GetWorld()->GetFirstPlayerController()->GetPawn()))
{
PlayerCharacter->EnableInput(GetWorld()->GetFirstPlayerController());
}
//Reset UI Mode
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
PlayerController->SetInputMode(FInputModeGameOnly());
PlayerController->bShowMouseCursor = false;
}