-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawStack.cpp
196 lines (165 loc) · 6.44 KB
/
DrawStack.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
193
194
195
196
// Author: Annie Berend (5033782) - Jonathan Verbeek (5058288)
#include "DrawStack.h"
#include "Main.h"
#include "CardVBoxLayout.h"
#include <QDebug>
CDrawStack::CDrawStack(QWidget* parent)
: CCardStack(parent)
{
// Create the dummy vbox layout with zero spacing, so they get overlayed
hbox = new CCardHBoxLayout(20, this);
// Create the boxLayout that contains the playholder and the cards
boxLayout = new QHBoxLayout();
// Set the space between the cards
boxLayout->setSpacing(10);
// Assign and scale the needed tiles to the pixmap variables
QPixmap* cardBackTile = new QPixmap(":/assets/card_back.png");
cardBackPixmap = cardBackTile->scaled(CCard::getCardScreenSize().width(), CCard::getCardScreenSize().height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap placeholderTilset = QPixmap(":/assets/card_placeholders.png");
QPixmap placeholderTile = placeholderTilset.copy(4 * CCard::cardTileSize.width(), 0, CCard::cardTileSize.width(), CCard::cardTileSize.height());
emptyDrawStackPixmap = placeholderTile.scaled(CCard::getCardScreenSize().width(), CCard::getCardScreenSize().height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
// Create a QLabel that represents the drawStack but needs no logic
drawStackPlaceholder = new CClickableLabel(this);
drawStackPlaceholder->setAlignment(Qt::AlignLeft | Qt::AlignTop);
drawStackPlaceholder->setPixmap(cardBackPixmap);
// Add the playeholder and the cards to the boxLayout
boxLayout->addWidget(drawStackPlaceholder);
boxLayout->addWidget(this);
// Connect the clicked event with the showNextCard function
QObject::connect(drawStackPlaceholder, &CClickableLabel::clicked, this, &CDrawStack::showNextCard);
}
void CDrawStack::removeCard(CCard* cardToRemove)
{
// Call the superclasses' removeCard
CCardStack::removeCard(cardToRemove);
// Remove the card from the UI
removeCardFromUi();
}
bool CDrawStack::canDropCard(CCard *cardToDrop)
{
Q_UNUSED(cardToDrop);
// Adding a card to the drawStack is not possible, due to that, this method is not necessary
qDebug() << "nothing happens";
return false;
}
CCard* CDrawStack::getTopCard()
{
// Return the current top card
return getCards()[currentCard];
}
void CDrawStack::addCardToUi()
{
// When the Boxlayout shows already 3 cards, the last one is removed, so that a new one can be displayed
if(hbox->count() == 3 && getNumCards() > 3)
{
hbox->removeItem(hbox->itemAt(0));
getCards()[currentCard+3]->setVisible(false);
}
// The following card is added to the boxlayout and set to visible
hbox->addWidget(getCards()[currentCard]);
getCards()[currentCard]->setVisible(true);
// Interaction with the added card should be possible, with the former front card impossible (only if there is a former card)
getCards()[currentCard]->setCanInteract(true);
if(currentCard < getNumCards()-1)
getCards()[currentCard+1]->setCanInteract(false);
// Additionally, if it is the last card of the stack or if there is no card left, the placeholder displays the empty tile
if(currentCard == 0 || getNumCards()-1 == 0)
drawStackPlaceholder->setPixmap(emptyDrawStackPixmap);
}
void CDrawStack::removeCardFromUi()
{
// Remove the card from the layout which is the first one
if(hbox->itemAt(2))
hbox->removeItem(hbox->itemAt(2));
else if(hbox->itemAt(1))
hbox->removeItem(hbox->itemAt(1));
else
hbox->removeItem(hbox->itemAt(0));
// The next card should be interactable, if there is one
if(currentCard <= getNumCards()-1)
{
getCards()[currentCard]->setCanInteract(true);
// When a card is removed, the last third card in the stack will be displayed, if existent
if(currentCard + 2 <= getNumCards()-1)
{
hbox->push_back(getCards()[currentCard+2]);
getCards()[currentCard+2]->setVisible(true);
}
}
}
void CDrawStack::showNextCard()
{
// The cards that shouldn't be displayed have to be invisible, even if the card is removed from
// the layout (the card is still in the background of the layout)
// If there are cards left on the stack, the variable "currentCard" is decremented
if(currentCard > 0)
{
--currentCard;
addCardToUi();
}
else if(currentCard == 0)
{
// CurrentCard = 0 means, that the stack is empty, so the drawStack gets recycled
drawStackPlaceholder->setPixmap(cardBackPixmap);
// Recycling the draw stack changes the score
CMain::get()->getGameInstance()->addScore(GameScoringAttributes::RECYCLING_DRAW_PILE);
// Iterating over the remaining cards and setting them to invisible and not interactable
for(int i = 0; i < hbox->getItemCount(); ++i)
{
getCards()[currentCard + i]->setCanInteract(false);
getCards()[currentCard + i]->setVisible(false);
}
hbox->clear();
// Decrementing currentCard again, so that we don't jump in the same condition next time
--currentCard;
}
// Otherwise, the currentCard is set to the topCard again, the first card is set to visible and caninteract
else if(getNumCards() > 0)
{
currentCard = getNumCards()-1;
addCardToUi();
}
// Register a new transaction that we drew a card
Transaction t;
t.type = Transaction::ETransactionType::DrawFromDrawStack;
t.stack1 = this;
CMain::get()->getGameInstance()->addTransaction(t);
// Drawing a card counts as a move
CMain::get()->getGameWindow()->incrementMove();
// Play a sound effect
CMain::get()->getSoundManager()->playSoundEffect(ESoundEffectType::CardStack);
}
void CDrawStack::undo(CCard* card)
{
// If a card has to be readded to the drawstack
if(card)
{
insertCardAt(currentCard, card);
addCardToUi();
}
else
{
// If the recycling has to be undone:
if (currentCard == -1)
{
// TODO: Beautify
currentCard = 2;
addCardToUi();
currentCard = 1;
addCardToUi();
currentCard = 0;
addCardToUi();
drawStackPlaceholder->setPixmap(cardBackPixmap);
}
else
{
getCards()[currentCard]->setVisible(false);
++currentCard;
removeCardFromUi();
}
}
}
QHBoxLayout* CDrawStack::getHBoxLayout()
{
return boxLayout;
}