diff options
| author | manuel <manuel@mausz.at> | 2011-05-05 00:57:07 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-05-05 00:57:07 +0200 |
| commit | ce48af53646cd9e7ec762fc1ac176b3aa620b11d (patch) | |
| tree | f8fbf2cae8c7d0cbac2696a8f4cf94410bfb4928 /pacman-c++/actor.cpp | |
| parent | e54ccad07e256ba877bd41d70bd358bd0085bd1e (diff) | |
| download | foop-ce48af53646cd9e7ec762fc1ac176b3aa620b11d.tar.gz foop-ce48af53646cd9e7ec762fc1ac176b3aa620b11d.tar.bz2 foop-ce48af53646cd9e7ec762fc1ac176b3aa620b11d.zip | |
- refactorized the whole project and made a few subprojects
- replaced tcp with enet
- added connect dialog
- some smaller bugfixes
Diffstat (limited to 'pacman-c++/actor.cpp')
| -rw-r--r-- | pacman-c++/actor.cpp | 375 |
1 files changed, 0 insertions, 375 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp deleted file mode 100644 index de8d77e..0000000 --- a/pacman-c++/actor.cpp +++ /dev/null | |||
| @@ -1,375 +0,0 @@ | |||
| 1 | #include "actor.h" | ||
| 2 | #include "util.h" | ||
| 3 | #include <QtCore/QPropertyAnimation> | ||
| 4 | #include <QtCore/QVariantAnimation> | ||
| 5 | #include <QDebug> | ||
| 6 | |||
| 7 | static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) | ||
| 8 | { | ||
| 9 | return (progress == 1.0) ? end : start; | ||
| 10 | } | ||
| 11 | |||
| 12 | Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | ||
| 13 | : GameEntity(color, parent), m_direction(Actor::None), m_local(local), m_reset(true), | ||
| 14 | m_wakaPlayer(NULL), m_roundPoints(0), m_gamePoints(0) | ||
| 15 | { | ||
| 16 | m_type = Type; | ||
| 17 | |||
| 18 | /* DON'T set any pixmap here. we've a pixmap in the animation | ||
| 19 | * but we need a sprite for the collision detection | ||
| 20 | */ | ||
| 21 | setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); | ||
| 22 | /* higher player "over" lower player */ | ||
| 23 | setZValue(m_color * 10); | ||
| 24 | |||
| 25 | /* rest of the ctor is only for clients */ | ||
| 26 | if (Constants::server) | ||
| 27 | return; | ||
| 28 | |||
| 29 | /* our actor pixmap. created after server part */ | ||
| 30 | m_pix = ":/" + QString("actor%1").arg(Util::floorLog2(color) + 1); | ||
| 31 | |||
| 32 | /* setup icon for player */ | ||
| 33 | m_icon.setPixmap(m_pix); | ||
| 34 | m_icon.setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); | ||
| 35 | |||
| 36 | /* setup static images first | ||
| 37 | * they are visible if no animation is running | ||
| 38 | * and will be the first in m_images | ||
| 39 | */ | ||
| 40 | for (int i = 0; i < 5; i++) | ||
| 41 | { | ||
| 42 | PixmapItem *img = new PixmapItem(m_pix, this); | ||
| 43 | m_images.append(img); | ||
| 44 | int x = i * Constants::sprite_offset + Constants::sprite_margin; | ||
| 45 | int y = Actor::None * Constants::sprite_offset + Constants::sprite_margin; | ||
| 46 | img->setSprite(x, y, Constants::field_size.width, Constants::field_size.height); | ||
| 47 | img->setVisible(false); | ||
| 48 | } | ||
| 49 | |||
| 50 | /* setup animation stuff */ | ||
| 51 | qRegisterAnimationInterpolator<bool>(myBooleanInterpolator); | ||
| 52 | m_moving = new QParallelAnimationGroup(this); | ||
| 53 | m_eating.append(NULL); // Actor::None | ||
| 54 | m_eating.append(setupEatingAnimation(Actor::Left)); | ||
| 55 | m_eating.append(setupEatingAnimation(Actor::Right)); | ||
| 56 | m_eating.append(setupEatingAnimation(Actor::Up)); | ||
| 57 | m_eating.append(setupEatingAnimation(Actor::Down)); | ||
| 58 | |||
| 59 | /* dieing animation */ | ||
| 60 | m_dieing = new QSequentialAnimationGroup(this); | ||
| 61 | for (int i = 0; i < 11; i++) | ||
| 62 | { | ||
| 63 | PixmapItem *img = new PixmapItem(m_pix, this); | ||
| 64 | m_images.append(img); | ||
| 65 | int x = i * Constants::sprite_offset + Constants::sprite_margin; | ||
| 66 | int y = 5 * Constants::sprite_offset + Constants::sprite_margin; | ||
| 67 | img->setSprite(x, y, Constants::field_size.width, Constants::field_size.height); | ||
| 68 | img->setVisible(false); | ||
| 69 | |||
| 70 | QPropertyAnimation *fadein = new QPropertyAnimation(img, "visible", m_dieing); | ||
| 71 | fadein->setDuration(0); | ||
| 72 | fadein->setEndValue(true); | ||
| 73 | |||
| 74 | m_dieing->addPause(130); | ||
| 75 | |||
| 76 | QPropertyAnimation *fadeout = new QPropertyAnimation(img, "visible", m_dieing); | ||
| 77 | fadeout->setDuration(0); | ||
| 78 | fadeout->setEndValue(false); | ||
| 79 | } | ||
| 80 | |||
| 81 | /* setup waka sound */ | ||
| 82 | if (local) | ||
| 83 | m_wakaPlayer = new GaplessAudioPlayer(Sound::WakaWaka, 100, this); | ||
| 84 | |||
| 85 | /* make the picture showing the current direction visible */ | ||
| 86 | m_images[m_direction]->setVisible(true); | ||
| 87 | } | ||
| 88 | |||
| 89 | QSequentialAnimationGroup *Actor::setupEatingAnimation(Actor::Movement direction) | ||
| 90 | { | ||
| 91 | QSequentialAnimationGroup *eating = new QSequentialAnimationGroup(this); | ||
| 92 | eating->setLoopCount(-1); | ||
| 93 | for (int i = 0; i < 4; i++) | ||
| 94 | { | ||
| 95 | PixmapItem *img = new PixmapItem(m_pix, this); | ||
| 96 | m_images.append(img); | ||
| 97 | int x = i * Constants::sprite_offset + Constants::sprite_margin; | ||
| 98 | int y = direction * Constants::sprite_offset + Constants::sprite_margin; | ||
| 99 | img->setSprite(x, y, Constants::field_size.width, Constants::field_size.height); | ||
| 100 | img->setVisible(false); | ||
| 101 | |||
| 102 | QPropertyAnimation *fadein = new QPropertyAnimation(img, "visible", eating); | ||
| 103 | fadein->setDuration(0); | ||
| 104 | fadein->setEndValue(true); | ||
| 105 | |||
| 106 | eating->addPause(100); | ||
| 107 | |||
| 108 | QPropertyAnimation *fadeout = new QPropertyAnimation(img, "visible", eating); | ||
| 109 | fadeout->setDuration(0); | ||
| 110 | fadeout->setEndValue(false); | ||
| 111 | |||
| 112 | QPropertyAnimation *move = new QPropertyAnimation(img, "pos", m_moving); | ||
| 113 | move->setDuration(Constants::tick - 30); | ||
| 114 | move->setEndValue(QPoint(0, 0)); | ||
| 115 | } | ||
| 116 | |||
| 117 | return eating; | ||
| 118 | } | ||
| 119 | |||
| 120 | PixmapItem &Actor::icon() | ||
| 121 | { | ||
| 122 | return m_icon; | ||
| 123 | } | ||
| 124 | |||
| 125 | const QString Actor::iconStr() | ||
| 126 | { | ||
| 127 | return QString(":/actor%1icon").arg(Util::floorLog2(m_color) + 1); | ||
| 128 | } | ||
| 129 | |||
| 130 | Actor::Movement Actor::direction() | ||
| 131 | { | ||
| 132 | return m_direction; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | void Actor::setDirection(Movement direction) | ||
| 137 | { | ||
| 138 | m_direction = direction; | ||
| 139 | } | ||
| 140 | |||
| 141 | bool Actor::isLocal() | ||
| 142 | { | ||
| 143 | return m_local; | ||
| 144 | } | ||
| 145 | |||
| 146 | bool Actor::hadReset() | ||
| 147 | { | ||
| 148 | if (!m_reset) | ||
| 149 | return false; | ||
| 150 | m_reset = false; | ||
| 151 | return true; | ||
| 152 | } | ||
| 153 | |||
| 154 | void Actor::reset() | ||
| 155 | { | ||
| 156 | m_reset = true; | ||
| 157 | if (Constants::server) | ||
| 158 | { | ||
| 159 | m_direction = Actor::None; | ||
| 160 | return; | ||
| 161 | } | ||
| 162 | |||
| 163 | stopEating(); | ||
| 164 | m_moving->stop(); | ||
| 165 | setZValue(m_color * 10); | ||
| 166 | m_dieing->stop(); | ||
| 167 | /* hide all pictures */ | ||
| 168 | for (int i = 0; i < m_images.size(); ++i) | ||
| 169 | m_images.at(i)->setVisible(false); | ||
| 170 | |||
| 171 | if (m_eating[m_direction] != NULL) | ||
| 172 | m_eating[m_direction]->stop(); | ||
| 173 | |||
| 174 | m_direction = Actor::None; | ||
| 175 | m_images[m_direction]->setVisible(true); | ||
| 176 | } | ||
| 177 | |||
| 178 | void Actor::move(QPoint newpos) | ||
| 179 | { | ||
| 180 | QPoint oldpos = pos().toPoint(); | ||
| 181 | Actor::Movement direction = Actor::None; | ||
| 182 | if (oldpos.x() - newpos.x() < 0) | ||
| 183 | direction = Actor::Right; | ||
| 184 | else if (oldpos.x() - newpos.x() > 0) | ||
| 185 | direction = Actor::Left; | ||
| 186 | else if (oldpos.y() - newpos.y() < 0) | ||
| 187 | direction = Actor::Down; | ||
| 188 | else if (oldpos.y() - newpos.y() > 0) | ||
| 189 | direction = Actor::Up; | ||
| 190 | move(direction); | ||
| 191 | } | ||
| 192 | |||
| 193 | void Actor::move(Actor::Movement direction) | ||
| 194 | { | ||
| 195 | if (Constants::server) | ||
| 196 | return moveByServer(direction); | ||
| 197 | |||
| 198 | /* stop current animation if direction changed */ | ||
| 199 | if (direction != m_direction) | ||
| 200 | { | ||
| 201 | /* hide all pictures */ | ||
| 202 | for (int i = 0; i < m_images.size(); ++i) | ||
| 203 | m_images.at(i)->setVisible(false); | ||
| 204 | |||
| 205 | if (m_eating[m_direction] != NULL) | ||
| 206 | m_eating[m_direction]->stop(); | ||
| 207 | } | ||
| 208 | |||
| 209 | QPointF endpos = movementToPoint(direction); | ||
| 210 | switch(direction) | ||
| 211 | { | ||
| 212 | case Actor::Left: | ||
| 213 | case Actor::Right: | ||
| 214 | endpos *= Constants::field_size.width; | ||
| 215 | break; | ||
| 216 | case Actor::Up: | ||
| 217 | case Actor::Down: | ||
| 218 | endpos *= Constants::field_size.height; | ||
| 219 | break; | ||
| 220 | case Actor::None: | ||
| 221 | default: | ||
| 222 | break; | ||
| 223 | } | ||
| 224 | |||
| 225 | for(int i = 0; i < m_moving->animationCount(); ++i) | ||
| 226 | { | ||
| 227 | QPropertyAnimation *move = dynamic_cast<QPropertyAnimation *>(m_moving->animationAt(i)); | ||
| 228 | move->setStartValue(QPoint(0, 0) - endpos); | ||
| 229 | } | ||
| 230 | setPos(pos() + endpos); | ||
| 231 | |||
| 232 | /* start new animation if direction changed */ | ||
| 233 | if (direction != m_direction) | ||
| 234 | { | ||
| 235 | if (direction == Actor::None) | ||
| 236 | m_images[m_direction]->setVisible(true); | ||
| 237 | else | ||
| 238 | m_eating[direction]->start(); | ||
| 239 | } | ||
| 240 | |||
| 241 | /* start moving animation */ | ||
| 242 | if (direction != Actor::None) | ||
| 243 | m_moving->start(); | ||
| 244 | |||
| 245 | m_direction = direction; | ||
| 246 | } | ||
| 247 | |||
| 248 | void Actor::moveByServer(Actor::Movement direction) | ||
| 249 | { | ||
| 250 | QPointF endpos = movementToPoint(direction); | ||
| 251 | switch(direction) | ||
| 252 | { | ||
| 253 | case Actor::Left: | ||
| 254 | case Actor::Right: | ||
| 255 | endpos *= Constants::field_size.width; | ||
| 256 | break; | ||
| 257 | case Actor::Up: | ||
| 258 | case Actor::Down: | ||
| 259 | endpos *= Constants::field_size.height; | ||
| 260 | break; | ||
| 261 | case Actor::None: | ||
| 262 | default: | ||
| 263 | break; | ||
| 264 | } | ||
| 265 | setPos(pos() + endpos); | ||
| 266 | m_direction = direction; | ||
| 267 | } | ||
| 268 | |||
| 269 | bool Actor::isMoving() | ||
| 270 | { | ||
| 271 | return (m_moving->state() == QAbstractAnimation::Running); | ||
| 272 | } | ||
| 273 | |||
| 274 | bool Actor::canEat(Actor *other, const QList<Color::Color> &order) | ||
| 275 | { | ||
| 276 | if (other == NULL || order.empty() || m_color == other->color()) | ||
| 277 | return false; | ||
| 278 | |||
| 279 | int idx = order.indexOf(m_color); | ||
| 280 | return (order.at(idx + 1) == other->color()); | ||
| 281 | } | ||
| 282 | |||
| 283 | void Actor::onDie(Actor *other) | ||
| 284 | { | ||
| 285 | other->eatingPacman(); | ||
| 286 | die(); | ||
| 287 | } | ||
| 288 | |||
| 289 | void Actor::die() | ||
| 290 | { | ||
| 291 | if (Constants::server) | ||
| 292 | return; | ||
| 293 | |||
| 294 | reset(); | ||
| 295 | m_images[m_direction]->setVisible(false); | ||
| 296 | setZValue(zValue() * 10); | ||
| 297 | m_dieing->start(); | ||
| 298 | if (m_local) | ||
| 299 | AudioManager::self()->play(Sound::Die); | ||
| 300 | } | ||
| 301 | |||
| 302 | void Actor::eatingFruit() | ||
| 303 | { | ||
| 304 | if (!m_local) | ||
| 305 | return; | ||
| 306 | AudioManager::self()->play(Sound::EatingFruit); | ||
| 307 | } | ||
| 308 | |||
| 309 | void Actor::eatingPacman() | ||
| 310 | { | ||
| 311 | if (!m_local) | ||
| 312 | return; | ||
| 313 | AudioManager::self()->play(Sound::EatingGhost); | ||
| 314 | } | ||
| 315 | |||
| 316 | void Actor::startEating() | ||
| 317 | { | ||
| 318 | if (!m_local || !m_wakaPlayer->isWorking()) | ||
| 319 | return; | ||
| 320 | m_wakaPlayer->play(); | ||
| 321 | } | ||
| 322 | |||
| 323 | void Actor::stopEating() | ||
| 324 | { | ||
| 325 | if (!m_local || !m_wakaPlayer->isWorking()) | ||
| 326 | return; | ||
| 327 | m_wakaPlayer->pause(); | ||
| 328 | } | ||
| 329 | |||
| 330 | unsigned int Actor::getRoundPoints() | ||
| 331 | { | ||
| 332 | return m_roundPoints; | ||
| 333 | } | ||
| 334 | |||
| 335 | unsigned int Actor::getGamePoints() | ||
| 336 | { | ||
| 337 | return m_gamePoints; | ||
| 338 | } | ||
| 339 | |||
| 340 | void Actor::addRoundPoints(unsigned int amount) | ||
| 341 | { | ||
| 342 | m_roundPoints += amount; | ||
| 343 | } | ||
| 344 | |||
| 345 | void Actor::finishRound(bool died) | ||
| 346 | { | ||
| 347 | if (!died) | ||
| 348 | m_gamePoints += m_roundPoints; | ||
| 349 | m_roundPoints = 0; | ||
| 350 | } | ||
| 351 | |||
| 352 | QPoint Actor::movementToPoint(const Actor::Movement direction) | ||
| 353 | { | ||
| 354 | QPoint endpos(0,0); | ||
| 355 | switch (direction) | ||
| 356 | { | ||
| 357 | case Actor::Up: | ||
| 358 | endpos = QPoint(0, -1); | ||
| 359 | break; | ||
| 360 | case Actor::Down: | ||
| 361 | endpos = QPoint(0, 1); | ||
| 362 | break; | ||
| 363 | case Actor::Left: | ||
| 364 | endpos = QPoint(-1, 0); | ||
| 365 | break; | ||
| 366 | case Actor::Right: | ||
| 367 | endpos = QPoint(1, 0); | ||
| 368 | break; | ||
| 369 | case Actor::None: | ||
| 370 | break; | ||
| 371 | default: | ||
| 372 | Q_ASSERT(false); | ||
| 373 | } | ||
| 374 | return endpos; | ||
| 375 | } | ||
