/
BackpackComponent.cpp
128 lines (103 loc) · 2.88 KB
/
BackpackComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Orfeas Eleftheriou 2016
#include "ProjectNA.h"
#include "BackpackComponent.h"
#include "Public/Controllers/MainCharacterController.h"
#include "Public/Character/MainCharacter.h"
DEFINE_LOG_CATEGORY_STATIC(BackpackComponent, All, All);
// Sets default values for this component's properties
UBackpackComponent::UBackpackComponent()
{
// 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.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UBackpackComponent::BeginPlay()
{
Super::BeginPlay();
BackpackItems.SetNum(MaxBackpackItems);
}
// Called every frame
void UBackpackComponent::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
// ...
}
bool UBackpackComponent::AddItemToBackpack(ABaseItem* ItemToAdd)
{
const AMainCharacter* MainCharacter = Cast<AMainCharacter>(GetOwner());
for (int32 i = 0; i < BackpackItems.Num(); i++)
{
if (!BackpackItems[i])
{
BackpackItems[i] = ItemToAdd;
MainCharacter->GetActionsTutorialComponent()->DisplayTutorialMessage(ETutorialMessageType::EPickupTutorial);
return true;
}
}
return false;
}
void UBackpackComponent::RemoveItemFromBackpack(ABaseItem* ItemToRemove)
{
EquipItem(nullptr);
for (int32 i = 0; i < BackpackItems.Num(); i++)
{
if (BackpackItems[i] && BackpackItems[i] == ItemToRemove)
{
BackpackItems[i] = nullptr;
}
}
}
void UBackpackComponent::RemoveItemFromBackpack(FString NameOfItemToRemove)
{
EquipItem(nullptr);
for (int32 i = 0; i < BackpackItems.Num();i++)
{
if (BackpackItems[i] && BackpackItems[i]->GetItemName().Equals(NameOfItemToRemove))
{
BackpackItems[i] = nullptr;
}
}
}
bool UBackpackComponent::ContainsItem(FString NameOfItem, ABaseItem*& FoundItem)
{
if (!NameOfItem.IsEmpty())
{
for (int32 i = 0; i < BackpackItems.Num();i++)
{
if (BackpackItems[i] && BackpackItems[i]->GetItemName().Equals(NameOfItem))
{
FoundItem = BackpackItems[i];
return true;
}
}
return false;
}
return true;
}
void UBackpackComponent::EquipItem(UTexture2D* TextureOfItem)
{
if (TextureOfItem)
{
EquippedItem = *BackpackItems.FindByPredicate([&TextureOfItem](ABaseItem* CurrentItem)
{
return (CurrentItem && CurrentItem->GetItemTexture() == TextureOfItem) ? CurrentItem : nullptr;
});
}
else
{
EquippedItem = nullptr;
}
Cast<AMainCharacterController>(UGameplayStatics::GetPlayerController(GetWorld(), 0))->SetEquippedItem(EquippedItem);
}
bool UBackpackComponent::GetEquippedItem(ABaseItem*& CurrentlyEquippedItem)
{
CurrentlyEquippedItem = this->EquippedItem;
return (CurrentlyEquippedItem) ? true : false;
}
void UBackpackComponent::RemoveEquippedItem()
{
EquipItem(nullptr);
}