Weapon Systems I: The Concrete

So, I’ve been thinking about how to approach my weapons system.

Right now, I have multiple weapon objects, each with their own “fire” method.  This method checks if a fire button has been pushed, then it checks the currently active weapon on the ship, then proceeds to shoot the weapon.

I believe when I first started to work on this, I had an idea for an approach and I didn’t use it.  My mind still thinks that approach is the right way, so I started to think about it, and I am running into an issue (and it might be that this is the issue that had me stop previously).

Basically, I’m going to have a “shoot” function on the ship.

The ship will have an array of array of weapons objects.

The first array is the weapon-type and associated trigger “button”.

The second is the “subtype” (as indicated by the names after the dash)

0. LeftMouseButton –  Gun/Cannon, Laser

1. RightMouseButton – Missile, Nuke

2. MiddleMouseButton – Shield, Vortex, AntiMatter Field)

 

So, to do something to fire a gun, I would do something like: ship[mouse_button_clicked][current_weapon_loaded_of_subtype].shoot()

(NOTE: This is pseudocode, with many warts, but meant to convey the concept)

This says, grab the mousebutton clicked, return it’s value, which then indicates which of the first arrays it is, then go into the second array and grab the weapon that is currently assigned for that slot and activate its “fire()” method.

This is all fine and dandy as long as one has each slot occupied by a lonely game object.

The problem arises when I have something like “spreadshot”, which is currently written as 3 gun objects.  The left and right objects are rotated to give an angle of spread, while the center gun object faces forward with the ship.  How do I assign all 3 to one “slot”.

On thinking about this, I’m pondering creating a generic container named “weapon” and it will contain a list of weapons that it contains.  Most weapons, then, will only have one item in it.  But for spreadshot (and if there is some other specialized weapon that has more than one “barrel”/weapon) it would probably be the best way to do this.  In such a case, I think I would merely activate and select the appropriate selected objects.  That is to say, I would not be required to create a single-shot weapon object, and then a spread-shot weapon object.  Instead, a single weapon container object will suffice, and then I activate or deactivate the subcomponents as needed.

I don’t particularly like making this deep of a hierarchy for something that should be fairly simple.

ship->weaponsystem->weaponcontainer->activeweapons->FIRE!

On the other hand, the goal right now is to architect something and do it properly.

—————–

The second issue with the weapon system is that I am going to properly use inheritance and private/protected variables this time.  To quickly do the prototype I made almost every variable in my classes public and that’s just bad design.

But more to the point, my general idea is to create a base weapon class.

This will have things like: active-state, fire-rate,  ammo-supply.

These traits will have methods to modify them.  Methods like: level-up, fire.

This base class can then have default values, and then the ability to be overridden by child classes, so a “laser” object will have a “fire” method and a “level-up” method, but it will be different from the “gun” object.

That part is relatively easy, it’s just a matter of doing it and doing it right, and that’s the reason I’m thinking through these processes now so I properly architect it this time (or rather MORE properly architected.  I’m still learning, so to say it is proper is a misnomer, it is merely one step on a path to getting better with my coding).

But in the end, that issue of multiple weapons is going to take some thought and play.  I have a feeling the array of arrays of weapon-containers which are defined merely as lists-of-weapons which can then be accessed via this hierarchy is the way to go, or close to it, but we’ll have to get some feedback.

The other issue is that I don’t like calling a “shield” a “weapon” but in terms of interacting with it, it pretty much is.  I click a button, an effect happens.  A defensive effect, as opposed to an offensive effect, but an effect nonetheless.

 

Leave a Reply

Your email address will not be published. Required fields are marked *