Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBeerNutts

Posted 22 August 2012 - 01:04 PM

Let me suggest something a little different than what webdevap... has.

You should not have the bullet list in the Player class. It should be a global list (or held in your Game class if you have one). That bullet list should be able to contain bullets fired by the player or by enemies.

Also, you might want to inherit from a Super class for any object that has physical properties (X, Y, Height, Width, Rotation, Image, and X and Y Velocities).

Something like this (in pseudo-code, only listing the members and some functions)
class PhysicalObject
{
  float xPos;
  float yPos;
  float xVelocity;
  float yVelocity;
  float Angle;
  int Height;
  int Width;
  Image ObjectImage; // or maybe you have an animation class, if it animates

  void Update()
  {
    xPos += xVelocity;
    yPos += yVelocity;
  }

  void Draw()
  {
    ScreenDraw(ObjectImage, xPos, yPos);
  }
};

class Bullet : public PhysicalObject
{
  int Damage; // how hard it hits
  bool IsPlayerOwner; // determines if it's a player bullet or enemy
  int TimeToLive; // how long the bullets stays alive
};

Then, when ever anything fires a bullet, you create Bullet, add it to a list. In your Game Update, you loop through the BulletList, move the bullets, and check for collisions. If it collides, you apply the damage and remove it from the list.

Good Luck!

#1BeerNutts

Posted 22 August 2012 - 12:58 PM

Let me suggest something a little different than what webdevap... has.

You should not have the bullet list in the Player class. It should be a global list (or held in your Game class if you have one). That bullet list should be able to contain bullets fired by the player or by enemies.

Also, you might want to inherit from a Super class for any object that has physical properties (X, Y, Height, Width, Rotation, Image, and X and Y Velocities).

Something like this (in pseudo-code, only listing the members)
class PhysicalObject
{
  float xPos;
  float yPos;
  float xVelocity;
  float yVelocity;
  float Angle;
  int Height;
  int Width;
  Image ObjectImage; // or maybe you have an animation class, if it animates
};

class Bullet : public PhysicalObject
{
  int Damage; // how hard it hits
  bool IsPlayerOwner; // determines if it's a player bullet or enemy
  int TimeToLive; // how long the bullets stays alive
};

Then, when ever anything fires a bullet, you create Bullet, add it to a list. In your Game Update, you loop through the BulletList, move the bullets, and check for collisions. If it collides, you apply the damage and remove it from the list.

Good Luck!

PARTNERS