-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrack.cpp
192 lines (153 loc) · 6 KB
/
Crack.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "Crack.hpp"
Crack::Crack(QWidget* parent, QStackedWidget *stackedWidget, Inventory *inventory)
: QWidget(parent), stackedWidget(stackedWidget), inventory(inventory)
{
// generate random pet:
rand_rarity = generateNumber();
pet_list = generatePets();
random_pet = generateRandomPet(pet_list, rand_rarity);
// updates between 2 sprites of random pet
random_pet_sprite = random_pet.getSprite();
// Create buttons
crackEgg = new QPushButton(QIcon(":sprites/Eggo.png"), "", this);
// Set button size to match the size of the sprites
QSize sizeButton(140, 130);
QSize sizeIcon(920, 300);
QString buttonStyleSheet = "QPushButton {"
" background-color: transparent;"
" border: none;"
"}";
crackEgg->setIconSize(sizeIcon);
crackEgg->setFixedSize(sizeButton);
crackEgg->setStyleSheet(buttonStyleSheet);
// Create egg and coin count labels
egg_count_label = new QLabel("", this);
coin_count_label = new QLabel("", this);
// Update the label texts with the current egg and coin counts
int egg_count = inventory->getEggCount();
int coin_count = inventory->getCoinCount();
egg_count_label->setText(QString("Egg: %1").arg(egg_count));
coin_count_label->setText(QString("Coin: %1").arg(coin_count));
// Create label for the text
label = new QLabel("Click to Crack~", this);
label->setAlignment(Qt::AlignCenter);
loop = 0;
// Connect the clicked signal of crackEgg to a lambda function that updates the egg and coin counts and label texts
connect(crackEgg, &QPushButton::clicked, this, &Crack::crackEggButton);
QTimer* loop_timer = new QTimer(this);
connect(loop_timer, &QTimer::timeout, this, &Crack::updateLoop);
loop_timer->start(50);
QTimer* sprite_timer = new QTimer(this);
connect(sprite_timer, &QTimer::timeout, this, &Crack::updatePetSprites);
sprite_timer->start(500);
// Create return button
returnButton = new QPushButton(QIcon(":sprites/Ret.png"), "", this);
// returnButton->move(10, 10);
connect(returnButton, &QPushButton::clicked, this, &Crack::on_returnButton_clicked);
returnButton->setStyleSheet(buttonStyleSheet);
// Set button size to match the size of the sprites
QSize ret_sizeButton(50, 50);
QSize ret_sizeIcon(100, 80);
returnButton->setIconSize(ret_sizeIcon);
returnButton->setFixedSize(ret_sizeButton);
// Create layout for crackEgg and center it
QVBoxLayout *layout1 = new QVBoxLayout();
layout1->addWidget(crackEgg);
layout1->addWidget(label);
layout1->setAlignment(Qt::AlignCenter);
// Create layout for returnButton and align it to the top left corner
QHBoxLayout *layout2 = new QHBoxLayout();
layout2->addWidget(returnButton);
layout2->addWidget(egg_count_label);
layout2->addWidget(coin_count_label);
layout2->setAlignment(Qt::AlignTop | Qt::AlignLeft);
// Create main layout and add the two layouts to it
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(layout2);
mainLayout->addLayout(layout1);
setLayout(mainLayout);
}
void Crack::returnToMain(){
loop = 0;
stackedWidget->setCurrentIndex(0);
}
void Crack::on_returnButton_clicked() {
// Emit a signal to indicate that the return button was clicked
emit returnToMain();
}
void Crack::crackEggButton(){
loop++;
if(loop == 1){
if(inventory->removeEgg() != 0) {
loop = 0;
}
} else if(loop == 2){
// generate random pet:
rand_rarity = generateNumber();
random_pet = generateRandomPet(pet_list, rand_rarity);
// std::cout << "size: " << inventory->user_list.size() << std::endl;
inventory->mutex.lockForWrite();
inventory->user_list.push_back(random_pet);
inventory->mutex.unlock();
// std::cout << "name: " << inventory->user_list.back().getName() << std::endl;
} else if(loop == 3){
// go back to cracking an egg
loop = 0;
}
}
void Crack::updateLoop(){
if(loop == 1){
// Change the button sprite when the last egg is removed
QPixmap newIcon(":sprites/EggCrack.png");
crackEgg->setIcon(QIcon(newIcon));
} else if(loop == 2){
// updates between 2 sprites of random pet
QString random_pet_name = QString::fromStdString(random_pet.getName());
label->setText(QString("You got a ") + random_pet_name);
random_pet_sprite = random_pet.getSprite();
crackEgg->setIcon(QIcon(random_pet_sprite));
} else if(loop == 0){
QPixmap newIcon(":sprites/Eggo.png");
crackEgg->setIcon(QIcon(newIcon));
label->setText(QString("Click to crack~"));
}
// Update the egg and coin counts
int egg_count = inventory->getEggCount();
int coin_count = inventory->getCoinCount();
// Update the label texts
egg_count_label->setText(QString("Egg: %1").arg(egg_count));
coin_count_label->setText(QString("Coin: %1").arg(coin_count));
}
void Crack::updatePetSprites()
{
if(loop == 2){
// updates between 2 sprites of random pet
random_pet.updateSprite();
}
}
int Crack::generateNumber(){
std::random_device rd;
std::mt19937 gen(rd());
// Set the probability of generating a 0 to 0.5
std::geometric_distribution<int> dist(0.3);
// Generate a number using the distribution
int number = dist(gen);
// Cap the number at 10 to ensure it is within the range 0-10
number = std::min(number, 10);
return number;
}
Pet Crack::generateRandomPet(std::vector<Pet> petlist, int rarity)
{
std::vector<Pet> rarity_pets;
for(auto pet : petlist)
{
if(pet.getRarity() == rarity){
rarity_pets.push_back(pet);
}
}
if(rarity_pets.empty()){
std::cout << "not ennough rarities: fix pets list" << std::endl;
}
int random_index = rand() % rarity_pets.size();
return rarity_pets[random_index];
}