tbadguy.cpp

来自「symbian 的一个 二维飞行游戏 源码 及相关技术文章」· C++ 代码 · 共 666 行 · 第 1/2 页

CPP
666
字号
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
      
    case EPathLineIfXLess:
      if(iX < aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfXGreater:
      if(iX > aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfXEqual:
      if(iX == aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfYLess:
      if(iY < aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfYGreater:
      if(iY > aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfYEqual:
      if(iY == aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
      
    case EPathLineIfHealthLess:
      if(iHealth < aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfHealthGreater:
      if(iHealth > aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfHealthEqual:
      if(iHealth == aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfCounterLess:
      if(iCounter < aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfCounterGreater:
      if(iCounter > aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      }
      break;
    case EPathLineIfCounterEqual:
      if(iCounter == aPaths[(iPath*4)+3]) {
        iPath+=aPaths[(iPath*4)+2];
      } else {
        iPath+=aPaths[(iPath*4)+1];
      } 
     break;
      // setting
      
    case EPathLineSetPoints:
      iScore = aPaths[(iPath*4)+2];
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineSetId:
      iId = aPaths[(iPath*4)+2];
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineSetHealth:
      iHealth = aPaths[(iPath*4)+2];
      // if health has got to zero die
      if(iHealth <= 0) {
        Die(aGame);
        // dead so end of path,
        foundMove=ETrue;
      }      
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineAddHealth:
      iHealth += aPaths[(iPath*4)+2];
      // if health has got to zero die
      if(iHealth <= 0) {
        Die(aGame);
        // dead so end of path,
        foundMove=ETrue;
      }      
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineSetVelocity:
      iXVel=aPaths[(iPath*4)+2];
      iYVel=aPaths[(iPath*4)+3];      
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineAddVelocity:
      iXVel+=aPaths[(iPath*4)+2];
      iYVel+=aPaths[(iPath*4)+3];      
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineSetSprite:
      SetSpriteNo(aPaths[(iPath*4)+2]);
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineSetClearFlags:
      iFlags |= aPaths[(iPath*4)+2];
      iFlags &= ~(aPaths[(iPath*4)+3]);
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineSetCounter:
      iCounter = aPaths[(iPath*4)+2];
      iPath+=aPaths[(iPath*4)+1];
      break;
    case EPathLineAddCounter:
      iCounter += aPaths[(iPath*4)+2];
      iPath+=aPaths[(iPath*4)+1];
      break;

    }
  }
}

/** Simple movement.
    
    Uses iFlags to determine whether to home in on the player, and whether
    to bounce against the sides.

    \param aPlayerX players X position.
    \param aPlayerY players Y position.

*/
void TBadGuy::SimpleMove(TInt16 aPlayerX, TInt16 aPlayerY) {
  if(iFlags & TBadGuyFlags::EHomingX) {
    if(iX < aPlayerX) {        
      iX += iXVel;
      if(iX > aPlayerX)
        iX=aPlayerX;
    } else if(iX > aPlayerX) {
      iX -= iXVel;
      if(iX < aPlayerX)
        iX=aPlayerX;
      }
  } else {
    iX+=iXVel;
    if(  (iX>303 && (iFlags & TBadGuyFlags::EBounceRight)) ||
         (iX<16 && (iFlags & TBadGuyFlags::EBounceLeft))) {
      iXVel= -iXVel;
      iX+= iXVel;
    }  
  }

  if(iFlags & TBadGuyFlags::EHomingY) {
    if(iY < aPlayerY) {        
      iY += iYVel;
      if(iY > aPlayerY)
          iY=aPlayerY;
    } else if(iY > aPlayerY) {
      iY -= iYVel;
      if(iY < aPlayerY)
        iY=aPlayerY;
    }
  } else {
    iY+=iYVel;
    if((iY<0 && (iFlags & TBadGuyFlags::EBounceTop)) ||
       (iY>176 && (iFlags & TBadGuyFlags::EBounceBottom))) {
      iYVel= -iYVel;
      iY+= iYVel;
    }            
  }
}

/** Handle collision between badguy and bullet.

    This can damage either, both or neither of the bad guy and the bullet.

    \param aBullet bullet that bad guy was plotted over
    \param aGame used to damage other bad guys if this is a multipart bad guy.

*/

void TBadGuy::HitBullet(TBullet& aBullet, CGame &aGame) {
  if(iHealth<=0)
     return;

  if(iFlags & TBadGuyFlags::EInvunerable) {
    // these just absord bullets
    aBullet.ClearSprite();
  } else if(iFlags & TBadGuyFlags::ETransparent) {
    // has no effect on bullets and vice versa
  } else {
    // damage done can't be more than bad guys health,
    // damage done is subtracted from bad guys health and bullets health
    TInt damageTaken=aBullet.Health();
    if(damageTaken > iHealth)
      damageTaken=iHealth;

    if(damageTaken<0) {
      TGamePanics::Panic(TGamePanics::ENegativeDamage);
    }
    
    aBullet.AddHealth(-damageTaken);

    if(iId > 0) {
      aGame.DamageBadGuys(iId,damageTaken);
    } else if(iId < 0) {
      // negative ids don't get damaged when other parts get hit
      // so damage this one directly,
      aGame.DamageBadGuys(-iId,damageTaken);
      Damage(damageTaken,aGame);
    } else {
      Damage(damageTaken,aGame);
    }
  }
}

/** Handle collision between bad guy and players ship.

    Either damage the ship, or collect the bonus depending
    on type of bad guy.

    \param aShip players ship, used to add/remove health
    \param aGame used to add lives, powerups etc.

*/
void TBadGuy::HitShip(TPlayersShip &aShip, CGame &aGame) {
  if(iHealth<=0)
     return;

  if(iFlags & TBadGuyFlags::EKillsPlayer) {
    if(iFlags & TBadGuyFlags::EInvunerable) {
      aShip.SetHealth(0);
    } else {
      aShip.AddHealth(-iHealth);
    }

    // if this is a multi part bad guy we need to damage the other
    // parts.
    if(iId > 0) {
      aGame.DamageBadGuys(iId,iHealth);
    } else if(iId < 0) {
      // negative ids don't get damaged when other parts get hit
      // so damage this one directly,
      aGame.DamageBadGuys(-iId,iHealth);
      Damage(iHealth,aGame);
    } else {
      Damage(iHealth,aGame);
    }

    
  } else {
    if(iFlags & TBadGuyFlags::EHealthBonus) {
      aShip.AddHealth(iHealth);
    }
    if(iFlags & TBadGuyFlags::ELifeBonus) {
      aGame.Bonus(CGame::EBonusLife);
    }
    if(iFlags & TBadGuyFlags::EWeaponBonus) {
      aGame.Bonus(CGame::EBonusPowerup);
    }
    Die(aGame);
  }
}

/** Damage done to the bad guy.

   \param aHealth amount of damage done, if iHealth <= 0 bad guy dies.
   \param aGame Used to add score if the bad guy had a score.

*/
void TBadGuy::Damage(TInt aHealth, CGame &aGame) {
  iHealth-= aHealth;
  if(iHealth <=0) {
    Die(aGame);
  }  
}

/** Death of a bad guy.

   Things that kill the player give the player points and blow
   up, other things just disappear.

   \param aGame used to increase players points

*/
void TBadGuy::Die(CGame &aGame) {
  iHealth=0;
  if(iFlags & TBadGuyFlags::EKillsPlayer) {
    aGame.AddScore(iScore);
  } else {
    ClearSprite();
  }
}

/** Get iId .

   \return iId multipart id
*/

TInt16 TBadGuy::Id() {
  return iId;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?