Update Inventory Component for UI [WIP]

This commit is contained in:
Philip W 2023-10-23 06:35:59 +01:00
parent 36daaaed16
commit 8cfe3b97a9
21 changed files with 188 additions and 33 deletions

BIN
EndlessVendetta/Content/FirstPerson/Blueprints/BP_PC.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
EndlessVendetta/Content/Inventory/M_Base.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
EndlessVendetta/Content/Inventory/MyBaseItem.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
EndlessVendetta/Content/Inventory/UI_Inventory.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
EndlessVendetta/Content/Inventory/UI_InventoryGrid.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
EndlessVendetta/Content/Inventory/UI_ItemWidget.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
EndlessVendetta/Content/Inventory/zeldamus.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -3,3 +3,27 @@
#include "BaseItem.h" #include "BaseItem.h"
void UBaseItem::RotateItem()
{
if (CurrentItemRotation == Horizontal)
{
CurrentItemRotation = Vertical;
ItemSize = FItemSize(ItemSize.Y, ItemSize.X);
bIsRotated = true;
}
else
{
CurrentItemRotation = Horizontal;
ItemSize = FItemSize(ItemSize.Y, ItemSize.X);
bIsRotated = false;
}
}
UMaterialInterface* UBaseItem::GetItemTexture() const
{
if (bIsRotated)
{
return ItemTextureRotated;
}
return ItemTexture;
}

View File

@ -10,7 +10,9 @@ struct FItemSize
{ {
GENERATED_BODY() GENERATED_BODY()
UPROPERTY(BlueprintReadOnly, Category = "ItemSize")
int X; int X;
UPROPERTY(BlueprintReadOnly, Category = "ItemSize")
int Y; int Y;
FItemSize(const int _X, const int _Y) FItemSize(const int _X, const int _Y)
@ -29,14 +31,14 @@ struct FItemSize
UENUM(BlueprintType) UENUM(BlueprintType)
enum EItemRotation enum EItemRotation
{ {
Horizontal UMETA(DisplayName = "Up"), Horizontal UMETA(DisplayName = "Horizontal"),
Vertical UMETA(DisplayName = "Down") Vertical UMETA(DisplayName = "Vertical")
}; };
/** /**
* *
*/ */
UCLASS(Abstract) UCLASS(Abstract, Blueprintable)
class ENDLESSVENDETTA_API UBaseItem : public UObject class ENDLESSVENDETTA_API UBaseItem : public UObject
{ {
GENERATED_BODY() GENERATED_BODY()
@ -50,14 +52,19 @@ public:
FText Description; FText Description;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
FItemSize ItemSize = FItemSize(); FItemSize ItemSize = FItemSize();
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Instanced, Category = "Item") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
UTexture2D* ItemTexture; UMaterialInterface* ItemTexture;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Instanced, Category = "Item") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
UTexture2D* ItemTextureRotated; UMaterialInterface* ItemTextureRotated;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
TEnumAsByte<EItemRotation> CurrentItemRotation = Horizontal; TEnumAsByte<EItemRotation> CurrentItemRotation = Horizontal;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
TSubclassOf<AActor> ItemActor; TSubclassOf<AActor> ItemActor;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
bool bIsRotated = false; bool bIsRotated = false;
UFUNCTION(BlueprintCallable, Category = "Item")
void RotateItem();
UFUNCTION(BlueprintCallable, Category = "Item")
UMaterialInterface* GetItemTexture() const;
}; };

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "BaseWorldItem.h"
// Sets default values
ABaseWorldItem::ABaseWorldItem()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ABaseWorldItem::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABaseWorldItem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

View File

@ -0,0 +1,25 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "BaseWorldItem.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ABaseWorldItem : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABaseWorldItem();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};

View File

