• 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
kapmanitem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3  * Copyright 2007-2008 Nathalie Liesse <nathalie.liesse@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 
20 #include "kapmanitem.h"
21 #include "characteritem.h"
22 #include "ghost.h"
23 #include "settings.h"
24 
25 #include <QGraphicsScene>
26 #include <KgDifficulty>
27 
28 const int KapmanItem::NB_FRAMES = 32;
29 const int KapmanItem::ANIM_LOW_SPEED = 500;
30 const int KapmanItem::ANIM_MEDIUM_SPEED = 400;
31 const int KapmanItem::ANIM_HIGH_SPEED = 300;
32 
33 KapmanItem::KapmanItem(Kapman* p_model) : CharacterItem(p_model) {
34  connect(p_model, SIGNAL(directionChanged()), this, SLOT(updateDirection()));
35  connect(p_model, SIGNAL(gameUpdated()), this, SLOT(manageCollision()));
36  connect(p_model, SIGNAL(stopped()), this, SLOT(stopAnim()));
37 
38  // A timeLine for the Kapman animation
39  m_animationTimer = new QTimeLine();
40  m_animationTimer->setCurveShape(QTimeLine::SineCurve);
41  m_animationTimer->setLoopCount(0);
42  m_animationTimer->setFrameRange(0, NB_FRAMES - 1);
43  // Animation speed
44  switch ((int) Kg::difficultyLevel())
45  {
46  case KgDifficultyLevel::Easy:
47  m_animationTimer->setDuration(KapmanItem::ANIM_LOW_SPEED);
48  break;
49  case KgDifficultyLevel::Medium:
50  m_animationTimer->setDuration(KapmanItem::ANIM_MEDIUM_SPEED);
51  break;
52  case KgDifficultyLevel::Hard:
53  m_animationTimer->setDuration(KapmanItem::ANIM_HIGH_SPEED);
54  break;
55  }
56  connect(m_animationTimer, SIGNAL(frameChanged(int)), this, SLOT(setFrame(int)));
57 
58  // Define the timer which sets the blinking frequency
59  m_blinkTimer = new QTimer(this);
60  m_blinkTimer->setInterval(400);
61  connect(m_blinkTimer, SIGNAL(timeout()), this, SLOT(blink()));
62 }
63 
64 KapmanItem::~KapmanItem() {
65  delete m_animationTimer;
66 }
67 
68 void KapmanItem::updateDirection() {
69  QTransform transform;
70  int angle = 0;
71  Kapman* model = (Kapman*)getModel();
72 
73  // Compute the angle
74  if (model->getXSpeed() > 0) {
75  angle = 0;
76  } else if (model->getXSpeed() < 0) {
77  angle = 180; // The default image is right oriented
78  }
79  if (model->getYSpeed() > 0) {
80  angle = 90;
81  } else if (model->getYSpeed() < 0) {
82  angle = -90;
83  }
84 
85  if (m_rotationFlag==0) {
86  angle=0;
87  }
88  // Rotate the item
89  transform.translate(boundingRect().width() / 2, boundingRect().height() / 2);
90  transform.rotate(angle);
91  transform.translate(-boundingRect().width() / 2, -boundingRect().height() / 2);
92  setTransform(transform);
93 }
94 
95 void KapmanItem::manageCollision() {
96  QList<QGraphicsItem*> collidingList = collidingItems();
97 
98  // The signal is emitted only if the list contains more than 1 items (to exclude the case
99  // when the kapman only collides with the maze)
100  if (collidingList.size() > 1) {
101  for (int i = 0; i < collidingList.size(); ++i) {
102  // The maze and the points labels have a negative zValue which allows to exclude them from the treatment of collisions
103  if (collidingList[i]->zValue() >= 0) {
104  ElementItem* item = dynamic_cast<ElementItem*>(collidingList[i]);
105  if (item) {
106  item->getModel()->doActionOnCollision((Kapman*)getModel());
107  }
108  }
109  }
110  }
111 }
112 
113 void KapmanItem::update(qreal p_x, qreal p_y) {
114  ElementItem::update(p_x, p_y);
115 
116  // If the kapman is moving
117  if (((Kapman*)getModel())->getXSpeed() != 0 || ((Kapman*)getModel())->getYSpeed() != 0) {
118  startAnim();
119  }
120 }
121 
122 void KapmanItem::startAnim() {
123  // Start the animation timer if it is not active
124  if (m_animationTimer->state() != QTimeLine::Running) {
125  m_animationTimer->start();
126  }
127 }
128 
129 void KapmanItem::pauseAnim() {
130  if (m_animationTimer->state() == QTimeLine::Running)
131  m_animationTimer->setPaused(true);
132 }
133 
134 void KapmanItem::resumeAnim() {
135  if (m_animationTimer->state() == QTimeLine::Running)
136  m_animationTimer->setPaused(false);
137 }
138 
139 void KapmanItem::stopAnim() {
140  setElementId("kapman_0");
141  if (m_animationTimer->state() == QTimeLine::Running)
142  m_animationTimer->stop();
143 }
144 
145 void KapmanItem::setFrame(const int p_frame) {
146  setElementId(QString("kapman_%1").arg(p_frame));
147 }
148 
149 void KapmanItem::startBlinking() {
150  stopAnim();
151  setElementId("kapman_0");
152  CharacterItem::startBlinking();
153 }
154 
155 void KapmanItem::blink() {
156  CharacterItem::blink();
157  if (m_nbBlinks % 2 == 0) {
158  setElementId("kapman_0");
159  } else {
160  setElementId("kapman_blink");
161  }
162  // Make the kapman blink 2 times (4 ticks)
163  if (m_nbBlinks == 4) {
164  m_blinkTimer->stop();
165  }
166 }
167 
QTimer::setInterval
void setInterval(int msec)
QTransform
Character::getXSpeed
qreal getXSpeed() const
Gets the Character x-speed value.
Definition: character.cpp:59
ElementItem::update
virtual void update(qreal p_x, qreal p_y)
Updates the ElementItem coordinates.
Definition: elementitem.cpp:45
QTimeLine::setFrameRange
void setFrameRange(int startFrame, int endFrame)
QTimeLine::setDuration
void setDuration(int duration)
QTimeLine::setPaused
void setPaused(bool paused)
KapmanItem::pauseAnim
void pauseAnim()
Pauses the KapmanItem animation.
Definition: kapmanitem.cpp:129
CharacterItem::startBlinking
virtual void startBlinking()
Starts the character blinking.
Definition: characteritem.cpp:48
CharacterItem::m_blinkTimer
QTimer * m_blinkTimer
Timer used to make the character blink.
Definition: characteritem.h:36
KapmanItem::~KapmanItem
~KapmanItem()
Deletes the KapmanItem instance.
Definition: kapmanitem.cpp:64
QTimeLine::setLoopCount
void setLoopCount(int count)
QGraphicsItem::collidingItems
QList< QGraphicsItem * > collidingItems(Qt::ItemSelectionMode mode) const
KapmanItem::stopAnim
void stopAnim()
Stops the KapmanItem animation.
Definition: kapmanitem.cpp:139
QList::size
int size() const
KapmanItem::update
void update(qreal p_x, qreal p_y)
Updates the KapmanItem coordinates.
Definition: kapmanitem.cpp:113
ElementItem::getModel
Element * getModel() const
Gets the Element model.
Definition: elementitem.cpp:35
QTimeLine::setCurveShape
void setCurveShape(CurveShape shape)
CharacterItem
This class is the graphical representation of a Character.
Definition: characteritem.h:29
QGraphicsItem::zValue
qreal zValue() const
QTransform::translate
QTransform & translate(qreal dx, qreal dy)
KapmanItem::startAnim
void startAnim()
Starts the KapmanItem animation.
Definition: kapmanitem.cpp:122
Character::getYSpeed
qreal getYSpeed() const
Gets the Character y-speed value.
Definition: character.cpp:63
QTimer
characteritem.h
QGraphicsSvgItem::boundingRect
virtual QRectF boundingRect() const
Element::doActionOnCollision
virtual void doActionOnCollision(Kapman *p_kapman)
Computes an action on a collision with the Kapman.
Definition: element.cpp:28
KapmanItem::startBlinking
void startBlinking()
Implements the CharacterItem method.
Definition: kapmanitem.cpp:149
KapmanItem::KapmanItem
KapmanItem(Kapman *p_model)
Creates a new KapmanItem instance.
Definition: kapmanitem.cpp:33
KapmanItem::resumeAnim
void resumeAnim()
Resumes the KapmanItem animation.
Definition: kapmanitem.cpp:134
KapmanItem::setFrame
void setFrame(const int p_frame)
Sets the given frame to the KapmanItem.
Definition: kapmanitem.cpp:145
ElementItem
This class is the graphical representation of a game Element.
Definition: elementitem.h:28
QString
QList
kapmanitem.h
QTimer::stop
void stop()
KapmanItem::updateDirection
void updateDirection()
Rotates the image function of the Kapman direction.
Definition: kapmanitem.cpp:68
CharacterItem::blink
virtual void blink()
Makes the character blink.
Definition: characteritem.cpp:53
QTransform::rotate
QTransform & rotate(qreal angle, Qt::Axis axis)
KapmanItem::blink
void blink()
Implements the CharacterItem method.
Definition: kapmanitem.cpp:155
settings.h
ghost.h
QGraphicsSvgItem::setElementId
void setElementId(const QString &id)
QGraphicsItem::transform
QTransform transform() const
QGraphicsItem::setTransform
void setTransform(const QTransform &matrix, bool combine)
QTimeLine::stop
void stop()
QTimeLine
CharacterItem::m_nbBlinks
int m_nbBlinks
Number of ticks of the blink timer.
Definition: characteritem.h:39
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KapmanItem::manageCollision
void manageCollision()
Manages the collisions with any Element.
Definition: kapmanitem.cpp:95
Kapman
This class represents the main character of the game.
Definition: kapman.h:26
QTimeLine::start
void start()
QTimeLine::state
State state() const
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