1 /* 2 SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de> 3 SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr> 4 5 SPDX-License-Identifier: GPL-2.0-or-later 6 */ 7 8 #ifndef PLAYER_H 9 #define PLAYER_H 10 11 #include "character.h" 12 13 class PlayerSettings; 14 class QKeyEvent; 15 class QString; 16 class QTimer; 17 class Bonus; 18 19 /** 20 * @brief This class represents the main character of the game. 21 */ 22 class Player : public Character 23 { 24 Q_OBJECT 25 26 private: 27 /** The path to the Player image */ 28 QString m_playerName; 29 QString m_desktopFilePath; 30 QString m_graphicsFile; 31 32 /** Player asked speed */ 33 qreal m_askedXSpeed, m_askedYSpeed; 34 35 /** the direction the player is moving/looking */ 36 int m_direction; 37 38 /** Player bomb power */ 39 int m_bombPower; 40 41 /** Player bomb total armory */ 42 int m_maxBombArmory; 43 44 /** Player bomb armory */ 45 int m_bombArmory; 46 47 /** Player death flag */ 48 bool m_death; 49 50 /** Points from the player */ 51 int m_points; 52 53 /** flag if the player is on ice */ 54 bool m_onIce; 55 56 /** flag if the player falls into a hole */ 57 bool m_falling; 58 59 /** flag if the player player has the throw bomb bonus */ 60 bool m_throwBomb; 61 62 /** flag if the player player has the kick bomb bonus */ 63 bool m_kickBomb; 64 65 /** flag if the player should omit bomb kicking if it's the bomb currently dropped and the player hasn't stopped in the mean time or changed direction */ 66 bool m_omitBombCurrentCell; 67 68 /** a list with the gathered shields from the shield bonus */ 69 QList <int> m_listShield; 70 71 /** the bad bonus type */ 72 Granatier::Bonus::Type m_badBonusType; 73 74 /** the speed before a bad bonus was taken */ 75 qreal m_normalSpeed; 76 77 /** the bomb armory before a bad bonus was taken */ 78 int m_normalBombArmory; 79 80 /** flag if the mirror bad bunus was taken */ 81 bool m_moveMirrored; 82 83 /** timer for the bad bonus to disapear */ 84 QTimer* m_badBonusCountdownTimer; 85 /** milliseconds to elapse till the last bad bonus will be removed */ 86 int m_badBonusMillisecondsToElapse; 87 88 89 90 public: 91 /** 92 * Creates a new Player instance. 93 * @param p_x the initial x-coordinate 94 * @param p_y the initial y-coordinate 95 * @param p_playerID the Player ID for PlayerSettings 96 * @param p_playerSettings the PlayerSettings 97 * @param p_arena the Arena the Player is on 98 */ 99 Player(qreal p_x, qreal p_y, const QString& p_playerID, const PlayerSettings* p_playerSettings, Arena* p_arena); 100 101 /** 102 * Deletes the Player instance. 103 */ 104 ~Player() override; 105 106 /** 107 * Shortcuts for moving and dropping bombs 108 */ 109 void setShortcuts(const Shortcuts &keys); 110 111 /** 112 * Gets the file for the Player SVG. 113 * @return the file for the Player SVG 114 */ 115 QString getGraphicsFile() const; 116 117 /** 118 * Gets the path to the Player Desktop file. 119 * @return the path to the Player Desktop file 120 */ 121 QString getDesktopFilePath() const; 122 123 /** 124 * Gets the Player name. 125 * @return the Player name 126 */ 127 QString getPlayerName() const; 128 129 /** 130 * Initializes the Player. 131 */ 132 void init(); 133 134 /** 135 * pause the timer 136 */ 137 void pause(); 138 139 /** 140 * resume the timer 141 */ 142 void resume(); 143 144 /** 145 * Makes the Player ask to go up 146 */ 147 void goUp() override; 148 149 /** 150 * Makes the Player ask to go down 151 */ 152 void goDown() override; 153 154 /** 155 * Makes the Player ask to go to the right 156 */ 157 void goRight() override; 158 159 /** 160 * Makes the Player ask to go to the left 161 */ 162 void goLeft() override; 163 164 /** 165 * Moves the Player function of its current coordinates and speed. 166 */ 167 void move(qreal x, qreal y); 168 169 /** 170 * Updates the Player move 171 */ 172 void updateMove() override; 173 174 /** 175 * @return the asked x speed value 176 */ 177 qreal getAskedXSpeed() const; 178 179 /** 180 * @return the asked y speed value 181 */ 182 qreal getAskedYSpeed() const; 183 184 /** 185 * @return the direction the player is moving/looking 186 */ 187 int direction(); 188 189 /** 190 * @return the bomb power 191 */ 192 int getBombPower() const; 193 194 /** 195 * decrements the bomb armory 196 */ 197 void decrementBombArmory(); 198 199 /** 200 * Manages the points won 201 * @param p_bonus reference to the bonus taken 202 */ 203 void addBonus(Bonus* p_bonus); 204 205 /** 206 * Manages the points won 207 * @param n_ExplosionID the ID from the explosion which hit the player 208 * @return true if there is an unused shield, false otherwise 209 */ 210 bool shield(int n_ExplosionID); 211 212 /** 213 * @return true if the player has the shield bonus 214 */ 215 bool hasShield(); 216 217 /** 218 * @return true if the player has the throw bonus 219 */ 220 bool hasThrowBomb(); 221 222 /** 223 * @return true if the player has the kick bonus 224 */ 225 bool hasKickBomb(); 226 227 /** 228 * @return true if the player has a bad bonus 229 */ 230 bool hasBadBonus(); 231 232 /** 233 * Implements the Character function 234 */ 235 void die(); 236 237 /** 238 * returns if the player is alive 239 */ 240 bool isAlive() const; 241 242 /** 243 * resurrects the player 244 */ 245 void resurrect(); 246 247 /** 248 * returns the points 249 */ 250 int points() const; 251 252 /** 253 * adds a point 254 */ 255 void addPoint(); 256 257 /** 258 * Emits a signal to PlayerItem in order to manage collisions 259 */ 260 void emitGameUpdated(); 261 262 /** 263 * Manages the keys for moving and dropping bombs. 264 */ 265 void keyPressed(QKeyEvent* keyEvent); 266 267 /** 268 * Manages the keys for moving and dropping bombs. 269 */ 270 void keyReleased(QKeyEvent* keyEvent); 271 272 /** 273 * Returns the sign of a value with a positive sign for zero 274 */ 275 //TODO: find a better place 276 int signZeroPositive(const qreal value); 277 /** 278 * Returns the sign of a value with 0 for zero 279 */ 280 int sign(const qreal value); 281 282 private: 283 /** 284 * Updates the Player direction with the asked direction 285 */ 286 void updateDirection(); 287 288 /** 289 * Stops moving the Player 290 */ 291 void stopMoving(); 292 293 public Q_SLOTS: 294 /** 295 * refills the bomb armory after a bomb is exploded 296 */ 297 void slot_refillBombArmory(); 298 299 private Q_SLOTS: 300 /** 301 * removes the bad bonus from the player 302 */ 303 void slot_removeBadBonus(); 304 305 /** 306 * emits the signal with the elapsed bad bonus time for the infosidebar 307 */ 308 void slot_badBonusTimerTimeout(); 309 310 Q_SIGNALS: 311 /** 312 * Emitted when the direction changed 313 */ 314 void directionChanged(); 315 316 /** 317 * Signals to PlayerItem that the game has been updated 318 */ 319 void gameUpdated(); 320 321 /** 322 * Emitted when the player stops moving 323 */ 324 void stopped(); 325 326 /** 327 * Emitted when the player drops a bomb 328 */ 329 void bombDropped(Player* player, qreal x, qreal y, bool newBomb, int throwDistance); 330 331 /** 332 * Emitted when the player is dying 333 */ 334 void dying(); 335 336 /** 337 * Emitted when the player is falling in a hole 338 */ 339 void falling(); 340 341 /** 342 * Emitted when the player has taken the resurrect bonus 343 */ 344 void resurrectBonusTaken(); 345 346 /** 347 * Emitted when the player has resurrected 348 */ 349 void resurrected(); 350 351 /** 352 * Emitted when the player has taken a bonus 353 * @param player the player which info changed 354 * @param bonusType the bonus that was taken 355 * @param percentageElapsed the bad bonus time that has elapsed 356 */ 357 void bonusUpdated(Player* player, Granatier::Bonus::Type bonusType, int percentageElapsed); 358 }; 359 360 #endif 361