-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.cpp
More file actions
165 lines (153 loc) · 4.58 KB
/
inventory.cpp
File metadata and controls
165 lines (153 loc) · 4.58 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "inventory.h"
#include "player.h"
#include "gameview.h"
#include "containingbox.h"
#include <QPainter>
#include <item.h>
#include <QDebug>
Inventory::Inventory(QColor color)
{
color_ = color;
}
QRectF Inventory::boundingRect() const
{
int height = int((consumable_items_.size()) + equipable_items_.size()) * 30;
return QRectF(x_,y_, width_, height);
}
/*
Coded By: Mikayla Pickett
Function: shape
Params: none
Desc: sets graph as square
Returns: QPainterPath
*/
QPainterPath Inventory::shape() const
{
int height = int((consumable_items_.size()) + equipable_items_.size()) * 30;
QPainterPath path;
path.addRect(x_,y_,width_,height);
return path;
}
/*
Coded By: Mikayla Pickett
Function: Paint
Params: QPainter, QStyleOptionGraphicsItem, QWidget
Desc: Draws the inventory
Returns: void
*/
void Inventory::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
int distance_between = 20;
int height = int((consumable_items_.size()) + equipable_items_.size()) * distance_between + 60;
QBrush b = painter->brush();
painter->setPen(Qt::GlobalColor::white);
painter->drawText(QPoint(this->x_, this->y_), "Inventory");
int current_y = this->y_;
int shifted_x = this->x_ + 10;
//For each consumable item, write the correct description and add it to the inventory
if (consumable_items_.size() > 0){
current_y += distance_between;
painter->drawText(QPoint(shifted_x, current_y), "Consumables:");
for(size_t i = 0; i < consumable_items_.size(); i ++) {
current_y += distance_between;
QString desc = QString::fromStdString(consumable_items_[i]->getDescription());
QString name = QString::fromStdString("[" + std::to_string(i + 1) + "] " + consumable_items_[i]->getName()) + ": " + desc;
painter->drawText(QPoint(shifted_x, current_y), name);
}
}
//For each equipable item, write the correct description and add it to the inventory.
if (equipable_items_.size() > 0) {
current_y += distance_between;
painter->drawText(QPoint(shifted_x, current_y), "Equipped:");
for(size_t i = 0; i < equipable_items_.size(); i ++) {
current_y += distance_between;
QString desc = QString::fromStdString(equipable_items_[i]->getDescription());
QString name = QString::fromStdString(equipable_items_[i]->getName()) + ": " + desc;
painter->drawText(QPoint(shifted_x, current_y), name);
}
}
painter->setPen(color_);
painter->drawRect(QRect(this->x_, this->y_, this-> width_, height));
painter->setBrush(b);
}
/*
Function: setPos
Params: int x, int y
Desc: Given points, set inventory to that point.
Returns: none
*/
void Inventory::setPos(int x, int y) {
x_ = x;
y_ = y;
}
/*
Function: GetItem
Params: int id, item id
Desc: Given an item id, returns a pointer to the correct item
Returns: pointer to item
*/
Item * Inventory::GetItem(int id) {
if (id < consumable_items_.size() && id >=0) {
return consumable_items_[id];
}
return nullptr;
}
/*
Function: RemoveItem
Params: int id
Desc: Given an item id, remove that item from the inventory.
Returns: none
*/
void Inventory::RemoveItem(int id) {
consumable_items_.erase(consumable_items_.begin() + id);
}
/*
Function: AddItem
Params: Item, bool underworld
Desc: Adds an item to the players inventory
Returns: none
*/
void Inventory::AddItem(Item *item, bool underworld)
{
// PopupText(item, underworld);
if (item->getItemType() == itemtype::Consumable) {
consumable_items_.push_back(item);
QString q = QString::fromStdString(item->getName());
//qDebug() << q;
} else {
equipable_items_.push_back(item);
}
}
/*
Function: PopupText
Params: Item to be described
Desc: Given an item, creates a popup text that describes the item received
Returns: none
*/
void Inventory::PopupText(Item *item, bool underworld)
{
if (underworld) {
return;
}
GameView &game = GameView::GetInstance();
std::string text= "You got a(n) " + item->getName() + ". " + item->getDescription();
ContainingBox *box = new ContainingBox(0,game.scene->height(),game.scene->width(),300,Qt::GlobalColor::white, text);
game.scene->addItem(box);
QTimer::singleShot(1000,[=](){
clearPopup(box);
});
}
/*
Function: clearPopup
Params: Containingbox
Desc: Removes given box from the scene
Returns: none
*/
void Inventory::clearPopup(ContainingBox *box)
{
//timer_->stop();
GameView &game = GameView::GetInstance();
if (!game.switching_to_underworld_){
game.scene->removeItem(box);
}
}