Sign in to follow this  

Weapon Class

This topic is 4342 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

Hey, I'm writing my weapon class for my new game and I wanted to show you all my ideas and maybe get some input. The idea is to build up a rocket launcher type weapon from the simplest and most defined up to the most general. So: Lets say I start with a projectile structure that includes information like:
Xpos, Ypos, Xvel, Yvel, and Sprite
Ok so now I make a Weapon class that has the following:
1) Fire() Method 2) an Update() Method to keep track of all the rockets positions and states. 3) an pool of rockets (with defined MAXROCKET limit to simulate a 'clip') 4) all the Xpos, Ypos, etc.
And finally, I can create an instance of my WEAPON class in my PLAYER class so that the player can use the weapon. Just trying to get my thoughts out on the page. Thanks for any critiques or advice. toXic

Share this post


Link to post
Share on other sites
You might also want to consider to have another set of coords, xoff and yoff, which will be the offset from the weapon position, where the projectile will be created and start from.

It doesn't make sense for all weapons to just have a projectile created right in the middle of it.

Share this post


Link to post
Share on other sites
Yeah that was something I was considering putting in the Fire() method. The position of the projectile would also be dependent on which direction the weapon was facing, which is something else I need to take into consideration.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
a good idea might be to make the projectile a class aswell. and let it have its own update methods, damage rating, etc.
thus the weapon simply creates a new projectile at a set position and direction, and possibly sets the starting speed. after that your game can just treat the projectile as any other entity.

a good idea might be to have a base entity class aswell that has an abstract update method, and stores the position etc. then the game simply has to loop through a list of entities, run their update methods and so on.
the projectile class would then inherit from entity, the player and enemies would ofcourse also inherit from the entity class. and ofcourse you can push it even further and have a rocket class that inherits from projectile and so on.

Share this post


Link to post
Share on other sites
i did an attack class and i just did

#define playerleft 1
#define playerright 2
#define playerup 3
#define playerdown 4


then like

if(Player.Status == playerleft)
{
rocket.x += xVel;
}
else if(Player.Status == playerright)
{
rocket.x -= xVel;
}

Share this post


Link to post
Share on other sites
That's a good start. Let me make a couple of points :
I think that a weapon should be derived from a more basic class that only has update, create, destroy.
why? : It lets you later derive a Rocket class and have a Fire() method ,-it shoots .., but more importantly, you can derive somthing like Sword without Fire() but maybe with Swing(), or Rock with Throw().

So what it boils down to is, it takes 2 minutes to code a most basic base class, or forget about other weapons, and have firing weapons ...

If you decide to split them apart , i would suggest:

class Thing {
private:
posx, y, z ...
public:
constructor, destructor ...
Init();
Update();
Destroy();
}

class Projectile derived from Thing {
velx, vely, velz ...
damage, sprite, other stuff
public:
BlowUp();
....
}

class Weapon derived from Thing{
array of Projectiles ...
public:
fire();
....
}

This design has issues, but it does split up the major differences while keeping it quite simple.

g'd luck

Share this post


Link to post
Share on other sites
Well, after organizing this on paper, it really only took about 10 minutes to write the code. I think I've found the key to sound code design... haha. Anyways, here's the product of my work thus far. A working example of a weapon and it's ability to fire a projectile.

Press "A" to fire!

Have fun!

DOWNLOAD

Share this post


Link to post
Share on other sites

This topic is 4342 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