diff --git a/Content/Dialogue/DialogueTest.umap b/Content/Dialogue/DialogueTest.umap index 244f14c..4a0c8c3 100644 --- a/Content/Dialogue/DialogueTest.umap +++ b/Content/Dialogue/DialogueTest.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:65fb1f5ae0a1d0e8503ac46af15e79d05afd9f83bf1a479bfc560df2f128c7b3 -size 40062 +oid sha256:911ac1a22605bd25c09862336710dd077c849e1bcbeb4046c70cd1e9ef698ff7 +size 41447 diff --git a/Content/Dialogue/NPCTest.uasset b/Content/Dialogue/NPCTest.uasset index 76db5fe..cdd38f9 100644 --- a/Content/Dialogue/NPCTest.uasset +++ b/Content/Dialogue/NPCTest.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f277997103dbdece2162edd9c73e22e3076859c04b59a6ca970471643c9f32c1 -size 29535 +oid sha256:0594afb06d0d1957dd84c2ee2ee8487e30962a5bc85fad7ac1deb64cd3586552 +size 30359 diff --git a/Content/Dialogue/TextPrompt.uasset b/Content/Dialogue/TextPrompt.uasset index 1777bcb..4630031 100644 --- a/Content/Dialogue/TextPrompt.uasset +++ b/Content/Dialogue/TextPrompt.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3faa628db877e647869edd000879734f6761c3a5e0bc8b21991a7d132a3828f7 -size 22011 +oid sha256:78605489bcbc381e7d4cd447e16413e8762156a38a47715305c8478c3df5eac3 +size 36800 diff --git a/Source/the_twilight_abyss/Dialogue/DialogueNPC.cpp b/Source/the_twilight_abyss/Dialogue/DialogueNPC.cpp new file mode 100644 index 0000000..74b35b0 --- /dev/null +++ b/Source/the_twilight_abyss/Dialogue/DialogueNPC.cpp @@ -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 DialogueWidgetClass(TEXT("/Game/Dialogue/TextPrompt")); + DialogueWidget = DialogueWidgetClass.Class; +} + + +// Called when the game starts +void UDialogueNPC::BeginPlay() +{ + Super::BeginPlay(); + + DialogueWidgetInstance = CreateWidget(GetWorld(), DialogueWidget); + + NPCNameText = Cast(DialogueWidgetInstance->GetWidgetFromName("Text_Name")); + DialogueText = Cast(DialogueWidgetInstance->GetWidgetFromName("Text_Dialogue")); + NextButton = Cast(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; +} diff --git a/Source/the_twilight_abyss/Dialogue/DialogueNPC.h b/Source/the_twilight_abyss/Dialogue/DialogueNPC.h new file mode 100644 index 0000000..3c084a2 --- /dev/null +++ b/Source/the_twilight_abyss/Dialogue/DialogueNPC.h @@ -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 DialogueType; + + UPROPERTY(EditAnywhere) + TArray Dialogue; + +protected: + // Called when the game starts + virtual void BeginPlay() override; + + UPROPERTY() + TSubclassOf 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(); +};