mirror of
https://github.com/Alee14/entertaining-mines.git
synced 2025-01-22 09:02:04 -05:00
Add timer to turn based gamemode
This commit is contained in:
parent
b51f9be69a
commit
bf728e2129
6 changed files with 1561 additions and 4836 deletions
Binary file not shown.
File diff suppressed because it is too large
Load diff
Before Width: | Height: | Size: 373 KiB After Width: | Height: | Size: 124 KiB |
|
@ -32,6 +32,7 @@
|
|||
struct PlayerCarouselPrivate {
|
||||
QMap<int, PlayerCarouselItem*> items;
|
||||
int currentTurn = -1;
|
||||
qint64 lastTimeout = 0;
|
||||
|
||||
int thisSessionId = -1;
|
||||
|
||||
|
@ -90,7 +91,7 @@ PlayerCarousel::PlayerCarousel(QWidget *parent) :
|
|||
} else if (type == "gamemodeChange") {
|
||||
d->gamemode = obj.value("gamemode").toString();
|
||||
} else if (type == "currentPlayerChange") {
|
||||
this->setCurrentPlayer(obj.value("session").toInt());
|
||||
this->setCurrentPlayer(obj.value("session").toInt(), obj.value("timeout").toVariant().toLongLong());
|
||||
|
||||
if (d->currentTurn == d->thisSessionId) MusicEngine::playSoundEffect("yourturn");
|
||||
} else if (type == "endGame") {
|
||||
|
@ -131,17 +132,20 @@ void PlayerCarousel::expand()
|
|||
this->setFixedHeight(this->preferredHeight());
|
||||
}
|
||||
|
||||
void PlayerCarousel::setCurrentPlayer(int session)
|
||||
void PlayerCarousel::setCurrentPlayer(int session, qint64 timeout)
|
||||
{
|
||||
if (d->items.contains(d->currentTurn)) {
|
||||
d->items.value(d->currentTurn)->setIsCurrentTurn(false);
|
||||
d->items.value(d->currentTurn)->clearCurrentTurn();
|
||||
}
|
||||
|
||||
d->currentTurn = session;
|
||||
|
||||
if (timeout == 0) timeout = d->lastTimeout;
|
||||
d->lastTimeout = timeout;
|
||||
|
||||
if (d->items.contains(d->currentTurn)) {
|
||||
PlayerCarouselItem* item = d->items.value(d->currentTurn);
|
||||
item->setIsCurrentTurn(true);
|
||||
item->setCurrentTurn(timeout);
|
||||
ui->scrollArea->ensureWidgetVisible(item, SC_DPI(100), 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ class PlayerCarousel : public QWidget
|
|||
Ui::PlayerCarousel *ui;
|
||||
PlayerCarouselPrivate* d;
|
||||
|
||||
void setCurrentPlayer(int session);
|
||||
void setCurrentPlayer(int session, qint64 timeout = 0);
|
||||
bool eventFilter(QObject* watched, QEvent* event);
|
||||
};
|
||||
|
||||
|
|
|
@ -22,12 +22,14 @@
|
|||
|
||||
#include <tvariantanimation.h>
|
||||
#include <QPainter>
|
||||
#include <QDateTime>
|
||||
|
||||
struct PlayerCarouselItemPrivate {
|
||||
QString playerName;
|
||||
QColor playerCol;
|
||||
|
||||
tVariantAnimation colHeight;
|
||||
tVariantAnimation timeoutBar;
|
||||
};
|
||||
|
||||
PlayerCarouselItem::PlayerCarouselItem(QWidget *parent) :
|
||||
|
@ -44,6 +46,11 @@ PlayerCarouselItem::PlayerCarouselItem(QWidget *parent) :
|
|||
connect(&d->colHeight, &tVariantAnimation::valueChanged, this, [=] {
|
||||
this->update();
|
||||
});
|
||||
|
||||
d->timeoutBar.setEasingCurve(QEasingCurve::Linear);
|
||||
connect(&d->timeoutBar, &tVariantAnimation::valueChanged, this, [=] {
|
||||
this->update();
|
||||
});
|
||||
}
|
||||
|
||||
PlayerCarouselItem::~PlayerCarouselItem()
|
||||
|
@ -68,14 +75,31 @@ void PlayerCarouselItem::setProfilePicture(QImage picture)
|
|||
ui->profilePictureIcon->setPixmap(QPixmap::fromImage(picture));
|
||||
}
|
||||
|
||||
void PlayerCarouselItem::setIsCurrentTurn(bool isCurrentTurn)
|
||||
void PlayerCarouselItem::setCurrentTurn(qint64 timeout)
|
||||
{
|
||||
if (isCurrentTurn) {
|
||||
d->colHeight.setDirection(tVariantAnimation::Forward);
|
||||
} else {
|
||||
d->colHeight.setDirection(tVariantAnimation::Backward);
|
||||
}
|
||||
d->colHeight.start();
|
||||
|
||||
qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
|
||||
if (timeout > currentTime) {
|
||||
d->timeoutBar.setStartValue(1.0);
|
||||
d->timeoutBar.setEndValue(0.0);
|
||||
d->timeoutBar.setDuration(static_cast<int>(timeout - currentTime));
|
||||
d->timeoutBar.setCurrentTime(0);
|
||||
d->timeoutBar.start();
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerCarouselItem::clearCurrentTurn()
|
||||
{
|
||||
d->colHeight.setDirection(tVariantAnimation::Backward);
|
||||
d->colHeight.start();
|
||||
|
||||
d->timeoutBar.setStartValue(d->timeoutBar.currentValue());
|
||||
d->timeoutBar.setEndValue(0.0);
|
||||
d->timeoutBar.setDuration(250);
|
||||
d->timeoutBar.setCurrentTime(0);
|
||||
d->timeoutBar.start();
|
||||
}
|
||||
|
||||
void PlayerCarouselItem::resizeEvent(QResizeEvent*event)
|
||||
|
@ -87,7 +111,13 @@ void PlayerCarouselItem::paintEvent(QPaintEvent*event)
|
|||
{
|
||||
QPainter painter(this);
|
||||
painter.setPen(Qt::transparent);
|
||||
painter.setBrush(d->playerCol);
|
||||
|
||||
painter.drawRect(0, this->height() - d->colHeight.currentValue().toInt(), this->width(), d->colHeight.currentValue().toInt());
|
||||
int colHeight = d->colHeight.currentValue().toInt();
|
||||
painter.setBrush(d->playerCol);
|
||||
painter.drawRect(0, this->height() - colHeight, this->width(), colHeight);
|
||||
|
||||
//Draw the timeout bar
|
||||
int timeoutBarHeight = static_cast<int>(colHeight * d->timeoutBar.currentValue().toDouble());
|
||||
painter.setBrush(d->playerCol.darker(175));
|
||||
painter.drawRect(0, this->height() - timeoutBarHeight, this->width(), timeoutBarHeight);
|
||||
}
|
||||
|
|
|
@ -38,7 +38,8 @@ class PlayerCarouselItem : public QWidget
|
|||
void setPlayerName(QString playerName);
|
||||
void setPlayerColor(QColor col);
|
||||
void setProfilePicture(QImage picture);
|
||||
void setIsCurrentTurn(bool isCurrentTurn);
|
||||
void setCurrentTurn(qint64 timeout);
|
||||
void clearCurrentTurn();
|
||||
|
||||
private:
|
||||
Ui::PlayerCarouselItem *ui;
|
||||
|
|
Loading…
Reference in a new issue