Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a few features of a strategy engine I'm making. I'm trying to figure out the best spots to separate the components as either option will provide the same amount of coupling with the same effectiveness.

An example. All players have a Damage component, a Player component, and a HealthBar

abstract class Damage
    protected abstract void sendDamage(Player targetPlayer, int rawDamage);
    public abstract int calculateRawDamage(Attack attackUsed);
    public abstract void receiveDamage(int rawDamage);

class Player
    List<Armor> equippedArmor;
    List<Buffs> currentBuffs;
    public Damage damageComponent;
    public List<HealthBars> healthBarComponents;

abstract class HealthBar //can be inherited for mana or health bar
    protected Color color;
    public float percent; //0.0 - 1.0

So, my problem. When a player recieves damage, I need to show a red number floating up from their head indicating the amount of damage. This can either be implemented in HealthBar or in Damage. It fits damage better, but then again heals work the same way and wouldn't be cast from damage. So I could abstract damage into something like ChangeValue which would change mana, health, etc in any direction. Or I could use the abstract HealthBar class to show this number being changed. Thoughts?

I'm trying to think of a general rule that would solve this and other problems where two classes use the same function equally. Note: This is an oversimplified version of my classes, but it makes more sense this way. Ignore small errors as this isn't the actual code.

share|improve this question
up vote 3 down vote accepted

I think you kinda just answered you own question. The number needs to appear not only on damage but also on heals, i.e. it appears whenever your health changes. And your Damage component has no connection with heals. So they don't really use the same functionality equally.

Because of that I think I would make it a part of your HealthBar. Make it react automatically to any variation in the health bar, and show that variation as a number in green or red depending on whether it increased or decreased. Besides, the HealthBar is by nature a GUI component, and so is the damage number you want to show. On the other hand Damage is conceptual.

But your HealthBar seems to be only holding a percent value at the moment. This would make it harder to know which number to show since there's no absolute range. Because of that I would also change it into a value + max pair instead, and keep it synced with the player's health.

Nonetheless, you might still want to generalize your Damage component a bit further too, to enable applying any sort of stat variation like you mentioned.

The bottomline is...

If and when both options really provide the same amount of coupling, then it doesn't matter where you add the functionality. Just pick the one and move on, since none of them will have a benefit over the other. Try to choose the one at the closest level of abstraction though.

share|improve this answer

Your Answer


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.