Skip to main content
gamedev.net
🔒 Locked

Weapon class structure

Started by XTAL256 Mar 5, 2008 at 4:05 AM 14 replies 10.3k views
Original Post
XTAL256
XTAL256
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]
[Window Detective] - Windows UI spy utility for programmers
speciesUnknown
speciesUnknown
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.

Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
XTAL256
XTAL256
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.
[Window Detective] - Windows UI spy utility for programmers
Sandman
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.

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.
Raghar
Raghar
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)

Shakedown
Shakedown
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.
XTAL256
XTAL256
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]
[Window Detective] - Windows UI spy utility for programmers
XTAL256
XTAL256
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 LINEARbool continuous;  // True for laser, machine gun, etc. False for missile, etc.

In the future i may use &#106avascript so i can make custom code for each weapon but for now i won't worry about it.
[Window Detective] - Windows UI spy utility for programmers
XTAL256
XTAL256
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?
[Window Detective] - Windows UI spy utility for programmers
jumpingrock
jumpingrock
I dunno much about C++ but I'm pretty sure you can use interfaces. I'd probably go with an interface idea. Such as:
class Weapon{IBullet bullet;}interface IBullet{CalculateTrajectory();}class LaserBlast : IBullet{CalculateTrajectory(){}}class ShotGunSlug : IBullet{CalculateTrajectory(){}}class ShotGun : Weapon{public ShotGun(){ bullet = new ShotGunSlug();}}

etc...

[Edited by - jumpingrock on March 7, 2008 12:48:43 PM]
Shakedown
Shakedown
If you're posting code, you should put your source in ['source']['/source'] tags (remove the all the ').

If you're thinking of an interface like in Java, C++ does not have the same thing. Well, C++ does have interfaces, but they're just classes with all functions pure virtual.
XTAL256
XTAL256
Well, yes, i would have used 'interfaces' if i were to do it that way. But since, like you said, "they're just classes with all functions pure virtual" i didn't bother mentioning such details.
And anyway, i have decided what i will do. I will just hard-code how a 'weapon' works by coding each case (projectile & linear). After i have finished the game i will implement a simple scripting system and use &#106avascript to code specific weapons, i.e. maybe an EMP gun that disables all players for a short while. Such a weapon would have to target <i>all</i> players so i would need custom code for it. But for now i will just settle with limited weapon types.
[Window Detective] - Windows UI spy utility for programmers
Mark Bevels
Mark Bevels
On 05/03/2008 at 5:26 AM, Sandman said:

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
}

Thanks a lot @Sandman. That is exactly what I needed. Data file driven approach is way more appealing than nested OOP structure.

Tom Sloper
Tom Sloper

please don't necro 9-year-old threads, okay? putting it back to sleep.

-- Tom Sloper    --      sloperama.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.