-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird.cpp
154 lines (118 loc) · 4.7 KB
/
bird.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
#include "bird.h"
Bird::Bird(QWidget *parent,uint32_t xPos, uint32_t yPos): Entity(parent, xPos, yPos)
{
if (birdPixMap.load("../../Assets/bird.png"))
{
qDebug() << "Bird image is loaded!";
}
else
{
qDebug() << "Failed to load bird image.";
}
// Resizing bird image:
birdPixMap = birdPixMap.scaled(100, 70, Qt::KeepAspectRatio, Qt::SmoothTransformation);
this->setPixmap(birdPixMap);
this->setGeometry(this->getPosX(), this->getPosY(), birdPixMap.width(), birdPixMap.height());
this->raise();
this->show();
// Debugging output:
qDebug() << "QLabel bird geometry:" << this->geometry();
qDebug() << "QLabel bird is visible:" << this->isVisible();
this->setFocusPolicy(Qt::StrongFocus);
m_audioOutput = std::make_unique<QAudioOutput>(this);
m_birdJumpEffect = std::make_unique<QMediaPlayer>(this);
m_birdJumpEffect->setAudioOutput(m_audioOutput.get());
m_birdJumpEffect->setSource(QUrl::fromLocalFile("../../Assets/sounds/flap.wav"));
m_audioOutput->setVolume(0.5);
if (m_birdJumpEffect->mediaStatus() == QMediaPlayer::LoadedMedia) {
qDebug() << "Media loaded successfully";
} else {
qDebug() << "Failed to load media";
}
// Initialization of jump animation:
m_jumpAnimation = std::make_unique<QPropertyAnimation>(this, "geometry");
m_jumpAnimation->setDuration(BIRD_JUMP_DURATION);
m_jumpAnimation->setEasingCurve(QEasingCurve::OutQuad);
m_freeFallAnimation = std::make_unique<QPropertyAnimation>(this,"geometry");
m_freeFallAnimation->setEasingCurve(QEasingCurve::InQuad);
connect(m_jumpAnimation.get(), &QPropertyAnimation::stateChanged, this, &Bird::updatePosition);
connect(m_freeFallAnimation.get(), &QPropertyAnimation::finished, this, &Bird::updatePosition);
connect(m_jumpAnimation.get(), &QPropertyAnimation::finished, this, &Bird::startFreeFallAnimation);
// Connect valueChanged signal to update position during animation
connect(m_freeFallAnimation.get(), &QPropertyAnimation::valueChanged, this, &Bird::updatePositionDuringAnimation);
// Start the free-fall animation initially
startFreeFallAnimation();
}
void Bird::keyPressEvent(QKeyEvent *event)
{
const int jumpStep = BIRD_JUMP_STEP;
QRect startRect = this->geometry();
QRect endRect = startRect;
if (event != nullptr)
{
switch (event->key())
{
case Qt::Key_Space:
if (m_yPos >= jumpStep) m_yPos -= jumpStep;
qDebug() << "Space Key is pressed";
m_birdJumpEffect->play();
break;
default:
QLabel::keyPressEvent(event); // Call base class implementation for unhandled keys
return;
}
endRect.moveTo(m_xPos, m_yPos);
m_freeFallAnimation->stop();
startjumpAnimation(startRect, endRect);
qDebug() << "Bird position after animation: " << endRect.topLeft();
qDebug() << "Bird position that class knows:" << this->getPosX() << this->getPosY();
}
}
void Bird::startjumpAnimation(const QRect &startRect, const QRect &endRect)
{
m_jumpAnimation->stop();
m_jumpAnimation->setStartValue(startRect);
m_jumpAnimation->setEndValue(endRect);
m_jumpAnimation->start();
rotateBird(-45);
}
void Bird::updatePosition()
{
// Update m_xPos and m_yPos based on the widget's current position
m_xPos = this->x();
m_yPos = this->y();
}
void Bird::updatePositionDuringAnimation(const QVariant &value)
{
// Update m_xPos and m_yPos based on the current geometry during the animation
QRect currentRect = value.toRect();
m_xPos = currentRect.x();
m_yPos = currentRect.y();
}
void Bird::startFreeFallAnimation()
{
QRect currentRect = this->geometry();
if (m_yPos < BIRD_FALL_Y_TARGET)
{
m_freeFallAnimation->stop();
m_freeFallAnimation->setStartValue(currentRect);
m_freeFallAnimation->setEndValue(QRect(m_xPos, BIRD_FALL_Y_TARGET, currentRect.width(), currentRect.height()));
m_freeFallAnimation->setDuration(BIRD_FALL_DURATION);
m_freeFallAnimation->start();
rotateBird(45);
}
else
{
m_freeFallAnimation->stop(); // Stop moving if we've reached the bottom
// Raise a flag so game can know when bird has fallen to the ground.
qDebug() << "Reached bottom, stopping downward movement.";
}
}
void Bird::rotateBird(int direction)
{
QTransform transform;
transform.rotate(direction); // Rotate by the value entered
QPixmap rotatedPixmap = birdPixMap.transformed(transform, Qt::SmoothTransformation);
this->setPixmap(rotatedPixmap);
this->setGeometry(this->x(), this->y(), rotatedPixmap.width(), rotatedPixmap.height());
}