From b955732fb8018dd35445baa17b3eae3278eb2b2b Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Thu, 12 Jan 2023 23:53:02 +0000 Subject: [PATCH] Add Weapon from Weapon Pickup to Current Inventory --- Source/Nakatomi/PlayerCharacter.cpp | 27 ++++++++++++++++ Source/Nakatomi/PlayerCharacter.h | 6 ++++ Source/Nakatomi/WeaponPickup.cpp | 49 ++++++++++++++++++----------- Source/Nakatomi/WeaponPickup.h | 1 + 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/Source/Nakatomi/PlayerCharacter.cpp b/Source/Nakatomi/PlayerCharacter.cpp index 05a5033..eb8b9e9 100644 --- a/Source/Nakatomi/PlayerCharacter.cpp +++ b/Source/Nakatomi/PlayerCharacter.cpp @@ -315,3 +315,30 @@ void APlayerCharacter::SetCurrentWeapon(AWeapon* weapon) CurrentWeapon->SetActorHiddenInGame(false); } } + +void APlayerCharacter::AddWeaponToInventory(TSubclassOf 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 + +} diff --git a/Source/Nakatomi/PlayerCharacter.h b/Source/Nakatomi/PlayerCharacter.h index c907883..9d8a72b 100644 --- a/Source/Nakatomi/PlayerCharacter.h +++ b/Source/Nakatomi/PlayerCharacter.h @@ -124,4 +124,10 @@ public: AWeapon* GetCurrentWeapon(); void SetCurrentWeapon(AWeapon* weapon); + + void AddWeaponToInventory(TSubclassOf weapon); + + void RemoveWeaponFromInventory(int i); + + void RemoveCurrentWeaponFromInventory(); }; diff --git a/Source/Nakatomi/WeaponPickup.cpp b/Source/Nakatomi/WeaponPickup.cpp index 939e7fc..33fa85b 100644 --- a/Source/Nakatomi/WeaponPickup.cpp +++ b/Source/Nakatomi/WeaponPickup.cpp @@ -23,16 +23,19 @@ void AWeaponPickup::BeginPlay() { Super::BeginPlay(); - 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(); + 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(); - 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(OtherActor); + + if (player && Weapon) + { + player->AddWeaponToInventory(Weapon); + + this->Destroy(); + WeaponComponent->Destroy(); + } } diff --git a/Source/Nakatomi/WeaponPickup.h b/Source/Nakatomi/WeaponPickup.h index 0ae430f..348f5e0 100644 --- a/Source/Nakatomi/WeaponPickup.h +++ b/Source/Nakatomi/WeaponPickup.h @@ -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"