home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / demo / qasteroids / view.cpp.z / view.cpp
Encoding:
C/C++ Source or Header  |  2002-04-08  |  19.2 KB  |  880 lines

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