From 5a8d8e828454bb80b0c8abee1f12666affb25f6c Mon Sep 17 00:00:00 2001 From: Rafal Swierczek Date: Sun, 26 Nov 2023 21:23:54 +0000 Subject: [PATCH] Implemented Mini Waypoint Appearing When Main Waypoint is Out of Focus --- .../BountySystem/Waypoint/BP_Waypoint.uasset | 4 +- .../Waypoint/WBP_MiniWaypoint.uasset | 4 +- .../Content/Levels/TrainingFacility.umap | 2 +- .../BountySystem/WaypointActor.cpp | 15 ++--- .../BountySystem/WaypointActor.h | 5 +- .../UserWidgets/MiniWaypoint.cpp | 60 +++++++++++++++++++ .../UserWidgets/MiniWaypoint.h | 16 +++++ 7 files changed, 88 insertions(+), 18 deletions(-) diff --git a/EndlessVendetta/Content/BountySystem/Waypoint/BP_Waypoint.uasset b/EndlessVendetta/Content/BountySystem/Waypoint/BP_Waypoint.uasset index 020a3c2c..d2678445 100644 --- a/EndlessVendetta/Content/BountySystem/Waypoint/BP_Waypoint.uasset +++ b/EndlessVendetta/Content/BountySystem/Waypoint/BP_Waypoint.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a523b2149e46a3b369ca97e969d9a3f241a7af78f45b6500febbe252b9a53193 -size 100785 +oid sha256:adf1682c60d18b92e71347784817def276af822981b49939047b0e98e8d1d1f5 +size 105318 diff --git a/EndlessVendetta/Content/BountySystem/Waypoint/WBP_MiniWaypoint.uasset b/EndlessVendetta/Content/BountySystem/Waypoint/WBP_MiniWaypoint.uasset index b4cca8c2..4162142b 100644 --- a/EndlessVendetta/Content/BountySystem/Waypoint/WBP_MiniWaypoint.uasset +++ b/EndlessVendetta/Content/BountySystem/Waypoint/WBP_MiniWaypoint.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:48aab2df957e2a9988712c7a2913d041000b8948f572298a8d8f23c759161e0a -size 39684 +oid sha256:d3d5b6b3ec13c451b42ae7c71efdfc6878d1e4dbdb366e7256834767a8cd6364 +size 42017 diff --git a/EndlessVendetta/Content/Levels/TrainingFacility.umap b/EndlessVendetta/Content/Levels/TrainingFacility.umap index 5efb1b8d..afb2d366 100644 --- a/EndlessVendetta/Content/Levels/TrainingFacility.umap +++ b/EndlessVendetta/Content/Levels/TrainingFacility.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:842aa6cfaae2bd2428eb51ca353b53825d15505936abd000d4d38a8ac0284305 +oid sha256:9b6671545b77ffaff9044f90b3f250e2384252cb1d7a122617969c932a0f863e size 654898 diff --git a/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.cpp b/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.cpp index ce186cbb..6c5f9674 100644 --- a/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.cpp @@ -13,19 +13,19 @@ AWaypointActor::AWaypointActor() PrimaryActorTick.bCanEverTick = true; } -// Called when the game starts or when spawned -void AWaypointActor::BeginPlay() +void AWaypointActor::SetupWaypoint_Implementation(UTexture2D* Icon, const FString& Desc) { - Super::BeginPlay(); - SetActorTickInterval(0.05); AActor* PlayerActor = Cast(GetWorld()->GetFirstPlayerController()->GetPawn()); PlayersCam = Cast(PlayerActor->GetComponentByClass(UCameraComponent::StaticClass())); ScalingMagnitude = (ScaleAtMaxDist - ScaleAtMinDist) / (MaxDist - MinDist); ScalingY_Intercept = ScaleAtMaxDist - (MaxDist * ScalingMagnitude); - + MiniWaypoint = CreateWidget(GetWorld(), MiniWaypointClass); MiniWaypoint->AddToViewport(1); + MiniWaypoint->WorldLoc = this->GetActorLocation(); + MiniWaypoint->PlayerController = GetWorld()->GetFirstPlayerController(); + MiniWaypoint->SetIcon(Icon); } // Called every frame @@ -33,18 +33,15 @@ void AWaypointActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); + UpdateScaleAndRotation(); if (SightCheck()) { UpdateVisibility(true); - UpdateScaleAndRotation(); MiniWaypoint->UpdateVisibility(false); return; } - UpdateVisibility(false); MiniWaypoint->UpdateVisibility(true); - - } bool AWaypointActor::SightCheck() diff --git a/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.h b/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.h index d86c1207..a8957749 100644 --- a/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.h +++ b/EndlessVendetta/Source/EndlessVendetta/BountySystem/WaypointActor.h @@ -44,9 +44,6 @@ class ENDLESSVENDETTA_API AWaypointActor : public AActor bool SightCheck(); protected: - // Called when the game starts or when spawned - virtual void BeginPlay() override; - UFUNCTION(BlueprintImplementableEvent) void UpdateVisibility(bool IsVisible); @@ -57,7 +54,7 @@ public: // Called every frame virtual void Tick(float DeltaTime) override; - UFUNCTION(BlueprintImplementableEvent) + UFUNCTION(BlueprintNativeEvent) void SetupWaypoint(UTexture2D* Icon, const FString& Desc); }; diff --git a/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.cpp b/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.cpp index 68f8f0f4..6ee988a9 100644 --- a/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.cpp @@ -3,3 +3,63 @@ #include "MiniWaypoint.h" + + +void UMiniWaypoint::NativeTick(const FGeometry& MyGeometry, float InDeltaTime) +{ + Super::NativeTick(MyGeometry, InDeltaTime); + + if (!IsValid(IconCanvasPanelSlot) || !IsValid(PlayerController)) return; + + FVector2D ScreenLoc; + PlayerController->ProjectWorldLocationToScreen(WorldLoc, ScreenLoc); + + // Check if waypoint is outside of players vision frustum + if (FMath::IsWithin(ScreenLoc.X, -0.01, 0.01) && FMath::IsWithin(ScreenLoc.Y, -0.01, 0.01)) + { + //UE_LOG(LogTemp, Warning, TEXT("IconPos: %f"), IconCanvasPanelSlot->GetPosition().X); + + float PlayerYaw = PlayerController->GetControlRotation().Yaw; + float TargetYaw = (WorldLoc - PlayerController->GetPawn()->GetActorLocation()).Rotation().Yaw; + if (TargetYaw < 0 ) TargetYaw += 360; + + UE_LOG(LogTemp, Warning, TEXT("Yaw: %f"), PlayerController->GetControlRotation().Yaw); + UE_LOG(LogTemp, Warning, TEXT("Look at Yaw: %f"), TargetYaw); + + // Check if waypoint is closer to the players right + bool TargetOnTheRight = false; + if (PlayerYaw >= TargetYaw) + { + TargetOnTheRight = FMath::Abs(TargetYaw - PlayerYaw) >= 180; + } + else + { + TargetOnTheRight = TargetYaw - PlayerYaw <= 180; + } + + UE_LOG(LogTemp, Warning, TEXT("On the right?...%s"), TargetOnTheRight ? TEXT("true") : TEXT("false")); + return; + } + + FVector2d ViewPortSize; + GEngine->GameViewport->GetViewportSize(ViewPortSize); + float ViewportHalfX = ViewPortSize.X / 2.f; + float ViewportHalfY = ViewPortSize.Y / 2.f; + + // Centers the icon + ScreenLoc.X -= ViewportHalfX; + ScreenLoc.Y -= ViewportHalfY; + + float ViewportMarginX = ViewportHalfX * 0.8f; + float ViewportMarginY = ViewportHalfY * 0.6f; + + ScreenLoc.X = FMath::Clamp(ScreenLoc.X, ViewportMarginX * -1.f, ViewportMarginX); + ScreenLoc.Y = FMath::Clamp(ScreenLoc.Y, ViewportMarginY * -1.f, ViewportMarginY); + + IconCanvasPanelSlot->SetPosition(ScreenLoc); +} + +void UMiniWaypoint::SetIconPanelSlot(UImage* IconImage) +{ + IconCanvasPanelSlot = Cast(IconImage->Slot); +} diff --git a/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.h b/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.h index a556167d..0089f226 100644 --- a/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.h +++ b/EndlessVendetta/Source/EndlessVendetta/UserWidgets/MiniWaypoint.h @@ -4,6 +4,8 @@ #include "CoreMinimal.h" #include "Blueprint/UserWidget.h" +#include "Components/CanvasPanelSlot.h" +#include "Components/Image.h" #include "MiniWaypoint.generated.h" /** @@ -13,9 +15,23 @@ UCLASS() class ENDLESSVENDETTA_API UMiniWaypoint : public UUserWidget { GENERATED_BODY() + + void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override; + + UCanvasPanelSlot* IconCanvasPanelSlot; + +protected: + UFUNCTION(BlueprintCallable) + void SetIconPanelSlot(UImage* IconImage); public: + FVector WorldLoc = FVector(0, 0, 0); + APlayerController* PlayerController; + UFUNCTION(BlueprintImplementableEvent) void UpdateVisibility(bool IsVisible); + + UFUNCTION(BlueprintImplementableEvent) + void SetIcon(UTexture2D* Icon); };