Game advice

This is a discussion on Game advice within the Game Programming forums, part of the General Programming Boards category; Hey, I'm working with a side scroller in SDL... things are flowing pretty smoothly, enemy behavior, levels, object interactions, but ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404

    Game advice

    Hey, I'm working with a side scroller in SDL... things are flowing pretty smoothly, enemy behavior, levels, object interactions, but I'm now working to add 30 new weapons to the game. I first thought about a weapon class, but realized each weapon would have different characteristics. I am not quite sure if I should make a weapon class with common variables then make a subclass for each of the 30 weapons, but that didn't really make sense to me because I'd never use the weapon class by itself so it almost seemed useless. I haven't done any subclasses yet so I'm not familiar with how they are defined called ect. The only other option I see is just creating 30 individual classes. I thought about also making these only called within the character class attack function, but it might be useful to use in other ways... say equiping enemies/bosses with any of the 30 weapons later if I want. Any opinions on how you would impliment this? I dont' really need code examples (unless you want to show me an example of creating a subclass lol), I'm just looking for tips of the best direction to start in so I don't write a ton of code and decide its useless when I'm done lol. Thanks again for your time.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    First you have to decide how customizable you want your weapons to be. In many games the weapons could just be one class defined by different values ex:

    BulletType
    FireAngleVariance
    FireVelocity
    ClipSize
    ReloadTime
    FireRate

    All being variables. Of course this falls apart if you want your weapons to be VERY different. In that case things become a bit more complicated. You probably want a base class.

    ex.
    Code:
    //I know you said you didn't need code samples, but they make things easier to
    //explain sometimes.
    class Weapon
    {
    public:
    	virtual void Equip()=0;
    	virtual void Unequip()=0;
    
    	virtual void TriggerDown()=0;
    	virtual void TriggerReleased()=0;
    	virtual void Reload()=0;
    };
    Of course now there is going to be potentially a lot of code duplication. You could have a happy median between the these two designs by making a class with some variables and some callback functions.

    Code:
    class DefaultWeapon : public Weapon
    {
    	int mFireRate;
            //... more variables
    private:
    	virtual void OnEquip(){}
    	virtual void OnUnequip(){}
    
    	virtual void OnFire(){}
    	virtual void OnReload(){}
    public:
            //I wish C++ had a way to guarantee no further overriding of these functions.
    	virtual void Equip()
    	{
    		DisplayModel();
    		//...
    		OnEquip();
    		//...
    	}
    	virtual void Unequip()
    	{
    		DisplayModel();
    		//...
    		OnEquip();
    		//...
    	}
    
    	virtual void TriggerDown()
    	{
    		if(mFireRate < gCurTime - mLastFireTime)
    		{
    			mLastFireTime = gCurTime;
                            //maybe actually fire a bullet, or leave that to OnFire()
    			OnFire();
    		}
    	}
    	virtual void TriggerReleased(){}
    	virtual void Reload()
    	{
                    //maybe actually do reload timing, or if that needs to be customized
                    //OnReload could do it.
    		OnReload();
    	}
    };
    That's how I would do it. You can see that DefaultWeapon inherited from Weapon. This way people can inherit from DefaultWeapon if they want to make a simple weapon quickly, and Weapon if they need a very customizable one. I have 2 layers of complexity here, but more may be useful. I would definitely have one layer with no callback functions so generic weapons can tested quickly. Depending on what your needs are you'll have to decide how many layers you need and how customizable they should be.


    I don't have too much experience with this because I've never made a game that required more variation than I could get with the first method (no virtual functions, just variables). So be wary that you may need to change my design a bit. I imagine it should work though, it's based off of a pattern I use often.

    edit:
    I assumed you were using C++. If you are using C then you would use function pointers for the callbacks instead of virtual functions.
    Last edited by Drac; 08-08-2010 at 01:18 AM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    >>//I wish C++ had a way to guarantee no further overriding of these functions.
    Why would you do that? That will only hurt your design.
    All members should also be protected. If someone wants to customize it further, they can inherit from your DefaultWeapon and customize it. If they don't want to change the defaults, then they simply don't override those virtual functions.
    Of course, perhaps you could place these in Weapon instead and avoid DefaultWeapon altogether with this model. I dunno.
    Still...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    If you truly believe weapons are varied enough to warrant new classes then that is your call. However it appears to me that weapons only differ by their 'data' and not by their 'behavior'. Therefore I think the following hierarchy under your design is sufficient:

    Code:
    class IWeapon
    {
        public:
          virtual ~IWeapon() { }
          ...
          ...
    };
    
    class Weapon : public IWeapon
    {
         ...
         ...
    };
    
    
    
    /// Might I also recommend straying away from the IWeapon concept and propose:
    class IGun { };
    class IMissile { };
    class IMine { };
    class ITurret { };
    Most weapons can fire and reload and that is about it. Turrets, missiles, and mines would probably be another class. Weapon, however, could cover a vast array of different weapons albeit I believe weapon is too generic to be of any use. I do not recommend you derive 30 different classes from weapon. Categorize your weapons by function/behavior: guns, turrets, missiles, mines, etc. Those are very different in behavior.
    Last edited by VirtualAce; 08-08-2010 at 08:25 AM.

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    The weapons aren't all shooting weapons... there will only be a HandGun, ShotGun, Sniper Rifle, and grenades as far as 'ballistic' weapons go. Basicly I break them down into two weapon "types" shooting/thrown (requires bullet handling for multiple turns after fired and collision checks) and melee... The game is a survival horror/type but in a 2d fashion. So too much ammo and powerful guns make the game easier than it should be. I'm going to have Swords, Knives, Skateboard, Hubcaps, Horshoes, Rope, Wrench, Pipe, even weird stuff like plates that are throwable and break on impact. The melee weapons will have a combo system and if you are good enough you'll be able to use a melee weapon finish with a combo that hits the enemy in the air, jump switch to shotgun to blast him while he's falling, toss a grenade where he lands, and throw a plate/hubcap where the monster lands.... obviously a japanese fighter style combat system.

    Making the weapons interchangable to diffrent combat styles will allow for me to make bosses more challenging, and some will require to character to "learn" a new style thats capable rather than just ... shoot shoot shoot reload jump shoot shoot, ect.

    did a little last night before I got a response. The code is attached if you care to give a tip on it.... not gonna copy paste 200 lines.
    Weapon.h

    I am a beginner so the code is pretty basic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  4. 12 year old in need of game programming advice
    By thehack in forum Game Programming
    Replies: 35
    Last Post: 07-03-2004, 11:04 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21