File indexing completed on 2024-05-12 05:46:45

0001 /*
0002  * Copyright (C) 2000-2005 Stefan Schimanski <1Stein@gmx.de>
0003  * Copyright (C) 2007 Tomasz Boczkowski <tboczkowski@onet.pl>
0004  *
0005  * This file is part of the KDE project "KBounce"
0006  *
0007  * This program is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public
0018  * License along with this program; if not, write to the Free
0019  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA  02110-1301, USA.
0021  */
0022 
0023 #ifndef BALL_H
0024 #define BALL_H
0025 
0026 #include <KGameRenderedItem>
0027 
0028 #include "gameobject.h"
0029 
0030 class KBounceRenderer;
0031 class KBounceBoard;
0032 
0033 /**
0034  * KGameRenderedItem representing a ball
0035  */
0036 class KBounceBall : public KGameRenderedItem
0037 {
0038     public:
0039         static const int BALL_ANIM_DELAY;
0040         static const qreal BALL_RELATIVE_SIZE;
0041 
0042         /**
0043          * Constructor
0044          */
0045         KBounceBall( KBounceRenderer* renderer, KBounceBoard* board );
0046         /**
0047          * Destructor
0048          */
0049         ~KBounceBall() override;
0050 
0051         /**
0052          * Changes ball's state when collisions have been detected
0053          * Called once per frame before advance()
0054          */
0055         void collide( const KBounceCollision& collision );
0056         /**
0057          * Performs move calculations
0058          * This method is called once per frame
0059          */
0060         void goForward();
0061         /**
0062          * Updates ball position and pixmap.
0063          * This method is called once per frame.
0064          */
0065         void update();
0066 
0067         /*
0068          * Returns ball's bounding rect in board coordinate system
0069          * @see relativePos()
0070          */
0071         QRectF ballBoundingRect() const;
0072         /*
0073          * Returns ball's bounding rect expected in next frame
0074          * used by colision test
0075          */
0076         QRectF nextBoundingRect() const;
0077         /**
0078          * Returns ball's position in board coordinate system.
0079          * Relative board's coordinates are indepentent of actual GameWidget size.
0080          */
0081         QPointF relativePos();
0082         /**
0083          * Sets ball's position in board coordinate system.
0084          * @see relativePos()
0085          */
0086         void setRelativePos( qreal x, qreal y );
0087         /**
0088          * Sets ball's position in board coordinate system
0089          */
0090         void setVelocity( qreal vX, qreal vY );
0091         /**
0092          * Returns ball's velocity in board coordinate system
0093          */
0094         KBounceVector velocity() const;
0095 
0096 
0097         /**
0098          * Sets width and height of ball.
0099          */
0100         void resize( const QSize& tileSize );
0101         /**
0102          * Rechecks the number of frames of ball animation and sets new pixmaps.
0103          * This method is useful when changing game theme.
0104          */
0105         void resetPixmaps();
0106         /**
0107          * Sets a random ball's frame
0108          */
0109         void setRandomFrame();
0110 
0111     protected:
0112         KBounceRenderer* m_renderer;
0113         KBounceBoard* m_board;
0114         /**
0115          * Time after emiting previous sound. If the value is too small,
0116          * ball will not emit hit sound.
0117          */
0118         int m_soundDelay;
0119         /**
0120          * Size of a ball in GameWidget depentant coordinate system
0121          */
0122         QSize m_size;
0123         /**
0124          * Number of frames of ball's animation.
0125          */
0126         int m_framesNum;
0127         qreal m_xPos;
0128         qreal m_yPos;
0129         KBounceVector m_velocity;
0130         bool m_reflectX;
0131         bool m_reflectY;
0132         QRectF m_nextBoundingRect;
0133 };
0134 
0135 #endif