vampires/Source/vampires/WeaponInventoryComponent.cpp

62 lines
1.3 KiB
C++
Raw Normal View History

2025-02-05 23:12:03 +00:00
// Louis Hobbs | 2024-2025
2024-07-18 01:39:58 +01:00
#include "WeaponInventoryComponent.h"
2024-11-14 18:39:20 +00:00
#include "Weapon.h"
2024-07-18 01:39:58 +01:00
// Sets default values for this component's properties
UWeaponInventoryComponent::UWeaponInventoryComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = false;
// ...
}
// Called when the game starts
void UWeaponInventoryComponent::BeginPlay()
{
Super::BeginPlay();
InitializeInventory();
2024-07-18 01:39:58 +01:00
}
void UWeaponInventoryComponent::InitializeInventory()
{
2025-07-29 22:06:42 +01:00
Inventory.Empty();
2024-07-18 01:39:58 +01:00
2025-07-29 22:06:42 +01:00
for (TSubclassOf<AWeapon> Weapon : InitialInventory)
2024-07-18 01:39:58 +01:00
{
2025-07-29 22:06:42 +01:00
if (IsValid(Weapon))
{
2025-07-29 22:06:42 +01:00
AddWeaponToInventory(Weapon);
}
2024-07-18 01:39:58 +01:00
}
}
2025-07-29 22:06:42 +01:00
void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> NewWeapon)
2024-07-18 01:39:58 +01:00
{
FActorSpawnParameters SpawnParameters;
SpawnParameters.Owner = GetOwner();
2025-07-29 22:06:42 +01:00
AWeapon* Weapon = GetWorld()->SpawnActor<AWeapon>(NewWeapon, SpawnParameters.Owner->GetTransform(), SpawnParameters);
if (Weapon->GetFollowPlayer())
2025-04-23 01:13:11 +01:00
{
2025-07-29 22:06:42 +01:00
Weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepWorldTransform);
2025-04-23 01:13:11 +01:00
}
else
{
2025-07-29 22:06:42 +01:00
Weapon->SetActorLocation(FVector::ZeroVector);
2025-04-23 01:13:11 +01:00
}
2025-07-29 22:06:42 +01:00
Inventory.Add(Weapon);
ObtainableWeapons.Remove(NewWeapon);
2024-07-18 01:39:58 +01:00
}
TArray<AWeapon*> UWeaponInventoryComponent::GetInventory()
{
2025-07-29 22:06:42 +01:00
return Inventory;
}