author | sandman <sandman> | 2002-10-03 13:01:51 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-10-03 13:01:51 (UTC) |
commit | ee864e9e055fd23778a8e5dd3c35e3a29cded368 (patch) (unidiff) | |
tree | 4362efd44484c1f9f56104b46fab06e1e51af125 | |
parent | f254ef08b354327d70d80690eff84dda15e592fc (diff) | |
download | opie-ee864e9e055fd23778a8e5dd3c35e3a29cded368.zip opie-ee864e9e055fd23778a8e5dd3c35e3a29cded368.tar.gz opie-ee864e9e055fd23778a8e5dd3c35e3a29cded368.tar.bz2 |
TT fixed a bug in QCanvasPixmapArray in 2.3.4 and qasteroids relied on the
buggy behaviour
-rw-r--r-- | noncore/games/qasteroids/view.cpp | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/noncore/games/qasteroids/view.cpp b/noncore/games/qasteroids/view.cpp index ef08343..448a54a 100644 --- a/noncore/games/qasteroids/view.cpp +++ b/noncore/games/qasteroids/view.cpp | |||
@@ -1,884 +1,884 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************//* | 19 | **********************************************************************//* |
20 | * KAsteroids - Copyright (c) Martin R. Jones 1997 | 20 | * KAsteroids - Copyright (c) Martin R. Jones 1997 |
21 | * | 21 | * |
22 | * Part of the KDE project | 22 | * Part of the KDE project |
23 | */ | 23 | */ |
24 | 24 | ||
25 | #include "view.h" | 25 | #include "view.h" |
26 | 26 | ||
27 | #include <qpe/resource.h> | 27 | #include <qpe/resource.h> |
28 | 28 | ||
29 | #include <qapplication.h> | 29 | #include <qapplication.h> |
30 | #include <qkeycode.h> | 30 | #include <qkeycode.h> |
31 | #include <qaccel.h> | 31 | #include <qaccel.h> |
32 | 32 | ||
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <math.h> | 34 | #include <math.h> |
35 | 35 | ||
36 | #define IMG_BACKGROUND "qasteroids/bg.png" | 36 | #define IMG_BACKGROUND "qasteroids/bg.png" |
37 | 37 | ||
38 | #define REFRESH_DELAY 33 | 38 | #define REFRESH_DELAY 33 |
39 | #define SHIP_SPEED 0.3 | 39 | #define SHIP_SPEED 0.3 |
40 | #define MISSILE_SPEED 10.0 | 40 | #define MISSILE_SPEED 10.0 |
41 | #define SHIP_STEPS 64 | 41 | #define SHIP_STEPS 64 |
42 | #define ROTATE_RATE 2 | 42 | #define ROTATE_RATE 2 |
43 | #define SHIELD_ON_COST 1 | 43 | #define SHIELD_ON_COST 1 |
44 | #define SHIELD_HIT_COST 30 | 44 | #define SHIELD_HIT_COST 30 |
45 | #define BRAKE_ON_COST 4 | 45 | #define BRAKE_ON_COST 4 |
46 | 46 | ||
47 | #define MAX_ROCK_SPEED 2.5 | 47 | #define MAX_ROCK_SPEED 2.5 |
48 | #define MAX_POWERUP_SPEED 1.5 | 48 | #define MAX_POWERUP_SPEED 1.5 |
49 | #define MAX_SHIP_SPEED 8 | 49 | #define MAX_SHIP_SPEED 8 |
50 | #define MAX_BRAKES 5 | 50 | #define MAX_BRAKES 5 |
51 | #define MAX_SHIELDS 5 | 51 | #define MAX_SHIELDS 5 |
52 | #define MAX_FIREPOWER 5 | 52 | #define MAX_FIREPOWER 5 |
53 | 53 | ||
54 | #define TEXT_SPEED 4 | 54 | #define TEXT_SPEED 4 |
55 | 55 | ||
56 | #define PI_X_2 6.283185307 | 56 | #define PI_X_2 6.283185307 |
57 | #ifndef M_PI | 57 | #ifndef M_PI |
58 | #define M_PI 3.141592654 | 58 | #define M_PI 3.141592654 |
59 | #endif | 59 | #endif |
60 | 60 | ||
61 | struct | 61 | struct |
62 | { | 62 | { |
63 | int id; | 63 | int id; |
64 | const char *path; | 64 | const char *path; |
65 | int frames; | 65 | int frames; |
66 | } | 66 | } |
67 | kas_animations [] = | 67 | kas_animations [] = |
68 | { | 68 | { |
69 | // { ID_ROCK_LARGE, "rock1/rock1\%1.png", 32 }, | 69 | // { ID_ROCK_LARGE, "rock1/rock1\%1.png", 32 }, |
70 | { ID_ROCK_MEDIUM, "rock2/rock2\%1.png", 32 }, | 70 | { ID_ROCK_MEDIUM, "rock2/rock2\%1.png", 32 }, |
71 | { ID_ROCK_SMALL, "rock3/rock3\%1.png", 32 }, | 71 | { ID_ROCK_SMALL, "rock3/rock3\%1.png", 32 }, |
72 | { ID_SHIP, "ship/ship\%1.png", 32 }, | 72 | { ID_SHIP, "ship/ship\%1.png", 32 }, |
73 | { ID_MISSILE, "missile/missile.png", 1 }, | 73 | { ID_MISSILE, "missile/missile.png", 0 }, |
74 | { ID_BIT, "bits/bits\%1.png", 16 }, | 74 | { ID_BIT, "bits/bits\%1.png", 16 }, |
75 | { ID_EXHAUST, "exhaust/exhaust.png", 1 }, | 75 | { ID_EXHAUST, "exhaust/exhaust.png", 0 }, |
76 | { ID_ENERGY_POWERUP, "powerups/energy.png", 1 }, | 76 | { ID_ENERGY_POWERUP, "powerups/energy.png", 0 }, |
77 | // { ID_TELEPORT_POWERUP, "powerups/teleport%1.png", 12 }, | 77 | // { ID_TELEPORT_POWERUP, "powerups/teleport%1.png", 12 }, |
78 | { ID_BRAKE_POWERUP, "powerups/brake.png", 1 }, | 78 | { ID_BRAKE_POWERUP, "powerups/brake.png", 0 }, |
79 | { ID_SHIELD_POWERUP, "powerups/shield.png", 1 }, | 79 | { ID_SHIELD_POWERUP, "powerups/shield.png", 0 }, |
80 | { ID_SHOOT_POWERUP, "powerups/shoot.png", 1 }, | 80 | { ID_SHOOT_POWERUP, "powerups/shoot.png", 0 }, |
81 | { ID_SHIELD, "shield/shield\%1.png", 6 }, | 81 | { ID_SHIELD, "shield/shield\%1.png", 6 }, |
82 | { 0, 0, 0 } | 82 | { 0, 0, 0 } |
83 | }; | 83 | }; |
84 | 84 | ||
85 | 85 | ||
86 | 86 | ||
87 | KAsteroidsView::KAsteroidsView( QWidget *parent, const char *name ) | 87 | KAsteroidsView::KAsteroidsView( QWidget *parent, const char *name ) |
88 | : QWidget( parent, name ), | 88 | : QWidget( parent, name ), |
89 | field(200, 200), | 89 | field(200, 200), |
90 | view(&field,this) | 90 | view(&field,this) |
91 | { | 91 | { |
92 | view.setVScrollBarMode( QScrollView::AlwaysOff ); | 92 | view.setVScrollBarMode( QScrollView::AlwaysOff ); |
93 | view.setHScrollBarMode( QScrollView::AlwaysOff ); | 93 | view.setHScrollBarMode( QScrollView::AlwaysOff ); |
94 | rocks.setAutoDelete( TRUE ); | 94 | rocks.setAutoDelete( TRUE ); |
95 | missiles.setAutoDelete( TRUE ); | 95 | missiles.setAutoDelete( TRUE ); |
96 | bits.setAutoDelete( TRUE ); | 96 | bits.setAutoDelete( TRUE ); |
97 | powerups.setAutoDelete( TRUE ); | 97 | powerups.setAutoDelete( TRUE ); |
98 | exhaust.setAutoDelete( TRUE ); | 98 | exhaust.setAutoDelete( TRUE ); |
99 | 99 | ||
100 | QPixmap pm( Resource::loadPixmap(IMG_BACKGROUND) ); | 100 | QPixmap pm( Resource::loadPixmap(IMG_BACKGROUND) ); |
101 | field.setBackgroundPixmap( pm ); | 101 | field.setBackgroundPixmap( pm ); |
102 | 102 | ||
103 | textSprite = new QCanvasText( &field ); | 103 | textSprite = new QCanvasText( &field ); |
104 | QFont font( "helvetica", 14 ); | 104 | QFont font( "helvetica", 14 ); |
105 | textSprite->setFont( font ); | 105 | textSprite->setFont( font ); |
106 | 106 | ||
107 | shield = 0; | 107 | shield = 0; |
108 | shieldOn = FALSE; | 108 | shieldOn = FALSE; |
109 | refreshRate = REFRESH_DELAY; | 109 | refreshRate = REFRESH_DELAY; |
110 | 110 | ||
111 | readSprites(); | 111 | readSprites(); |
112 | 112 | ||
113 | shieldTimer = new QTimer( this ); | 113 | shieldTimer = new QTimer( this ); |
114 | connect( shieldTimer, SIGNAL(timeout()), this, SLOT(hideShield()) ); | 114 | connect( shieldTimer, SIGNAL(timeout()), this, SLOT(hideShield()) ); |
115 | mTimerId = -1; | 115 | mTimerId = -1; |
116 | 116 | ||
117 | shipPower = MAX_POWER_LEVEL; | 117 | shipPower = MAX_POWER_LEVEL; |
118 | vitalsChanged = TRUE; | 118 | vitalsChanged = TRUE; |
119 | can_destroy_powerups = FALSE; | 119 | can_destroy_powerups = FALSE; |
120 | 120 | ||
121 | mPaused = TRUE; | 121 | mPaused = TRUE; |
122 | } | 122 | } |
123 | 123 | ||
124 | // - - - | 124 | // - - - |
125 | 125 | ||
126 | KAsteroidsView::~KAsteroidsView() | 126 | KAsteroidsView::~KAsteroidsView() |
127 | { | 127 | { |
128 | } | 128 | } |
129 | 129 | ||
130 | // - - - | 130 | // - - - |
131 | 131 | ||
132 | void KAsteroidsView::reset() | 132 | void KAsteroidsView::reset() |
133 | { | 133 | { |
134 | rocks.clear(); | 134 | rocks.clear(); |
135 | missiles.clear(); | 135 | missiles.clear(); |
136 | bits.clear(); | 136 | bits.clear(); |
137 | powerups.clear(); | 137 | powerups.clear(); |
138 | exhaust.clear(); | 138 | exhaust.clear(); |
139 | 139 | ||
140 | shotsFired = 0; | 140 | shotsFired = 0; |
141 | shotsHit = 0; | 141 | shotsHit = 0; |
142 | 142 | ||
143 | rockSpeed = 1.0; | 143 | rockSpeed = 1.0; |
144 | powerupSpeed = 1.0; | 144 | powerupSpeed = 1.0; |
145 | mFrameNum = 0; | 145 | mFrameNum = 0; |
146 | mPaused = FALSE; | 146 | mPaused = FALSE; |
147 | 147 | ||
148 | ship->hide(); | 148 | ship->hide(); |
149 | shield->hide(); | 149 | shield->hide(); |
150 | /* | 150 | /* |
151 | if ( mTimerId >= 0 ) { | 151 | if ( mTimerId >= 0 ) { |
152 | killTimer( mTimerId ); | 152 | killTimer( mTimerId ); |
153 | mTimerId = -1; | 153 | mTimerId = -1; |
154 | } | 154 | } |
155 | */ | 155 | */ |
156 | } | 156 | } |
157 | 157 | ||
158 | // - -- | 158 | // - -- |
159 | 159 | ||
160 | void KAsteroidsView::newGame() | 160 | void KAsteroidsView::newGame() |
161 | { | 161 | { |
162 | if ( shieldOn ) | 162 | if ( shieldOn ) |
163 | { | 163 | { |
164 | shield->hide(); | 164 | shield->hide(); |
165 | shieldOn = FALSE; | 165 | shieldOn = FALSE; |
166 | } | 166 | } |
167 | reset(); | 167 | reset(); |
168 | if ( mTimerId < 0 ) | 168 | if ( mTimerId < 0 ) |
169 | mTimerId = startTimer( REFRESH_DELAY ); | 169 | mTimerId = startTimer( REFRESH_DELAY ); |
170 | emit updateVitals(); | 170 | emit updateVitals(); |
171 | } | 171 | } |
172 | 172 | ||
173 | // - - - | 173 | // - - - |
174 | 174 | ||
175 | void KAsteroidsView::endGame() | 175 | void KAsteroidsView::endGame() |
176 | { | 176 | { |
177 | } | 177 | } |
178 | 178 | ||
179 | void KAsteroidsView::pause( bool p ) | 179 | void KAsteroidsView::pause( bool p ) |
180 | { | 180 | { |
181 | if ( !mPaused && p ) { | 181 | if ( !mPaused && p ) { |
182 | if ( mTimerId >= 0 ) { | 182 | if ( mTimerId >= 0 ) { |
183 | killTimer( mTimerId ); | 183 | killTimer( mTimerId ); |
184 | mTimerId = -1; | 184 | mTimerId = -1; |
185 | } | 185 | } |
186 | } else if ( mPaused && !p ) | 186 | } else if ( mPaused && !p ) |
187 | mTimerId = startTimer( REFRESH_DELAY ); | 187 | mTimerId = startTimer( REFRESH_DELAY ); |
188 | mPaused = p; | 188 | mPaused = p; |
189 | } | 189 | } |
190 | 190 | ||
191 | // - - - | 191 | // - - - |
192 | 192 | ||
193 | void KAsteroidsView::newShip() | 193 | void KAsteroidsView::newShip() |
194 | { | 194 | { |
195 | ship->move( field.width()/2, field.height()/2, 0 ); | 195 | ship->move( field.width()/2, field.height()/2, 0 ); |
196 | shield->move( field.width()/2, field.height()/2, 0 ); | 196 | shield->move( field.width()/2, field.height()/2, 0 ); |
197 | ship->setVelocity( 0.0, 0.0 ); | 197 | ship->setVelocity( 0.0, 0.0 ); |
198 | shipDx = 0; | 198 | shipDx = 0; |
199 | shipDy = 0; | 199 | shipDy = 0; |
200 | shipAngle = 0; | 200 | shipAngle = 0; |
201 | rotateL = FALSE; | 201 | rotateL = FALSE; |
202 | rotateR = FALSE; | 202 | rotateR = FALSE; |
203 | thrustShip = FALSE; | 203 | thrustShip = FALSE; |
204 | shootShip = FALSE; | 204 | shootShip = FALSE; |
205 | brakeShip = FALSE; | 205 | brakeShip = FALSE; |
206 | teleportShip = FALSE; | 206 | teleportShip = FALSE; |
207 | shieldOn = TRUE; | 207 | shieldOn = TRUE; |
208 | shootDelay = 0; | 208 | shootDelay = 0; |
209 | shipPower = MAX_POWER_LEVEL; | 209 | shipPower = MAX_POWER_LEVEL; |
210 | rotateRate = ROTATE_RATE; | 210 | rotateRate = ROTATE_RATE; |
211 | rotateSlow = 0; | 211 | rotateSlow = 0; |
212 | 212 | ||
213 | mBrakeCount = 0; | 213 | mBrakeCount = 0; |
214 | mTeleportCount = 0; | 214 | mTeleportCount = 0; |
215 | mShootCount = 0; | 215 | mShootCount = 0; |
216 | 216 | ||
217 | ship->show(); | 217 | ship->show(); |
218 | shield->show(); | 218 | shield->show(); |
219 | mShieldCount = 1; // just in case the ship appears on a rock. | 219 | mShieldCount = 1; // just in case the ship appears on a rock. |
220 | shieldTimer->start( 1000, TRUE ); | 220 | shieldTimer->start( 1000, TRUE ); |
221 | } | 221 | } |
222 | 222 | ||
223 | void KAsteroidsView::setShield( bool s ) | 223 | void KAsteroidsView::setShield( bool s ) |
224 | { | 224 | { |
225 | if ( shieldTimer->isActive() && !s ) { | 225 | if ( shieldTimer->isActive() && !s ) { |
226 | shieldTimer->stop(); | 226 | shieldTimer->stop(); |
227 | hideShield(); | 227 | hideShield(); |
228 | } else { | 228 | } else { |
229 | shieldOn = s && mShieldCount; | 229 | shieldOn = s && mShieldCount; |
230 | } | 230 | } |
231 | } | 231 | } |
232 | 232 | ||
233 | void KAsteroidsView::brake( bool b ) | 233 | void KAsteroidsView::brake( bool b ) |
234 | { | 234 | { |
235 | if ( mBrakeCount ) | 235 | if ( mBrakeCount ) |
236 | { | 236 | { |
237 | if ( brakeShip && !b ) | 237 | if ( brakeShip && !b ) |
238 | { | 238 | { |
239 | rotateL = FALSE; | 239 | rotateL = FALSE; |
240 | rotateR = FALSE; | 240 | rotateR = FALSE; |
241 | thrustShip = FALSE; | 241 | thrustShip = FALSE; |
242 | rotateRate = ROTATE_RATE; | 242 | rotateRate = ROTATE_RATE; |
243 | } | 243 | } |
244 | 244 | ||
245 | brakeShip = b; | 245 | brakeShip = b; |
246 | } | 246 | } |
247 | } | 247 | } |
248 | 248 | ||
249 | // - - - | 249 | // - - - |
250 | 250 | ||
251 | void KAsteroidsView::readSprites() | 251 | void KAsteroidsView::readSprites() |
252 | { | 252 | { |
253 | QString sprites_prefix = Resource::findPixmap( IMG_BACKGROUND ); | 253 | QString sprites_prefix = Resource::findPixmap( IMG_BACKGROUND ); |
254 | int sep = sprites_prefix.findRev( "/" ); | 254 | int sep = sprites_prefix.findRev( "/" ); |
255 | 255 | ||
256 | sprites_prefix.truncate( sep ); | 256 | sprites_prefix.truncate( sep ); |
257 | 257 | ||
258 | int i = 0; | 258 | int i = 0; |
259 | while ( kas_animations[i].id ) | 259 | while ( kas_animations[i].id ) |
260 | { | 260 | { |
261 | animation.insert( kas_animations[i].id, | 261 | animation.insert( kas_animations[i].id, |
262 | new QCanvasPixmapArray( sprites_prefix + "/" + kas_animations[i].path, | 262 | new QCanvasPixmapArray( sprites_prefix + "/" + kas_animations[i].path, |
263 | kas_animations[i].frames ) ); | 263 | kas_animations[i].frames ) ); |
264 | i++; | 264 | i++; |
265 | } | 265 | } |
266 | 266 | ||
267 | ship = new QCanvasSprite( animation[ID_SHIP], &field ); | 267 | ship = new QCanvasSprite( animation[ID_SHIP], &field ); |
268 | ship->hide(); | 268 | ship->hide(); |
269 | 269 | ||
270 | shield = new KShield( animation[ID_SHIELD], &field ); | 270 | shield = new KShield( animation[ID_SHIELD], &field ); |
271 | shield->hide(); | 271 | shield->hide(); |
272 | } | 272 | } |
273 | 273 | ||
274 | // - - - | 274 | // - - - |
275 | 275 | ||
276 | void KAsteroidsView::addRocks( int num ) | 276 | void KAsteroidsView::addRocks( int num ) |
277 | { | 277 | { |
278 | for ( int i = 0; i < num; i++ ) | 278 | for ( int i = 0; i < num; i++ ) |
279 | { | 279 | { |
280 | KRock *rock = new KRock( animation[ID_ROCK_MEDIUM], &field, | 280 | KRock *rock = new KRock( animation[ID_ROCK_MEDIUM], &field, |
281 | ID_ROCK_MEDIUM, randInt(2), randInt(2) ? -1 : 1 ); | 281 | ID_ROCK_MEDIUM, randInt(2), randInt(2) ? -1 : 1 ); |
282 | double dx = (2.0 - randDouble()*4.0) * rockSpeed; | 282 | double dx = (2.0 - randDouble()*4.0) * rockSpeed; |
283 | double dy = (2.0 - randDouble()*4.0) * rockSpeed; | 283 | double dy = (2.0 - randDouble()*4.0) * rockSpeed; |
284 | rock->setVelocity( dx, dy ); | 284 | rock->setVelocity( dx, dy ); |
285 | rock->setFrame( randInt( rock->frameCount() ) ); | 285 | rock->setFrame( randInt( rock->frameCount() ) ); |
286 | if ( dx > 0 ) | 286 | if ( dx > 0 ) |
287 | { | 287 | { |
288 | if ( dy > 0 ) | 288 | if ( dy > 0 ) |
289 | rock->move( 5, 5, 0 ); | 289 | rock->move( 5, 5, 0 ); |
290 | else | 290 | else |
291 | rock->move( 5, field.height() - 25, 0 ); | 291 | rock->move( 5, field.height() - 25, 0 ); |
292 | } | 292 | } |
293 | else | 293 | else |
294 | { | 294 | { |
295 | if ( dy > 0 ) | 295 | if ( dy > 0 ) |
296 | rock->move( field.width() - 25, 5, 0 ); | 296 | rock->move( field.width() - 25, 5, 0 ); |
297 | else | 297 | else |
298 | rock->move( field.width() - 25, field.height() - 25, 0 ); | 298 | rock->move( field.width() - 25, field.height() - 25, 0 ); |
299 | } | 299 | } |
300 | rock->show( ); | 300 | rock->show( ); |
301 | rocks.append( rock ); | 301 | rocks.append( rock ); |
302 | } | 302 | } |
303 | } | 303 | } |
304 | 304 | ||
305 | // - - - | 305 | // - - - |
306 | 306 | ||
307 | void KAsteroidsView::showText( const QString &text, const QColor &color, bool scroll ) | 307 | void KAsteroidsView::showText( const QString &text, const QColor &color, bool scroll ) |
308 | { | 308 | { |
309 | textSprite->setTextFlags( AlignLeft | AlignVCenter ); | 309 | textSprite->setTextFlags( AlignLeft | AlignVCenter ); |
310 | textSprite->setText( text ); | 310 | textSprite->setText( text ); |
311 | textSprite->setColor( color ); | 311 | textSprite->setColor( color ); |
312 | 312 | ||
313 | if ( scroll ) { | 313 | if ( scroll ) { |
314 | textSprite->move( (field.width()-textSprite->boundingRect().width()) / 2, | 314 | textSprite->move( (field.width()-textSprite->boundingRect().width()) / 2, |
315 | -textSprite->boundingRect().height() ); | 315 | -textSprite->boundingRect().height() ); |
316 | textDy = TEXT_SPEED; | 316 | textDy = TEXT_SPEED; |
317 | } else { | 317 | } else { |
318 | textSprite->move( (field.width()-textSprite->boundingRect().width()) / 2, | 318 | textSprite->move( (field.width()-textSprite->boundingRect().width()) / 2, |
319 | (field.height()-textSprite->boundingRect().height()) / 2 ); | 319 | (field.height()-textSprite->boundingRect().height()) / 2 ); |
320 | textDy = 0; | 320 | textDy = 0; |
321 | } | 321 | } |
322 | textSprite->show(); | 322 | textSprite->show(); |
323 | } | 323 | } |
324 | 324 | ||
325 | // - - - | 325 | // - - - |
326 | 326 | ||
327 | void KAsteroidsView::hideText() | 327 | void KAsteroidsView::hideText() |
328 | { | 328 | { |
329 | textDy = -TEXT_SPEED; | 329 | textDy = -TEXT_SPEED; |
330 | } | 330 | } |
331 | 331 | ||
332 | // - - - | 332 | // - - - |
333 | 333 | ||
334 | void KAsteroidsView::resizeEvent(QResizeEvent* event) | 334 | void KAsteroidsView::resizeEvent(QResizeEvent* event) |
335 | { | 335 | { |
336 | QWidget::resizeEvent(event); | 336 | QWidget::resizeEvent(event); |
337 | field.resize(width()-4, height()-4); | 337 | field.resize(width()-4, height()-4); |
338 | view.resize(width(),height()); | 338 | view.resize(width(),height()); |
339 | } | 339 | } |
340 | 340 | ||
341 | // - - - | 341 | // - - - |
342 | 342 | ||
343 | void KAsteroidsView::timerEvent( QTimerEvent * ) | 343 | void KAsteroidsView::timerEvent( QTimerEvent * ) |
344 | { | 344 | { |
345 | field.advance(); | 345 | field.advance(); |
346 | 346 | ||
347 | QCanvasSprite *rock; | 347 | QCanvasSprite *rock; |
348 | 348 | ||
349 | // move rocks forward | 349 | // move rocks forward |
350 | for ( rock = rocks.first(); rock; rock = rocks.next() ) { | 350 | for ( rock = rocks.first(); rock; rock = rocks.next() ) { |
351 | ((KRock *)rock)->nextFrame(); | 351 | ((KRock *)rock)->nextFrame(); |
352 | wrapSprite( rock ); | 352 | wrapSprite( rock ); |
353 | } | 353 | } |
354 | 354 | ||
355 | wrapSprite( ship ); | 355 | wrapSprite( ship ); |
356 | 356 | ||
357 | // check for missile collision with rocks. | 357 | // check for missile collision with rocks. |
358 | processMissiles(); | 358 | processMissiles(); |
359 | 359 | ||
360 | // these are generated when a ship explodes | 360 | // these are generated when a ship explodes |
361 | for ( KBit *bit = bits.first(); bit; bit = bits.next() ) | 361 | for ( KBit *bit = bits.first(); bit; bit = bits.next() ) |
362 | { | 362 | { |
363 | if ( bit->expired() ) | 363 | if ( bit->expired() ) |
364 | { | 364 | { |
365 | bits.removeRef( bit ); | 365 | bits.removeRef( bit ); |
366 | } | 366 | } |
367 | else | 367 | else |
368 | { | 368 | { |
369 | bit->growOlder(); | 369 | bit->growOlder(); |
370 | bit->setFrame( ( bit->frame()+1 ) % bit->frameCount() ); | 370 | bit->setFrame( ( bit->frame()+1 ) % bit->frameCount() ); |
371 | } | 371 | } |
372 | } | 372 | } |
373 | 373 | ||
374 | for ( KExhaust *e = exhaust.first(); e; e = exhaust.next() ) | 374 | for ( KExhaust *e = exhaust.first(); e; e = exhaust.next() ) |
375 | exhaust.removeRef( e ); | 375 | exhaust.removeRef( e ); |
376 | 376 | ||
377 | // move / rotate ship. | 377 | // move / rotate ship. |
378 | // check for collision with a rock. | 378 | // check for collision with a rock. |
379 | processShip(); | 379 | processShip(); |
380 | 380 | ||
381 | // move powerups and check for collision with player and missiles | 381 | // move powerups and check for collision with player and missiles |
382 | processPowerups(); | 382 | processPowerups(); |
383 | 383 | ||
384 | if ( textSprite->visible() ) | 384 | if ( textSprite->visible() ) |
385 | { | 385 | { |
386 | if ( textDy < 0 && | 386 | if ( textDy < 0 && |
387 | textSprite->boundingRect().y() <= -textSprite->boundingRect().height() ) { | 387 | textSprite->boundingRect().y() <= -textSprite->boundingRect().height() ) { |
388 | textSprite->hide(); | 388 | textSprite->hide(); |
389 | } else { | 389 | } else { |
390 | textSprite->moveBy( 0, textDy ); | 390 | textSprite->moveBy( 0, textDy ); |
391 | } | 391 | } |
392 | if ( textSprite->boundingRect().y() > (field.height()-textSprite->boundingRect().height())/2 ) | 392 | if ( textSprite->boundingRect().y() > (field.height()-textSprite->boundingRect().height())/2 ) |
393 | textDy = 0; | 393 | textDy = 0; |
394 | } | 394 | } |
395 | 395 | ||
396 | if ( vitalsChanged && !(mFrameNum % 10) ) { | 396 | if ( vitalsChanged && !(mFrameNum % 10) ) { |
397 | emit updateVitals(); | 397 | emit updateVitals(); |
398 | vitalsChanged = FALSE; | 398 | vitalsChanged = FALSE; |
399 | } | 399 | } |
400 | 400 | ||
401 | mFrameNum++; | 401 | mFrameNum++; |
402 | } | 402 | } |
403 | 403 | ||
404 | void KAsteroidsView::wrapSprite( QCanvasItem *s ) | 404 | void KAsteroidsView::wrapSprite( QCanvasItem *s ) |
405 | { | 405 | { |
406 | int x = int(s->x() + s->boundingRect().width() / 2); | 406 | int x = int(s->x() + s->boundingRect().width() / 2); |
407 | int y = int(s->y() + s->boundingRect().height() / 2); | 407 | int y = int(s->y() + s->boundingRect().height() / 2); |
408 | 408 | ||
409 | if ( x > field.width() ) | 409 | if ( x > field.width() ) |
410 | s->move( s->x() - field.width(), s->y() ); | 410 | s->move( s->x() - field.width(), s->y() ); |
411 | else if ( x < 0 ) | 411 | else if ( x < 0 ) |
412 | s->move( field.width() + s->x(), s->y() ); | 412 | s->move( field.width() + s->x(), s->y() ); |
413 | 413 | ||
414 | if ( y > field.height() ) | 414 | if ( y > field.height() ) |
415 | s->move( s->x(), s->y() - field.height() ); | 415 | s->move( s->x(), s->y() - field.height() ); |
416 | else if ( y < 0 ) | 416 | else if ( y < 0 ) |
417 | s->move( s->x(), field.height() + s->y() ); | 417 | s->move( s->x(), field.height() + s->y() ); |
418 | } | 418 | } |
419 | 419 | ||
420 | // - - - | 420 | // - - - |
421 | 421 | ||
422 | void KAsteroidsView::rockHit( QCanvasItem *hit ) | 422 | void KAsteroidsView::rockHit( QCanvasItem *hit ) |
423 | { | 423 | { |
424 | KPowerup *nPup = 0; | 424 | KPowerup *nPup = 0; |
425 | int rnd = static_cast<int>(randDouble()*30.0) % 30; | 425 | int rnd = static_cast<int>(randDouble()*30.0) % 30; |
426 | switch( rnd ) | 426 | switch( rnd ) |
427 | { | 427 | { |
428 | case 4: | 428 | case 4: |
429 | case 5: | 429 | case 5: |
430 | nPup = new KPowerup( animation[ID_ENERGY_POWERUP], &field, | 430 | nPup = new KPowerup( animation[ID_ENERGY_POWERUP], &field, |
431 | ID_ENERGY_POWERUP ); | 431 | ID_ENERGY_POWERUP ); |
432 | break; | 432 | break; |
433 | case 10: | 433 | case 10: |
434 | // nPup = new KPowerup( animation[ID_TELEPORT_POWERUP], &field, | 434 | // nPup = new KPowerup( animation[ID_TELEPORT_POWERUP], &field, |
435 | // ID_TELEPORT_POWERUP ); | 435 | // ID_TELEPORT_POWERUP ); |
436 | break; | 436 | break; |
437 | case 15: | 437 | case 15: |
438 | nPup = new KPowerup( animation[ID_BRAKE_POWERUP], &field, | 438 | nPup = new KPowerup( animation[ID_BRAKE_POWERUP], &field, |
439 | ID_BRAKE_POWERUP ); | 439 | ID_BRAKE_POWERUP ); |
440 | break; | 440 | break; |
441 | case 20: | 441 | case 20: |
442 | nPup = new KPowerup( animation[ID_SHIELD_POWERUP], &field, | 442 | nPup = new KPowerup( animation[ID_SHIELD_POWERUP], &field, |
443 | ID_SHIELD_POWERUP ); | 443 | ID_SHIELD_POWERUP ); |
444 | break; | 444 | break; |
445 | case 24: | 445 | case 24: |
446 | case 25: | 446 | case 25: |
447 | nPup = new KPowerup( animation[ID_SHOOT_POWERUP], &field, | 447 | nPup = new KPowerup( animation[ID_SHOOT_POWERUP], &field, |
448 | ID_SHOOT_POWERUP ); | 448 | ID_SHOOT_POWERUP ); |
449 | break; | 449 | break; |
450 | } | 450 | } |
451 | if ( nPup ) | 451 | if ( nPup ) |
452 | { | 452 | { |
453 | double r = 0.5 - randDouble(); | 453 | double r = 0.5 - randDouble(); |
454 | nPup->move( hit->x(), hit->y(), 0 ); | 454 | nPup->move( hit->x(), hit->y(), 0 ); |
455 | nPup->setVelocity( hit->xVelocity() + r, hit->yVelocity() + r ); | 455 | nPup->setVelocity( hit->xVelocity() + r, hit->yVelocity() + r ); |
456 | nPup->show( ); | 456 | nPup->show( ); |
457 | powerups.append( nPup ); | 457 | powerups.append( nPup ); |
458 | } | 458 | } |
459 | 459 | ||
460 | if ( hit->rtti() == ID_ROCK_LARGE || hit->rtti() == ID_ROCK_MEDIUM ) | 460 | if ( hit->rtti() == ID_ROCK_LARGE || hit->rtti() == ID_ROCK_MEDIUM ) |
461 | { | 461 | { |
462 | // break into smaller rocks | 462 | // break into smaller rocks |
463 | double addx[4] = { 1.0, 1.0, -1.0, -1.0 }; | 463 | double addx[4] = { 1.0, 1.0, -1.0, -1.0 }; |
464 | double addy[4] = { -1.0, 1.0, -1.0, 1.0 }; | 464 | double addy[4] = { -1.0, 1.0, -1.0, 1.0 }; |
465 | 465 | ||
466 | double dx = hit->xVelocity(); | 466 | double dx = hit->xVelocity(); |
467 | double dy = hit->yVelocity(); | 467 | double dy = hit->yVelocity(); |
468 | 468 | ||
469 | double maxRockSpeed = MAX_ROCK_SPEED * rockSpeed; | 469 | double maxRockSpeed = MAX_ROCK_SPEED * rockSpeed; |
470 | if ( dx > maxRockSpeed ) | 470 | if ( dx > maxRockSpeed ) |
471 | dx = maxRockSpeed; | 471 | dx = maxRockSpeed; |
472 | else if ( dx < -maxRockSpeed ) | 472 | else if ( dx < -maxRockSpeed ) |
473 | dx = -maxRockSpeed; | 473 | dx = -maxRockSpeed; |
474 | if ( dy > maxRockSpeed ) | 474 | if ( dy > maxRockSpeed ) |
475 | dy = maxRockSpeed; | 475 | dy = maxRockSpeed; |
476 | else if ( dy < -maxRockSpeed ) | 476 | else if ( dy < -maxRockSpeed ) |
477 | dy = -maxRockSpeed; | 477 | dy = -maxRockSpeed; |
478 | 478 | ||
479 | QCanvasSprite *nrock; | 479 | QCanvasSprite *nrock; |
480 | 480 | ||
481 | for ( int i = 0; i < 4; i++ ) | 481 | for ( int i = 0; i < 4; i++ ) |
482 | { | 482 | { |
483 | double r = rockSpeed/2 - randDouble()*rockSpeed; | 483 | double r = rockSpeed/2 - randDouble()*rockSpeed; |
484 | if ( hit->rtti() == ID_ROCK_LARGE ) | 484 | if ( hit->rtti() == ID_ROCK_LARGE ) |
485 | { | 485 | { |
486 | nrock = new KRock( animation[ID_ROCK_MEDIUM], &field, | 486 | nrock = new KRock( animation[ID_ROCK_MEDIUM], &field, |
487 | ID_ROCK_MEDIUM, randInt(2), randInt(2) ? -1 : 1 ); | 487 | ID_ROCK_MEDIUM, randInt(2), randInt(2) ? -1 : 1 ); |
488 | emit rockHit( 0 ); | 488 | emit rockHit( 0 ); |
489 | } | 489 | } |
490 | else | 490 | else |
491 | { | 491 | { |
492 | nrock = new KRock( animation[ID_ROCK_SMALL], &field, | 492 | nrock = new KRock( animation[ID_ROCK_SMALL], &field, |
493 | ID_ROCK_SMALL, randInt(2), randInt(2) ? -1 : 1 ); | 493 | ID_ROCK_SMALL, randInt(2), randInt(2) ? -1 : 1 ); |
494 | emit rockHit( 1 ); | 494 | emit rockHit( 1 ); |
495 | } | 495 | } |
496 | 496 | ||
497 | nrock->move( hit->x(), hit->y(), 0 ); | 497 | nrock->move( hit->x(), hit->y(), 0 ); |
498 | nrock->setVelocity( dx+addx[i]*rockSpeed+r, dy+addy[i]*rockSpeed+r ); | 498 | nrock->setVelocity( dx+addx[i]*rockSpeed+r, dy+addy[i]*rockSpeed+r ); |
499 | nrock->setFrame( randInt( nrock->frameCount() ) ); | 499 | nrock->setFrame( randInt( nrock->frameCount() ) ); |
500 | nrock->show( ); | 500 | nrock->show( ); |
501 | rocks.append( nrock ); | 501 | rocks.append( nrock ); |
502 | } | 502 | } |
503 | } | 503 | } |
504 | else if ( hit->rtti() == ID_ROCK_SMALL ) | 504 | else if ( hit->rtti() == ID_ROCK_SMALL ) |
505 | emit rockHit( 2 ); | 505 | emit rockHit( 2 ); |
506 | rocks.removeRef( (QCanvasSprite *)hit ); | 506 | rocks.removeRef( (QCanvasSprite *)hit ); |
507 | if ( rocks.count() == 0 ) | 507 | if ( rocks.count() == 0 ) |
508 | emit rocksRemoved(); | 508 | emit rocksRemoved(); |
509 | } | 509 | } |
510 | 510 | ||
511 | void KAsteroidsView::reducePower( int val ) | 511 | void KAsteroidsView::reducePower( int val ) |
512 | { | 512 | { |
513 | shipPower -= val; | 513 | shipPower -= val; |
514 | if ( shipPower <= 0 ) | 514 | if ( shipPower <= 0 ) |
515 | { | 515 | { |
516 | shipPower = 0; | 516 | shipPower = 0; |
517 | thrustShip = FALSE; | 517 | thrustShip = FALSE; |
518 | if ( shieldOn ) | 518 | if ( shieldOn ) |
519 | { | 519 | { |
520 | shieldOn = FALSE; | 520 | shieldOn = FALSE; |
521 | shield->hide(); | 521 | shield->hide(); |
522 | } | 522 | } |
523 | } | 523 | } |
524 | vitalsChanged = TRUE; | 524 | vitalsChanged = TRUE; |
525 | } | 525 | } |
526 | 526 | ||
527 | void KAsteroidsView::addExhaust( double x, double y, double dx, | 527 | void KAsteroidsView::addExhaust( double x, double y, double dx, |
528 | double dy, int count ) | 528 | double dy, int count ) |
529 | { | 529 | { |
530 | for ( int i = 0; i < count; i++ ) | 530 | for ( int i = 0; i < count; i++ ) |
531 | { | 531 | { |
532 | KExhaust *e = new KExhaust( animation[ID_EXHAUST], &field ); | 532 | KExhaust *e = new KExhaust( animation[ID_EXHAUST], &field ); |
533 | e->move( x + 2 - randDouble()*4, y + 2 - randDouble()*4 ); | 533 | e->move( x + 2 - randDouble()*4, y + 2 - randDouble()*4 ); |
534 | e->setVelocity( dx, dy ); | 534 | e->setVelocity( dx, dy ); |
535 | e->show( ); | 535 | e->show( ); |
536 | exhaust.append( e ); | 536 | exhaust.append( e ); |
537 | } | 537 | } |
538 | } | 538 | } |
539 | 539 | ||
540 | void KAsteroidsView::processMissiles() | 540 | void KAsteroidsView::processMissiles() |
541 | { | 541 | { |
542 | KMissile *missile; | 542 | KMissile *missile; |
543 | 543 | ||
544 | // if a missile has hit a rock, remove missile and break rock into smaller | 544 | // if a missile has hit a rock, remove missile and break rock into smaller |
545 | // rocks or remove completely. | 545 | // rocks or remove completely. |
546 | QPtrListIterator<KMissile> it(missiles); | 546 | QPtrListIterator<KMissile> it(missiles); |
547 | 547 | ||
548 | for ( ; it.current(); ++it ) | 548 | for ( ; it.current(); ++it ) |
549 | { | 549 | { |
550 | missile = it.current(); | 550 | missile = it.current(); |
551 | missile->growOlder(); | 551 | missile->growOlder(); |
552 | 552 | ||
553 | if ( missile->expired() ) | 553 | if ( missile->expired() ) |
554 | { | 554 | { |
555 | missiles.removeRef( missile ); | 555 | missiles.removeRef( missile ); |
556 | continue; | 556 | continue; |
557 | } | 557 | } |
558 | 558 | ||
559 | wrapSprite( missile ); | 559 | wrapSprite( missile ); |
560 | 560 | ||
561 | QCanvasItemList hits = missile->collisions( TRUE ); | 561 | QCanvasItemList hits = missile->collisions( TRUE ); |
562 | QCanvasItemList::Iterator hit; | 562 | QCanvasItemList::Iterator hit; |
563 | for ( hit = hits.begin(); hit != hits.end(); ++hit ) | 563 | for ( hit = hits.begin(); hit != hits.end(); ++hit ) |
564 | { | 564 | { |
565 | if ( (*hit)->rtti() >= ID_ROCK_LARGE && | 565 | if ( (*hit)->rtti() >= ID_ROCK_LARGE && |
566 | (*hit)->rtti() <= ID_ROCK_SMALL ) | 566 | (*hit)->rtti() <= ID_ROCK_SMALL ) |
567 | { | 567 | { |
568 | shotsHit++; | 568 | shotsHit++; |
569 | rockHit( *hit ); | 569 | rockHit( *hit ); |
570 | missiles.removeRef( missile ); | 570 | missiles.removeRef( missile ); |
571 | break; | 571 | break; |
572 | } | 572 | } |
573 | } | 573 | } |
574 | } | 574 | } |
575 | } | 575 | } |
576 | 576 | ||
577 | // - - - | 577 | // - - - |
578 | 578 | ||
579 | void KAsteroidsView::processShip() | 579 | void KAsteroidsView::processShip() |
580 | { | 580 | { |
581 | if ( ship->visible() ) | 581 | if ( ship->visible() ) |
582 | { | 582 | { |
583 | if ( shieldOn ) | 583 | if ( shieldOn ) |
584 | { | 584 | { |
585 | shield->show(); | 585 | shield->show(); |
586 | reducePower( SHIELD_ON_COST ); | 586 | reducePower( SHIELD_ON_COST ); |
587 | static int sf = 0; | 587 | static int sf = 0; |
588 | sf++; | 588 | sf++; |
589 | 589 | ||
590 | if ( sf % 2 ) | 590 | if ( sf % 2 ) |
591 | shield->setFrame( (shield->frame()+1) % shield->frameCount() ); | 591 | shield->setFrame( (shield->frame()+1) % shield->frameCount() ); |
592 | shield->move( ship->x() - 5, ship->y() - 5 ); | 592 | shield->move( ship->x() - 5, ship->y() - 5 ); |
593 | 593 | ||
594 | QCanvasItemList hits = shield->collisions( TRUE ); | 594 | QCanvasItemList hits = shield->collisions( TRUE ); |
595 | QCanvasItemList::Iterator it; | 595 | QCanvasItemList::Iterator it; |
596 | for ( it = hits.begin(); it != hits.end(); ++it ) | 596 | for ( it = hits.begin(); it != hits.end(); ++it ) |
597 | { | 597 | { |
598 | if ( (*it)->rtti() >= ID_ROCK_LARGE && | 598 | if ( (*it)->rtti() >= ID_ROCK_LARGE && |
599 | (*it)->rtti() <= ID_ROCK_SMALL ) | 599 | (*it)->rtti() <= ID_ROCK_SMALL ) |
600 | { | 600 | { |
601 | int factor; | 601 | int factor; |
602 | switch ( (*it)->rtti() ) | 602 | switch ( (*it)->rtti() ) |
603 | { | 603 | { |
604 | case ID_ROCK_LARGE: | 604 | case ID_ROCK_LARGE: |
605 | factor = 3; | 605 | factor = 3; |
606 | break; | 606 | break; |
607 | 607 | ||
608 | case ID_ROCK_MEDIUM: | 608 | case ID_ROCK_MEDIUM: |
609 | factor = 2; | 609 | factor = 2; |
610 | break; | 610 | break; |
611 | 611 | ||
612 | default: | 612 | default: |
613 | factor = 1; | 613 | factor = 1; |
614 | } | 614 | } |
615 | 615 | ||
616 | if ( factor > mShieldCount ) | 616 | if ( factor > mShieldCount ) |
617 | { | 617 | { |
618 | // shield not strong enough | 618 | // shield not strong enough |
619 | shieldOn = FALSE; | 619 | shieldOn = FALSE; |
620 | break; | 620 | break; |
621 | } | 621 | } |
622 | rockHit( *it ); | 622 | rockHit( *it ); |
623 | // the more shields we have the less costly | 623 | // the more shields we have the less costly |
624 | reducePower( factor * (SHIELD_HIT_COST - mShieldCount*2) ); | 624 | reducePower( factor * (SHIELD_HIT_COST - mShieldCount*2) ); |
625 | } | 625 | } |
626 | } | 626 | } |
627 | } | 627 | } |
628 | 628 | ||
629 | if ( !shieldOn ) | 629 | if ( !shieldOn ) |
630 | { | 630 | { |
631 | shield->hide(); | 631 | shield->hide(); |
632 | QCanvasItemList hits = ship->collisions( TRUE ); | 632 | QCanvasItemList hits = ship->collisions( TRUE ); |
633 | QCanvasItemList::Iterator it; | 633 | QCanvasItemList::Iterator it; |
634 | for ( it = hits.begin(); it != hits.end(); ++it ) | 634 | for ( it = hits.begin(); it != hits.end(); ++it ) |
635 | { | 635 | { |
636 | if ( (*it)->rtti() >= ID_ROCK_LARGE && | 636 | if ( (*it)->rtti() >= ID_ROCK_LARGE && |
637 | (*it)->rtti() <= ID_ROCK_SMALL ) | 637 | (*it)->rtti() <= ID_ROCK_SMALL ) |
638 | { | 638 | { |
639 | KBit *bit; | 639 | KBit *bit; |
640 | for ( int i = 0; i < 8; i++ ) | 640 | for ( int i = 0; i < 8; i++ ) |
641 | { | 641 | { |
642 | bit = new KBit( animation[ID_BIT], &field ); | 642 | bit = new KBit( animation[ID_BIT], &field ); |
643 | bit->move( ship->x() + 5 - randDouble() * 10, | 643 | bit->move( ship->x() + 5 - randDouble() * 10, |
644 | ship->y() + 5 - randDouble() * 10, | 644 | ship->y() + 5 - randDouble() * 10, |
645 | randInt(bit->frameCount()) ); | 645 | randInt(bit->frameCount()) ); |
646 | bit->setVelocity( 1-randDouble()*2, | 646 | bit->setVelocity( 1-randDouble()*2, |
647 | 1-randDouble()*2 ); | 647 | 1-randDouble()*2 ); |
648 | bit->setDeath( 60 + randInt(60) ); | 648 | bit->setDeath( 60 + randInt(60) ); |
649 | bit->show( ); | 649 | bit->show( ); |
650 | bits.append( bit ); | 650 | bits.append( bit ); |
651 | } | 651 | } |
652 | ship->hide(); | 652 | ship->hide(); |
653 | shield->hide(); | 653 | shield->hide(); |
654 | emit shipKilled(); | 654 | emit shipKilled(); |
655 | break; | 655 | break; |
656 | } | 656 | } |
657 | } | 657 | } |
658 | } | 658 | } |
659 | 659 | ||
660 | 660 | ||
661 | if ( rotateSlow ) | 661 | if ( rotateSlow ) |
662 | rotateSlow--; | 662 | rotateSlow--; |
663 | 663 | ||
664 | if ( rotateL ) | 664 | if ( rotateL ) |
665 | { | 665 | { |
666 | shipAngle -= rotateSlow ? 1 : rotateRate; | 666 | shipAngle -= rotateSlow ? 1 : rotateRate; |
667 | if ( shipAngle < 0 ) | 667 | if ( shipAngle < 0 ) |
668 | shipAngle += SHIP_STEPS; | 668 | shipAngle += SHIP_STEPS; |
669 | } | 669 | } |
670 | 670 | ||
671 | if ( rotateR ) | 671 | if ( rotateR ) |
672 | { | 672 | { |
673 | shipAngle += rotateSlow ? 1 : rotateRate; | 673 | shipAngle += rotateSlow ? 1 : rotateRate; |
674 | if ( shipAngle >= SHIP_STEPS ) | 674 | if ( shipAngle >= SHIP_STEPS ) |
675 | shipAngle -= SHIP_STEPS; | 675 | shipAngle -= SHIP_STEPS; |
676 | } | 676 | } |
677 | 677 | ||
678 | double angle = shipAngle * PI_X_2 / SHIP_STEPS; | 678 | double angle = shipAngle * PI_X_2 / SHIP_STEPS; |
679 | double cosangle = cos( angle ); | 679 | double cosangle = cos( angle ); |
680 | double sinangle = sin( angle ); | 680 | double sinangle = sin( angle ); |
681 | 681 | ||
682 | if ( brakeShip ) | 682 | if ( brakeShip ) |
683 | { | 683 | { |
684 | thrustShip = FALSE; | 684 | thrustShip = FALSE; |
685 | rotateL = FALSE; | 685 | rotateL = FALSE; |
686 | rotateR = FALSE; | 686 | rotateR = FALSE; |
687 | rotateRate = ROTATE_RATE; | 687 | rotateRate = ROTATE_RATE; |
688 | if ( fabs(shipDx) < 2.5 && fabs(shipDy) < 2.5 ) | 688 | if ( fabs(shipDx) < 2.5 && fabs(shipDy) < 2.5 ) |
689 | { | 689 | { |
690 | shipDx = 0.0; | 690 | shipDx = 0.0; |
691 | shipDy = 0.0; | 691 | shipDy = 0.0; |
692 | ship->setVelocity( shipDx, shipDy ); | 692 | ship->setVelocity( shipDx, shipDy ); |
693 | brakeShip = FALSE; | 693 | brakeShip = FALSE; |
694 | } | 694 | } |
695 | else | 695 | else |
696 | { | 696 | { |
697 | double motionAngle = atan2( -shipDy, -shipDx ); | 697 | double motionAngle = atan2( -shipDy, -shipDx ); |
698 | if ( angle > M_PI ) | 698 | if ( angle > M_PI ) |
699 | angle -= PI_X_2; | 699 | angle -= PI_X_2; |
700 | double angleDiff = angle - motionAngle; | 700 | double angleDiff = angle - motionAngle; |
701 | if ( angleDiff > M_PI ) | 701 | if ( angleDiff > M_PI ) |
702 | angleDiff = PI_X_2 - angleDiff; | 702 | angleDiff = PI_X_2 - angleDiff; |
703 | else if ( angleDiff < -M_PI ) | 703 | else if ( angleDiff < -M_PI ) |
704 | angleDiff = PI_X_2 + angleDiff; | 704 | angleDiff = PI_X_2 + angleDiff; |
705 | double fdiff = fabs( angleDiff ); | 705 | double fdiff = fabs( angleDiff ); |
706 | if ( fdiff > 0.08 ) | 706 | if ( fdiff > 0.08 ) |
707 | { | 707 | { |
708 | if ( angleDiff > 0 ) | 708 | if ( angleDiff > 0 ) |
709 | rotateL = TRUE; | 709 | rotateL = TRUE; |
710 | else if ( angleDiff < 0 ) | 710 | else if ( angleDiff < 0 ) |
711 | rotateR = TRUE; | 711 | rotateR = TRUE; |
712 | if ( fdiff > 0.6 ) | 712 | if ( fdiff > 0.6 ) |
713 | rotateRate = mBrakeCount + 1; | 713 | rotateRate = mBrakeCount + 1; |
714 | else if ( fdiff > 0.4 ) | 714 | else if ( fdiff > 0.4 ) |
715 | rotateRate = 2; | 715 | rotateRate = 2; |
716 | else | 716 | else |
717 | rotateRate = 1; | 717 | rotateRate = 1; |
718 | 718 | ||
719 | if ( rotateRate > 5 ) | 719 | if ( rotateRate > 5 ) |
720 | rotateRate = 5; | 720 | rotateRate = 5; |
721 | } | 721 | } |
722 | else if ( fabs(shipDx) > 1 || fabs(shipDy) > 1 ) | 722 | else if ( fabs(shipDx) > 1 || fabs(shipDy) > 1 ) |
723 | { | 723 | { |
724 | thrustShip = TRUE; | 724 | thrustShip = TRUE; |
725 | // we'll make braking a bit faster | 725 | // we'll make braking a bit faster |
726 | shipDx += cosangle/6 * (mBrakeCount - 1); | 726 | shipDx += cosangle/6 * (mBrakeCount - 1); |
727 | shipDy += sinangle/6 * (mBrakeCount - 1); | 727 | shipDy += sinangle/6 * (mBrakeCount - 1); |
728 | reducePower( BRAKE_ON_COST ); | 728 | reducePower( BRAKE_ON_COST ); |
729 | addExhaust( ship->x() + 10 - cosangle*11, | 729 | addExhaust( ship->x() + 10 - cosangle*11, |
730 | ship->y() + 10 - sinangle*11, | 730 | ship->y() + 10 - sinangle*11, |
731 | shipDx-cosangle, shipDy-sinangle, | 731 | shipDx-cosangle, shipDy-sinangle, |
732 | mBrakeCount+1 ); | 732 | mBrakeCount+1 ); |
733 | } | 733 | } |
734 | } | 734 | } |
735 | } | 735 | } |
736 | 736 | ||
737 | if ( thrustShip ) | 737 | if ( thrustShip ) |
738 | { | 738 | { |
739 | // The ship has a terminal velocity, but trying to go faster | 739 | // The ship has a terminal velocity, but trying to go faster |
740 | // still uses fuel (can go faster diagonally - don't care). | 740 | // still uses fuel (can go faster diagonally - don't care). |
741 | double thrustx = cosangle/8; | 741 | double thrustx = cosangle/8; |
742 | double thrusty = sinangle/8; | 742 | double thrusty = sinangle/8; |
743 | if ( fabs(shipDx + thrustx) < MAX_SHIP_SPEED ) | 743 | if ( fabs(shipDx + thrustx) < MAX_SHIP_SPEED ) |
744 | shipDx += thrustx; | 744 | shipDx += thrustx; |
745 | if ( fabs(shipDy + thrusty) < MAX_SHIP_SPEED ) | 745 | if ( fabs(shipDy + thrusty) < MAX_SHIP_SPEED ) |
746 | shipDy += thrusty; | 746 | shipDy += thrusty; |
747 | ship->setVelocity( shipDx, shipDy ); | 747 | ship->setVelocity( shipDx, shipDy ); |
748 | reducePower( 1 ); | 748 | reducePower( 1 ); |
749 | addExhaust( ship->x() + 10 - cosangle*10, | 749 | addExhaust( ship->x() + 10 - cosangle*10, |
750 | ship->y() + 10 - sinangle*10, | 750 | ship->y() + 10 - sinangle*10, |
751 | shipDx-cosangle, shipDy-sinangle, 3 ); | 751 | shipDx-cosangle, shipDy-sinangle, 3 ); |
752 | } | 752 | } |
753 | 753 | ||
754 | ship->setFrame( shipAngle >> 1 ); | 754 | ship->setFrame( shipAngle >> 1 ); |
755 | 755 | ||
756 | if ( shootShip ) | 756 | if ( shootShip ) |
757 | { | 757 | { |
758 | if ( !shootDelay && (int)missiles.count() < mShootCount + 2 ) | 758 | if ( !shootDelay && (int)missiles.count() < mShootCount + 2 ) |
759 | { | 759 | { |
760 | KMissile *missile = new KMissile( animation[ID_MISSILE], &field ); | 760 | KMissile *missile = new KMissile( animation[ID_MISSILE], &field ); |
761 | missile->move( 11+ship->x()+cosangle*11, | 761 | missile->move( 11+ship->x()+cosangle*11, |
762 | 11+ship->y()+sinangle*11, 0 ); | 762 | 11+ship->y()+sinangle*11, 0 ); |
763 | missile->setVelocity( shipDx + cosangle*MISSILE_SPEED, | 763 | missile->setVelocity( shipDx + cosangle*MISSILE_SPEED, |
764 | shipDy + sinangle*MISSILE_SPEED ); | 764 | shipDy + sinangle*MISSILE_SPEED ); |
765 | missile->show( ); | 765 | missile->show( ); |
766 | missiles.append( missile ); | 766 | missiles.append( missile ); |
767 | shotsFired++; | 767 | shotsFired++; |
768 | reducePower( 1 ); | 768 | reducePower( 1 ); |
769 | 769 | ||
770 | shootDelay = 5; | 770 | shootDelay = 5; |
771 | } | 771 | } |
772 | 772 | ||
773 | if ( shootDelay ) | 773 | if ( shootDelay ) |
774 | shootDelay--; | 774 | shootDelay--; |
775 | } | 775 | } |
776 | 776 | ||
777 | if ( teleportShip ) | 777 | if ( teleportShip ) |
778 | { | 778 | { |
779 | int ra = rand() % 10; | 779 | int ra = rand() % 10; |
780 | if( ra == 0 ) | 780 | if( ra == 0 ) |
781 | ra += rand() % 20; | 781 | ra += rand() % 20; |
782 | int xra = ra * 60 + ( (rand() % 20) * (rand() % 20) ); | 782 | int xra = ra * 60 + ( (rand() % 20) * (rand() % 20) ); |
783 | int yra = ra * 50 - ( (rand() % 20) * (rand() % 20) ); | 783 | int yra = ra * 50 - ( (rand() % 20) * (rand() % 20) ); |
784 | ship->move( xra, yra ); | 784 | ship->move( xra, yra ); |
785 | } | 785 | } |
786 | 786 | ||
787 | vitalsChanged = TRUE; | 787 | vitalsChanged = TRUE; |
788 | } | 788 | } |
789 | } | 789 | } |
790 | 790 | ||
791 | // - - - | 791 | // - - - |
792 | 792 | ||
793 | void KAsteroidsView::processPowerups() | 793 | void KAsteroidsView::processPowerups() |
794 | { | 794 | { |
795 | if ( !powerups.isEmpty() ) | 795 | if ( !powerups.isEmpty() ) |
796 | { | 796 | { |
797 | // if player gets the powerup remove it from the screen, if option | 797 | // if player gets the powerup remove it from the screen, if option |
798 | // "Can destroy powerups" is enabled and a missile hits the powerup | 798 | // "Can destroy powerups" is enabled and a missile hits the powerup |
799 | // destroy it | 799 | // destroy it |
800 | 800 | ||
801 | KPowerup *pup; | 801 | KPowerup *pup; |
802 | QPtrListIterator<KPowerup> it( powerups ); | 802 | QPtrListIterator<KPowerup> it( powerups ); |
803 | 803 | ||
804 | for( ; it.current(); ++it ) | 804 | for( ; it.current(); ++it ) |
805 | { | 805 | { |
806 | pup = it.current(); | 806 | pup = it.current(); |
807 | pup->growOlder(); | 807 | pup->growOlder(); |
808 | 808 | ||
809 | if( pup->expired() ) | 809 | if( pup->expired() ) |
810 | { | 810 | { |
811 | powerups.removeRef( pup ); | 811 | powerups.removeRef( pup ); |
812 | continue; | 812 | continue; |
813 | } | 813 | } |
814 | 814 | ||
815 | wrapSprite( pup ); | 815 | wrapSprite( pup ); |
816 | 816 | ||
817 | QCanvasItemList hits = pup->collisions( TRUE ); | 817 | QCanvasItemList hits = pup->collisions( TRUE ); |
818 | QCanvasItemList::Iterator it; | 818 | QCanvasItemList::Iterator it; |
819 | for ( it = hits.begin(); it != hits.end(); ++it ) | 819 | for ( it = hits.begin(); it != hits.end(); ++it ) |
820 | { | 820 | { |
821 | if ( (*it) == ship ) | 821 | if ( (*it) == ship ) |
822 | { | 822 | { |
823 | switch( pup->rtti() ) | 823 | switch( pup->rtti() ) |
824 | { | 824 | { |
825 | case ID_ENERGY_POWERUP: | 825 | case ID_ENERGY_POWERUP: |
826 | shipPower += 150; | 826 | shipPower += 150; |
827 | if ( shipPower > MAX_POWER_LEVEL ) | 827 | if ( shipPower > MAX_POWER_LEVEL ) |
828 | shipPower = MAX_POWER_LEVEL; | 828 | shipPower = MAX_POWER_LEVEL; |
829 | break; | 829 | break; |
830 | case ID_TELEPORT_POWERUP: | 830 | case ID_TELEPORT_POWERUP: |
831 | mTeleportCount++; | 831 | mTeleportCount++; |
832 | break; | 832 | break; |
833 | case ID_BRAKE_POWERUP: | 833 | case ID_BRAKE_POWERUP: |
834 | if ( mBrakeCount < MAX_BRAKES ) | 834 | if ( mBrakeCount < MAX_BRAKES ) |
835 | mBrakeCount++; | 835 | mBrakeCount++; |
836 | break; | 836 | break; |
837 | case ID_SHIELD_POWERUP: | 837 | case ID_SHIELD_POWERUP: |
838 | if ( mShieldCount < MAX_SHIELDS ) | 838 | if ( mShieldCount < MAX_SHIELDS ) |
839 | mShieldCount++; | 839 | mShieldCount++; |
840 | break; | 840 | break; |
841 | case ID_SHOOT_POWERUP: | 841 | case ID_SHOOT_POWERUP: |
842 | if ( mShootCount < MAX_FIREPOWER ) | 842 | if ( mShootCount < MAX_FIREPOWER ) |
843 | mShootCount++; | 843 | mShootCount++; |
844 | break; | 844 | break; |
845 | } | 845 | } |
846 | 846 | ||
847 | powerups.removeRef( pup ); | 847 | powerups.removeRef( pup ); |
848 | vitalsChanged = TRUE; | 848 | vitalsChanged = TRUE; |
849 | } | 849 | } |
850 | else if ( (*it) == shield ) | 850 | else if ( (*it) == shield ) |
851 | { | 851 | { |
852 | powerups.removeRef( pup ); | 852 | powerups.removeRef( pup ); |
853 | } | 853 | } |
854 | else if ( (*it)->rtti() == ID_MISSILE ) | 854 | else if ( (*it)->rtti() == ID_MISSILE ) |
855 | { | 855 | { |
856 | if ( can_destroy_powerups ) | 856 | if ( can_destroy_powerups ) |
857 | { | 857 | { |
858 | powerups.removeRef( pup ); | 858 | powerups.removeRef( pup ); |
859 | } | 859 | } |
860 | } | 860 | } |
861 | } | 861 | } |
862 | } | 862 | } |
863 | } // -- if( powerups.isEmpty() ) | 863 | } // -- if( powerups.isEmpty() ) |
864 | } | 864 | } |
865 | 865 | ||
866 | // - - - | 866 | // - - - |
867 | 867 | ||
868 | void KAsteroidsView::hideShield() | 868 | void KAsteroidsView::hideShield() |
869 | { | 869 | { |
870 | shield->hide(); | 870 | shield->hide(); |
871 | mShieldCount = 0; | 871 | mShieldCount = 0; |
872 | shieldOn = FALSE; | 872 | shieldOn = FALSE; |
873 | } | 873 | } |
874 | 874 | ||
875 | double KAsteroidsView::randDouble() | 875 | double KAsteroidsView::randDouble() |
876 | { | 876 | { |
877 | int v = rand(); | 877 | int v = rand(); |
878 | return (double)v / (double)RAND_MAX; | 878 | return (double)v / (double)RAND_MAX; |
879 | } | 879 | } |
880 | 880 | ||
881 | int KAsteroidsView::randInt( int range ) | 881 | int KAsteroidsView::randInt( int range ) |
882 | { | 882 | { |
883 | return rand() % range; | 883 | return rand() % range; |
884 | } | 884 | } |