Changeset View
Changeset View
Standalone View
Standalone View
game.cpp
Show All 39 Lines | |||||
40 | #include <KMessageBox> | 40 | #include <KMessageBox> | ||
41 | #include <KRandom> | 41 | #include <KRandom> | ||
42 | #include <KUrl> | 42 | #include <KUrl> | ||
43 | #include <Box2D/Dynamics/b2Body.h> | 43 | #include <Box2D/Dynamics/b2Body.h> | ||
44 | #include <Box2D/Dynamics/Contacts/b2Contact.h> | 44 | #include <Box2D/Dynamics/Contacts/b2Contact.h> | ||
45 | #include <Box2D/Dynamics/b2Fixture.h> | 45 | #include <Box2D/Dynamics/b2Fixture.h> | ||
46 | #include <Box2D/Dynamics/b2World.h> | 46 | #include <Box2D/Dynamics/b2World.h> | ||
47 | #include <Box2D/Dynamics/b2WorldCallbacks.h> | 47 | #include <Box2D/Dynamics/b2WorldCallbacks.h> | ||
48 | 48 | | |||
aacid: this should go now? | |||||
49 | inline QString makeGroup(int id, int hole, const QString &name, int x, int y) | 49 | inline QString makeGroup(int id, int hole, const QString &name, int x, int y) | ||
50 | { | 50 | { | ||
51 | return QString("%1-%2@%3,%4|%5").arg(hole).arg(name).arg(x).arg(y).arg(id); | 51 | return QString("%1-%2@%3,%4|%5").arg(hole).arg(name).arg(x).arg(y).arg(id); | ||
52 | } | 52 | } | ||
53 | 53 | | |||
54 | inline QString makeStateGroup(int id, const QString &name) | 54 | inline QString makeStateGroup(int id, const QString &name) | ||
55 | { | 55 | { | ||
56 | return QString("%1|%2").arg(name).arg(id); | 56 | return QString("%1|%2").arg(name).arg(id); | ||
▲ Show 20 Lines • Show All 383 Lines • ▼ Show 20 Line(s) | 407 | { | |||
440 | p->setPen(QPen(Qt::white, 3)); | 440 | p->setPen(QPen(Qt::white, 3)); | ||
441 | p->drawLine(QPointF(x() + iwidth / 2, y() + iheight - ithickness * 1.5), QPointF(x() + iwidth / 2, y() + iheight)); | 441 | p->drawLine(QPointF(x() + iwidth / 2, y() + iheight - ithickness * 1.5), QPointF(x() + iwidth / 2, y() + iheight)); | ||
442 | p->drawLine(QPointF(x() + iwidth / 4 - iwidth / 20, y() + iheight - iheight / 4 + iheight / 20), QPointF(x() + iwidth / 4 + iwidth / 20, y() + iheight - iheight / 4 - iheight / 20)); | 442 | p->drawLine(QPointF(x() + iwidth / 4 - iwidth / 20, y() + iheight - iheight / 4 + iheight / 20), QPointF(x() + iwidth / 4 + iwidth / 20, y() + iheight - iheight / 4 - iheight / 20)); | ||
443 | p->drawLine(QPointF(x() + iwidth - iwidth / 4 + iwidth / 20, y() + iheight - iheight / 4 + iheight / 20), QPointF(x() + iwidth - iwidth / 4 - iwidth / 20, y() + iheight - iheight / 4 - iheight / 20)); | 443 | p->drawLine(QPointF(x() + iwidth - iwidth / 4 + iwidth / 20, y() + iheight - iheight / 4 + iheight / 20), QPointF(x() + iwidth - iwidth / 4 - iwidth / 20, y() + iheight - iheight / 4 - iheight / 20)); | ||
444 | } | 444 | } | ||
445 | ///////////////////////////////////////// | 445 | ///////////////////////////////////////// | ||
446 | 446 | | |||
447 | KolfGame::KolfGame(const Kolf::ItemFactory& factory, PlayerList *players, const QString &filename, QWidget *parent) | 447 | KolfGame::KolfGame(const Kolf::ItemFactory& factory, PlayerList *players, const QString &filename, QWidget *parent) | ||
448 | : QGraphicsView(parent) | 448 | : QGraphicsView(parent), | ||
449 | , m_factory(factory) | 449 | m_factory(factory), | ||
450 | , holeInfo(g_world) | 450 | m_soundBlackHole(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/blackhole.wav")), | ||
aacid: Please fix the reordering warning this creates. | |||||
451 | m_soundBlackHoleEject(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/blackholeeject.wav")), | ||||
452 | m_soundBlackHolePutIn(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/blackholeputin.wav")), | ||||
453 | m_soundHit(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/hit.wav")), | ||||
454 | m_soundHoled(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/holed.wav")), | ||||
455 | m_soundHoleINone(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/holeinone.wav")), | ||||
456 | m_soundPuddle(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/puddle.wav")), | ||||
457 | m_soundWall(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/wall.wav")), | ||||
458 | m_soundWooHoo(QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds/woohoo.wav")), | ||||
459 | holeInfo(g_world) | ||||
451 | { | 460 | { | ||
452 | setRenderHint(QPainter::Antialiasing); | 461 | setRenderHint(QPainter::Antialiasing); | ||
453 | // for mouse control | 462 | // for mouse control | ||
454 | setMouseTracking(true); | 463 | setMouseTracking(true); | ||
455 | viewport()->setMouseTracking(true); | 464 | viewport()->setMouseTracking(true); | ||
456 | setFrameShape(NoFrame); | 465 | setFrameShape(NoFrame); | ||
457 | 466 | | |||
458 | regAdv = false; | 467 | regAdv = false; | ||
Show All 9 Lines | |||||
468 | inPlay = false; | 477 | inPlay = false; | ||
469 | putting = false; | 478 | putting = false; | ||
470 | stroking = false; | 479 | stroking = false; | ||
471 | editing = false; | 480 | editing = false; | ||
472 | strict = false; | 481 | strict = false; | ||
473 | lastDelId = -1; | 482 | lastDelId = -1; | ||
474 | m_showInfo = false; | 483 | m_showInfo = false; | ||
475 | ballStateList.canUndo = false; | 484 | ballStateList.canUndo = false; | ||
476 | soundDir = QStandardPaths::locate(QStandardPaths::AppDataLocation, "sounds", QStandardPaths::LocateDirectory); | | |||
477 | dontAddStroke = false; | 485 | dontAddStroke = false; | ||
478 | addingNewHole = false; | 486 | addingNewHole = false; | ||
479 | scoreboardHoles = 0; | 487 | scoreboardHoles = 0; | ||
480 | infoShown = false; | 488 | infoShown = false; | ||
481 | m_useMouse = true; | 489 | m_useMouse = true; | ||
482 | m_useAdvancedPutting = true; | 490 | m_useAdvancedPutting = true; | ||
483 | m_sound = true; | 491 | m_sound = true; | ||
484 | m_ignoreEvents = false; | 492 | m_ignoreEvents = false; | ||
485 | highestHole = 0; | 493 | highestHole = 0; | ||
486 | recalcHighestHole = false; | 494 | recalcHighestHole = false; | ||
487 | banner = 0; | 495 | banner = 0; | ||
488 | 496 | | |||
489 | #ifdef SOUND | | |||
490 | m_player = Phonon::createPlayer(Phonon::GameCategory); | | |||
491 | #endif | | |||
492 | | ||||
493 | holeInfo.setGame(this); | 497 | holeInfo.setGame(this); | ||
494 | holeInfo.setAuthor(i18n("Course Author")); | 498 | holeInfo.setAuthor(i18n("Course Author")); | ||
495 | holeInfo.setName(i18n("Course Name")); | 499 | holeInfo.setName(i18n("Course Name")); | ||
496 | holeInfo.setUntranslatedName(i18n("Course Name")); | 500 | holeInfo.setUntranslatedName(i18n("Course Name")); | ||
497 | holeInfo.setMaxStrokes(10); | 501 | holeInfo.setMaxStrokes(10); | ||
498 | holeInfo.borderWallsChanged(true); | 502 | holeInfo.borderWallsChanged(true); | ||
499 | 503 | | |||
500 | // width and height are the width and height of the scene | 504 | // width and height are the width and height of the scene | ||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Line(s) | |||||
597 | setUseAdvancedPutting(false); | 601 | setUseAdvancedPutting(false); | ||
598 | 602 | | |||
599 | putting = false; | 603 | putting = false; | ||
600 | putterTimer = new QTimer(this); | 604 | putterTimer = new QTimer(this); | ||
601 | connect(putterTimer, &QTimer::timeout, this, &KolfGame::putterTimeout); | 605 | connect(putterTimer, &QTimer::timeout, this, &KolfGame::putterTimeout); | ||
602 | putterTimerMsec = 20; | 606 | putterTimerMsec = 20; | ||
603 | } | 607 | } | ||
604 | 608 | | |||
609 | void KolfGame::playSound(Sound soundType) | ||||
610 | { | ||||
611 | if (m_sound) { | ||||
612 | switch (soundType) { | ||||
613 | case Sound::BlackHole: | ||||
614 | m_soundBlackHole.start(); | ||||
615 | break; | ||||
616 | case Sound::BlackHoleEject: | ||||
617 | m_soundBlackHoleEject.start(); | ||||
618 | break; | ||||
619 | case Sound::BlackHolePutIn: | ||||
620 | m_soundBlackHolePutIn.start(); | ||||
621 | break; | ||||
622 | case Sound::Hit: | ||||
623 | m_soundHit.start(); | ||||
624 | break; | ||||
625 | case Sound::Holed: | ||||
626 | m_soundHoled.start(); | ||||
627 | break; | ||||
628 | case Sound::HoleINone: | ||||
629 | m_soundHoleINone.start(); | ||||
630 | break; | ||||
631 | case Sound::Puddle: | ||||
632 | m_soundPuddle.start(); | ||||
633 | break; | ||||
634 | case Sound::Wall: | ||||
635 | m_soundWall.start(); | ||||
636 | break; | ||||
637 | case Sound::WooHoo: | ||||
638 | m_soundWooHoo.start(); | ||||
639 | break; | ||||
640 | default: | ||||
641 | qWarning() << "There was a request to play an unknown sound."; | ||||
642 | break; | ||||
643 | } | ||||
644 | } | ||||
645 | } | ||||
646 | | ||||
605 | void KolfGame::startFirstHole(int hole) | 647 | void KolfGame::startFirstHole(int hole) | ||
606 | { | 648 | { | ||
607 | if (curHole > 0) // if there was saved game, sync scoreboard | 649 | if (curHole > 0) // if there was saved game, sync scoreboard | ||
608 | // with number of holes | 650 | // with number of holes | ||
609 | { | 651 | { | ||
610 | for (; scoreboardHoles < curHole; ++scoreboardHoles) | 652 | for (; scoreboardHoles < curHole; ++scoreboardHoles) | ||
611 | { | 653 | { | ||
612 | cfgGroup = KConfigGroup(cfg->group(QString("%1-hole@-50,-50|0").arg(scoreboardHoles + 1))); | 654 | cfgGroup = KConfigGroup(cfg->group(QString("%1-hole@-50,-50|0").arg(scoreboardHoles + 1))); | ||
Show All 27 Lines | 681 | { | |||
640 | QList<QGraphicsItem*> itemsCopy(m_topLevelQItems); //this list will be modified soon, so take a copy | 682 | QList<QGraphicsItem*> itemsCopy(m_topLevelQItems); //this list will be modified soon, so take a copy | ||
641 | foreach (QGraphicsItem* item, itemsCopy) | 683 | foreach (QGraphicsItem* item, itemsCopy) | ||
642 | { | 684 | { | ||
643 | CanvasItem* citem = dynamic_cast<CanvasItem*>(item); | 685 | CanvasItem* citem = dynamic_cast<CanvasItem*>(item); | ||
644 | delete citem; | 686 | delete citem; | ||
645 | } | 687 | } | ||
646 | 688 | | |||
647 | delete cfg; | 689 | delete cfg; | ||
648 | #ifdef SOUND | | |||
649 | delete m_player; | | |||
650 | #endif | | |||
651 | } | 690 | } | ||
652 | 691 | | |||
653 | void KolfGame::setModified(bool mod) | 692 | void KolfGame::setModified(bool mod) | ||
654 | { | 693 | { | ||
655 | modified = mod; | 694 | modified = mod; | ||
656 | emit modifiedChanged(mod); | 695 | emit modifiedChanged(mod); | ||
657 | } | 696 | } | ||
658 | 697 | | |||
▲ Show 20 Lines • Show All 386 Lines • ▼ Show 20 Line(s) | 1083 | { | |||
1045 | emit inPlayEnd(); | 1084 | emit inPlayEnd(); | ||
1046 | 1085 | | |||
1047 | int curScore = (*curPlayer).score(curHole); | 1086 | int curScore = (*curPlayer).score(curHole); | ||
1048 | if (!dontAddStroke) | 1087 | if (!dontAddStroke) | ||
1049 | curScore++; | 1088 | curScore++; | ||
1050 | 1089 | | |||
1051 | if (curScore == 1) | 1090 | if (curScore == 1) | ||
1052 | { | 1091 | { | ||
1053 | playSound("holeinone"); | 1092 | playSound(Sound::HoleINone); | ||
1054 | } | 1093 | } | ||
1055 | else if (curScore <= holeInfo.par()) | 1094 | else if (curScore <= holeInfo.par()) | ||
1056 | { | 1095 | { | ||
1057 | // I don't have a sound!! | 1096 | playSound(Sound::WooHoo); | ||
1058 | // *sob* | | |||
1059 | // playSound("woohoo"); | | |||
1060 | } | 1097 | } | ||
1061 | 1098 | | |||
1062 | (*curPlayer).ball()->setZValue((*curPlayer).ball()->zValue() + .1 - (.1)/(curScore)); | 1099 | (*curPlayer).ball()->setZValue((*curPlayer).ball()->zValue() + .1 - (.1)/(curScore)); | ||
1063 | 1100 | | |||
1064 | if (allPlayersDone()) | 1101 | if (allPlayersDone()) | ||
1065 | { | 1102 | { | ||
1066 | inPlay = false; | 1103 | inPlay = false; | ||
1067 | 1104 | | |||
▲ Show 20 Lines • Show All 420 Lines • ▼ Show 20 Line(s) | |||||
1488 | 1525 | | |||
1489 | void KolfGame::emitMax() | 1526 | void KolfGame::emitMax() | ||
1490 | { | 1527 | { | ||
1491 | emit maxStrokesReached(playerWhoMaxed); | 1528 | emit maxStrokesReached(playerWhoMaxed); | ||
1492 | } | 1529 | } | ||
1493 | 1530 | | |||
1494 | void KolfGame::startBall(const Vector &velocity) | 1531 | void KolfGame::startBall(const Vector &velocity) | ||
1495 | { | 1532 | { | ||
1496 | playSound("hit"); | 1533 | playSound(Sound::Hit); | ||
1497 | | ||||
1498 | emit inPlayStart(); | 1534 | emit inPlayStart(); | ||
1499 | putter->setVisible(false); | 1535 | putter->setVisible(false); | ||
1500 | 1536 | | |||
1501 | (*curPlayer).ball()->setState(Rolling); | 1537 | (*curPlayer).ball()->setState(Rolling); | ||
1502 | (*curPlayer).ball()->setVelocity(velocity); | 1538 | (*curPlayer).ball()->setVelocity(velocity); | ||
1503 | (*curPlayer).ball()->shotStarted(); | 1539 | (*curPlayer).ball()->shotStarted(); | ||
1504 | 1540 | | |||
1505 | foreach (QGraphicsItem* qitem, m_topLevelQItems) | 1541 | foreach (QGraphicsItem* qitem, m_topLevelQItems) | ||
▲ Show 20 Lines • Show All 732 Lines • ▼ Show 20 Line(s) | 2273 | { | |||
2238 | //false = do not create overlay if it does not exist yet | 2274 | //false = do not create overlay if it does not exist yet | ||
2239 | Kolf::Overlay* otherOverlay = otherCitem->overlay(false); | 2275 | Kolf::Overlay* otherOverlay = otherCitem->overlay(false); | ||
2240 | if (otherOverlay) | 2276 | if (otherOverlay) | ||
2241 | otherOverlay->setState(Kolf::Overlay::Passive); | 2277 | otherOverlay->setState(Kolf::Overlay::Passive); | ||
2242 | } | 2278 | } | ||
2243 | } | 2279 | } | ||
2244 | } | 2280 | } | ||
2245 | 2281 | | |||
2246 | #ifdef SOUND | | |||
2247 | void KolfGame::playSound(const QString& file, float vol) | | |||
2248 | { | | |||
2249 | if (m_sound) | | |||
2250 | { | | |||
2251 | QString resFile = soundDir + '/' + file + QString::fromLatin1(".wav"); | | |||
2252 | | ||||
2253 | // not needed when all of the files are in the distribution | | |||
2254 | //if (!QFile::exists(resFile)) | | |||
2255 | //return; | | |||
2256 | if (vol > 1) | | |||
2257 | vol = 1; | | |||
2258 | m_player->setCurrentSource(resFile); | | |||
2259 | m_player->play(); | | |||
2260 | } | | |||
2261 | } | | |||
2262 | #else //SOUND | | |||
2263 | void KolfGame::playSound( const QString&, float ) | | |||
2264 | { | | |||
2265 | } | | |||
2266 | #endif //SOUND | | |||
2267 | | ||||
2268 | void HoleInfo::borderWallsChanged(bool yes) | 2282 | void HoleInfo::borderWallsChanged(bool yes) | ||
2269 | { | 2283 | { | ||
2270 | m_borderWalls = yes; | 2284 | m_borderWalls = yes; | ||
2271 | game->setBorderWalls(yes); | 2285 | game->setBorderWalls(yes); | ||
2272 | } | 2286 | } | ||
2273 | 2287 | | |||
2274 | bool KolfGame::allPlayersDone() | 2288 | bool KolfGame::allPlayersDone() | ||
2275 | { | 2289 | { | ||
▲ Show 20 Lines • Show All 127 Lines • Show Last 20 Lines |
this should go now?