Changeset View
Changeset View
Standalone View
Standalone View
ball.cpp
Show All 10 Lines | |||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Library General Public License for more details. | 12 | * Library General Public License for more details. | ||
13 | * | 13 | * | ||
14 | * You should have received a copy of the GNU Library General Public | 14 | * You should have received a copy of the GNU Library General Public | ||
15 | * License along with this program; if not, write to the Free | 15 | * License along with this program; if not, write to the Free | ||
16 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 16 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | | ||||
20 | #include "ball.h" | | |||
21 | | ||||
22 | #include <cmath> | | |||
23 | | ||||
24 | #include <KRandom> | 19 | #include <KRandom> | ||
25 | 20 | | |||
21 | #include "ball.h" | ||||
26 | #include "board.h" | 22 | #include "board.h" | ||
27 | #include "renderer.h" | 23 | #include "renderer.h" | ||
28 | #include "debug.h" | 24 | #include "debug.h" | ||
29 | 25 | | |||
26 | #include <cmath> | ||||
27 | | ||||
30 | const int KBounceBall::BALL_ANIM_DELAY = 50; | 28 | const int KBounceBall::BALL_ANIM_DELAY = 50; | ||
31 | const qreal KBounceBall::BALL_RELATIVE_SIZE = 0.8; | 29 | const qreal KBounceBall::BALL_RELATIVE_SIZE = 0.8; | ||
32 | 30 | | |||
33 | KBounceBall::KBounceBall( KBounceRenderer* renderer, KBounceBoard* board ) | 31 | KBounceBall::KBounceBall( KBounceRenderer* renderer, KBounceBoard* board ) | ||
34 | : KGameRenderedItem(renderer,QLatin1String(""), board ), m_renderer( renderer ), m_board( board ), | 32 | : KGameRenderedItem(renderer,QLatin1String(""), board ), | ||
35 | m_soundDelay( 0 ), m_size( QSize( 16, 16 ) ), m_framesNum( 0 ), m_xPos( 0 ), m_yPos( 0 ) | 33 | m_renderer( renderer ), | ||
34 | m_board( board ), | ||||
35 | m_soundDelay( 0 ), | ||||
36 | m_size( QSize( 16, 16 ) ), | ||||
37 | m_framesNum( 0 ), | ||||
38 | m_xPos( 0 ), | ||||
39 | m_yPos( 0 ) | ||||
36 | { | 40 | { | ||
37 | setSpriteKey(QStringLiteral("ball")); | 41 | setSpriteKey(QStringLiteral("ball")); | ||
38 | resetPixmaps(); | 42 | resetPixmaps(); | ||
39 | m_nextBoundingRect.setSize( QSizeF( BALL_RELATIVE_SIZE, BALL_RELATIVE_SIZE ) ); | 43 | m_nextBoundingRect.setSize( QSizeF( BALL_RELATIVE_SIZE, BALL_RELATIVE_SIZE ) ); | ||
40 | } | 44 | } | ||
41 | 45 | | |||
42 | | ||||
43 | KBounceBall::~KBounceBall() | 46 | KBounceBall::~KBounceBall() | ||
44 | { | 47 | { | ||
45 | } | 48 | } | ||
46 | 49 | | |||
50 | /** | ||||
51 | * Performs move calculations | ||||
52 | * This method is called once per frame | ||||
53 | */ | ||||
47 | void KBounceBall::goForward() | 54 | void KBounceBall::goForward() | ||
48 | { | 55 | { | ||
49 | if ( m_reflectX ) | 56 | if ( m_reflectX ) | ||
50 | { | 57 | { | ||
51 | m_velocity.x = -m_velocity.x; | 58 | m_velocity.x = -m_velocity.x; | ||
52 | m_reflectX = false; | 59 | m_reflectX = false; | ||
53 | } | 60 | } | ||
54 | 61 | | |||
55 | if ( m_reflectY ) | 62 | if ( m_reflectY ) | ||
56 | { | 63 | { | ||
57 | qCDebug(KBOUNCE_LOG) << "Reflecting ball Y"; | 64 | qCDebug(KBOUNCE_LOG) << "Reflecting ball Y"; | ||
58 | m_velocity.y = -m_velocity.y; | 65 | m_velocity.y = -m_velocity.y; | ||
59 | m_reflectY = false; | 66 | m_reflectY = false; | ||
60 | } | 67 | } | ||
61 | 68 | | |||
62 | m_xPos += m_velocity.x; | 69 | m_xPos += m_velocity.x; | ||
63 | m_yPos += m_velocity.y; | 70 | m_yPos += m_velocity.y; | ||
64 | 71 | | |||
65 | m_nextBoundingRect.moveTo( m_xPos + m_velocity.x, m_yPos + m_velocity.y ); | 72 | m_nextBoundingRect.moveTo( m_xPos + m_velocity.x, m_yPos + m_velocity.y ); | ||
66 | } | 73 | } | ||
67 | 74 | | |||
75 | /** | ||||
76 | * Changes ball's state when collisions have been detected | ||||
77 | * Called once per frame before advance() | ||||
78 | * @param collision list of objects collided | ||||
79 | */ | ||||
68 | void KBounceBall::collide( const KBounceCollision& collision ) | 80 | void KBounceBall::collide( const KBounceCollision& collision ) | ||
69 | { | 81 | { | ||
70 | foreach ( const KBounceHit &hit, collision ) | 82 | foreach ( const KBounceHit &hit, collision ) | ||
71 | { | 83 | { | ||
72 | if ( hit.type == TILE || hit.type == WALL ) | 84 | if ( hit.type == TILE || hit.type == WALL ) | ||
73 | { | 85 | { | ||
74 | if ( hit.normal.x > 0 && m_velocity.x < 0 ) | 86 | if ( hit.normal.x > 0 && m_velocity.x < 0 ) | ||
75 | m_reflectX = true; | 87 | m_reflectX = true; | ||
76 | if ( hit.normal.x < 0 && m_velocity.x > 0 ) | 88 | if ( hit.normal.x < 0 && m_velocity.x > 0 ) | ||
77 | m_reflectX = true; | 89 | m_reflectX = true; | ||
78 | if ( hit.normal.y > 0 && m_velocity.y < 0 ) | 90 | if ( hit.normal.y > 0 && m_velocity.y < 0 ) | ||
79 | m_reflectY = true; | 91 | m_reflectY = true; | ||
80 | if ( hit.normal.y < 0 && m_velocity.y > 0 ) | 92 | if ( hit.normal.y < 0 && m_velocity.y > 0 ) | ||
81 | m_reflectY = true; | 93 | m_reflectY = true; | ||
82 | } | 94 | } | ||
83 | } | 95 | } | ||
84 | } | 96 | } | ||
85 | 97 | | |||
98 | /** | ||||
99 | * Updates ball position and pixmap. | ||||
100 | * This method is called once per frame. | ||||
101 | */ | ||||
86 | void KBounceBall::update() | 102 | void KBounceBall::update() | ||
87 | { | 103 | { | ||
88 | setFrame( frame()+1 ); | 104 | setFrame( frame()+1 ); | ||
89 | setPos( m_board->mapPosition( QPointF( m_xPos, m_yPos ) ) ); | 105 | setPos( m_board->mapPosition( QPointF( m_xPos, m_yPos ) ) ); | ||
90 | } | 106 | } | ||
91 | 107 | | |||
108 | /** | ||||
109 | * Sets width and height of ball. | ||||
110 | * @param tileSize new tile size | ||||
111 | */ | ||||
92 | void KBounceBall::resize( const QSize& tileSize ) | 112 | void KBounceBall::resize( const QSize& tileSize ) | ||
93 | { | 113 | { | ||
94 | qCDebug(KBOUNCE_LOG) << "New size:" << tileSize; | 114 | qCDebug(KBOUNCE_LOG) << "New size:" << tileSize; | ||
95 | 115 | | |||
96 | m_size.setWidth( static_cast<int>( BALL_RELATIVE_SIZE * tileSize.width() ) ); | 116 | m_size.setWidth( static_cast<int>( BALL_RELATIVE_SIZE * tileSize.width() ) ); | ||
97 | m_size.setHeight( static_cast<int> ( BALL_RELATIVE_SIZE * tileSize.height() ) ); | 117 | m_size.setHeight( static_cast<int> ( BALL_RELATIVE_SIZE * tileSize.height() ) ); | ||
98 | setRenderSize(m_size); | 118 | setRenderSize(m_size); | ||
99 | setPos( m_board->mapPosition( QPointF( m_xPos, m_yPos ) ) ); | 119 | setPos( m_board->mapPosition( QPointF( m_xPos, m_yPos ) ) ); | ||
100 | } | 120 | } | ||
101 | 121 | | |||
122 | /** | ||||
123 | * Rechecks the number of frames of ball animation and sets new pixmaps. | ||||
124 | * This method is useful when changing game theme. | ||||
125 | */ | ||||
102 | void KBounceBall::resetPixmaps() | 126 | void KBounceBall::resetPixmaps() | ||
103 | { | 127 | { | ||
104 | m_framesNum = frameCount(); | 128 | m_framesNum = frameCount(); | ||
105 | setFrame( 1 ); | 129 | setFrame( 1 ); | ||
106 | } | 130 | } | ||
107 | 131 | | |||
132 | /** | ||||
133 | * Sets a random ball's frame | ||||
134 | */ | ||||
108 | void KBounceBall::setRandomFrame() | 135 | void KBounceBall::setRandomFrame() | ||
109 | { | 136 | { | ||
110 | int frame = 1; | 137 | int frame = 1; | ||
111 | if ( m_framesNum > 1 ) | 138 | if ( m_framesNum > 1 ) | ||
112 | { | | |||
113 | frame = KRandom::random() % m_framesNum; | 139 | frame = KRandom::random() % m_framesNum; | ||
114 | } | 140 | | ||
schwarzer: This is valid C++ and some style guides allow leaving out the braces ... however, during my day… | |||||
115 | setFrame( frame ); | 141 | setFrame( frame ); | ||
116 | } | 142 | } | ||
117 | 143 | | |||
144 | /* | ||||
145 | * Returns ball's bounding rect in board coordinate system | ||||
146 | * @see relativePos() | ||||
147 | */ | ||||
118 | QRectF KBounceBall::ballBoundingRect() const | 148 | QRectF KBounceBall::ballBoundingRect() const | ||
119 | { | 149 | { | ||
120 | return QRectF( m_xPos, m_yPos, BALL_RELATIVE_SIZE, BALL_RELATIVE_SIZE ); | 150 | return QRectF( m_xPos, m_yPos, BALL_RELATIVE_SIZE, BALL_RELATIVE_SIZE ); | ||
121 | } | 151 | } | ||
122 | 152 | | |||
153 | /* | ||||
154 | * Returns ball's bounding rect expected in next frame | ||||
155 | * used by colision test | ||||
156 | */ | ||||
123 | QRectF KBounceBall::nextBoundingRect() const | 157 | QRectF KBounceBall::nextBoundingRect() const | ||
124 | { | 158 | { | ||
125 | return m_nextBoundingRect; | 159 | return m_nextBoundingRect; | ||
126 | } | 160 | } | ||
127 | 161 | | |||
162 | /** | ||||
163 | * Returns ball's position in board coordinate system. | ||||
164 | * Relative board's coordinates are indepentent of actual GameWidget size. | ||||
165 | */ | ||||
128 | QPointF KBounceBall::relativePos() | 166 | QPointF KBounceBall::relativePos() | ||
129 | { | 167 | { | ||
130 | return QPointF( m_xPos, m_yPos ); | 168 | return QPointF( m_xPos, m_yPos ); | ||
131 | } | 169 | } | ||
132 | 170 | | |||
171 | /** | ||||
172 | * Sets ball's position in board coordinate system. | ||||
173 | * @param x position on x axis | ||||
174 | * @param y position on y axis | ||||
175 | * @see relativePos() | ||||
176 | */ | ||||
133 | void KBounceBall::setRelativePos( qreal x, qreal y ) | 177 | void KBounceBall::setRelativePos( qreal x, qreal y ) | ||
134 | { | 178 | { | ||
135 | m_xPos = x; | 179 | m_xPos = x; | ||
136 | m_yPos = y; | 180 | m_yPos = y; | ||
137 | setPos( m_board->mapPosition( QPointF( m_xPos, m_yPos ) ) ); | 181 | setPos( m_board->mapPosition( QPointF( m_xPos, m_yPos ) ) ); | ||
138 | } | 182 | } | ||
139 | 183 | | |||
184 | /** | ||||
185 | * Sets ball's position in board coordinate system | ||||
186 | * @param vx velocity on x axis | ||||
187 | * @param vy velocity on y axis | ||||
yurchor: Same here. | |||||
188 | */ | ||||
140 | void KBounceBall::setVelocity( qreal vX, qreal vY ) | 189 | void KBounceBall::setVelocity( qreal vX, qreal vY ) | ||
141 | { | 190 | { | ||
142 | m_velocity.x = vX; | 191 | m_velocity.x = vX; | ||
143 | m_velocity.y = vY; | 192 | m_velocity.y = vY; | ||
144 | } | 193 | } | ||
145 | 194 | | |||
195 | /** | ||||
196 | * Returns ball's velocity in board coordinate system | ||||
197 | */ | ||||
146 | KBounceVector KBounceBall::velocity() const | 198 | KBounceVector KBounceBall::velocity() const | ||
147 | { | 199 | { | ||
148 | return KBounceVector( m_velocity.x, m_velocity.y ); | 200 | return KBounceVector( m_velocity.x, m_velocity.y ); | ||
149 | } | 201 | } | ||
150 | 202 | |
This is valid C++ and some style guides allow leaving out the braces ... however, during my day job I found cases, where the code was altered later and the developer added one line and forgot about adding braces. So I would always use braces on an if statement. ... But this is just my opinion.