@ -4,6 +4,7 @@
#include "InventoryComponent.h" #include "InventoryComponent.h"
#include "BaseItem.h" #include "BaseItem.h"
#include "Camera/CameraComponent.h"
// Sets default values for this component's properties // Sets default values for this component's properties
@ -11,9 +12,15 @@ UInventoryComponent::UInventoryComponent()
{ {
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // 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. // off to improve performance if you don't need them.
bWantsInitializeComponent = true;
PrimaryComponentTick.bCanEverTick = true; PrimaryComponentTick.bCanEverTick = true;
}
// ... void UInventoryComponent::InitializeComponent()
{
Super::InitializeComponent();
PlayerCharacter = Cast<AEndlessVendettaCharacter>(GetOwner());
ItemToPickup = nullptr;
} }
@ -22,7 +29,9 @@ void UInventoryComponent::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
// ... //FTimerHandle TraceTimerHandle;
//GetWorld()->GetTimerManager().SetTimer(TraceTimerHandle, this, &UInventoryComponent::ProjectTraceForItem, 0.1f, true);
InventoryItems.Init(nullptr, Columns * Rows);
} }
@ -31,16 +40,55 @@ void UInventoryComponent::TickComponent(float DeltaTime, ELevelTick TickType, FA
{ {
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ... if (IsDirty)
{
IsDirty = false;
OnInventoryUpdated.Broadcast();
}
} }
bool UInventoryComponent::AddItem(UBaseItem* Item) bool UInventoryComponent::AddItem(UBaseItem* Item)
{ {
if (!IsValid(Item)) return false;
for (int i = 0; i < InventoryItems.Num(); i++)
{
if (IsRoomAvailable(Item, i))
{
AddItemAt(Item, i);
return true;
}
}
Item->RotateItem();
for (int i = 0; i < InventoryItems.Num(); i++)
{
if (IsRoomAvailable(Item, i))
{
AddItemAt(Item, i);
return true;
}
}
Item->RotateItem();
return false; return false;
} }
void UInventoryComponent::LineTraceForItemCheck() void UInventoryComponent::ProjectTraceForItem()
{ {
if (!IsValid(PlayerCharacter)) return;
FVector StartLocation = Cast<UCameraComponent>(PlayerCharacter->GetComponentByClass(UCameraComponent::StaticClass()))->GetComponentLocation();
FVector EndLocation = StartLocation + Cast<UCameraComponent>(PlayerCharacter->GetComponentByClass(UCameraComponent::StaticClass()))->GetForwardVector() * MaxPickupDistance;
FCollisionQueryParams CollisionQueryParams;
CollisionQueryParams.AddIgnoredActor(PlayerCharacter);
if (FHitResult HitResult; GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility, CollisionQueryParams))
{
if (Cast<UBaseItem>(HitResult.GetActor()))
{
ItemToPickup = HitResult.GetActor();
Cast<UStaticMeshComponent>(ItemToPickup->GetComponentByClass(UStaticMeshComponent::StaticClass()))->SetRenderCustomDepth(true);
return;
}
}
if (!IsValid(ItemToPickup)) return;
Cast<UStaticMeshComponent>(ItemToPickup->GetComponentByClass(UStaticMeshComponent::StaticClass()))->SetRenderCustomDepth(false);
} }
void UInventoryComponent::Pickup() void UInventoryComponent::Pickup()
@ -51,9 +99,9 @@ void UInventoryComponent::Pickup()
bool UInventoryComponent::IsRoomAvailable(UBaseItem* Item, const int TopLeftIndex) bool UInventoryComponent::IsRoomAvailable(UBaseItem* Item, const int TopLeftIndex)
{ {
for (int i = 0; i < IndexToTile(TopLeftIndex).X + (Item->ItemSize.X - 1); i++) for (int i = IndexToTile(TopLeftIndex).X; i < IndexToTile(TopLeftIndex).X + Item->ItemSize.X; i++)
{ {
for (int j = 0; j < IndexToTile(TopLeftIndex).Y + (Item->ItemSize.Y - 1); j++) for (int j = IndexToTile(TopLeftIndex).Y; j < IndexToTile(TopLeftIndex).Y + Item->ItemSize.Y; j++)
{ {
FInventoryTile TileToCheck; FInventoryTile TileToCheck;
TileToCheck.X = i; TileToCheck.X = i;
@ -61,7 +109,7 @@ bool UInventoryComponent::IsRoomAvailable(UBaseItem* Item, const int TopLeftInde
if (!IsTileValid(TileToCheck)) return false; if (!IsTileValid(TileToCheck)) return false;
const TTuple<UBaseItem*, bool> ItemAtIndex = GetItemAtIndex(TileToIndex(TileToCheck)); const TTuple<UBaseItem*, bool> ItemAtIndex = GetItemAtIndex(TileToIndex(TileToCheck));
if (ItemAtIndex.Get<1>()) return false; if (ItemAtIndex.Get<1>()) return false;
if (IsValid(ItemAtIndex.Get<0>())) return false; if (IsValid(ItemAtIndex.Get<0>())) return true;
} }
} }
return true; return true;
@ -88,9 +136,9 @@ int UInventoryComponent::TileToIndex(const FInventoryTile InventoryTile) const
void UInventoryComponent::AddItemAt(UBaseItem* Item, const int TopLeftIndex) void UInventoryComponent::AddItemAt(UBaseItem* Item, const int TopLeftIndex)
{ {
for (int i = 0; i < IndexToTile(TopLeftIndex).X + (Item->ItemSize.X - 1); i++) for (int i = IndexToTile(TopLeftIndex).X; i < IndexToTile(TopLeftIndex).X + Item->ItemSize.X; i++)
{ {
for (int j = 0; j < IndexToTile(TopLeftIndex).Y + (Item->ItemSize.Y - 1); j++) for (int j = IndexToTile(TopLeftIndex).Y; j < IndexToTile(TopLeftIndex).Y + Item->ItemSize.Y; j++)
{ {
FInventoryTile TileToCheck; FInventoryTile TileToCheck;
TileToCheck.X = i; TileToCheck.X = i;

View File

@ -17,6 +17,7 @@ class ENDLESSVENDETTA_API UInventoryComponent : public UActorComponent
public: public:
// Sets default values for this component's properties // Sets default values for this component's properties
UInventoryComponent(); UInventoryComponent();
virtual void InitializeComponent() override;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory")
int Columns = 10; int Columns = 10;
@ -25,6 +26,7 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory")
float MaxPickupDistance = 200.0f; float MaxPickupDistance = 200.0f;
protected: protected:
// Called when the game starts // Called when the game starts
virtual void BeginPlay() override; virtual void BeginPlay() override;
@ -33,10 +35,14 @@ public:
// Called every frame // Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnInventoryUpdated);
UPROPERTY(BlueprintAssignable, Category="Inventory")
FOnInventoryUpdated OnInventoryUpdated;
UFUNCTION(BlueprintCallable, Category="Inventory") UFUNCTION(BlueprintCallable, Category="Inventory")
bool AddItem(class UBaseItem* Item); bool AddItem(class UBaseItem* Item);
UFUNCTION(BlueprintCallable, Category="Inventory") UFUNCTION(BlueprintCallable, Category="Inventory")
void LineTraceForItemCheck(); void ProjectTraceForItem();
UFUNCTION(BlueprintCallable, Category="Inventory") UFUNCTION(BlueprintCallable, Category="Inventory")
void Pickup(); void Pickup();
UFUNCTION(BlueprintCallable, Category="Inventory") UFUNCTION(BlueprintCallable, Category="Inventory")

View File

@ -14,7 +14,9 @@ struct FInventoryLine
{ {
GENERATED_BODY() GENERATED_BODY()
UPROPERTY(BlueprintReadWrite)
FVector2D Start; FVector2D Start;
UPROPERTY(BlueprintReadWrite)
FVector2D End; FVector2D End;
}; };
@ -22,7 +24,9 @@ USTRUCT(BlueprintType)
struct FInventoryTile struct FInventoryTile
{ {
GENERATED_BODY() GENERATED_BODY()
UPROPERTY(BlueprintReadWrite)
int X; int X;
UPROPERTY(BlueprintReadWrite)
int Y; int Y;
}; };