• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kapman

  • sources
  • kde-4.14
  • kdegames
  • kapman
ghostitem.cpp
Go to the documentation of this file.
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 
22 GhostItem::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 
41 GhostItem::~GhostItem() {
42  delete m_startBlinkingTimer;
43 }
44 
45 void 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 
53 void 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 
61 void 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 
90 void GhostItem::blink() {
91  CharacterItem::blink();
92  if (m_nbBlinks % 2 == 0) {
93  setElementId("scaredghost");
94  } else {
95  setElementId("whitescaredghost");
96  }
97 }
98 
QTimer::setInterval
void setInterval(int msec)
QGraphicsItem::x
qreal x() const
QGraphicsItem::y
qreal y() const
Ghost::EATEN
Definition: ghost.h:39
CharacterItem::startBlinking
virtual void startBlinking()
Starts the character blinking.
Definition: characteritem.cpp:48
GhostItem::updateState
void updateState()
Update the image function of the Ghost state.
Definition: ghostitem.cpp:61
CharacterItem::m_blinkTimer
QTimer * m_blinkTimer
Timer used to make the character blink.
Definition: characteritem.h:36
GhostItem::~GhostItem
~GhostItem()
Deletes the CharacterItem instance.
Definition: ghostitem.cpp:41
game.h
Ghost::PREY
Definition: ghost.h:38
ElementItem::getModel
Element * getModel() const
Gets the Element model.
Definition: elementitem.cpp:35
CharacterItem
This class is the graphical representation of a Character.
Definition: characteritem.h:29
Ghost
This class represents a Ghost for kapman.
Definition: ghost.h:29
QTimer
QGraphicsSvgItem::boundingRect
virtual QRectF boundingRect() const
QGraphicsItem::setPos
void setPos(const QPointF &pos)
Ghost::HUNTER
Definition: ghost.h:37
GhostItem::GhostItem
GhostItem(Ghost *p_model)
Creates a new GhostItem instance.
Definition: ghostitem.cpp:22
Game::s_preyStateDuration
static int s_preyStateDuration
Timer duration for prey state in medium difficulty.
Definition: game.h:44
Game::s_durationRatio
static qreal s_durationRatio
Ratio which modify the timers function of the difficulty.
Definition: game.h:41
QTimer::stop
void stop()
CharacterItem::blink
virtual void blink()
Makes the character blink.
Definition: characteritem.cpp:53
GhostItem::blink
void blink()
Implements the CharacterItem method.
Definition: ghostitem.cpp:90
GhostItem::updateBlinkTimersDuration
void updateBlinkTimersDuration()
Ensures the blink timers are correctly set.
Definition: ghostitem.cpp:45
QRectF::width
qreal width() const
QGraphicsSvgItem::setElementId
void setElementId(const QString &id)
ghostitem.h
QTimer::start
void start(int msec)
QRectF::height
qreal height() const
CharacterItem::m_nbBlinks
int m_nbBlinks
Number of ticks of the blink timer.
Definition: characteritem.h:39
QTimer::isActive
bool isActive() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QGraphicsItem::setZValue
void setZValue(qreal z)
QTimer::setSingleShot
void setSingleShot(bool singleShot)
GhostItem::update
void update(qreal p_x, qreal p_y)
Updates the view coordinates.
Definition: ghostitem.cpp:53
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kapman

Skip menu "kapman"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal