From 25854041692959b765d228971863ecf1e3efdaa0 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Fri, 10 Feb 2023 23:20:36 +0000 Subject: [PATCH] Add public methods to set Weapon in WeaponPickup --- Source/Nakatomi/WeaponPickup.cpp | 41 ++++++++++++++++++++++---------- Source/Nakatomi/WeaponPickup.h | 5 ++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Source/Nakatomi/WeaponPickup.cpp b/Source/Nakatomi/WeaponPickup.cpp index 0f8adb6..3ce4f65 100644 --- a/Source/Nakatomi/WeaponPickup.cpp +++ b/Source/Nakatomi/WeaponPickup.cpp @@ -25,19 +25,10 @@ void AWeaponPickup::BeginPlay() if (Weapon) { - 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(); - WeaponStartingLocation += ((MovementDirection * MovementDistance) / 2); - - SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); + SpawnWeapon(); } + + SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); } // Called every frame @@ -72,3 +63,29 @@ void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AAc } } +void AWeaponPickup::SetWeapon(TSubclassOf weapon) +{ + Weapon = weapon; + + if (WeaponComponent) + { + WeaponComponent->Destroy(); + } + + SpawnWeapon(); +} + +void AWeaponPickup::SpawnWeapon() +{ + 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(); + WeaponStartingLocation += ((MovementDirection * MovementDistance) / 2); +} + diff --git a/Source/Nakatomi/WeaponPickup.h b/Source/Nakatomi/WeaponPickup.h index 19bc348..d801144 100644 --- a/Source/Nakatomi/WeaponPickup.h +++ b/Source/Nakatomi/WeaponPickup.h @@ -60,4 +60,9 @@ public: UFUNCTION() void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); + void SetWeapon(TSubclassOf weapon); + +private: + + void SpawnWeapon(); };