source: trunk/examples/demo/qasteroids/view.cpp@ 168

Last change on this file since 168 was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

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