Add `OnComponentBeginOverlap` delegate to `AWeaponPickup`

This commit is contained in:
Louis Hobbs 2023-01-09 22:48:53 +00:00
parent 2d48d7c703
commit f556d1d176
2 changed files with 19 additions and 3 deletions

View File

@ -12,6 +12,9 @@ AWeaponPickup::AWeaponPickup()
PrimaryActorTick.bStartWithTickEnabled = true;
SphereComponent = CreateDefaultSubobject<USphereComponent>(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<AWeapon>(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();
}

View File

@ -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);
};