Sign in to follow this  
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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627636
    • Total Posts
      2978331
  • Similar Content

    • By Strifexxx
      Hi guys, check out our new game about sticks. Feedback welcome!
      Download on Google Play: https://play.google.com/store/apps/details?id=com.stickman.annihilation.dismount
       
      Youtube: 

       
      Stickman Dismount 2 Annihilation is a sequel to the legendary game of survival, where to make incredible tricks, driving different transport and getting into different crash! The game is made in the best traditions of simulator games with ragdoll physics elements. Make incredible jumps and tricks and destroy the enemy! Your task is make the finish alive or dead! 
       





      Website: http://strifexxx.wixsite.com/cyberpony
      Facebook: https://www.facebook.com/groups/577850349043463/
      VK: https://vk.com/club122273704
    • By Knife Entertainment

      New indie game development studio Knife Entertainment expects to release their debut project My Little Story: Reborn.

      Reborn is the first-person horror containing the spirit of the occultism and mystery. We decided to give players something, which they miss so much in games of this genre: you don't have to hide in dark corners trying to escape from some invulnerable monster. It will be vice versa. We gave players everything to make even the most savage and malicious enemy tremble with fear. You'll face the cruel world full of danger, to expose dark secrets and to fight with ancient evil.

      Our principal credo is our great attention to various details. That's why there are a lot of easter eggs in the form of references and visual quoting of such cult films, games and art works, as The Evil Dead, True Detective, Silent Hill, etc. The release is scheduled for the end of this year.
      Contact us: knifeent@gmail.com
       
       






       
    • By ilovegames
      Have you ever dreamed of fighting with dinosaurs? If so, then this game is for you. Hold out against the bloodthirsty prehistoric lizards for as long as possible. Seize more weapons, including a shot gun, sword and other stuff. Good luck!
      Download https://falcoware.com/DinoSurvival.php



    • By INTwindwolf
      COMPANY AND THE PROJECT
       

      We are an indie game studio consisted of professional and friendly people. Additionally, we are a team of skilled artists and dedicated indie enthusiasts. Our current project is INT, developed on Unity Engine 5 for platforms Windows, Linux, and Mac.
      INT is a 3D Sci-fi RPG with a strong emphasis on story, role playing, and innovative RPG features such as randomized companions. The focus is on the journey through a war-torn world with fast-paced combat against hordes of enemies. The player must accomplish quests like a traditional RPG, complete objectives, and meet lively crew members who will aid in the player's survival. Throughout the game you can side and complete missions through criminal cartels, and the two major combatants, the UCE and ACP, of the Interstellar Civil War.
      Please note that all of our current positions are remote work. You will not be required to travel.
      For more information about us, follow the links listed below.
      INT Official website
      Steam Greenlight
      IndieDB page
      Also follow social media platforms for the latest news regarding our projects.
      Facebook
      Twitter
       
      TALENTS NEEDED
       
      Animation Rigger
      Community Manager
      Website Administrator
       
      REVENUE - SHARE
       
      This is the perfect opportunity to get into the game development industry. Being an Indie team we do not have the creative restrictions often imposed by publishers or other third parties. We are extremely conscientious of our work and continuously uphold a high level of quality throughout our project.
      We are unable to offer wages or per-item payments at this time. However revenue-sharing from crowd-funding is offered to team members who contribute 15-20 hours per week to company projects, as well as maintain constant communication and adhere to deadlines. Currently the crowd-funding campaign is scheduled for the year 2018. Your understanding is dearly appreciated.
       
      Thank you for your time! We look forward to hearing from you!
       
      John Shen
      HR Lead
      Starboard Games LLC
    • By Misantes
      Hey everyone
      I've hit a milestone for a side-project of mine I've been working on for some time. It's been a frustrating labor of love, but reached my version 1.0 mark recently
      The asset can be used to create procedural music configurations that are editable in real time in a game. You're able to edit dozens of variables like the scale, mode, tempo, instruments, effects and dozens of other settings to react and adapt to gameplay or other events (or just use it as an easy tool to custom make music for your game). 
      It's available on the unity asset store here: https://www.assetstore.unity3d.com/#!/content/99791
      A demo is available on my github page: https://stickandbindlegames.github.io
      Video Link-Main: https://youtu.be/kDLc6GFdZTk
      Video Link-Presets showcase: https://youtu.be/dcVjY5rkUqU
      Video Link-Features Overview: https://youtu.be/TGQM11iRRUQ
      Video Link-Chiptune example: https://youtu.be/P3BNIbOhSfU
      It's still in active development, despite this release, so I'd be very interested in any feedback ( good or bad), advice, or suggestions for what may make it more appealing to you as developers. It'd be useful to know what features would make it more useful to you as a developer, or feedback on any aspect of the player, really, as I have a few directions I can take the generator for future updates.
      Thanks for your time! I look forward to anyone's thoughts :).

  • Popular Now