From f556d1d17608a496f52045c75e45a50531ee0362 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Mon, 9 Jan 2023 22:48:53 +0000 Subject: [PATCH] Add `OnComponentBeginOverlap` delegate to `AWeaponPickup` --- Source/Nakatomi/WeaponPickup.cpp | 16 +++++++++++++--- Source/Nakatomi/WeaponPickup.h | 6 ++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Source/Nakatomi/WeaponPickup.cpp b/Source/Nakatomi/WeaponPickup.cpp index 09fc449..939e7fc 100644 --- a/Source/Nakatomi/WeaponPickup.cpp +++ b/Source/Nakatomi/WeaponPickup.cpp @@ -12,6 +12,9 @@ AWeaponPickup::AWeaponPickup() PrimaryActorTick.bStartWithTickEnabled = true; SphereComponent = CreateDefaultSubobject(TEXT("SphereComponent")); + SphereComponent->SetSphereRadius(25.0f, true); + SphereComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe); + SphereComponent->SetupAttachment(RootComponent); } @@ -22,15 +25,14 @@ void AWeaponPickup::BeginPlay() FActorSpawnParameters SpawnParameters; SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; - WeaponComponent = GetWorld()->SpawnActor(Weapon, SpawnParameters); - FAttachmentTransformRules TransformRules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true); WeaponComponent->AttachToComponent(RootComponent, TransformRules); WeaponComponent->SetActorRelativeLocation(FVector(0.0f, 0.0f, 5.0f)); WeaponComponent->SetActorEnableCollision(false); - WeaponStartingLocation = WeaponComponent->GetActorLocation(); + + SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); } // Called every frame @@ -47,3 +49,11 @@ void AWeaponPickup::Tick(float DeltaTime) WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine)); } +void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) +{ + // TODO: Add weapon to player inventory + this->Destroy(); + WeaponComponent->Destroy(); +} + diff --git a/Source/Nakatomi/WeaponPickup.h b/Source/Nakatomi/WeaponPickup.h index 70e1429..0ae430f 100644 --- a/Source/Nakatomi/WeaponPickup.h +++ b/Source/Nakatomi/WeaponPickup.h @@ -32,10 +32,13 @@ public: private: + UPROPERTY() USphereComponent* SphereComponent; + UPROPERTY() AWeapon* WeaponComponent; + UPROPERTY() FVector WeaponStartingLocation; public: @@ -50,4 +53,7 @@ public: // Called every frame virtual void Tick(float DeltaTime) override; + UFUNCTION() + void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); + };