File indexing completed on 2024-04-07 03:56:34

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0004     SPDX-FileCopyrightText: 2007-2008 Alexandre Galinier <alex.galinier@hotmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef GAMESCENE_H
0010 #define GAMESCENE_H
0011 
0012 #include "granatierglobals.h"
0013 
0014 #include <QGraphicsScene>
0015 #include <QList>
0016 #include <QHash>
0017 #include <QMap>
0018 
0019 class Game;
0020 class BlockItem;
0021 class BonusItem;
0022 class ArenaItem;
0023 class Player;
0024 class PlayerItem;
0025 class Bomb;
0026 class BombItem;
0027 class BombExplosionItem;
0028 class InfoOverlay;
0029 class InfoSidebar;
0030 class QTimer;
0031 
0032 class KGameRenderer;
0033 class KGameRenderedItem;
0034 class KGameThemeProvider;
0035 
0036 /**
0037  * @brief This class contains all the Game elements to be drawn on the screen by the GameView instance.
0038  */
0039 class GameScene : public QGraphicsScene {
0040 
0041 Q_OBJECT
0042 
0043 private:
0044 
0045     /** The min size for the gamescene */
0046     QRectF m_minSize;
0047 
0048     /** The Game instance */
0049     Game* m_game;
0050 
0051     /** The ArenaItems to be drawn */
0052     ArenaItem*** m_arenaItem;
0053 
0054     /** The PlayerItem of each Player to be drawn */
0055     QList<PlayerItem*> m_playerItems;
0056 
0057     /** The BombItem of each Bomb to be drawn */
0058     QHash<BombItem*, QList<BombExplosionItem*> > m_bombItems;
0059     QList<Bomb*> m_tempBombList;
0060 
0061     /** The ElementItem to be drawn (each Block) */
0062     BlockItem*** m_blockItems;
0063 
0064     /** The Bonus ElementItem */
0065     BonusItem*** m_bonusItems;
0066 
0067     /** The Arena background */
0068     KGameRenderedItem* m_arenaBackground;
0069 
0070     /** The overlay to show the score at the end of a round*/
0071     InfoOverlay* m_infoOverlay;
0072 
0073     /** The sidebar to show the players game info*/
0074     InfoSidebar* m_infoSidebar;
0075 
0076     /** The labels to displayed the arena name */
0077     QGraphicsTextItem* m_arenaNameLabel;
0078 
0079     /** The labels to displayed the remaining time */
0080     QGraphicsTextItem* m_remainingTimeLabel;
0081 
0082     qreal m_SvgScaleFactor;
0083 
0084     /** The KGameRenderer */
0085     KGameRenderer* m_rendererSelectedTheme;
0086     KGameRenderer* m_rendererDefaultTheme;
0087     KGameRenderer* m_rendererBackground;
0088     KGameRenderer* m_rendererArenaItems;
0089     KGameRenderer* m_rendererBonusItems;
0090     KGameRenderer* m_rendererBombItems;
0091     KGameRenderer* m_rendererScoreItems;
0092     KGameRenderer* m_rendererInfoSidebar;
0093     QMap <Player*, KGameRenderer*> m_mapRendererPlayerItems;
0094     KGameThemeProvider* m_themeProvider;
0095     QTimer* m_backgroundResizeTimer;
0096 
0097 public:
0098 
0099     /**
0100       * Creates a new GameScene instance.
0101       * @param p_game the Game instance whose elements must be contained in the GameScene in order to be drawn
0102       * @param p_themeProvider the Game theme provider
0103       */
0104     GameScene(Game* p_game, KGameThemeProvider* p_themeProvider);
0105 
0106     /**
0107       * Deletes the Game instance.
0108       */
0109     ~GameScene() override;
0110 
0111     /**
0112       * @return the Game instance
0113       */
0114     Game* getGame() const;
0115 
0116     /**
0117      * Initializes class
0118      */
0119     void init();
0120 
0121     /**
0122      * Initializes all items with graphics from the theme
0123      */
0124     void initItemsWithGraphicsFromTheme();
0125 
0126     /**
0127      * Cleans class
0128      */
0129     void cleanUp();
0130 
0131     /**
0132      * Initializes all items with graphics from the theme
0133      */
0134     void cleanUpItemsWithGraphicsFromTheme();
0135 
0136     /**
0137      * Shows the labels with the points.
0138      */
0139     void showScore();
0140 
0141     /**
0142      * Updates the sprites after a resize.
0143      */
0144     void resizeSprites(int delayForBackground = 0);
0145 
0146     /**
0147      * Returns the renderer for the requested Element Type
0148      * @param type the type
0149      * @param player the player
0150      */
0151     KGameRenderer* renderer(Granatier::Element::Type type, Player* player = nullptr);
0152 
0153     /** setup the KGameRenderer for the selected theme and if necessary the default theme */
0154     void setupThemeRenderer();
0155 
0156 private Q_SLOTS:
0157 
0158     /**
0159     * Updates the elements to be drawn when the Game starts.
0160     */
0161     void start();
0162 
0163     void themeChanged();
0164 
0165     /**
0166      * Updates the background to fit into the QGraphicsView after a resize.
0167      */
0168     void resizeBackground();
0169 
0170     /**
0171     * Updates the elements to be drawn considering the Game state (paused or running).
0172     * @param p_pause if true the Game has been paused, if false the Game has been resumed
0173     * @param p_fromUser if true the Game has been paused due to an action from the user
0174     */
0175     void setPaused(const bool p_pause, const bool p_fromUser);
0176 
0177     /**
0178     * Remove the Block from the GameScene.
0179     */
0180     void removeBlockItem(BlockItem* blockItem);
0181 
0182     /**
0183     * Remove the Bonus from the GameScene.
0184     */
0185     void removeBonusItem(BonusItem* bonusItem);
0186 
0187     /**
0188     * Upadates the Game information labels.
0189     * @param p_info the type of the information to be updated
0190     */
0191     void updateInfo(const Granatier::Info::Type p_info);
0192 
0193     /**
0194     * Creates a BombItem
0195     */
0196     void createBombItem(Bomb* bomb);
0197 
0198     /**
0199     * Removes a BombItem
0200     */
0201     void removeBombItem(BombItem* bombItem);
0202 
0203     /**
0204     * Creates the explosion items
0205     */
0206     void bombDetonated(Bomb* bomb);
0207 
0208     /**
0209     * Updates the BombExplosionItems
0210     */
0211     void updateBombExplosionItemAnimation(BombItem* bombItem, int nFrame);
0212 
0213 Q_SIGNALS:
0214 
0215     /**
0216      * Signals that the graphics need an update
0217      */
0218     void resizeGraphics(qreal svgScaleFactor);
0219 };
0220 
0221 #endif
0222