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

0001 /*
0002     This file is part of the KDE project "KBounce"
0003 
0004     SPDX-FileCopyrightText: 2000-2005 Stefan Schimanski <1Stein@gmx.de>
0005     SPDX-FileCopyrightText: 2007 Tomasz Boczkowski <tboczkowski@onet.pl>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef BALL_H
0011 #define BALL_H
0012 
0013 #include <KGameRenderedItem>
0014 
0015 #include "gameobject.h"
0016 
0017 class KBounceRenderer;
0018 class KBounceBoard;
0019 
0020 /**
0021  * KGameRenderedItem representing a ball
0022  */
0023 class KBounceBall : public KGameRenderedItem
0024 {
0025     public:
0026         static const int BALL_ANIM_DELAY;
0027         static const qreal BALL_RELATIVE_SIZE;
0028 
0029         /**
0030          * Constructor
0031          */
0032         KBounceBall( KBounceRenderer* renderer, KBounceBoard* board );
0033         /**
0034          * Destructor
0035          */
0036         ~KBounceBall() override;
0037 
0038         /**
0039          * Changes ball's state when collisions have been detected
0040          * Called once per frame before advance()
0041          */
0042         void collide( const KBounceCollision& collision );
0043         /**
0044          * Performs move calculations
0045          * This method is called once per frame
0046          */
0047         void goForward();
0048         /**
0049          * Updates ball position and pixmap.
0050          * This method is called once per frame.
0051          */
0052         void update();
0053 
0054         /*
0055          * Returns ball's bounding rect in board coordinate system
0056          * @see relativePos()
0057          */
0058         QRectF ballBoundingRect() const;
0059         /*
0060          * Returns ball's bounding rect expected in next frame
0061          * used by colision test
0062          */
0063         QRectF nextBoundingRect() const;
0064         /**
0065          * Returns ball's position in board coordinate system.
0066          * Relative board's coordinates are indepentent of actual GameWidget size.
0067          */
0068         QPointF relativePos();
0069         /**
0070          * Sets ball's position in board coordinate system.
0071          * @see relativePos()
0072          */
0073         void setRelativePos( qreal x, qreal y );
0074         /**
0075          * Sets ball's position in board coordinate system
0076          */
0077         void setVelocity( qreal vX, qreal vY );
0078         /**
0079          * Returns ball's velocity in board coordinate system
0080          */
0081         KBounceVector velocity() const;
0082 
0083 
0084         /**
0085          * Sets width and height of ball.
0086          */
0087         void resize( const QSize& tileSize );
0088         /**
0089          * Rechecks the number of frames of ball animation and sets new pixmaps.
0090          * This method is useful when changing game theme.
0091          */
0092         void resetPixmaps();
0093         /**
0094          * Sets a random ball's frame
0095          */
0096         void setRandomFrame();
0097 
0098     protected:
0099         KBounceRenderer* m_renderer;
0100         KBounceBoard* m_board;
0101         /**
0102          * Time after emiting previous sound. If the value is too small,
0103          * ball will not emit hit sound.
0104          */
0105         int m_soundDelay;
0106         /**
0107          * Size of a ball in GameWidget depentant coordinate system
0108          */
0109         QSize m_size;
0110         /**
0111          * Number of frames of ball's animation.
0112          */
0113         int m_framesNum;
0114         qreal m_xPos;
0115         qreal m_yPos;
0116         KBounceVector m_velocity;
0117         bool m_reflectX;
0118         bool m_reflectY;
0119         QRectF m_nextBoundingRect;
0120 };
0121 
0122 #endif