Implemented Mini Waypoint Appearing When Main Waypoint is Out of Focus

This commit is contained in:
Rafal Swierczek 2023-11-26 21:23:54 +00:00
parent 153e026354
commit 5a8d8e8284
7 changed files with 88 additions and 18 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:a523b2149e46a3b369ca97e969d9a3f241a7af78f45b6500febbe252b9a53193 oid sha256:adf1682c60d18b92e71347784817def276af822981b49939047b0e98e8d1d1f5
size 100785 size 105318

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:48aab2df957e2a9988712c7a2913d041000b8948f572298a8d8f23c759161e0a oid sha256:d3d5b6b3ec13c451b42ae7c71efdfc6878d1e4dbdb366e7256834767a8cd6364
size 39684 size 42017

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:842aa6cfaae2bd2428eb51ca353b53825d15505936abd000d4d38a8ac0284305 oid sha256:9b6671545b77ffaff9044f90b3f250e2384252cb1d7a122617969c932a0f863e
size 654898 size 654898

View File

@ -13,11 +13,8 @@ AWaypointActor::AWaypointActor()
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
} }
// Called when the game starts or when spawned void AWaypointActor::SetupWaypoint_Implementation(UTexture2D* Icon, const FString& Desc)
void AWaypointActor::BeginPlay()
{ {
Super::BeginPlay();
SetActorTickInterval(0.05); SetActorTickInterval(0.05);
AActor* PlayerActor = Cast<AActor>(GetWorld()->GetFirstPlayerController()->GetPawn()); AActor* PlayerActor = Cast<AActor>(GetWorld()->GetFirstPlayerController()->GetPawn());
PlayersCam = Cast<UCameraComponent>(PlayerActor->GetComponentByClass(UCameraComponent::StaticClass())); PlayersCam = Cast<UCameraComponent>(PlayerActor->GetComponentByClass(UCameraComponent::StaticClass()));
@ -26,6 +23,9 @@ void AWaypointActor::BeginPlay()
MiniWaypoint = CreateWidget<UMiniWaypoint>(GetWorld(), MiniWaypointClass); MiniWaypoint = CreateWidget<UMiniWaypoint>(GetWorld(), MiniWaypointClass);
MiniWaypoint->AddToViewport(1); MiniWaypoint->AddToViewport(1);
MiniWaypoint->WorldLoc = this->GetActorLocation();
MiniWaypoint->PlayerController = GetWorld()->GetFirstPlayerController();
MiniWaypoint->SetIcon(Icon);
} }
// Called every frame // Called every frame
@ -33,18 +33,15 @@ void AWaypointActor::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
UpdateScaleAndRotation();
if (SightCheck()) if (SightCheck())
{ {
UpdateVisibility(true); UpdateVisibility(true);
UpdateScaleAndRotation();
MiniWaypoint->UpdateVisibility(false); MiniWaypoint->UpdateVisibility(false);
return; return;
} }
UpdateVisibility(false); UpdateVisibility(false);
MiniWaypoint->UpdateVisibility(true); MiniWaypoint->UpdateVisibility(true);
} }
bool AWaypointActor::SightCheck() bool AWaypointActor::SightCheck()

View File

@ -44,9 +44,6 @@ class ENDLESSVENDETTA_API AWaypointActor : public AActor
bool SightCheck(); bool SightCheck();
protected: protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UFUNCTION(BlueprintImplementableEvent) UFUNCTION(BlueprintImplementableEvent)
void UpdateVisibility(bool IsVisible); void UpdateVisibility(bool IsVisible);
@ -57,7 +54,7 @@ public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintImplementableEvent) UFUNCTION(BlueprintNativeEvent)
void SetupWaypoint(UTexture2D* Icon, const FString& Desc); void SetupWaypoint(UTexture2D* Icon, const FString& Desc);
}; };

View File

@ -3,3 +3,63 @@
#include "MiniWaypoint.h" #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<UCanvasPanelSlot>(IconImage->Slot);
}

View File

@ -4,6 +4,8 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "Components/CanvasPanelSlot.h"
#include "Components/Image.h"
#include "MiniWaypoint.generated.h" #include "MiniWaypoint.generated.h"
/** /**
@ -14,8 +16,22 @@ class ENDLESSVENDETTA_API UMiniWaypoint : public UUserWidget
{ {
GENERATED_BODY() GENERATED_BODY()
void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;
UCanvasPanelSlot* IconCanvasPanelSlot;
protected:
UFUNCTION(BlueprintCallable)
void SetIconPanelSlot(UImage* IconImage);
public: public:
FVector WorldLoc = FVector(0, 0, 0);
APlayerController* PlayerController;
UFUNCTION(BlueprintImplementableEvent) UFUNCTION(BlueprintImplementableEvent)
void UpdateVisibility(bool IsVisible); void UpdateVisibility(bool IsVisible);
UFUNCTION(BlueprintImplementableEvent)
void SetIcon(UTexture2D* Icon);
}; };