Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In my RPG, all Food objects should be possible weapons. For instance, I want the player to be able to to try kill bunnies with a cherry or a watermelon.

The problem is that the easiest way to do this is a bit counter-intuitive (and ugly, IMHO):

Item < Weapon < Food

(superclass < subclass)

I wrote an interface (IWeapon) and made Weapon and Food implement it, but the amount of repeated code was ridiculously huge (what indicates that they should share a common class, right?).

I do not think that I want Potions, for instance, to be usable as weapons (I don't want to let the player hit the target with a filled vial).

How should I solve my current problem? Should I use inheritance or program to the interface?

What about extensibility? Does it make sense to let all items have attributes such as damage, hitRate, integrity, etc.?

share|improve this question
    
Why bother creating fruit weapons? Why dont you use normal weapon, sword for example, but draw a fruit instead? –  wondra 7 hours ago
    
@wondra a sword will break after 100 hits against a leather shield. An apple will only last 10. Apples can be consumed to regenerate HP and to feed the hero. They are distinct enough to be objects of different classes. The "goal" of the apple is not being a weapon. –  mafagafogigante 6 hours ago
    
Then set correct "breaks in x hits" to the apple sword. The apple can still remain where it is - in inventory just unequip your apple sword if eaten. –  wondra 6 hours ago
    
I still consider this unsatisfactory. What if swords in the future get sword-specific characteristics that an apple should not have? Will you suggest an apple mace instead? It should not be done that way. All food will have the is-a relationship with the Weapon class, I just asked if this was acceptable or if I should use a common interface between ([common weapon classes], Food, Miscellaneous, ...) to enable them as weapons. –  mafagafogigante 6 hours ago
1  
A weapon is set of stats, damage, range, speed etc., right? Set those stats to your liking and then draw apple instead of sword, pole, mace or whatever. You are overcomplicating things - appearance and behavior are two different things(and should be separated), it is not relevant for weapon if it looks like a sword or an apple. In eyes of engine it is(should be) the same. –  wondra 6 hours ago

1 Answer 1

@wondra's comment that anything that does damage is a Weapon is spot-on. Don't get confused by having the class name be restrictive when it's not the right word. Perhaps in this case you want the class to be DamageDealer instead of Weapon.

Another, and better, option is to use interfaces for the object types. This way you get a broad hierarchy instead of a deep one, which is easier to manage when it gets large.

A third, and even better, option is to use composition. In this case your Item class would have objects for DamageDealing and Edible and Wearable and each individual object may or may not have those objects exist. This gives you the advantage of being able to at run time decide that jello can't ever be a weapon but flan is both a weapon and wearable.

The third option is what I've done in my overly-complex rpg in which anything can potentially be a weapon, or container, or wearable. It's worked out quite well for me as the complexity has grown.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.