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
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
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
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 javascript 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 all players so i would need custom code for it. But for now i will just settle with limited weapon types.

Share this post


Link to post
Share on other sites
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.

Share this post


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

  • Forum Statistics

    • Total Topics
      628642
    • Total Posts
      2983998
  • Similar Content

    • By arash khalaqhdoust
      hey guys i hope you doing all well. last night i released my first game in google app store, i really appreciate you guys  to download it. and share your reviews about it
      the idea of game comes from mini hackgame of Bioshock.
       link of download:
      https://play.google.com/store/apps/details?id=com.RVBinary.piperist
      many thanks
    • By ForgedInteractive
      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! Currently, we're progressing very quickly with our first project and there are plenty of opportunities and work for new interested programmers. With this project, our development platform is Unity 5.5.2 and C# as our behavioral language. Since this project is our first release, the game itself is a smaller project though progress is moving quickly. We are looking to finalize the current project and get started on future projects in the near future and are expanding our team to do so.
       
      Who We Are Looking For:
      Programmer Level Designer  
      About the Game
      Ours is the tale of two siblings, thrown into a world of chaos. Living in the shadow of their parents' heroic deeds and their Uncle's colorful military career, Finn and Atia are about to become the next force to shape our world. How will you rise through the ranks of Hereilla and what will be your legacy? Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.
       
      Previous Work by Team
      Although we are working towards our first game as Forged Interactive, our team members themselves have worked on titles including and not limited to:
      Final Fantasy Kingsglaive FIFA 2017 Xcom 2 Civilization  
      What do we expect?
      Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
       
      What can we offer?
      Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
       
      Contact
      If you are interested in knowing more or joining, please email or PM us on Skype. A member of our management team will reply to you within 48 hours.
       
      E-mail: Recruitment@ForgedInteractive.com
      Skype: ForgedInteractive
       
      Regards,
      David, Colin and Joseph
       
      Follow us on:
      Facebook: https://www.facebook.com/ForgedInteractive/
      Twitter: @ForgedInteract
      Youtube: https://www.youtube.com/channel/UCpK3zhq5ToOeDpdI0Eik-Ug?view_as=subscriber
      Reddit: www.reddit.com/user/Forged_Interactive

    • By dell96
      I'm trying to make my first project but I'm stuck i don't know how to make my crate to start to spawn again when i hit the start button after i die.
      hoping someone can help!!!
      Crate.cs
      CrateSpawn.cs
      Cratework.cs
      GameController.cs
      GameManager.cs
  • Popular Now