• Advertisement

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

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
    • By Vu Chi Thien
      Hi fellow game devs,
      First, I would like to apologize for the wall of text.
      As you may notice I have been digging in vehicle simulation for some times now through my clutch question posts. And thanks to the generous help of you guys, especially @CombatWombat I have finished my clutch model (Really CombatWombat you deserve much more than a post upvote, I would buy you a drink if I could ha ha). 
      Now the final piece in my vehicle physic model is the differential. For now I have an open-differential model working quite well by just outputting torque 50-50 to left and right wheel. Now I would like to implement a Limited Slip Differential. I have very limited knowledge about LSD, and what I know about LSD is through readings on racer.nl documentation, watching Youtube videos, and playing around with games like Assetto Corsa and Project Cars. So this is what I understand so far:
      - The LSD acts like an open-diff when there is no torque from engine applied to the input shaft of the diff. However, in clutch-type LSD there is still an amount of binding between the left and right wheel due to preload spring.
      - When there is torque to the input shaft (on power and off power in 2 ways LSD), in ramp LSD, the ramp will push the clutch patch together, creating binding force. The amount of binding force depends on the amount of clutch patch and ramp angle, so the diff will not completely locked up and there is still difference in wheel speed between left and right wheel, but when the locking force is enough the diff will lock.
      - There also something I'm not sure is the amount of torque ratio based on road resistance torque (rolling resistance I guess)., but since I cannot extract rolling resistance from the tire model I'm using (Unity wheelCollider), I think I would not use this approach. Instead I'm going to use the speed difference in left and right wheel, similar to torsen diff. Below is my rough model with the clutch type LSD:
      speedDiff = leftWheelSpeed - rightWheelSpeed; //torque to differential input shaft. //first treat the diff as an open diff with equal torque to both wheels inputTorque = gearBoxTorque * 0.5f; //then modify torque to each wheel based on wheel speed difference //the difference in torque depends on speed difference, throttleInput (on/off power) //amount of locking force wanted at different amount of speed difference, //and preload force //torque to left wheel leftWheelTorque = inputTorque - (speedDiff * preLoadForce + lockingForce * throttleInput); //torque to right wheel rightWheelTorque = inputTorque + (speedDiff * preLoadForce + lockingForce * throttleInput); I'm putting throttle input in because from what I've read the amount of locking also depends on the amount of throttle input (harder throttle -> higher  torque input -> stronger locking). The model is nowhere near good, so please jump in and correct me.
      Also I have a few questions:
      - In torsen/geared LSD, is it correct that the diff actually never lock but only split torque based on bias ratio, which also based on speed difference between wheels? And does the bias only happen when the speed difference reaches the ratio (say 2:1 or 3:1) and below that it will act like an open diff, which basically like an open diff with an if statement to switch state?
      - Is it correct that the amount of locking force in clutch LSD depends on amount of input torque? If so, what is the threshold of the input torque to "activate" the diff (start splitting torque)? How can I get the amount of torque bias ratio (in wheelTorque = inputTorque * biasRatio) based on the speed difference or rolling resistance at wheel?
      - Is the speed at the input shaft of the diff always equals to the average speed of 2 wheels ie (left + right) / 2?
      Please help me out with this. I haven't found any topic about this yet on gamedev, and this is my final piece of the puzzle. Thank you guys very very much.
    • By Estra
      Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
      Looking for
      Programmer
      1 experience using Unity/C++
      2 have a portfolio of Programmer
      3 like RPG game ( Rune rune factory / zelda series / FF series )
      4 Have responsibility + Time Management
      and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
      Split %: Revenue share. We can discuss. Fully Funded servers and contents
      and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
      we can talk more detail in Estherfanworld@gmail.com Don't comment here
      Thank you so much for reading
      More about our game
      Memory Trees : forget me not

      Thank you so much for reading
      Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
      because If not it will waste time not one but both of us
       

  • Advertisement