-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryContainer.cpp
149 lines (118 loc) · 3.31 KB
/
InventoryContainer.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Fill out your copyright notice in the Description page of Project Settings.
#include "rpg.h"
#include "UnrealNetwork.h"
#include "InventoryContainer.h"
// Sets default values
AInventoryContainer::AInventoryContainer()
{
// 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 AInventoryContainer::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AInventoryContainer::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
//Our network replication here.
void AInventoryContainer::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AInventoryContainer, Items);
}
//The system allows for an auto stack creation method.
bool AInventoryContainer::AddItem(TSubclassOf<AItemBase> Item, int Quantity) {
auto obj = Item.GetDefaultObject();
bool CreatedNewStack = false;
if (obj->Stackable && GetItemCount(Item) > 0) {
TArray<ItemDistributor> AvailableSlots;
int QuantityRemaining = Quantity;
int MaxStack = obj->MaxStack;
for (int i = 0; i < Items.Num(); i++) {
if (Items[i].Reference == Item && QuantityRemaining > 0) {
if (Items[i].Quantity + QuantityRemaining <= MaxStack) {
ItemDistributor Ref;
Ref.Slot = i;
Ref.QuantityToFill = Items[i].Quantity + QuantityRemaining;
AvailableSlots.Add(Ref);
QuantityRemaining = 0;
break;
}
else
{
ItemDistributor Ref;
Ref.Slot = i;
Ref.QuantityToFill = MaxStack;
AvailableSlots.Add(Ref);
QuantityRemaining = QuantityRemaining - (MaxStack - Items[i].Quantity);
}
}
else { break; }
}
if (QuantityRemaining > 0) {
while (QuantityRemaining > 0) {
ItemDistributor Ref;
Ref.CreateNewSlot = true;
if (QuantityRemaining - MaxStack <= 0) {
Ref.QuantityToFill = QuantityRemaining - MaxStack;
QuantityRemaining = 0;
break;
}
else {
Ref.QuantityToFill = MaxStack;
QuantityRemaining = QuantityRemaining - MaxStack;
}
AvailableSlots.Add(Ref);
}
}
for (ItemDistributor Dist : AvailableSlots) {
if (!Dist.CreateNewSlot) {
Items[Dist.Slot].Quantity = Dist.QuantityToFill;
}
else {
FItemData LocalizedData;
LocalizedData = obj->Initialize(Dist.QuantityToFill, obj->Name);
LocalizedData.Reference = Item;
Items.Add(LocalizedData);
CreatedNewStack = true;
}
}
}
else
{
FItemData LocalizedData;
LocalizedData = obj->Initialize(Quantity, obj->Name);
LocalizedData.Reference = Item;
Items.Add(LocalizedData);
CreatedNewStack = true;
}
return CreatedNewStack;
}
//Simple function primarily used internally.
int AInventoryContainer::GetSlot(TSubclassOf<AItemBase> Item) {
for (int i = 0; i < Items.Num(); i++) {
if (Items[i].Reference == Item) {
return i;
}
}
return -1;
}
int AInventoryContainer::GetItemCount(TSubclassOf<AItemBase> Item) {
if (GetSlot(Item) != -1) {
int Count = 0;
for (int i = 0; i < Items.Num(); i++) {
if (Items[i].Reference == Item) {
Count = Count + Items[i].Quantity;
}
}
return Count;
}
else { return 0; }
}
bool AInventoryContainer::HasItem(TSubclassOf<AItemBase> Item) {
return GetItemCount(Item) > 0;
}