Add Weapon from Weapon Pickup to Current Inventory

This commit is contained in:
Louis Hobbs 2023-01-12 23:53:02 +00:00
parent 0f0ab4c6e7
commit b955732fb8
4 changed files with 65 additions and 18 deletions

View File

@ -315,3 +315,30 @@ void APlayerCharacter::SetCurrentWeapon(AWeapon* weapon)
CurrentWeapon->SetActorHiddenInGame(false); CurrentWeapon->SetActorHiddenInGame(false);
} }
} }
void APlayerCharacter::AddWeaponToInventory(TSubclassOf<class AWeapon> weapon)
{
if (weapon)
{
AWeapon* newWeapon = InitializeWeapon(weapon);
WeaponInventory.Add(newWeapon);
if (WeaponInventory.Num() == 1)
{
SetCurrentWeapon(WeaponInventory[0]);
}
}
}
void APlayerCharacter::RemoveWeaponFromInventory(int i)
{
// TODO: Add more checking here
WeaponInventory[i]->Destroy();
WeaponInventory.RemoveAt(i);
}
void APlayerCharacter::RemoveCurrentWeaponFromInventory()
{
// TODO: Add more checking here
}

View File

@ -124,4 +124,10 @@ public:
AWeapon* GetCurrentWeapon(); AWeapon* GetCurrentWeapon();
void SetCurrentWeapon(AWeapon* weapon); void SetCurrentWeapon(AWeapon* weapon);
void AddWeaponToInventory(TSubclassOf<class AWeapon> weapon);
void RemoveWeaponFromInventory(int i);
void RemoveCurrentWeaponFromInventory();
}; };

View File

@ -23,6 +23,8 @@ void AWeaponPickup::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
if (Weapon)
{
FActorSpawnParameters SpawnParameters; FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters); WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters);
@ -34,12 +36,15 @@ void AWeaponPickup::BeginPlay()
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)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
if (WeaponComponent)
{
// Rotate Weapon in desired direction // Rotate Weapon in desired direction
WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime); WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime);
@ -48,12 +53,20 @@ void AWeaponPickup::Tick(float DeltaTime)
float Sine = FMath::Sin(Time * MovementSpeed); float Sine = FMath::Sin(Time * MovementSpeed);
WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine)); WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine));
} }
}
void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{ {
// TODO: Add weapon to player inventory // TODO: Add extra checking here
auto player = Cast<APlayerCharacter>(OtherActor);
if (player && Weapon)
{
player->AddWeaponToInventory(Weapon);
this->Destroy(); this->Destroy();
WeaponComponent->Destroy(); WeaponComponent->Destroy();
} }
}

View File

@ -5,6 +5,7 @@
#include "Components/SphereComponent.h" #include "Components/SphereComponent.h"
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "PlayerCharacter.h"
#include "Weapon.h" #include "Weapon.h"
#include "WeaponPickup.generated.h" #include "WeaponPickup.generated.h"