implementing weapon system

Started by
4 comments, last by Marmin 18 years, 10 months ago
Hi I've a problem implementing a weapon system, that's described as follows: (slightly minimized): The player has 5 weapon 'slots'. Each slot can hold any weapon type. There are 3 weapon types: Laser, Bullet, and Saw. -- Now I use oop- so i create a class Weapon. And create 5 instances: Weapon[1].. weapon [5]. So the problem is: how can I dynamically create a weapon type in each slot, and let the program decide which weapon type to draw? Thanks.
Advertisement
Well, I will assume you're using C++!

In this case, each slot must contain a pointer to a weapon, and not a weapon. You should have something like this:

Weapon * slot[5];

If the player has a laser in the first slot, you just create a new instance of the object

slot[0] = new Laser;


After this, just use your generic slot code, and the program will know what to do. Something like

slot[currentSlot]->Draw()


should do the trick!
How are you going to handle ammo? I mean, what is the point of having 5 slots for 3 types of weapons?

Can you be more specific about your question because it's quite difficult to understand what you are asking.
Quote:
and let the program decide which weapon type to draw?

For this you can just keep a index into the weapon list that describes the current/active weapon.
Quote:Original post by doho
How are you going to handle ammo? I mean, what is the point of having 5 slots for 3 types of weapons?

Can you be more specific about your question because it's quite difficult to understand what you are asking.
Quote:
and let the program decide which weapon type to draw?

For this you can just keep a index into the weapon list that describes the current/active weapon.


OK- the example was a bit simple, but my main problem will be- where in the code do I place the actual movement of the bullet, etcetera. I assume this goes into a seperate class because if the player uses a real complicated bullet system (like 9 bullets in all directions and simultaniously rotating) there will be some deep math involved.
To keep it simple: if i press the spacebar the program must know what weapon system to initiate in slot 0, and process the proper math involved, and render the triangles to the screen.

quote: and let the program decide which weapon type to draw?

Id would be nice to have some automated system, so that i do not have to check each time what kind of weapon there is in the slots.
If I understand your question correctly, what you want is object polymorphism.

Define an abstract interface for your weapons, and derive the weapon types off of it. For example:

// if your using an MS compiler, you should use the '__interface' keyword here.// it provides some little optimizations, and allows only public pure virtual methods.struct IWeapon {       virtual void render() = 0;       virtual void fire() = 0;       // ...};class WeaponLaserGun : public IWeapon{public:       virtual void render() { /* provide custom implementation for laser guns */ }       virtual void fire() { /* provide custom implementation for laser guns */ }private:       // keep data specific to laser guns here};class WeaponMachineGun : public IWeapon{public:       // override all needed methods here, just as in WeaponLaserGunprivate:       // again, put specific data here};// ... much code here ...// Keep an array of 5 weapon slots with *pointers* to IWeapon.// You must use pointers for polymorphism to work.// Besides that, IWeapon is abstract, meaning it can't be instantiated.IWeapon* weapons[5];// during initialization:weapons[0] = new WeaponLaserGun();weapons[1] = new WeaponMachineGun();// ...// Then, to fire the weapon in slot 3, just do:weapons[3]->fire();// This will call the method of the appropriate derived class.
Quote:Original post by nilkn
If I understand your question correctly, what you want is object polymorphism.

Define an abstract interface for your weapons, and derive the weapon types off of it. For example:

*** Source Snippet Removed ***
That's very interesting, thank you. I was thinking about such a solution. Only my OOP skills are not 100 % (yet) :)



This topic is closed to new replies.

Advertisement