File indexing completed on 2024-04-28 04:02:03

0001 /*
0002     This file is part of the KDE project "KBounce"
0003 
0004     SPDX-FileCopyrightText: 2007 Tomasz Boczkowski <tboczkowski@onet.pl>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef WALL_H
0010 #define WALL_H
0011 
0012 #include <KGameRenderedItem>
0013 
0014 #include <QObject>
0015 #include <KGameSound>
0016 #include "gameobject.h"
0017 
0018 class KBounceRenderer;
0019 class KBounceBoard;
0020 
0021 /**
0022  * KGameRenderedItem representing a wall "under-construction"
0023  *
0024  * There are four walls in a board, each of which extends in
0025  * other direction: up, right, down or left
0026  */ 
0027 class KBounceWall : public QObject, public KGameRenderedItem
0028 {
0029     Q_OBJECT
0030 
0031     public:
0032         enum Direction { Up = 0, Down, Left, Right };
0033 
0034         /**
0035          * Constructor
0036          * @param dir is set in construction and does not change in gameplay
0037          * that means there will be no two walls extending in the same direction
0038          * in the same time
0039          */
0040         KBounceWall( Direction dir, KBounceRenderer* renderer, KBounceBoard* board );
0041         ~KBounceWall() override;
0042 
0043         /**
0044          * Changes object's state when collisions have been detected
0045          * Called once per frame before advance() and update()
0046          */
0047         void collide(const KBounceCollision &collision);
0048         /**
0049          * Performs various movement and state calculations non-related
0050          * to collision responses. Also updates m_boundingRect and 
0051          * m_nextBoundingRect. This method is called once per frame
0052          * after collide() and before update()
0053          */
0054         void goForward();
0055         /**
0056          * Updates object's pixmap and position on screen
0057          * Called once per frame after update()
0058          */
0059         void update();
0060 
0061         /**
0062          * Starts building wall beginning from tile specified by x and y
0063          * The direction has been specified in constructor
0064          */
0065         void build( int x, int y );
0066         /**
0067          * Returns the bounding rect that is expected for wall to
0068          * have in next frame. Collision in KBounceBoard are based on
0069          * the result of this method
0070          */
0071         QRectF nextBoundingRect() const;
0072 
0073         /**
0074          * Changes on-screen dimensions of the wall. 
0075          * Calculations are based on tile size, that is the on-screen
0076          * size of board's tile
0077          */
0078         void resize( const QSize& tileSize );
0079 
0080         /**
0081          * Set the wall velocity for wall filling speed.
0082          */
0083         void setWallVelocity(qreal velocity);
0084 
0085         /**
0086          * Load all sprites for top, down, left and right walls as well as for
0087          * the vertical and horizontal semi transparent bars drawn before a wall is
0088          * built.
0089          */
0090         void static loadSprites();
0091 
0092     Q_SIGNALS:
0093         void finished( int left, int top, int right, int bottom );
0094         void died();
0095 
0096     private:
0097         /**
0098          * Returns true if rect2 intersects the edge at the end of wall
0099          * e.g for wall extending in direction Up this will be the upper
0100          * edge.
0101          */
0102         bool safeEdgeHit( const QRectF& rect2 ) const;
0103         /**
0104          * Helper function replacing emiting long finished signal
0105          * It also hides the wall and plays corresponding sound
0106          * If &param shorten is true the wall will be one unit in
0107          * direction &param dir shorter than normal 
0108          */
0109         void finish( bool shorten = false, Direction dir = Up);
0110 
0111         KBounceBoard *m_board;
0112         Direction m_dir;
0113 
0114         KGameSound m_soundWallstart;
0115         KGameSound m_soundReflect;
0116 
0117         QRectF m_boundingRect;
0118         QRectF m_nextBoundingRect;
0119         qreal m_wallVelocity;
0120 
0121         typedef struct {
0122             QPixmap wallEndLeft;
0123             QPixmap wallEndUp;
0124             QPixmap wallEndRight;
0125             QPixmap wallEndDown;
0126             QPixmap wallH;
0127             QPixmap wallV;
0128 
0129         } Sprites;
0130         static Sprites *s_sprites;
0131 
0132         static QSize s_tileSize;
0133         static KBounceRenderer *m_renderer;
0134 };
0135 
0136 #endif
0137 
0138