From 18702cea0c51b6602f8a04c4be6d3363aaa1b418 Mon Sep 17 00:00:00 2001 From: baz Date: Thu, 14 Nov 2024 22:49:59 +0000 Subject: [PATCH] Use timeline for updating pickup position --- Content/Pickups/C_Pickup.uasset | 3 + Content/Pickups/EXP/BP_BlueEXPPickup.uasset | 4 +- Content/Pickups/EXP/BP_GreenEXPPickup.uasset | 4 +- Content/Pickups/EXP/BP_RedEXPPickup.uasset | 4 +- Content/Pickups/Gold/BP_GoldPickup.uasset | 4 +- Source/vampires/EXPPickup.cpp | 4 +- Source/vampires/EXPPickup.h | 2 +- Source/vampires/GoldPickup.cpp | 4 +- Source/vampires/GoldPickup.h | 2 +- Source/vampires/Pickup.cpp | 77 ++++++++++++++------ Source/vampires/Pickup.h | 38 ++++++++-- Source/vampires/PlayerCharacter.cpp | 1 + 12 files changed, 106 insertions(+), 41 deletions(-) create mode 100644 Content/Pickups/C_Pickup.uasset diff --git a/Content/Pickups/C_Pickup.uasset b/Content/Pickups/C_Pickup.uasset new file mode 100644 index 0000000..f358659 --- /dev/null +++ b/Content/Pickups/C_Pickup.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eb85e03bcb86bb75675d2749393b043578e765b1cdbac01b53d1e06c1efcebb0 +size 4936 diff --git a/Content/Pickups/EXP/BP_BlueEXPPickup.uasset b/Content/Pickups/EXP/BP_BlueEXPPickup.uasset index 5d5b6c0..2e4baaa 100644 --- a/Content/Pickups/EXP/BP_BlueEXPPickup.uasset +++ b/Content/Pickups/EXP/BP_BlueEXPPickup.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bdd56ecc52e1743a5fb15cce620b932d1506d297aa4757d9e8d1ebe126dafdd4 -size 24365 +oid sha256:1cdd4047693b3fbe6d752fb28d2cadb6193b39129f09c5f679dde0f88f504c62 +size 26076 diff --git a/Content/Pickups/EXP/BP_GreenEXPPickup.uasset b/Content/Pickups/EXP/BP_GreenEXPPickup.uasset index 63c31cc..7f86575 100644 --- a/Content/Pickups/EXP/BP_GreenEXPPickup.uasset +++ b/Content/Pickups/EXP/BP_GreenEXPPickup.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a8a5940d909f9d7c89f30b2130a7f807b1bb60e68dc2f1c9a97e2399437c7c27 -size 24265 +oid sha256:08fe5d31af79c6f7cf3cb9e8a6b305b80e2813df3c6890d729625c4abb063c2e +size 25062 diff --git a/Content/Pickups/EXP/BP_RedEXPPickup.uasset b/Content/Pickups/EXP/BP_RedEXPPickup.uasset index 3215288..c8cd282 100644 --- a/Content/Pickups/EXP/BP_RedEXPPickup.uasset +++ b/Content/Pickups/EXP/BP_RedEXPPickup.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cac58b08cf6ccd71d8173da2c6e2c10ee357fa48aa4daf9737743d2b8829a92f -size 24223 +oid sha256:7664008e33e851844025795902ebbf2e3c04ad43789a6d3b53be3c0ef464fe4c +size 25020 diff --git a/Content/Pickups/Gold/BP_GoldPickup.uasset b/Content/Pickups/Gold/BP_GoldPickup.uasset index b185c30..02349aa 100644 --- a/Content/Pickups/Gold/BP_GoldPickup.uasset +++ b/Content/Pickups/Gold/BP_GoldPickup.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2d35028ee621f020df40b6b0222b5f0f8b7c3105514ebd4d1e5a495feb851407 -size 26076 +oid sha256:8d4701416e59b92800856f821ee5a8465b4c576893b69085158955c856435ec9 +size 24994 diff --git a/Source/vampires/EXPPickup.cpp b/Source/vampires/EXPPickup.cpp index e776bc0..f7605cb 100644 --- a/Source/vampires/EXPPickup.cpp +++ b/Source/vampires/EXPPickup.cpp @@ -16,12 +16,12 @@ void AEXPPickup::Tick(float DeltaSeconds) Super::Tick(DeltaSeconds); } -void AEXPPickup::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, +void AEXPPickup::OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { if (APlayerCharacter* PlayerCharacter = Cast(OtherActor)) { PlayerCharacter->GetEXPComponent()->IncrementEXP(EXP); - Super::OnBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult); + Super::OnInnerBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult); } } diff --git a/Source/vampires/EXPPickup.h b/Source/vampires/EXPPickup.h index acbdf83..aad0368 100644 --- a/Source/vampires/EXPPickup.h +++ b/Source/vampires/EXPPickup.h @@ -25,7 +25,7 @@ protected: public: virtual void Tick(float DeltaSeconds) override; - virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + virtual void OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override; }; diff --git a/Source/vampires/GoldPickup.cpp b/Source/vampires/GoldPickup.cpp index ec1eaf1..c7fc9c7 100644 --- a/Source/vampires/GoldPickup.cpp +++ b/Source/vampires/GoldPickup.cpp @@ -18,12 +18,12 @@ void AGoldPickup::Tick(float DeltaSeconds) Super::Tick(DeltaSeconds); } -void AGoldPickup::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, +void AGoldPickup::OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { if (APlayerCharacter* PlayerCharacter = Cast(OtherActor)) { PlayerCharacter->GetGoldComponent()->IncrementGold(Gold); - Super::OnBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult); + Super::OnInnerBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult); } } diff --git a/Source/vampires/GoldPickup.h b/Source/vampires/GoldPickup.h index 6fdf37f..881bd32 100644 --- a/Source/vampires/GoldPickup.h +++ b/Source/vampires/GoldPickup.h @@ -25,7 +25,7 @@ protected: public: virtual void Tick(float DeltaSeconds) override; - virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + virtual void OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override; }; diff --git a/Source/vampires/Pickup.cpp b/Source/vampires/Pickup.cpp index 37fcf50..01c3200 100644 --- a/Source/vampires/Pickup.cpp +++ b/Source/vampires/Pickup.cpp @@ -2,7 +2,6 @@ #include "Pickup.h" - #include "PlayerCharacter.h" #include "Components/SphereComponent.h" #include "Kismet/GameplayStatics.h" @@ -12,46 +11,52 @@ APickup::APickup() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. - PrimaryActorTick.bCanEverTick = true; + PrimaryActorTick.bCanEverTick = false; // Create Sphere Component - SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); - SetRootComponent(SphereComponent); - SphereComponent->SetSphereRadius(25.0f); + InnerSphereComponent = CreateDefaultSubobject(TEXT("Inner Sphere Component")); + SetRootComponent(InnerSphereComponent); + InnerSphereComponent->SetSphereRadius(25.0f); + + OuterSphereComponent = CreateDefaultSubobject(TEXT("Outer Sphere Component")); + OuterSphereComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform); + OuterSphereComponent->SetSphereRadius(250.0f); SpriteComponent = CreateDefaultSubobject(TEXT("Sprite Component")); SpriteComponent->SetRelativeRotation(FRotator(0.0f, 90.0f, -90.0f)); SpriteComponent->SetRelativeScale3D(FVector(.5f, .5f, .5f)); + SpriteComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision); SpriteComponent->SetupAttachment(RootComponent); + + TimelineComponent = CreateDefaultSubobject(TEXT("Timeline Component")); + TimelineComponent->SetDirectionPropertyName(FName("TimelineDirection")); + TimelineComponent->SetLooping(false); + TimelineComponent->SetTimelineLength(5.0f); + TimelineComponent->SetTimelineLengthMode(TL_TimelineLength); + TimelineComponent->SetPlaybackPosition(0.0f, false); + + onTimelineCallback.BindUFunction(this, FName(TEXT("TimelineCallback"))); + onTimelineFinishedCallback.BindUFunction(this, FName(TEXT("TimelineFinishedCallback"))); } // Called when the game starts or when spawned void APickup::BeginPlay() { Super::BeginPlay(); - SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnBeginOverlap); -} -void APickup::Tick(float DeltaSeconds) -{ - Super::Tick(DeltaSeconds); + InnerSphereComponent->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnInnerBeginOverlap); + OuterSphereComponent->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnOuterBeginOverlap); - // TODO: Move actor towards player when in range - FVector actorLocation = GetActorLocation(); - FVector playerLocation = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)->GetActorLocation(); - double currentDistance = FVector::Distance(actorLocation, playerLocation); - - if (currentDistance <= PickupMovementRange) + if (CurveFloat != nullptr) { - double speed = 400 / currentDistance; - FVector location = FMath::VInterpTo(actorLocation, playerLocation, DeltaSeconds, speed); - SetActorLocation(location); + TimelineComponent->AddInterpFloat(CurveFloat, onTimelineCallback); + TimelineComponent->SetTimelineFinishedFunc(onTimelineFinishedCallback); } } -void APickup::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, - const FHitResult& SweepResult) +void APickup::OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult) { if (APlayerCharacter* PlayerCharacter = Cast(OtherActor)) { @@ -64,3 +69,31 @@ void APickup::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* O Destroy(); } } + +void APickup::OnOuterBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult) +{ + if (APlayerCharacter* PlayerCharacter = Cast(OtherActor)) + { + PickupLocation = GetActorLocation(); + PlayTimeLine(); + } +} + +void APickup::TimelineCallback(float val) +{ + FVector actorLocation = PickupLocation; + FVector playerLocation = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)->GetActorLocation(); + FVector location = FMath::Lerp(actorLocation, playerLocation, val); + SetActorLocation(location); +} + +void APickup::TimelineFinishedCallback() +{ +} + +void APickup::PlayTimeLine() +{ + TimelineComponent->Play(); +} diff --git a/Source/vampires/Pickup.h b/Source/vampires/Pickup.h index 68c86b8..078f5d7 100644 --- a/Source/vampires/Pickup.h +++ b/Source/vampires/Pickup.h @@ -3,9 +3,11 @@ #pragma once #include "CoreMinimal.h" +#include "Components/TimelineComponent.h" #include "GameFramework/Actor.h" #include "Pickup.generated.h" +class UTimelineComponent; class USphereComponent; class UPaperSpriteComponent; @@ -23,13 +25,28 @@ public: double PickupMovementSpeed = 1000; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) - USphereComponent* SphereComponent = nullptr; + USphereComponent* InnerSphereComponent = nullptr; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + USphereComponent* OuterSphereComponent = nullptr; UPROPERTY(EditAnywhere) UPaperSpriteComponent* SpriteComponent = nullptr; UPROPERTY(EditDefaultsOnly) USoundBase* PickupSoundBase = nullptr; + + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Timeline") + TObjectPtr TimelineComponent; + + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Timeline") + UCurveFloat* CurveFloat; + +private: + FOnTimelineFloat onTimelineCallback; + FOnTimelineEventStatic onTimelineFinishedCallback; + + FVector PickupLocation; public: // Sets default values for this actor's properties @@ -39,11 +56,22 @@ protected: // Called when the game starts or when spawned virtual void BeginPlay() override; -public: - virtual void Tick(float DeltaSeconds) override; - UFUNCTION() - virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + virtual void OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); + + UFUNCTION() + virtual void OnOuterBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult); + + UFUNCTION() + void TimelineCallback(float val); + + UFUNCTION() + void TimelineFinishedCallback(); + + UFUNCTION(BlueprintCallable) + void PlayTimeLine(); }; diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index c81daa7..f5682f2 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -48,6 +48,7 @@ APlayerCharacter::APlayerCharacter() HealthBarWidgetComponent->SetRelativeLocation(FVector(0, 0, 90)); HealthBarWidgetComponent->SetTwoSided(true); HealthBarWidgetComponent->SetBackgroundColor(FLinearColor(1, 1, 1, 0)); + HealthBarWidgetComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision); } void APlayerCharacter::BeginPlay()