Jump to content
  • Advertisement
XTAL256

Unity Weapon class structure

Recommended Posts

Hi, i am working on a game (Ballistic) and i am having trouble with the Weapon class diagram/structure. I have a Weapon class which is the parent class of all weapon classes. The main concern i have is with the bullets that are fired out of the weapon. I am going to have many different weapons but they will all fire different bullets. Some fire missiles which will follow a parabola, while others are linear (machine gun, laser). So i have two subclasses; ProjectileWeapon and LinearWeapon. Then each weapon extends from that. But i don't know if i should use the Weapon class or the Bullet class. That is, the bullet is the object that actually fires. All weapons are the same, it is the bullets they fire that is different. So should i do it that way or have different Weapon classes which is responsible for calculating bullet trajectory, etc? Here is a class diagram for each solution: thanx [Edited by - XTAL256 on March 5, 2008 4:50:38 AM]

Share this post


Link to post
Share on other sites
Advertisement
Arn't the projectiles themselves the candidates for sub classes? I have class 'object' which is inherited by class 'projectile_weapon' and class 'melee_weapon', and that's where it ends. The projectile weapons are aware of what projectile they spit out, at what rate, and a few other properties. I haven't hard coded my weapons.

Share this post


Link to post
Share on other sites
Quote:
Original post by speciesUnknown
I haven't hard coded my weapons.

Right, well i was thinking of ways to create weapons without hard-coding (scripting) but thought it would be easier just to hard code it in.
And what do you mean when you say "projectiles themselves are the candidates for sub classes"? Projectile weapons inherit from Weapon and distinct projectile weapons inherit from ProjectileWeapon (or ProjectileBullet, whichever way i decide). I want each weapon a subclass so i can write custom code in each. I dunno, maybe i won't need to. It depends on how different each type of weapon is going to be.

Share this post


Link to post
Share on other sites
Firstly, this isn't quite the right forum. Game Design is more about the gameplay rules and mechanics, rather than technical code design. Moving to Game Programming.

Secondly, I think you need to think carefully about the granularity of your class hierarchy.


class Ammo
{
public:
virtual void CalculateTrajectory();
virtual void DoDamage();
};
class Weapon
{
public:
Weapon(Ammo&);
virtual void Fire();
virtual void Reload();
virtual Ammo GetAmmoType();

private:
Ammo mAmmoType;
};

class LaserBeam : public Ammo
{
// blah
};
class LaserGun : public Weapon;
{
// blah
};

class Missile : public Ammo
{
// blah
};

class Missile Launcher : public Weapon
{
// blah
};






This is OO gone mad. And it's simpler than the approach you've proposed because I've left out the LinearWeapon and ProjectileWeapon intermediate classes.

You don't need a class for Ammo at all. And you don't need a separate class for each different weapon type, let alone each different weapon.
Something more like:


class Weapon
{
public:
enum eWeaponType
{
WEAPONTYPE_LINEAR,
WEAPONTYPE_PROJECTILE
};
void Fire();
void Reload();
void CalculateTrajectory();
void DoDamage();

private:

uint mMaxRange;
uint mCooldown;
uint mAmmoMass;
uint mAmmoSpeed;
ParticleEffect* mpFireEffect;
ParticleEffect* mpHitEffect;
// etc
}





This way you have a single weapon class that does everything you need for every weapon. The different weapon parameters can be loaded from a file, so almost nothing is hardcoded. If you want to add a new weapon type, instead of having to derive a whole bunch of new classes, you can just add a new entry into your weapons data file.

Share this post


Link to post
Share on other sites
Quote:
Original post by XTAL256
So should i do it that way or have different Weapon classes which is responsible for calculating bullet trajectory, etc?


Calculation of a trajectory is a problem of a game/3D/physics/world engine, not a data class. Thought you might append trajectory object to each bullet. (You know if a long barrel weapon would change trajectory, you'd be at safe side.)~_^

resolveHit(ammo, trajectory)

Share this post


Link to post
Share on other sites
In my shooter, I've got a Weapon hierarchy and a Projectile hierarchy, and my Weapons create and shoot Projectiles when they're fired. It seems to work out pretty nice. This allows for my Weapons to focus on shooting whatever ammo is loaded into them (as long as it is appropriate), and the Projectiles can focus on what they do best: damage! And flying, or "projecting," of course.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sandman
Firstly, this isn't quite the right forum. Game Design is more about the gameplay rules and mechanics, rather than technical code design. Moving to Game Programming.

Right, thanks. I didn't know if this was the right forum but i thought Game Programming was more for actual code rather than class design and stuff.

First of all, i think i would need a Bullet class because this is the thing that is going to hit the target and my event system needs two objects when it detects a collision.

@Sandman: yes, your first way is a bit crazy but i was honestly thinking about doing it that way for a second. Although I would much rather do it the second way, with only one Weapon and one Bullet class. And for projectile/linear i could just have a mass variable which is 0 for linear. But i may want to code each weapon differently. For example, a laser will have a continuous stream while a missile fires a single bullet.

@Raghar: yes the trajectory will be done in a physics component. But how it will be done will be in the Weapon or Bullet class.

[Edited by - XTAL256 on March 6, 2008 6:26:26 PM]

Share this post


Link to post
Share on other sites
Ok, i have decided to use just one Weapon and Bullet class and hard-code the projectile and linear types in. A Weapon will have this:

int type; // Either PROJECTILE or LINEAR
bool continuous; // True for laser, machine gun, etc. False for missile, etc.

In the future i may use javascript so i can make custom code for each weapon but for now i won't worry about it.

Share this post


Link to post
Share on other sites
Quote:
Original post by XTAL256

int type; // Either PROJECTILE or LINEAR


This should be an enum, enum WeaponType {PROJECTILE, LINEAR}.

Share this post


Link to post
Share on other sites
Yes, i will probably use enum. I don't really use enum as much as i should.

off topic: how do i include my sig in a post without constantly clicking "Check here to include your profile signature."? Is there an option in the control panel?

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!