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);
}
}
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();
void SetCurrentWeapon(AWeapon* weapon);
void AddWeaponToInventory(TSubclassOf<class AWeapon> weapon);
void RemoveWeaponFromInventory(int i);
void RemoveCurrentWeaponFromInventory();
};

View File

@ -23,16 +23,19 @@ void AWeaponPickup::BeginPlay()
{
Super::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();
if (Weapon)
{
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);
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin);
}
}
// Called every frame
@ -40,20 +43,30 @@ void AWeaponPickup::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Rotate Weapon in desired direction
WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime);
if (WeaponComponent)
{
// Rotate Weapon in desired direction
WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime);
// Bob weapon up and down
float Time = GetWorld()->GetRealTimeSeconds();
float Sine = FMath::Sin(Time * MovementSpeed);
WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine));
// Bob weapon up and down
float Time = GetWorld()->GetRealTimeSeconds();
float Sine = FMath::Sin(Time * MovementSpeed);
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();
// TODO: Add extra checking here
auto player = Cast<APlayerCharacter>(OtherActor);
if (player && Weapon)
{
player->AddWeaponToInventory(Weapon);
this->Destroy();
WeaponComponent->Destroy();
}
}

View File

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