1 | /* |
2 | * Copyright 2006-2009 Parker Coates <coates@kde.org> |
3 | * |
4 | * This file is part of Killbots. |
5 | * |
6 | * Killbots is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * Killbots is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with Killbots. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #ifndef KILLBOTS_ENGINE_H |
21 | #define KILLBOTS_ENGINE_H |
22 | |
23 | #include "actions.h" |
24 | #include "ruleset.h" |
25 | |
26 | #include <QtCore/QObject> |
27 | #include <QtCore/QHash> |
28 | class QPoint; |
29 | |
30 | namespace Killbots |
31 | { |
32 | class Coordinator; |
33 | class Sprite; |
34 | |
35 | class Engine : public QObject |
36 | { |
37 | Q_OBJECT |
38 | |
39 | public: // functions |
40 | explicit Engine( Coordinator * scene, QObject * parent = 0 ); |
41 | virtual ~Engine(); |
42 | |
43 | void setRuleset( const Ruleset * ruleset ); |
44 | const Ruleset * ruleset() const; |
45 | |
46 | bool gameHasStarted() const; |
47 | bool isRoundComplete() const; |
48 | bool isHeroDead() const; |
49 | bool isBoardFull() const; |
50 | bool canSafeTeleport() const; |
51 | bool canUseVaporizer() const; |
52 | |
53 | void startNewGame(); |
54 | void startNewRound( bool incrementRound = true, const QString & layout = QString() ); |
55 | |
56 | bool moveHero( Killbots::HeroAction direction ); |
57 | bool teleportHero(); |
58 | bool teleportHeroSafely(); |
59 | bool useVaporizer(); |
60 | bool waitOutRound(); |
61 | |
62 | void moveRobots( bool justFastbots = false ); |
63 | void assessDamage(); |
64 | void resetBotCounts(); |
65 | void endGame(); |
66 | |
67 | signals: |
68 | void roundChanged( int round ); |
69 | void scoreChanged( int score ); |
70 | void enemyCountChanged( int enemyCount ); |
71 | void energyChanged( int energy ); |
72 | void gameOver( int score, int round ); |
73 | |
74 | void showNewGameMessage(); |
75 | void showRoundCompleteMessage(); |
76 | void showBoardFullMessage(); |
77 | void showGameOverMessage(); |
78 | |
79 | void teleportAllowed( bool allowed ); |
80 | void teleportSafelyAllowed( bool allowed ); |
81 | void vaporizerAllowed( bool allowed ); |
82 | void waitOutRoundAllowed( bool allowed ); |
83 | |
84 | private: // functions |
85 | void refreshSpriteMap(); |
86 | int spriteTypeAt( const QPoint & cell ) const; |
87 | QPoint offsetFromDirection( int direction ) const; |
88 | QPoint randomEmptyCell() const; |
89 | |
90 | bool cellIsValid( const QPoint & cell ) const; |
91 | bool moveIsValid( const QPoint & cell, HeroAction direction ) const; |
92 | bool moveIsSafe( const QPoint & cell, HeroAction direction ) const; |
93 | bool canPushJunkheap( const Sprite * junkheap, HeroAction direction ) const; |
94 | |
95 | void pushJunkheap( Sprite * junkheap, HeroAction direction ); |
96 | void cleanUpRound(); |
97 | void destroySprite( Sprite * sprite, bool calculatePoints = true ); |
98 | bool destroyAllCollidingBots( const Sprite * sprite, bool calculatePoints = true ); |
99 | void updateScore( int changeInScore ); |
100 | void updateEnergy( int changeInEnergy ); |
101 | |
102 | QString gridToString() const; |
103 | |
104 | private: // data members |
105 | Coordinator * m_coordinator; |
106 | |
107 | Sprite * m_hero; |
108 | QList<Sprite *> m_bots; |
109 | QList<Sprite *> m_junkheaps; |
110 | |
111 | const Ruleset * m_rules; |
112 | int m_round; |
113 | int m_score; |
114 | int m_energy; |
115 | qreal m_maxEnergy; |
116 | qreal m_robotCount; |
117 | qreal m_fastbotCount; |
118 | qreal m_junkheapCount; |
119 | |
120 | bool m_heroIsDead; |
121 | bool m_waitingOutRound; |
122 | |
123 | QMultiHash<QPoint, Sprite *> m_spriteMap; |
124 | |
125 | friend class EngineTest; |
126 | }; |
127 | } |
128 | |
129 | #endif |
130 | |