Added DialogueNPC Actor Component with Text Animation

This commit is contained in:
Philip W 2023-02-02 01:59:33 +00:00
parent 5988272890
commit 3433254463
5 changed files with 181 additions and 6 deletions

BIN
Content/Dialogue/DialogueTest.umap (Stored with Git LFS)

Binary file not shown.

BIN
Content/Dialogue/NPCTest.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Dialogue/TextPrompt.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -0,0 +1,99 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "DialogueNPC.h"
#include "Blueprint/UserWidget.h"
#include "Components/TextBlock.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);
StartDialogue();
}
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()
{
//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();
//Reset UI Mode
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
PlayerController->SetInputMode(FInputModeGameOnly());
PlayerController->bShowMouseCursor = false;
}

View File

@ -0,0 +1,76 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Components/Button.h"
#include "Components/TextBlock.h"
#include "DialogueNPC.generated.h"
UENUM()
enum class EDialogueType
{
Text UMETA(DisplayName="Text"),
End UMETA(DisplayName="End"),
Action UMETA(DisplayName="Action"),
};
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class THE_TWILIGHT_ABYSS_API UDialogueNPC : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UDialogueNPC();
UPROPERTY(EditAnywhere)
FString NPCName;
UPROPERTY(EditAnywhere)
TArray<EDialogueType> DialogueType;
UPROPERTY(EditAnywhere)
TArray<FString> Dialogue;
protected:
// Called when the game starts
virtual void BeginPlay() override;
UPROPERTY()
TSubclassOf<UUserWidget> DialogueWidget;
private:
UPROPERTY()
UUserWidget* DialogueWidgetInstance;
UPROPERTY()
UTextBlock* NPCNameText;
UPROPERTY()
UTextBlock* DialogueText;
UPROPERTY()
UButton* NextButton;
int DialogueIndex = 0;
FString CurrentDialogue;
UPROPERTY()
FTimerHandle TextAnimationTimerHandle;
UFUNCTION()
void NextDialogue();
void NextCharacter();
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UFUNCTION(BlueprintCallable)
void StartDialogue();
UFUNCTION(BlueprintCallable)
void EndDialogue();
};