Sign in to follow this  

Classes in Classes?

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

Yo. Say if you have a shooter game, where you have several weapons with different effects. So you have to decide how to manage this in a fairly clean way. Well, would it work to use a class that also uses another class? I do not mean a derived class. Here is an example: There's a standard 'player' class. This will include animation, interation, and what have you. Now you have a weapons class. it also controls the animations and effects of the gun. Now you have the projectile class. This gives the bullets an effect as they impact each surface. So, is it possible to manage the shooter game that way? If not, how else can this be acomplished?

Share this post


Link to post
Share on other sites
I do believe that would be inheritance. Someone else should fill you in with enough information.

// EDIT: I'm new to C++, or fairly new, so I can't answer.

Share this post


Link to post
Share on other sites
Well i have never written an FPS, but i'll throw some ideas around.

I would have a player class that holds the information about what gun etc. Then i would have a collection of these players in something like a 'game analyser' class. This would look through all the entities in the game and depending on what ever stimulai, like input, ai etc, alters the players attributes, like if the up key is pressed change the location coords. Have this collection referencable by a renderer so that it can do all the transformations and display the entities.

Typically in professional engines, the 'player', be it AI or human, would become a client to an in-game server. Then the server would 'refresh' each client, i would have thought.

Hope this helps, although im not knowledgable with FPS.

ace

Share this post


Link to post
Share on other sites
What you are describing is roughly "composition", that is objects that contain other objects as componant parts. It's not true composition, but yes, it would work for a decent model. If you compartmentalize each different object, you have more control over changes and a more flexible design overall. If you have different classes for players, weapons, projectiles (not all weapons have projectiles, now do they?), etc, you can subclass those things to create new objects in the future.

Share this post


Link to post
Share on other sites
Well, I have no idea how to actually invoke animations, or any type of graphical elements, except with the Visual Studio Template stuff. It's only because Direct-X is so freakin hard. All the tutorials move fast-pace and rarely explain line by line what the code means or how it works.


Anyways, I'll look into other methods for the battle system later tonight.

Share this post


Link to post
Share on other sites
hl1 did it they way you explained above

CBaseWeapon is derived from CBaseEntity ....
class Canyweapon: public CBaseWeapon
{
public:

CBaseEntity * m_pOwner;
}



so you have the actual entity and you have a pointer to the owner *the player that carries the weapon

in order to animate the player you tell them which animation to play

....

its actually pretty simple

each entity has a method that sets up the animations to play *interpolate between current and last frame to make the animation smooth*
you usually animate with 30 FPS

just check out the hl1 sourcecode

Share this post


Link to post
Share on other sites
Well, I would go something like the above route as did what Half-Life 1 and Half-Life 2 did (you need to have HL2 in order to get the SDK). I'm most likely going to have a basic object/entity for the game and then devrive everything from that. This is also somewhat like what the Unreal engine (Unrealscript) does; everything (usually) is devrived from a base object.

As for affects on the weapons, there is any number of methods that you could do. You could have an array of objects "IAffectors" that can be added or removed from an object. There could really be any number of ways to do something like this:

class IAffector {
public:
virtual void DoAffect(void)=0; // pure virtual
private:
int m_iFlags;
};

class CAffectors : public std::vector<IAffector*> {
..
};

class CHandgun : public CBaseWeapon {
...
private:
CAffectorList m_Affectors;
};

Somefunc() {
for( list of affectors ) {
if( p->flag & whatever ) {
p->DoAffect()
}
}
}


You would make each affect a class and have a specific "DoAffect" for each; also flags could be, let's say, "IS_NPC", "IS_PLAYER", "IS_RUNNING", etc. Just something I would think about. If you wanted to get a bit more in depth, you might want to look into a scripting language that you could script events (or affects, spells, etc) on objects. I'm looking into Angelscript right now, I believe you can find that somewhere on Angelcode.com.

Hope that helped.

Share this post


Link to post
Share on other sites
I think you can just use a function pointer to establish a different firing effect for a different gun. So you would only have one gun class, but each instance of that class would recieve a pointer to a function that would be called when the fun fires and one that governs how the gun is drawn. I've never tried anything like that, but I think the theory is sound. Anyone care to refute?

The simple method would just be to set your firing method as a virtual function and derive a class for each different weapon type. Although this might be a good route if you're going to have multiple actors weilding these guns. Using the above method, you would have to instantiate a gun and then point it to the function(s) that handle the gun specifics. If you use inheritence, you can just create an instance of that gun type. I think I like the inheritence idea better, now that I think about it.

Share this post


Link to post
Share on other sites

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