File indexing completed on 2024-04-28 07:51:04

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef PLAYER_H
0009 #define PLAYER_H
0010 
0011 #include "character.h"
0012 
0013 class PlayerSettings;
0014 class QKeyEvent;
0015 class QString;
0016 class QTimer;
0017 class Bonus;
0018 
0019 /**
0020  * @brief This class represents the main character of the game.
0021  */
0022 class Player : public Character
0023 {
0024     Q_OBJECT
0025 
0026 private:
0027     /** The path to the Player image */
0028     QString m_playerName;
0029     QString m_desktopFilePath;
0030     QString m_graphicsFile;
0031 
0032     /** Player asked speed */
0033     qreal m_askedXSpeed, m_askedYSpeed;
0034 
0035     /** the direction the player is moving/looking */
0036     int m_direction;
0037 
0038     /** Player bomb power */
0039     int m_bombPower;
0040 
0041     /** Player bomb total armory */
0042     int m_maxBombArmory;
0043 
0044     /** Player bomb armory */
0045     int m_bombArmory;
0046 
0047     /** Player death flag */
0048     bool m_death;
0049 
0050     /** Points from the player */
0051     int m_points;
0052 
0053     /** flag if the player is on ice */
0054     bool m_onIce;
0055 
0056     /** flag if the player falls into a hole */
0057     bool m_falling;
0058 
0059     /** flag if the player player has the throw bomb bonus */
0060     bool m_throwBomb;
0061 
0062     /** flag if the player player has the kick bomb bonus */
0063     bool m_kickBomb;
0064 
0065     /** 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 */
0066     bool m_omitBombCurrentCell;
0067 
0068     /** a list with the gathered shields from the shield bonus */
0069     QList <int> m_listShield;
0070 
0071     /** the bad bonus type */
0072     Granatier::Bonus::Type m_badBonusType;
0073 
0074     /** the speed before a bad bonus was taken */
0075     qreal m_normalSpeed;
0076 
0077     /** the bomb armory before a bad bonus was taken */
0078     int m_normalBombArmory;
0079 
0080     /** flag if the mirror bad bunus was taken */
0081     bool m_moveMirrored;
0082 
0083     /** timer for the bad bonus to disapear */
0084     QTimer* m_badBonusCountdownTimer;
0085     /** milliseconds to elapse till the last bad bonus will be removed */
0086     int m_badBonusMillisecondsToElapse;
0087 
0088 
0089 
0090 public:
0091     /**
0092       * Creates a new Player instance.
0093       * @param p_x the initial x-coordinate
0094       * @param p_y the initial y-coordinate
0095       * @param p_playerID the Player ID for PlayerSettings
0096       * @param p_playerSettings the PlayerSettings
0097       * @param p_arena the Arena the Player is on
0098       */
0099     Player(qreal p_x, qreal p_y, const QString& p_playerID, const PlayerSettings* p_playerSettings, Arena* p_arena);
0100 
0101     /**
0102       * Deletes the Player instance.
0103       */
0104     ~Player() override;
0105 
0106     /**
0107       * Shortcuts for moving and dropping bombs
0108       */
0109     void setShortcuts(const Shortcuts &keys);
0110 
0111     /**
0112       * Gets the file for the Player SVG.
0113       * @return the file for the Player SVG
0114       */
0115     QString getGraphicsFile() const;
0116 
0117     /**
0118      * Gets the path to the Player Desktop file.
0119      * @return the path to the Player Desktop file
0120      */
0121     QString getDesktopFilePath() const;
0122 
0123     /**
0124       * Gets the Player name.
0125       * @return the Player name
0126       */
0127     QString getPlayerName() const;
0128 
0129     /**
0130       * Initializes the Player.
0131       */
0132     void init();
0133 
0134     /**
0135       * pause the timer
0136       */
0137     void pause();
0138 
0139     /**
0140       * resume the timer
0141       */
0142     void resume();
0143 
0144     /**
0145       * Makes the Player ask to go up
0146       */
0147     void goUp() override;
0148 
0149     /**
0150       * Makes the Player ask to go down
0151       */
0152     void goDown() override;
0153 
0154     /**
0155       * Makes the Player ask to go to the right
0156       */
0157     void goRight() override;
0158 
0159     /**
0160       * Makes the Player ask to go to the left
0161       */
0162     void goLeft() override;
0163 
0164     /**
0165       * Moves the Player function of its current coordinates and speed.
0166       */
0167     void move(qreal x, qreal y);
0168 
0169     /**
0170       * Updates the Player move
0171       */
0172     void updateMove() override;
0173 
0174     /**
0175       * @return the asked x speed value
0176       */
0177     qreal getAskedXSpeed() const;
0178 
0179     /**
0180       * @return the asked y speed value
0181       */
0182     qreal getAskedYSpeed() const;
0183 
0184     /**
0185       * @return the direction the player is moving/looking
0186       */
0187     int direction();
0188 
0189     /**
0190       * @return the bomb power
0191       */
0192     int getBombPower() const;
0193 
0194     /**
0195       * decrements the bomb armory
0196       */
0197     void decrementBombArmory();
0198 
0199     /**
0200       * Manages the points won
0201       * @param p_bonus reference to the bonus taken
0202       */
0203     void addBonus(Bonus* p_bonus);
0204 
0205     /**
0206       * Manages the points won
0207       * @param n_ExplosionID the ID from the explosion which hit the player
0208       * @return true if there is an unused shield, false otherwise
0209       */
0210     bool shield(int n_ExplosionID);
0211 
0212     /**
0213       * @return true if the player has the shield bonus
0214       */
0215     bool hasShield();
0216 
0217     /**
0218       * @return true if the player has the throw bonus
0219       */
0220     bool hasThrowBomb();
0221 
0222     /**
0223       * @return true if the player has the kick bonus
0224       */
0225     bool hasKickBomb();
0226 
0227     /**
0228       * @return true if the player has a bad bonus
0229       */
0230     bool hasBadBonus();
0231 
0232     /**
0233       * Implements the Character function
0234       */
0235     void die();
0236 
0237     /**
0238       *  returns if the player is alive
0239       */
0240     bool isAlive() const;
0241 
0242     /**
0243       * resurrects the player
0244       */
0245     void resurrect();
0246 
0247     /**
0248       * returns the points
0249       */
0250     int points() const;
0251 
0252     /**
0253       * adds a point
0254       */
0255     void addPoint();
0256 
0257     /**
0258       * Emits a signal to PlayerItem in order to manage collisions
0259       */
0260     void emitGameUpdated();
0261 
0262     /**
0263       * Manages the keys for moving and dropping bombs.
0264       */
0265     void keyPressed(QKeyEvent* keyEvent);
0266 
0267     /**
0268       * Manages the keys for moving and dropping bombs.
0269       */
0270     void keyReleased(QKeyEvent* keyEvent);
0271 
0272     /**
0273       * Returns the sign of a value with a positive sign for zero
0274       */
0275       //TODO: find a better place
0276     int signZeroPositive(const qreal value);
0277     /**
0278       * Returns the sign of a value with 0 for zero
0279       */
0280     int sign(const qreal value);
0281 
0282 private:
0283     /**
0284       * Updates the Player direction with the asked direction
0285       */
0286     void updateDirection();
0287 
0288     /**
0289       * Stops moving the Player
0290       */
0291     void stopMoving();
0292 
0293 public Q_SLOTS:
0294     /**
0295      * refills the bomb armory after a bomb is exploded
0296      */
0297     void slot_refillBombArmory();
0298 
0299 private Q_SLOTS:
0300     /**
0301      * removes the bad bonus from the player
0302      */
0303     void slot_removeBadBonus();
0304 
0305     /**
0306      * emits the signal with the elapsed bad bonus time for the infosidebar
0307      */
0308     void slot_badBonusTimerTimeout();
0309 
0310 Q_SIGNALS:
0311     /**
0312       * Emitted when the direction changed
0313       */
0314     void directionChanged();
0315 
0316     /**
0317       * Signals to PlayerItem that the game has been updated
0318       */
0319       void gameUpdated();
0320 
0321       /**
0322       * Emitted when the player stops moving
0323       */
0324       void stopped();
0325 
0326       /**
0327       * Emitted when the player drops a bomb
0328       */
0329       void bombDropped(Player* player, qreal x, qreal y, bool newBomb, int throwDistance);
0330 
0331       /**
0332       * Emitted when the player is dying
0333       */
0334       void dying();
0335 
0336       /**
0337       * Emitted when the player is falling in a hole
0338       */
0339       void falling();
0340 
0341       /**
0342       * Emitted when the player has taken the resurrect bonus
0343       */
0344       void resurrectBonusTaken();
0345 
0346       /**
0347       * Emitted when the player has resurrected
0348       */
0349       void resurrected();
0350 
0351        /**
0352       * Emitted when the player has taken a bonus
0353       * @param player the player which info changed
0354       * @param bonusType the bonus that was taken
0355       * @param percentageElapsed the bad bonus time that has elapsed
0356       */
0357       void bonusUpdated(Player* player, Granatier::Bonus::Type bonusType, int percentageElapsed);
0358 };
0359 
0360 #endif