# Design Issue Weapon / Projectile

This topic is 4561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

heya, I'm still busy building my gameengine. To demonstrate the features I'm currently writing a demo application but I came across a design issue I'm not too sure how to handle: I've got a scenegraph and all game objects are derived from the scenenode base class (or an child class of the base class). For my demo (it's a 2D space shooter) I got a spaceship class (derived from scenenode). I would like to be able to attach some sort of weaponbay (also scenenode) onto my ships which should be able to shoot different kind of projectiles (again scenenode). The problem I got is, I'm not too sure how to implement the weaponbay and projectiles so that I can stick different kind of projectiles in the same weaponbay. At the moment I implemented it so that I call the constructor of the weaponbay with an instance of the projectile I'd like it to shoot. The baseclass of all projectiles has got a virtual clone() methode which basically creates a new projectile with the same attributes as the original instance passed to the constructor. The weaponbay would then call the clone method if a fire event is issued. I wanted to ask if that is a good way to handle this problem or if anybody got a better (or different) suggestion. thanks, benderB

##### Share on other sites
The solution you've chosen is called the "prototype" design pattern. It is a valid solution.

You could also consider to plug a factory class into the bay, so that instances of the projectile are created _only_ if fired. But the you have to write a factory class per kind of projectile. Or you think to derive several sub-types of Bay, one for each kind of projectile, and making the bay itself a factory for their respective projectiles.

Choosing the best solution depends on the environment. Are there informative things provided to the player, like descriptive string, different amount of load dependent on the kind of projectiles, or perhaps should the bay be upgradeable in any kind?

You mentioned a single kind of weaponbay euipable with different projectiles. If this situation is fixed, then using the prototype pattern is IMHO okay.

##### Share on other sites
Thanks for the info!

At the moment it's just a demo of my engine but in a future project i would like to be able to create different "weaponbays" with description and different attributes (loading time, which projectile-types can fit...) as well as different projectiles.

I thought about the factory methode but I haven't implemented one yet and wasn't keen of the thought that I would have to hardcode the different types of projectiles to create in the factory.

I also think it would be a bit unflexible if I would create an extra weaponbay class for each particle type as I would like to enable the player to stick different ammo in the same weaponbay (similar to the system in eve online).

Anyway thanks for the thoughts and if anybody got something else to comment I would appreciate the effort!

benderB

P.S.: I just realized that the clone method is a bit unnecessary as I could as well implement a copy constructor...

##### Share on other sites
Quote:
 Original post by benderBAt the moment it's just a demo of my engine but in a future project i would like to be able to create different "weaponbays" with description and different attributes (loading time, which projectile-types can fit...) as well as different projectiles.

Nice.

Quote:
 Original post by benderBI thought about the factory methode but I haven't implemented one yet and wasn't keen of the thought that I would have to hardcode the different types of projectiles to create in the factory.

My hint was not to have one single factory for all but for each particular kind of projectile. Say, N factories for N kinds of projectiles needed. However, that would make real sense if other attributes could be transported by those classes (besides the factories), so in your case now it would be oversized, I agree.

Quote:
 Original post by benderBP.S.: I just realized that the clone method is a bit unnecessary as I could as well implement a copy constructor...

Are you sure? IMHO using nothing more than the copy c'tor would need to know the exact type of projectile. Or else the projectile class has to be so universal to represent all and every projectile kind, but that would be inflexible.

The clone method could _use_ the copy c'tor, that's right, but IMHO the clone method is still senseful. As a C++ sketch:
class Projectile: public SceneNode {public: // x'tors   Projectile(const Projectile& specimen);   /// results in a new Projectile w/ the same set-up as this one   virtual Projectile* clone() const =0;};class ProjectileA: public Projectile {public: // x'tors   ProjectileA(const ProjectileA& specimen)   : Projectile(specimen) { }   /// results in a new ProjectileA w/ the same set-up as this one   virtual ProjectileA* clone() const {      return new ProjectileA(*this);   }};

##### Share on other sites
thanks again for the input, it's highly appreciated!

Quote:
Original post by haegarr
Quote:
 Original post by benderBP.S.: I just realized that the clone method is a bit unnecessary as I could as well implement a copy constructor...

Are you sure? IMHO using nothing more than the copy c'tor would need to know the exact type of projectile. Or else the projectile class has to be so universal to represent all and every projectile kind, but that would be inflexible.

The clone method could _use_ the copy c'tor, that's right, but IMHO the clone method is still senseful. As a C++ sketch:
*** Source Snippet Removed ***

You are absolutly right, I haven't thought it all the way through.

thanks again & keep the comments comming :)
benderB

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002055
×