Inventory System

#pragma once
#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "ItemDataStructs.generated.h"

UENUM()
enum class EItemQuality : uint8
{
	Shoddy UMETA(DisplayName = "Shoddy"),
	Common UMETA(DisplayName = "Common"),
	Quality UMETA(DisplayName = "Quality"),
	Masterwork UMETA(DisplayName = "Masterwork"),
	Grandmaster UMETA(DisplayName = "Grandmaster"),
};

UENUM()
enum class EItemType : uint8 
{
	Armor  		UMETA(DisplayName = "Armor"),
	Weapon 		UMETA(DisplayName = "Weapon"),
	Shield		UMETA(DisplayName = "Shield"),
	Spell		UMETA(DisplayName = "Spell"),
	Consumable	UMETA(DisplayName = "Consumable"),
	Quest		UMETA(DisplayName = "Quest"),
	Mundane		UMETA(DisplayName = "Mundane"),
};


USTRUCT()
struct FItemStatistics
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere) float ArmorRating;
	UPROPERTY(EditAnywhere) float DamageValue;
	UPROPERTY(EditAnywhere) float RestorationAmount;	 
	UPROPERTY(EditAnywhere) float SellValue;
};

USTRUCT()
struct FItemTextData
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere) FText Name;
	UPROPERTY(EditAnywhere) FText Description;
	UPROPERTY(EditAnywhere) FText InteractionText;
	UPROPERTY(EditAnywhere) FText UsageText;
};

USTRUCT()
struct FItemNumericData
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere) float Weight;
	UPROPERTY(EditAnywhere) int32 MaxStackSize;
	UPROPERTY(EditAnywhere) bool  bIsStackable;
};

USTRUCT()
struct FItemAssetData
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere) UTexture2D* Icon;
	UPROPERTY(EditAnywhere) UStaticMesh* Mesh;
};


USTRUCT()
struct FItemData : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, Category = "Item Data") FName   		 ID				 ;
	UPROPERTY(EditAnywhere, Category = "Item Data") EItemType   	 ItemType   	 ;
	UPROPERTY(EditAnywhere, Category = "Item Data") EItemQuality 	 ItemQuality 	 ;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemStatistics	 ItemStatistics	 ;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemTextData	 TextData		 ;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemNumericData NumericData	 ;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemAssetData	 AssetData		 ;
};

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "ItemDataStructs.h"
#include "UObject/NoExportTypes.h"
#include "ItemBase.generated.h"

/**
 * 
 */
UCLASS()
class SHADOWSINTHELIGHT_API UItemBase : public UObject
{
	GENERATED_BODY()

public:
	//================================================================================
	// PROPERTIES & VARIABLES
	//================================================================================
	//UPROPERTY()
	//UInventoryComponent* OwningInventory;
	
	UPROPERTY(VisibleAnywhere, Category = "Item Data", meta = (UIMin=1, UIMax=100))
	int32 Quantity;

	UPROPERTY(EditAnywhere, Category = "Item Data") FName   		 ID;
	UPROPERTY(EditAnywhere, Category = "Item Data") EItemType   	 ItemType;
	UPROPERTY(EditAnywhere, Category = "Item Data") EItemQuality 	 ItemQuality;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemStatistics	 ItemStatistics;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemTextData	 TextData;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemNumericData NumericData;
	UPROPERTY(EditAnywhere, Category = "Item Data") FItemAssetData	 AssetData;


	//================================================================================
	// FUNCTIONS
	//================================================================================

	UItemBase();

	UFUNCTION(Category = "Item") UItemBase* CreateItemCopy();

	UFUNCTION(Category = "Item") 
	FORCEINLINE float GetItemStackWeight() const { return Quantity * NumericData.Weight; };
	UFUNCTION(Category = "Item") 
	FORCEINLINE float GetItemSingleWeight() const { return NumericData.Weight; };
	UFUNCTION(Category = "Item") 
	FORCEINLINE bool IsFullItemStack() const { return Quantity == NumericData.MaxStackSize; };
	
	UFUNCTION(Category = "Item")void SetQuantity(const int32 NewQuantity);
	UFUNCTION(Category = "Item")virtual void Use(AActor* Character);

	bool operator ==(const FName& OtherID)const
	{
		return ID == OtherID;
	}


};
// Fill out your copyright notice in the Description page of Project Settings.


#include "ItemBase.h"

UItemBase::UItemBase()
{
}

UItemBase* UItemBase::CreateItemCopy()
{
	UItemBase* ItemCopy = NewObject<UItemBase>(StaticClass());

	ItemCopy->ID = this->ID;
	ItemCopy->Quantity = this->Quantity;
	ItemCopy->ItemQuality = this->ItemQuality;
	ItemCopy->ItemType = this->ItemType;
	ItemCopy->TextData = this->TextData;
	ItemCopy->NumericData = this->NumericData;
	ItemCopy->ItemStatistics = this->ItemStatistics;
	ItemCopy->AssetData = this->AssetData;

	return ItemCopy;
}

void UItemBase::SetQuantity(const int32 NewQuantity)
{
	if (NewQuantity != Quantity) {
		Quantity = FMath::Clamp(NewQuantity, 0, NumericData.bIsStackable ? NumericData.MaxStackSize : 1);
		
		// if (OwningInventory)
		// {
		//	if (Quantity <= 0) {
		//		OwningInventory->RemoveItem(this);
		//	}
		// }
	}
}

void UItemBase::Use(AActor* Character)
{
}


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *