Add public methods to set Weapon in WeaponPickup

This commit is contained in:
Louis Hobbs 2023-02-10 23:20:36 +00:00
parent 0e95cf7239
commit 2585404169
2 changed files with 34 additions and 12 deletions

View File

@ -25,20 +25,11 @@ void AWeaponPickup::BeginPlay()
if (Weapon) if (Weapon)
{ {
FActorSpawnParameters SpawnParameters; SpawnWeapon();
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();
WeaponStartingLocation += ((MovementDirection * MovementDistance) / 2);
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin);
} }
}
// Called every frame // Called every frame
void AWeaponPickup::Tick(float DeltaTime) void AWeaponPickup::Tick(float DeltaTime)
@ -72,3 +63,29 @@ void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AAc
} }
} }
void AWeaponPickup::SetWeapon(TSubclassOf<class AWeapon> weapon)
{
Weapon = weapon;
if (WeaponComponent)
{
WeaponComponent->Destroy();
}
SpawnWeapon();
}
void AWeaponPickup::SpawnWeapon()
{
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();
WeaponStartingLocation += ((MovementDirection * MovementDistance) / 2);
}

View File

@ -60,4 +60,9 @@ public:
UFUNCTION() UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
void SetWeapon(TSubclassOf<class AWeapon> weapon);
private:
void SpawnWeapon();
}; };