Change TNT to throwing bombs

Version:

1.0

Added on:

23 Feb 2013 15:18

Tags:

Description:
Does what the title says. The bomb explosion is the same as a TNT explosion, so setting tntdamage or extended tnt range will also affect these bombs. You can change behaviour by changing the bomb constants at the top of the code.
//Bomb Constants
const float cBombXSpeed = 8; //Initial x speed
const float cBombYSpeed = -3; //Initial y speed
const float cBombPlayerSpeed = 6; //x speed that the player can add by running
const int cBombLifeTime = 140; //Life time of the bomb in gameticks (70 ticks = 1 second)

//i and Bullet are globally declared (if you use i or Bullet in your own code you can put i or Bullet in the main)
int i, Bullet;

//Returns the first bullet found of type BulletType shot by the player with playerID
int FindBulletID(int playerID, int BulletType) {
  int BulletID = -1;
  for (int i = 1; i < jjObjectCount; i++) {
    if (jjObjects[i].isActive && jjObjects[i].eventID == BulletType && jjObjects[i].creatorType == CREATOR::PLAYER && (jjObjects[i].creator - 32768) == playerID && jjObjects[i].state != STATE::EXPLODE) {
      BulletID = i;
      break;
    }
  }
  return BulletID;
}

//Creates a bomb
void CreateBomb(int playerID) {
  int bombID;
  bombID = jjAddObject(OBJECT::APPLE, jjPlayers[playerID].xPos, jjPlayers[playerID].yPos, jjPlayers[playerID].playerID, CREATOR::PLAYER);
  jjObjects[bombID].objType = 3;
  jjObjects[bombID].xSpeed = cBombXSpeed*jjPlayers[playerID].direction + cBombPlayerSpeed*jjPlayers[playerID].xSpeed/16;
  jjObjects[bombID].ySpeed = cBombYSpeed;
  jjObjects[bombID].determineCurAnim(ANIM::AMMO, 1, true);
}

//Explodes a bomb
void DestroyBomb(int objectID) {
  int bombID = jjAddObject(OBJECT::TNT, jjObjects[objectID].xPos, jjObjects[objectID].yPos, jjObjects[objectID].creator, CREATOR::PLAYER);
  jjDeleteObject(objectID);
  jjObjects[bombID].determineCurAnim(ANIM::AMMO, 1, true);
  jjObjects[bombID].killAnim = ANIM::AMMO;
  jjObjects[bombID].state = STATE::EXPLODE;
}

void onMain() {
  for (i = 0; i < 32; i++) {
    Bullet = FindBulletID(i, OBJECT::TNT);
    if (Bullet != -1) {
      jjDeleteObject(Bullet);
      CreateBomb(i);
    }
  }
  for (i = 1;  i < jjObjectCount; i++)
    if (jjObjects[i].isActive && jjObjects[i].creatorType == CREATOR::PLAYER && jjObjects[i].eventID == OBJECT::APPLE) {
      jjObjects[i].age++;
      if (jjObjects[i].age > cBombLifeTime)
        DestroyBomb(i);
    }
}