From f25e6366f5771b67afafa01a8f27aa62b89607d5 Mon Sep 17 00:00:00 2001 From: baz Date: Thu, 18 Jul 2024 01:39:58 +0100 Subject: [PATCH] Add Weapon Inventory Component --- Source/vampires/WeaponInventoryComponent.cpp | 46 ++++++++++++++++++++ Source/vampires/WeaponInventoryComponent.h | 41 +++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 Source/vampires/WeaponInventoryComponent.cpp create mode 100644 Source/vampires/WeaponInventoryComponent.h diff --git a/Source/vampires/WeaponInventoryComponent.cpp b/Source/vampires/WeaponInventoryComponent.cpp new file mode 100644 index 0000000..99411f7 --- /dev/null +++ b/Source/vampires/WeaponInventoryComponent.cpp @@ -0,0 +1,46 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "WeaponInventoryComponent.h" + +#include "Kismet/GameplayStatics.h" + +// 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(); + + // ... + +} + +void UWeaponInventoryComponent::InitializeInventory() +{ + inventory.Empty(); + + for (TSubclassOf weapon : initialInventory) + { + AddWeaponToInventory(weapon); + } +} + +void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf Weapon) +{ + AWeapon* weapon = GetWorld()->SpawnActor(Weapon); + weapon->SetActorTransform(GetOwner()->GetTransform()); + weapon->SetOwner(GetOwner()); + weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform); + inventory.Add(weapon); +} + diff --git a/Source/vampires/WeaponInventoryComponent.h b/Source/vampires/WeaponInventoryComponent.h new file mode 100644 index 0000000..8ce8c87 --- /dev/null +++ b/Source/vampires/WeaponInventoryComponent.h @@ -0,0 +1,41 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Weapon.h" +#include "Components/ActorComponent.h" +#include "WeaponInventoryComponent.generated.h" + + +UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) +class VAMPIRES_API UWeaponInventoryComponent : public UActorComponent +{ + GENERATED_BODY() + +public: + + UPROPERTY() + TArray> initialInventory; + +private: + UPROPERTY() + TArray inventory; + +public: + // Sets default values for this component's properties + UWeaponInventoryComponent(); + +protected: + // Called when the game starts + virtual void BeginPlay() override; + + +public: + + UFUNCTION() + void InitializeInventory(); + + UFUNCTION() + void AddWeaponToInventory(TSubclassOf Weapon); +};