From 9362f478aacfb47caae79e0d40ab0f78f94d2fa5 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Thu, 12 Oct 2023 14:11:59 +0100 Subject: [PATCH 1/9] Add Base Item Class for Inventory --- EndlessVendetta/EndlessVendetta.uproject | 3 +- .../EndlessVendetta/Inventory/BaseItem.cpp | 5 +++ .../EndlessVendetta/Inventory/BaseItem.h | 42 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp create mode 100644 EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h diff --git a/EndlessVendetta/EndlessVendetta.uproject b/EndlessVendetta/EndlessVendetta.uproject index 8aea7a6c..1a1190f2 100644 --- a/EndlessVendetta/EndlessVendetta.uproject +++ b/EndlessVendetta/EndlessVendetta.uproject @@ -10,7 +10,8 @@ "LoadingPhase": "Default", "AdditionalDependencies": [ "Engine", - "AIModule" + "AIModule", + "CoreUObject" ] } ], diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp new file mode 100644 index 00000000..7418614d --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp @@ -0,0 +1,5 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "BaseItem.h" + diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h new file mode 100644 index 00000000..f2c70656 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h @@ -0,0 +1,42 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "UObject/NoExportTypes.h" +#include "BaseItem.generated.h" + +USTRUCT() +struct FItemSize +{ + GENERATED_BODY() + + int x; + int y; + + FItemSize(int _x, int _y) + { + x = _x; + y = _y; + } + FItemSize() + { + x = 1; + y = 1; + } +}; + +/** + * + */ +UCLASS() +class ENDLESSVENDETTA_API UBaseItem : public UObject +{ + GENERATED_BODY() + +public: + + const FName ItemName; + const FText Description; + const FItemSize ItemSize = FItemSize(1, 1); +}; From 17ecf7a3e63e8f354ffee024dbd78cc3ab06fe57 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 13 Oct 2023 20:10:51 +0100 Subject: [PATCH 2/9] Update Base Item for BP Properties --- .../.idea.EndlessVendetta.dir/.idea/vcs.xml | 6 +++ .../AI/Enemy/Basic/BT_BasicEnemy.uasset | 4 +- .../EndlessVendetta/Inventory/BaseItem.h | 48 +++++++++++++------ 3 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 EndlessVendetta/.idea/.idea.EndlessVendetta.dir/.idea/vcs.xml diff --git a/EndlessVendetta/.idea/.idea.EndlessVendetta.dir/.idea/vcs.xml b/EndlessVendetta/.idea/.idea.EndlessVendetta.dir/.idea/vcs.xml new file mode 100644 index 00000000..6c0b8635 --- /dev/null +++ b/EndlessVendetta/.idea/.idea.EndlessVendetta.dir/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset b/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset index 45c0d2a1..615de8fc 100644 --- a/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset +++ b/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b818b3645e40ca126811ffdc0a3d59b41fec38fc50f493cf9a7f9b78aa41554e -size 60604 +oid sha256:26f40ffcc7b5be8434ade7524abecd41141b751f268291f5966f6e3ef6a3fc7a +size 63113 diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h index f2c70656..10d36545 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h @@ -6,37 +6,55 @@ #include "UObject/NoExportTypes.h" #include "BaseItem.generated.h" -USTRUCT() -struct FItemSize +USTRUCT(BlueprintType) +struct FItemSize { GENERATED_BODY() - int x; - int y; + int X; + int Y; - FItemSize(int _x, int _y) + FItemSize(const int _X, const int _Y) { - x = _x; - y = _y; + X = _X; + Y = _Y; } + FItemSize() { - x = 1; - y = 1; + X = 1; + Y = 1; } }; +UENUM(BlueprintType) +enum EItemRotation +{ + Horizontal UMETA(DisplayName = "Up"), + Vertical UMETA(DisplayName = "Down") +}; + /** * */ -UCLASS() +UCLASS(Abstract) class ENDLESSVENDETTA_API UBaseItem : public UObject { GENERATED_BODY() - -public: - const FName ItemName; - const FText Description; - const FItemSize ItemSize = FItemSize(1, 1); +public: + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + int ItemID; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + FName ItemName; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + FText Description; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + FItemSize ItemSize = FItemSize(); + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Instanced, Category = "Item") + TArray ItemTextures; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + TEnumAsByte CurrentItemRotation = Horizontal; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + TSubclassOf ItemActor; }; From 9bd3b0c4e86969f8ee4dba78447970199142822e Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 13 Oct 2023 20:45:24 +0100 Subject: [PATCH 3/9] Update Base Item for Rotated Properties --- EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h index 10d36545..ac76c9c4 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h @@ -52,9 +52,13 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") FItemSize ItemSize = FItemSize(); UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Instanced, Category = "Item") - TArray ItemTextures; + UTexture2D* ItemTexture; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Instanced, Category = "Item") + UTexture2D* ItemTextureRotated; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") TEnumAsByte CurrentItemRotation = Horizontal; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") TSubclassOf ItemActor; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + bool bIsRotated = false; }; From fbe820853ccfddf4ba58ba7860ef4c5252a293e1 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 13 Oct 2023 20:45:48 +0100 Subject: [PATCH 4/9] Add BP Widget Components for Inventory --- EndlessVendetta/Content/Inventory/BP_Inventory.uasset | 3 +++ EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset | 3 +++ EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 EndlessVendetta/Content/Inventory/BP_Inventory.uasset create mode 100644 EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset create mode 100644 EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset diff --git a/EndlessVendetta/Content/Inventory/BP_Inventory.uasset b/EndlessVendetta/Content/Inventory/BP_Inventory.uasset new file mode 100644 index 00000000..ab185334 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/BP_Inventory.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a466d5d3690801264bd7549de93612bd5f10b0f8d8c26d9cece9d3d7105c4245 +size 18589 diff --git a/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset b/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset new file mode 100644 index 00000000..55fe4f89 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0ac52209fdb5a1553d095b04258b3fb9cf99e566d2de4ce802cf2e8bbe2395c9 +size 18641 diff --git a/EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset b/EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset new file mode 100644 index 00000000..1071edc7 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bec37e4f48c1d422c6f9d506c98f65f4132318f29afeafe93f874c811eb42561 +size 18641 From c75125027b1158dffc17b4a859729f9e1b9ccc23 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 13 Oct 2023 21:25:01 +0100 Subject: [PATCH 5/9] Add Structs for Inventory Grid --- .../Content/Inventory/BP_Inventory.uasset | 4 +-- .../Content/Inventory/BP_InventoryGrid.uasset | 4 +-- .../EndlessVendetta/Inventory/BaseItem.h | 1 - .../Inventory/InventoryStructs.h | 28 +++++++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h diff --git a/EndlessVendetta/Content/Inventory/BP_Inventory.uasset b/EndlessVendetta/Content/Inventory/BP_Inventory.uasset index ab185334..fcd21dcf 100644 --- a/EndlessVendetta/Content/Inventory/BP_Inventory.uasset +++ b/EndlessVendetta/Content/Inventory/BP_Inventory.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a466d5d3690801264bd7549de93612bd5f10b0f8d8c26d9cece9d3d7105c4245 -size 18589 +oid sha256:72974c08342cd605641d080348b7871cb6ae40cb6057ca15daf6071195a9bc05 +size 27656 diff --git a/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset b/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset index 55fe4f89..03c7d869 100644 --- a/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset +++ b/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0ac52209fdb5a1553d095b04258b3fb9cf99e566d2de4ce802cf2e8bbe2395c9 -size 18641 +oid sha256:50072ca6628bee1c25749aa7a98ebdf1d06ba835595140aea8f8e3f672722707 +size 26373 diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h index ac76c9c4..3c6cc7a1 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h @@ -3,7 +3,6 @@ #pragma once #include "CoreMinimal.h" -#include "UObject/NoExportTypes.h" #include "BaseItem.generated.h" USTRUCT(BlueprintType) diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h new file mode 100644 index 00000000..a72a3d65 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h @@ -0,0 +1,28 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "InventoryStructs.generated.h" + +/** + * + */ + +USTRUCT(BlueprintType) +struct FInventoryLines +{ + GENERATED_BODY() + + FVector2D Start; + FVector2D End; +}; + +USTRUCT(BlueprintType) +struct FInventoryTiles +{ + GENERATED_BODY() + + int X; + int Y; +}; From 5d009904f862d5c9bc0eea0f58a7141edc263f7b Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Wed, 18 Oct 2023 18:34:05 +0100 Subject: [PATCH 6/9] Add Function Stubs for Inventory Component --- .../Inventory/InventoryComponent.cpp | 79 +++++++++++++++++++ .../Inventory/InventoryComponent.h | 50 ++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp create mode 100644 EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp new file mode 100644 index 00000000..7f48f011 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp @@ -0,0 +1,79 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "InventoryComponent.h" + + +// Sets default values for this component's properties +UInventoryComponent::UInventoryComponent() +{ + // 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 = true; + + // ... +} + + +// Called when the game starts +void UInventoryComponent::BeginPlay() +{ + Super::BeginPlay(); + + // ... + +} + + +// Called every frame +void UInventoryComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) +{ + Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + + // ... +} + +bool UInventoryComponent::AddItem(UBaseItem* Item) +{ +} + +void UInventoryComponent::LineTraceForItemCheck() +{ +} + +void UInventoryComponent::Pickup(UBaseItem* Item) +{ +} + +bool UInventoryComponent::IsRoomAvailable(UBaseItem* Item, int TopLeftIndex) +{ +} + +FVector UInventoryComponent::IndexToTile(int Index) +{ +} + +UBaseItem* UInventoryComponent::GetItemAtIndex(int Index) +{ +} + +FInventoryTiles UInventoryComponent::TileToIndex(int Index) +{ +} + +void UInventoryComponent::AddItemAt(UBaseItem* Item, int TopLeftIndex) +{ +} + +void UInventoryComponent::GetAllItems() +{ +} + +void UInventoryComponent::RemoveItem(UBaseItem* Item) +{ +} + +void UInventoryComponent::SpawnItem(UBaseItem* Item, FVector Location, FRotator Rotation) +{ +} + diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h new file mode 100644 index 00000000..0744afb0 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h @@ -0,0 +1,50 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Components/ActorComponent.h" +#include "InventoryStructs.h" +#include "InventoryComponent.generated.h" + + +UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) +class ENDLESSVENDETTA_API UInventoryComponent : public UActorComponent +{ + GENERATED_BODY() + +public: + // Sets default values for this component's properties + UInventoryComponent(); + +protected: + // Called when the game starts + virtual void BeginPlay() override; + +public: + // Called every frame + virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; + + UFUNCTION(BlueprintCallable, Category="Inventory") + bool AddItem(class UBaseItem* Item); + UFUNCTION(BlueprintCallable, Category="Inventory") + void LineTraceForItemCheck(); + UFUNCTION(BlueprintCallable, Category="Inventory") + void Pickup(class UBaseItem* Item); + UFUNCTION(BlueprintCallable, Category="Inventory") + bool IsRoomAvailable(class UBaseItem* Item, int TopLeftIndex); + UFUNCTION(BlueprintCallable, Category="Inventory") + FVector IndexToTile(int Index); + UFUNCTION(BlueprintCallable, Category="Inventory") + UBaseItem* GetItemAtIndex(int Index); + UFUNCTION(BlueprintCallable, Category="Inventory") + FInventoryTiles TileToIndex(int Index); + UFUNCTION(BlueprintCallable, Category="Inventory") + void AddItemAt(class UBaseItem* Item, int TopLeftIndex); + UFUNCTION(BlueprintCallable, Category="Inventory") + void GetAllItems(); + UFUNCTION(BlueprintCallable, Category="Inventory") + void RemoveItem(class UBaseItem* Item); + UFUNCTION(BlueprintCallable, Category="Inventory") + void SpawnItem(class UBaseItem* Item, FVector Location, FRotator Rotation); +}; From 6609476eecaf86bd8b9b1405539bb1a4cf13411d Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 20 Oct 2023 22:59:54 +0100 Subject: [PATCH 7/9] Update Inventory Component for Helper Functions --- .../Inventory/InventoryComponent.cpp | 78 +++++++++++++++++-- .../Inventory/InventoryComponent.h | 33 ++++++-- .../Inventory/InventoryStructs.h | 4 +- 3 files changed, 98 insertions(+), 17 deletions(-) diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp index 7f48f011..260d4637 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp @@ -3,6 +3,8 @@ #include "InventoryComponent.h" +#include "BaseItem.h" + // Sets default values for this component's properties UInventoryComponent::UInventoryComponent() @@ -21,7 +23,6 @@ void UInventoryComponent::BeginPlay() Super::BeginPlay(); // ... - } @@ -41,39 +42,100 @@ void UInventoryComponent::LineTraceForItemCheck() { } -void UInventoryComponent::Pickup(UBaseItem* Item) +void UInventoryComponent::Pickup() { + //TODO: Add pickup logic and create pickup Actor + if (!IsValid(ItemToPickup)) return; } -bool UInventoryComponent::IsRoomAvailable(UBaseItem* Item, 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 j = 0; j < IndexToTile(TopLeftIndex).Y + (Item->ItemSize.Y - 1); j++) + { + FInventoryTile TileToCheck; + TileToCheck.X = i; + TileToCheck.Y = j; + if (!IsTileValid(TileToCheck)) return false; + const TTuple ItemAtIndex = GetItemAtIndex(TileToIndex(TileToCheck)); + if (ItemAtIndex.Get<1>()) return false; + if (IsValid(ItemAtIndex.Get<0>())) return false; + } + } + return true; } -FVector UInventoryComponent::IndexToTile(int Index) +FInventoryTile UInventoryComponent::IndexToTile(const int Index) const { + FInventoryTile Tile; + Tile.X = Index % Columns; + Tile.Y = Index / Columns; + return Tile; } -UBaseItem* UInventoryComponent::GetItemAtIndex(int Index) +TTuple UInventoryComponent::GetItemAtIndex(const int Index) { + if (!InventoryItems.IsValidIndex(Index)) return std::make_tuple(nullptr, false); + return std::make_tuple(InventoryItems[Index], true); } -FInventoryTiles UInventoryComponent::TileToIndex(int Index) +int UInventoryComponent::TileToIndex(const FInventoryTile InventoryTile) const { + return InventoryTile.X + InventoryTile.Y * Columns; } -void UInventoryComponent::AddItemAt(UBaseItem* Item, 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 j = 0; j < IndexToTile(TopLeftIndex).Y + (Item->ItemSize.Y - 1); j++) + { + FInventoryTile TileToCheck; + TileToCheck.X = i; + TileToCheck.Y = j; + if (!IsTileValid(TileToCheck)) return; + InventoryItems.Insert(Item, TileToIndex(TileToCheck)); + } + } + IsDirty = true; } -void UInventoryComponent::GetAllItems() +TMap UInventoryComponent::GetAllItems() { + TMap Items; + for (int i = 0; i < InventoryItems.Num(); i++) + { + UBaseItem* Item = InventoryItems[i]; + if (!IsValid(Item)) continue; + if (!Items.Contains(Item)) continue; + Items.Add(Item, IndexToTile(i)); + } + return Items; } void UInventoryComponent::RemoveItem(UBaseItem* Item) { + if (!IsValid(Item)) return; + for (int i = 0; i < InventoryItems.Num(); i++) + { + if (InventoryItems[i] == Item) + { + InventoryItems.RemoveAt(i); + IsDirty = true; + } + } } void UInventoryComponent::SpawnItem(UBaseItem* Item, FVector Location, FRotator Rotation) { } +bool UInventoryComponent::IsTileValid(const FInventoryTile InventoryTile) const +{ + if (InventoryTile.X >= 0 && InventoryTile.Y >= 0 && InventoryTile.Y < Columns && InventoryTile.Y < Rows) + { + return true; + } + return false; +} diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h index 0744afb0..8aee354c 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h @@ -5,6 +5,7 @@ #include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "InventoryStructs.h" +#include "EndlessVendetta/EndlessVendettaCharacter.h" #include "InventoryComponent.generated.h" @@ -17,6 +18,13 @@ public: // Sets default values for this component's properties UInventoryComponent(); + UPROPERTY(EditDefaultsOnly, BlueprintRead, Category="Inventory") + int Columns = 10; + UPROPERTY(EditDefaultsOnly, BlueprintRead, Category="Inventory") + int Rows = 10; + UPROPERTY(EditDefaultsOnly, BlueprintRead, Category="Inventory") + float MaxPickupDistance = 200.0f; + protected: // Called when the game starts virtual void BeginPlay() override; @@ -30,21 +38,32 @@ public: UFUNCTION(BlueprintCallable, Category="Inventory") void LineTraceForItemCheck(); UFUNCTION(BlueprintCallable, Category="Inventory") - void Pickup(class UBaseItem* Item); + void Pickup(); UFUNCTION(BlueprintCallable, Category="Inventory") - bool IsRoomAvailable(class UBaseItem* Item, int TopLeftIndex); + bool IsRoomAvailable(class UBaseItem* Item, const int TopLeftIndex); UFUNCTION(BlueprintCallable, Category="Inventory") - FVector IndexToTile(int Index); + FInventoryTile IndexToTile(const int Index) const; UFUNCTION(BlueprintCallable, Category="Inventory") - UBaseItem* GetItemAtIndex(int Index); + TTuple GetItemAtIndex(const int Index); UFUNCTION(BlueprintCallable, Category="Inventory") - FInventoryTiles TileToIndex(int Index); + int TileToIndex(const FInventoryTile InventoryTile) const; UFUNCTION(BlueprintCallable, Category="Inventory") - void AddItemAt(class UBaseItem* Item, int TopLeftIndex); + void AddItemAt(class UBaseItem* Item, const int TopLeftIndex); UFUNCTION(BlueprintCallable, Category="Inventory") - void GetAllItems(); + TMap GetAllItems(); UFUNCTION(BlueprintCallable, Category="Inventory") void RemoveItem(class UBaseItem* Item); UFUNCTION(BlueprintCallable, Category="Inventory") void SpawnItem(class UBaseItem* Item, FVector Location, FRotator Rotation); + +private: + bool IsTileValid(const FInventoryTile InventoryTile) const; + + UPROPERTY() + AEndlessVendettaCharacter* PlayerCharacter; + UPROPERTY() + AActor* ItemToPickup; + bool IsDirty = false; + UPROPERTY() + TArray InventoryItems; }; diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h index a72a3d65..742bae19 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h @@ -10,7 +10,7 @@ */ USTRUCT(BlueprintType) -struct FInventoryLines +struct FInventoryLine { GENERATED_BODY() @@ -19,7 +19,7 @@ struct FInventoryLines }; USTRUCT(BlueprintType) -struct FInventoryTiles +struct FInventoryTile { GENERATED_BODY() From 36daaaed16c3f6e6685f996dbb6a408c6b49325e Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 20 Oct 2023 23:10:10 +0100 Subject: [PATCH 8/9] Bugfix TTuple Error Cannot be Macroed --- .../EndlessVendetta/Inventory/InventoryComponent.cpp | 5 +++-- .../Source/EndlessVendetta/Inventory/InventoryComponent.h | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp index 260d4637..5378ddcb 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp @@ -36,6 +36,7 @@ void UInventoryComponent::TickComponent(float DeltaTime, ELevelTick TickType, FA bool UInventoryComponent::AddItem(UBaseItem* Item) { + return false; } void UInventoryComponent::LineTraceForItemCheck() @@ -76,8 +77,8 @@ FInventoryTile UInventoryComponent::IndexToTile(const int Index) const TTuple UInventoryComponent::GetItemAtIndex(const int Index) { - if (!InventoryItems.IsValidIndex(Index)) return std::make_tuple(nullptr, false); - return std::make_tuple(InventoryItems[Index], true); + if (!InventoryItems.IsValidIndex(Index)) return MakeTuple(nullptr, false); + return MakeTuple(InventoryItems[Index], true); } int UInventoryComponent::TileToIndex(const FInventoryTile InventoryTile) const diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h index 8aee354c..608455c0 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h @@ -18,11 +18,11 @@ public: // Sets default values for this component's properties UInventoryComponent(); - UPROPERTY(EditDefaultsOnly, BlueprintRead, Category="Inventory") + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory") int Columns = 10; - UPROPERTY(EditDefaultsOnly, BlueprintRead, Category="Inventory") + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory") int Rows = 10; - UPROPERTY(EditDefaultsOnly, BlueprintRead, Category="Inventory") + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory") float MaxPickupDistance = 200.0f; protected: @@ -43,7 +43,7 @@ public: bool IsRoomAvailable(class UBaseItem* Item, const int TopLeftIndex); UFUNCTION(BlueprintCallable, Category="Inventory") FInventoryTile IndexToTile(const int Index) const; - UFUNCTION(BlueprintCallable, Category="Inventory") + //UFUNCTION(BlueprintCallable, Category="Inventory") TTuple GetItemAtIndex(const int Index); UFUNCTION(BlueprintCallable, Category="Inventory") int TileToIndex(const FInventoryTile InventoryTile) const; From 8cfe3b97a9dc87e3ee76149f88c75e3919add944 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Mon, 23 Oct 2023 06:35:59 +0100 Subject: [PATCH 9/9] Update Inventory Component for UI [WIP] --- .../Blueprints/BP_FirstPersonCharacter.uasset | 4 +- .../FirstPerson/Blueprints/BP_PC.uasset | 3 + .../FirstPerson/GunMechanicWorld.uasset | 4 +- .../Input/Actions/IA_Inventory.uasset | 3 + .../FirstPerson/Input/IMC_Default.uasset | 4 +- .../Content/Inventory/BP_Inventory.uasset | 3 - .../Content/Inventory/BP_InventoryGrid.uasset | 3 - .../Content/Inventory/BP_InventoryItem.uasset | 3 - .../Content/Inventory/M_Base.uasset | 3 + .../Content/Inventory/MyBaseItem.uasset | 3 + .../Content/Inventory/UI_Inventory.uasset | 3 + .../Content/Inventory/UI_InventoryGrid.uasset | 3 + .../Content/Inventory/UI_ItemWidget.uasset | 3 + .../Content/Inventory/zeldamus.uasset | 3 + .../EndlessVendetta/Inventory/BaseItem.cpp | 24 +++++++ .../EndlessVendetta/Inventory/BaseItem.h | 21 ++++-- .../Inventory/BaseWorldItem.cpp | 26 ++++++++ .../EndlessVendetta/Inventory/BaseWorldItem.h | 25 +++++++ .../Inventory/InventoryComponent.cpp | 66 ++++++++++++++++--- .../Inventory/InventoryComponent.h | 8 ++- .../Inventory/InventoryStructs.h | 6 +- 21 files changed, 188 insertions(+), 33 deletions(-) create mode 100644 EndlessVendetta/Content/FirstPerson/Blueprints/BP_PC.uasset create mode 100644 EndlessVendetta/Content/FirstPerson/Input/Actions/IA_Inventory.uasset delete mode 100644 EndlessVendetta/Content/Inventory/BP_Inventory.uasset delete mode 100644 EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset delete mode 100644 EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset create mode 100644 EndlessVendetta/Content/Inventory/M_Base.uasset create mode 100644 EndlessVendetta/Content/Inventory/MyBaseItem.uasset create mode 100644 EndlessVendetta/Content/Inventory/UI_Inventory.uasset create mode 100644 EndlessVendetta/Content/Inventory/UI_InventoryGrid.uasset create mode 100644 EndlessVendetta/Content/Inventory/UI_ItemWidget.uasset create mode 100644 EndlessVendetta/Content/Inventory/zeldamus.uasset create mode 100644 EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.cpp create mode 100644 EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.h diff --git a/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset b/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset index e7dcb857..eb7c9e38 100644 --- a/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset +++ b/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:65d9f1286e3e2a0afeb00fd61e2a4ad5d105f8c4ea61cf146761901c810ea4bb -size 42451 +oid sha256:b16d2dc2079c962cb71ea8ce5309aa078115d780d3dff8fe944447f9b39e2362 +size 62566 diff --git a/EndlessVendetta/Content/FirstPerson/Blueprints/BP_PC.uasset b/EndlessVendetta/Content/FirstPerson/Blueprints/BP_PC.uasset new file mode 100644 index 00000000..e3cbe293 --- /dev/null +++ b/EndlessVendetta/Content/FirstPerson/Blueprints/BP_PC.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0ed077fa7c656b64c3f81cb8812cc4fc339d8b4f470dbcb1426f799bb1977438 +size 109892 diff --git a/EndlessVendetta/Content/FirstPerson/GunMechanicWorld.uasset b/EndlessVendetta/Content/FirstPerson/GunMechanicWorld.uasset index 551d8f69..921b01db 100644 --- a/EndlessVendetta/Content/FirstPerson/GunMechanicWorld.uasset +++ b/EndlessVendetta/Content/FirstPerson/GunMechanicWorld.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:50316ead250bb810a80831ade33e194f2de7ccb433dc172e5217540998e2854e -size 19098 +oid sha256:f01d66661ebe4dc7038e9105e8c156e3186fdaeafea8e59cfbcbc7dde0741029 +size 19619 diff --git a/EndlessVendetta/Content/FirstPerson/Input/Actions/IA_Inventory.uasset b/EndlessVendetta/Content/FirstPerson/Input/Actions/IA_Inventory.uasset new file mode 100644 index 00000000..385c9c5f --- /dev/null +++ b/EndlessVendetta/Content/FirstPerson/Input/Actions/IA_Inventory.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab49ececebc9a9d1d7038a1901d08caef22732ffc625ab37a8f71f54846a4809 +size 1375 diff --git a/EndlessVendetta/Content/FirstPerson/Input/IMC_Default.uasset b/EndlessVendetta/Content/FirstPerson/Input/IMC_Default.uasset index 2bc21ec7..00f26444 100644 --- a/EndlessVendetta/Content/FirstPerson/Input/IMC_Default.uasset +++ b/EndlessVendetta/Content/FirstPerson/Input/IMC_Default.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e84853e2ab5cb8653ef3686fdd8ce458fbadd1261807bdc73feb5212015290ce -size 17496 +oid sha256:78fa3a2622e7e2208ff72aebca44e7b2f57ae96c42dc2bb95ebeb1a87d6b696b +size 18080 diff --git a/EndlessVendetta/Content/Inventory/BP_Inventory.uasset b/EndlessVendetta/Content/Inventory/BP_Inventory.uasset deleted file mode 100644 index fcd21dcf..00000000 --- a/EndlessVendetta/Content/Inventory/BP_Inventory.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:72974c08342cd605641d080348b7871cb6ae40cb6057ca15daf6071195a9bc05 -size 27656 diff --git a/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset b/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset deleted file mode 100644 index 03c7d869..00000000 --- a/EndlessVendetta/Content/Inventory/BP_InventoryGrid.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:50072ca6628bee1c25749aa7a98ebdf1d06ba835595140aea8f8e3f672722707 -size 26373 diff --git a/EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset b/EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset deleted file mode 100644 index 1071edc7..00000000 --- a/EndlessVendetta/Content/Inventory/BP_InventoryItem.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:bec37e4f48c1d422c6f9d506c98f65f4132318f29afeafe93f874c811eb42561 -size 18641 diff --git a/EndlessVendetta/Content/Inventory/M_Base.uasset b/EndlessVendetta/Content/Inventory/M_Base.uasset new file mode 100644 index 00000000..ff9c03b9 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/M_Base.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:46f740304c399e9d98a0a1744f320928201179685c6de52013d02fde640b0aa5 +size 21988 diff --git a/EndlessVendetta/Content/Inventory/MyBaseItem.uasset b/EndlessVendetta/Content/Inventory/MyBaseItem.uasset new file mode 100644 index 00000000..4c1d1f22 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/MyBaseItem.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a60300e2d3a093d49cc47288fadddb35de6038282d0a4a9ede6d695cbd09b2e +size 6303 diff --git a/EndlessVendetta/Content/Inventory/UI_Inventory.uasset b/EndlessVendetta/Content/Inventory/UI_Inventory.uasset new file mode 100644 index 00000000..c2322151 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/UI_Inventory.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3fa985cf21e27c882c72e15d3482c45829fcaefedf52ad4b287874b229287555 +size 87053 diff --git a/EndlessVendetta/Content/Inventory/UI_InventoryGrid.uasset b/EndlessVendetta/Content/Inventory/UI_InventoryGrid.uasset new file mode 100644 index 00000000..7d6cc8d5 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/UI_InventoryGrid.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b7e8fc53c8f2ae5213bda1d4b5758ccfcc7255b8577b5bd6abaa0fbbb558430b +size 568064 diff --git a/EndlessVendetta/Content/Inventory/UI_ItemWidget.uasset b/EndlessVendetta/Content/Inventory/UI_ItemWidget.uasset new file mode 100644 index 00000000..6773c468 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/UI_ItemWidget.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0d6849ec4eed10578619d45a3633b4d8540ce11c4b56ef00bfca43fe2429b480 +size 148756 diff --git a/EndlessVendetta/Content/Inventory/zeldamus.uasset b/EndlessVendetta/Content/Inventory/zeldamus.uasset new file mode 100644 index 00000000..840e1137 --- /dev/null +++ b/EndlessVendetta/Content/Inventory/zeldamus.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5616afc778197366258235add689576f3323e03e78a5ec49000fbac40dd6674b +size 745780 diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp index 7418614d..b79e1425 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.cpp @@ -3,3 +3,27 @@ #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; +} diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h index 3c6cc7a1..da7ed6d9 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseItem.h @@ -10,7 +10,9 @@ struct FItemSize { GENERATED_BODY() + UPROPERTY(BlueprintReadOnly, Category = "ItemSize") int X; + UPROPERTY(BlueprintReadOnly, Category = "ItemSize") int Y; FItemSize(const int _X, const int _Y) @@ -29,14 +31,14 @@ struct FItemSize UENUM(BlueprintType) enum EItemRotation { - Horizontal UMETA(DisplayName = "Up"), - Vertical UMETA(DisplayName = "Down") + Horizontal UMETA(DisplayName = "Horizontal"), + Vertical UMETA(DisplayName = "Vertical") }; /** * */ -UCLASS(Abstract) +UCLASS(Abstract, Blueprintable) class ENDLESSVENDETTA_API UBaseItem : public UObject { GENERATED_BODY() @@ -50,14 +52,19 @@ public: FText Description; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") FItemSize ItemSize = FItemSize(); - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Instanced, Category = "Item") - UTexture2D* ItemTexture; - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Instanced, Category = "Item") - UTexture2D* ItemTextureRotated; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + UMaterialInterface* ItemTexture; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") + UMaterialInterface* ItemTextureRotated; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") TEnumAsByte CurrentItemRotation = Horizontal; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") TSubclassOf ItemActor; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item") bool bIsRotated = false; + + UFUNCTION(BlueprintCallable, Category = "Item") + void RotateItem(); + UFUNCTION(BlueprintCallable, Category = "Item") + UMaterialInterface* GetItemTexture() const; }; diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.cpp b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.cpp new file mode 100644 index 00000000..ffa855c9 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.cpp @@ -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); +} + diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.h new file mode 100644 index 00000000..c83609d9 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/BaseWorldItem.h @@ -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; +}; diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp index 5378ddcb..5e8f39de 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.cpp @@ -4,6 +4,7 @@ #include "InventoryComponent.h" #include "BaseItem.h" +#include "Camera/CameraComponent.h" // 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 // off to improve performance if you don't need them. + bWantsInitializeComponent = true; PrimaryComponentTick.bCanEverTick = true; +} - // ... +void UInventoryComponent::InitializeComponent() +{ + Super::InitializeComponent(); + PlayerCharacter = Cast(GetOwner()); + ItemToPickup = nullptr; } @@ -22,7 +29,9 @@ void UInventoryComponent::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); - // ... + if (IsDirty) + { + IsDirty = false; + OnInventoryUpdated.Broadcast(); + } } 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; } -void UInventoryComponent::LineTraceForItemCheck() +void UInventoryComponent::ProjectTraceForItem() { + if (!IsValid(PlayerCharacter)) return; + FVector StartLocation = Cast(PlayerCharacter->GetComponentByClass(UCameraComponent::StaticClass()))->GetComponentLocation(); + FVector EndLocation = StartLocation + Cast(PlayerCharacter->GetComponentByClass(UCameraComponent::StaticClass()))->GetForwardVector() * MaxPickupDistance; + FCollisionQueryParams CollisionQueryParams; + CollisionQueryParams.AddIgnoredActor(PlayerCharacter); + if (FHitResult HitResult; GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility, CollisionQueryParams)) + { + if (Cast(HitResult.GetActor())) + { + ItemToPickup = HitResult.GetActor(); + Cast(ItemToPickup->GetComponentByClass(UStaticMeshComponent::StaticClass()))->SetRenderCustomDepth(true); + return; + } + } + if (!IsValid(ItemToPickup)) return; + Cast(ItemToPickup->GetComponentByClass(UStaticMeshComponent::StaticClass()))->SetRenderCustomDepth(false); } void UInventoryComponent::Pickup() @@ -51,9 +99,9 @@ void UInventoryComponent::Pickup() 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; TileToCheck.X = i; @@ -61,7 +109,7 @@ bool UInventoryComponent::IsRoomAvailable(UBaseItem* Item, const int TopLeftInde if (!IsTileValid(TileToCheck)) return false; const TTuple ItemAtIndex = GetItemAtIndex(TileToIndex(TileToCheck)); if (ItemAtIndex.Get<1>()) return false; - if (IsValid(ItemAtIndex.Get<0>())) return false; + if (IsValid(ItemAtIndex.Get<0>())) return true; } } return true; @@ -88,9 +136,9 @@ int UInventoryComponent::TileToIndex(const FInventoryTile InventoryTile) const 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; TileToCheck.X = i; diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h index 608455c0..5ba7ba28 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryComponent.h @@ -17,6 +17,7 @@ class ENDLESSVENDETTA_API UInventoryComponent : public UActorComponent public: // Sets default values for this component's properties UInventoryComponent(); + virtual void InitializeComponent() override; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory") int Columns = 10; @@ -25,6 +26,7 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory") float MaxPickupDistance = 200.0f; + protected: // Called when the game starts virtual void BeginPlay() override; @@ -33,10 +35,14 @@ public: // Called every frame 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") bool AddItem(class UBaseItem* Item); UFUNCTION(BlueprintCallable, Category="Inventory") - void LineTraceForItemCheck(); + void ProjectTraceForItem(); UFUNCTION(BlueprintCallable, Category="Inventory") void Pickup(); UFUNCTION(BlueprintCallable, Category="Inventory") diff --git a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h index 742bae19..6d0b93be 100644 --- a/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h +++ b/EndlessVendetta/Source/EndlessVendetta/Inventory/InventoryStructs.h @@ -14,7 +14,9 @@ struct FInventoryLine { GENERATED_BODY() + UPROPERTY(BlueprintReadWrite) FVector2D Start; + UPROPERTY(BlueprintReadWrite) FVector2D End; }; @@ -22,7 +24,9 @@ USTRUCT(BlueprintType) struct FInventoryTile { GENERATED_BODY() - + + UPROPERTY(BlueprintReadWrite) int X; + UPROPERTY(BlueprintReadWrite) int Y; };