Sign in to follow this  

Weapon/Projectile Classes

This topic is 3846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a new 3D space rpg type thing and I'm trying to write the weapon handlers for it and I am having trouble with it. Can anyone suggest a relatively simple way to create and handle instances of specific weapon and projectile types? The general way this needs to work is: A ship has a number of weapons (with a certain position and orientation relative to the parent ship) of a certain type which, when fired, create an instance of a projectile of a certain type which then acts like a normal projectile should -- flies and collides (and consequently explodes if need be) with whatever is in its way.

Share this post


Link to post
Share on other sites
I would suggest creating a few classes to handle that kind of thing. First off you probably want to create some sort of weapons handler class to hold your weapons. It could hold a linked list of weapons or a vector and then just make some functions to add and delete weapons, possibly have primary and secondary weapon data structures. Next you'll have to create an actual weapon class which will have a few virtual functions so it can be polymorphic. Just create the most basic functions and variables which all weapons will share in that class. Next you'll want to make your actual weapon classes, such as a laser class or a bomb class and they'll be inheritance classes of the weapon class. So when your weapons handler class makes a call such as primaryFire(), then it'll call the fire function of the weapon class, which would be virtual and in turn call the fire function of the correct weapon.

In terms of projectiles I'd suggest having something similar, like a projectile handler class that will deal with all projectiles in the world at any given time. It will deal with calling the update and render functions for the projectiles. It would probably be best to make it a singleton class. Then make a generic projectile class which has the most basic functions and variables which all projectiles would share and then make your specific projectiles which will be inheritance classes so you can add them to your projectile handler data structure. Below is a basic example of what I mean.


class CWeaponHandler
{
public:
//constructor
//destructor

void addWeapon(CWeapon *weapon);
void firePrimary();
void fireSecondary();
void selectWeapon(int id)

//etc
private:
vector<CWeapon*> primary;
vector<CWeapon*> secondary;
};

class CWeapon
{
public:
//constructor
//destructor

virtual void fire() { return; } // pure virtual

private:
int damage;
int ammo;
int clip;
//etc
};

class CLaser : public CWeapon
{
public:
//constructor
//destructor

void fire();
void reload();


private:
float r,g,b; // laser color
};


So that's a basic weapons system. Now here's an example of a singleton class that you could use for the projectile system

class CProjectileHandler
{
public:
//constructor
//destructor

// call CProjectileHandler::instance()-> to access all public functions and variables from anywhere.
static CProjectileHandler *instance();

void addProjectile(CProjectile *projectile);
void update(); // update all active projectiles, also check for collisions
void render(); // render all active projectiles

//etc
private:
static CProjectileHandler *inst;
vector<CProjectile*> projectiles;

};

class CProjectile
{
public:
//constructor
//destructor

virtual void update() { return; } // pure virtual
virtual void render() { return; } // pure virtual

private:
vector3 position;
vector3 velocity;
vector3 acceleration;

//etc
};

class CLaser : public CProjectile
{
public:
//constructor
//destructor

virtual void update() { return; } // pure virtual
virtual void render() { return; } // pure virtual

private:
vector3 position;
vector3 velocity;
vector3 acceleration;

//etc
};

That's how I'd set it up. It also makes it incredibly easy to reuse in pretty much any application. Also, if only one place is going to use the weapons handler, say, only the main player can hold various weapons, then you could make that a singleton class. Generally I just create an instance of the class in my player class to store the weapons but either way works and if you have to access the weapons handler from somewhere other than your player class it would be easier to use a singleton, assuming it's only used for that one system. I hope that helps.

Share this post


Link to post
Share on other sites
wow, thanks a lot! I'll try to work up something like that and see if it is sufficient for the application. The only major modification i'll have to make is that I don't plan to have a central weapon handler function (perhaps I am misunderstanding). I plan for the firing of weapons to be a function associated with the AI of each ship. Each ship object will contain a list of weapons on the ship, and the AI will iterate through each weapon and determine whether or not to fire, etc. A Big important thing is that the weapon instance will have a specific location and orientation which it will pass to the projectile instances it creates as initial values.

(I'm probably just over thinking this horribly... my first attempt at writing these classes (from a couple months ago) is a 419 line mess. But nonetheless, you've definitely clarified the problem a lot.

Share this post


Link to post
Share on other sites
Well if each of your AI classes are going to have a choice of weapons then you can easily just make a CWeaponHandler variable in each class. Do your check before you call the fire function of the CWeaponHandler variable. Then that'll call the selected weapon's fire function, which would create a projectile and add it to the CProjectileHandler, which would then do all the necessary rendering and checks for collisions.

Share this post


Link to post
Share on other sites

This topic is 3846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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