File indexing completed on 2024-04-28 04:01:51

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0003     SPDX-FileCopyrightText: 2007-2008 Pierre-Benoit Besse <besse.pb@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "ghostitem.h"
0009 #include "game.h"
0010 
0011 GhostItem::GhostItem(Ghost *p_model)
0012     : CharacterItem(p_model)
0013 {
0014     connect(p_model, &Ghost::stateChanged, this, &GhostItem::updateState);
0015 
0016     // Calculations for the duration of blinking stuff
0017     const int blinkTimerDuration = (int)(500 * Game::s_durationRatio);
0018     // Define the timer which sets the blinking frequency
0019     m_blinkTimer = new QTimer(this);
0020     m_blinkTimer->setInterval(blinkTimerDuration);
0021     connect(m_blinkTimer, &QTimer::timeout, this, &GhostItem::blink);
0022 }
0023 
0024 GhostItem::~GhostItem()
0025 {
0026     delete m_blinkTimer;
0027 }
0028 
0029 void GhostItem::update(qreal p_x, qreal p_y)
0030 {
0031     // Compute the top-right coordinates of the item
0032     qreal x = p_x - boundingRect().width() / 2;
0033     qreal y = p_y - boundingRect().height() / 2;
0034     // Updates the view coordinates
0035     setPos(x, y);
0036 }
0037 
0038 void GhostItem::updateState()
0039 {
0040     if (m_blinkTimer->isActive()) {
0041         m_blinkTimer->stop();
0042     }
0043     switch (((Ghost *)getModel())->getState()) {
0044     case Ghost::PREY:
0045         m_blinkTimer->start();
0046         setElementId(QStringLiteral("scaredghost"));
0047         // The ghosts are now weaker than the kapman, so they are under him
0048         setZValue(1);
0049         break;
0050     case Ghost::HUNTER:
0051         setElementId(((Ghost *)getModel())->getImageId());
0052         // The ghosts are stronger than the kapman, they are above him
0053         setZValue(3);
0054         break;
0055     case Ghost::EATEN:
0056         setElementId(QStringLiteral("ghosteye"));
0057         // The ghosts are now weaker than the kapman, so they are under him
0058         setZValue(1);
0059         break;
0060     }
0061 }
0062 
0063 void GhostItem::blink()
0064 {
0065     if (((Ghost *)getModel())->getState() == Ghost::PREY && ((Ghost *)getModel())->preyStateAlmostOver()) {
0066         CharacterItem::blink();
0067         if (m_nbBlinks % 2 == 0) {
0068             setElementId(QStringLiteral("scaredghost"));
0069         } else {
0070             setElementId(QStringLiteral("whitescaredghost"));
0071         }
0072     }
0073 }
0074 
0075 #include "moc_ghostitem.cpp"