Commit 84cff24b authored by Frederik Schwarzer's avatar Frederik Schwarzer
Browse files

use existing game timer instead of own timer for ghost blinking

parent 9f10c019
......@@ -392,6 +392,13 @@ void Game::update()
} else {
m_ghosts[i]->updateMove();
}
const int startBlinkingDelay = s_preyStateDuration * s_durationRatio - 5 * (500 * s_durationRatio);
if (m_preyTimer->remainingTime() < (m_preyTimer->interval() - startBlinkingDelay)) {
m_ghosts[i]->setPreyStateAlmostOverEnabled(true);
} else {
m_ghosts[i]->setPreyStateAlmostOverEnabled(false);
}
}
m_kapman->updateMove();
m_kapman->emitGameUpdated();
......
......@@ -17,6 +17,7 @@ const qreal Ghost::MAX_SPEED_RATIO = 2.0;
const int Ghost::POINTS = 200;
Ghost::Ghost(qreal p_x, qreal p_y, const QString &p_imageId, Maze *p_maze) : Character(p_x, p_y, p_maze)
, m_preyStateAlmostOver(false)
{
// Initialize the ghost attributes
m_imageId = p_imageId;
......@@ -231,3 +232,12 @@ void Ghost::initSpeedInc()
}
}
void Ghost::setPreyStateAlmostOverEnabled(bool enable)
{
m_preyStateAlmostOver = enable;
}
bool Ghost::preyStateAlmostOver() const
{
return m_preyStateAlmostOver;
}
......@@ -45,6 +45,8 @@ private:
/** A list of Cells to go to the camp from the current cell */
QList<QPoint> m_pathToCamp;
bool m_preyStateAlmostOver;
public:
/**
......@@ -102,6 +104,8 @@ public:
*/
void initSpeedInc() override;
void setPreyStateAlmostOverEnabled(bool enable);
bool preyStateAlmostOver() const;
private:
/**
......
......@@ -13,15 +13,7 @@ GhostItem::GhostItem(Ghost *p_model) : CharacterItem(p_model)
connect(p_model, &Ghost::stateChanged, this, &GhostItem::updateState);
// Calculations for the duration of blinking stuff
int blinkTimerDuration = (int)(500 * Game::s_durationRatio);
int startBlinkingTimerDuration = (int)(Game::s_preyStateDuration * Game::s_durationRatio - 5 * blinkTimerDuration);
// Define the timer which tells the ghosts to start blinking when about to leave prey state
m_startBlinkingTimer = new QTimer(this);
m_startBlinkingTimer->setInterval(startBlinkingTimerDuration);
m_startBlinkingTimer->setSingleShot(true);
connect(m_startBlinkingTimer, &QTimer::timeout, this, &GhostItem::startBlinking);
const int blinkTimerDuration = (int)(500 * Game::s_durationRatio);
// Define the timer which sets the blinking frequency
m_blinkTimer = new QTimer(this);
m_blinkTimer->setInterval(blinkTimerDuration);
......@@ -30,16 +22,7 @@ GhostItem::GhostItem(Ghost *p_model) : CharacterItem(p_model)
GhostItem::~GhostItem()
{
delete m_startBlinkingTimer;
}
void GhostItem::updateBlinkTimersDuration()
{
// Set the timers duration depending on the prey state duration
int blinkTimerDuration = (int)((Game::s_preyStateDuration * Game::s_durationRatio) / 20);
int startBlinkingTimerDuration = (int)(blinkTimerDuration * 15);
m_blinkTimer->setInterval(blinkTimerDuration);
m_startBlinkingTimer->setInterval(startBlinkingTimerDuration);
delete m_blinkTimer;
}
void GhostItem::update(qreal p_x, qreal p_y)
......@@ -53,18 +36,13 @@ void GhostItem::update(qreal p_x, qreal p_y)
void GhostItem::updateState()
{
// Stop timers
if (m_startBlinkingTimer->isActive()) {
m_startBlinkingTimer->stop();
}
if (m_blinkTimer->isActive()) {
m_blinkTimer->stop();
}
switch (((Ghost *)getModel())->getState()) {
case Ghost::PREY:
updateBlinkTimersDuration();
m_blinkTimer->start();
setElementId(QStringLiteral("scaredghost"));
m_startBlinkingTimer->start();
// The ghosts are now weaker than the kapman, so they are under him
setZValue(1);
break;
......@@ -83,11 +61,13 @@ void GhostItem::updateState()
void GhostItem::blink()
{
CharacterItem::blink();
if (m_nbBlinks % 2 == 0) {
setElementId(QStringLiteral("scaredghost"));
} else {
setElementId(QStringLiteral("whitescaredghost"));
if (((Ghost *)getModel())->getState() == Ghost::PREY
&& ((Ghost *)getModel())->preyStateAlmostOver()) {
CharacterItem::blink();
if (m_nbBlinks % 2 == 0) {
setElementId(QStringLiteral("scaredghost"));
} else {
setElementId(QStringLiteral("whitescaredghost"));
}
}
}
......@@ -19,11 +19,6 @@ class GhostItem : public CharacterItem
Q_OBJECT
private:
/** Timer to start the ghosts blinking */
QTimer *m_startBlinkingTimer = nullptr;
public:
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment