1/*
2 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3 * Copyright 2007-2008 Pierre-Benoit Besse <besse.pb@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "ghostitem.h"
20#include "game.h"
21
22GhostItem::GhostItem(Ghost* p_model) : CharacterItem (p_model) {
23 connect(p_model, SIGNAL(stateChanged()), this, SLOT(updateState()));
24
25 // Calculations for the duration of blinking stuff
26 int blinkTimerDuration = (int)(500 * Game::s_durationRatio);
27 int startBlinkingTimerDuration = (int)(Game::s_preyStateDuration*Game::s_durationRatio - 5*blinkTimerDuration);
28
29 // Define the timer which tells the ghosts to start blinking when about to leave prey state
30 m_startBlinkingTimer = new QTimer(this);
31 m_startBlinkingTimer->setInterval(startBlinkingTimerDuration);
32 m_startBlinkingTimer->setSingleShot(true);
33 connect(m_startBlinkingTimer, SIGNAL(timeout()), this, SLOT(startBlinking()));
34
35 // Define the timer which sets the blinking frequency
36 m_blinkTimer = new QTimer(this);
37 m_blinkTimer->setInterval(blinkTimerDuration);
38 connect(m_blinkTimer, SIGNAL(timeout()), this, SLOT(blink()));
39}
40
41GhostItem::~GhostItem() {
42 delete m_startBlinkingTimer;
43}
44
45void GhostItem::updateBlinkTimersDuration() {
46 // Set the timers duration depending on the prey state duration
47 int blinkTimerDuration = (int)( (Game::s_preyStateDuration*Game::s_durationRatio) / 20);
48 int startBlinkingTimerDuration = (int)(blinkTimerDuration * 15);
49 m_blinkTimer->setInterval(blinkTimerDuration);
50 m_startBlinkingTimer->setInterval(startBlinkingTimerDuration);
51}
52
53void GhostItem::update(qreal p_x, qreal p_y) {
54 // Compute the top-right coordinates of the item
55 qreal x = p_x - boundingRect().width() / 2;
56 qreal y = p_y - boundingRect().height() / 2;
57 // Updates the view coordinates
58 setPos(x, y);
59}
60
61void GhostItem::updateState() {
62 // Stop timers
63 if (m_startBlinkingTimer->isActive()) {
64 m_startBlinkingTimer->stop();
65 }
66 if (m_blinkTimer->isActive()) {
67 m_blinkTimer->stop();
68 }
69 switch (((Ghost*)getModel())->getState()) {
70 case Ghost::PREY:
71 updateBlinkTimersDuration();
72 setElementId("scaredghost");
73 m_startBlinkingTimer->start();
74 // The ghosts are now weaker than the kapman, so they are under him
75 setZValue(1);
76 break;
77 case Ghost::HUNTER:
78 setElementId( ((Ghost*)getModel())->getImageId() );
79 // The ghosts are stronger than the kapman, they are above him
80 setZValue(3);
81 break;
82 case Ghost::EATEN:
83 setElementId("ghosteye");
84 // The ghosts are now weaker than the kapman, so they are under him
85 setZValue(1);
86 break;
87 }
88}
89
90void GhostItem::blink() {
91 CharacterItem::blink();
92 if (m_nbBlinks % 2 == 0) {
93 setElementId("scaredghost");
94 } else {
95 setElementId("whitescaredghost");
96 }
97}
98
99