I'm going over different ways to implement attacking in a "dungeon crawler".

What I have kind of abstracted out...

There are 2 kinds of people, The Player, and the enemies, these are both living creatures.

Living Creature will the the superclass for all living entitie's in the game(NPC is still a little murky...

Player and Enemy both use weapons, even it is only a fist.

I have decided that creating a Attack, on the weapon superclass, may be the best way to implement.

LivingCreature.Weapon.Attack(LivingCreature) will be the implementation.

enter image description here

This means no player attacks anything, the weapon itself will be the attacker. My question is, are there any negative side effects to this that I am not seeing. I have looked through some different design structures of games, and I haven't seen this exact implementation, and wasn't sure why.

To clarify-

Should Player have knowledge of how attacks works, or should the weapon know how attacks work.

  • \$\begingroup\$ Grats for the UML graphic! Now try to avoid super inheritance structures as you'll probably expand it into a mess and realize you shot yourself in the foot. The trend these days is to use an entity-component architecture, where you give behaviours to your actors in a semi liberal way. What if someday you decide that your player loses his weapon (OH NOES!)? He won't be able to fight with his bare hands and pick up a new one. \$\endgroup\$
    – Vaillancourt
    Mar 26, 2015 at 18:43
  • \$\begingroup\$ @AlexandreVaillancourt I agree, but in your example his weapon is fist, btw thank you :) \$\endgroup\$ Mar 26, 2015 at 19:57

1 Answer 1


Generally, you want to avoid excessive hierarchy. What if a player wants to attack an object (such as a trap, or door?) How would a trap attack a player? What about environmental hazards?

I would suggest an entity-component system over inheritance here. Give everything that has health a Health component, and give everything that can attack a Weapon component. For the player, give things that are controllable a Player component. Then you can mix and match things as necessary whenever these situations arise.


That aside, I think your situation (weapon does the attack), makes perfect sense. But there are some things (namely, stats like STR, WIS, etc.) that have to get passed from the player to the weapon. A level 1 peasant is not going to do much damage even if he has a level 100 supersword, for instance.

  • \$\begingroup\$ I understand the component theory, and im not arguing inheritence over component, im more asking should the player know about attacks, or should the weapon know about attacks. Also trap is a weapon which still has attack( no player involved) \$\endgroup\$ Mar 26, 2015 at 19:56
  • \$\begingroup\$ I think its clear that you want the weapon to know about the attack, unless the player has some special ability that gets imbued on weapons (which is not unreasonable in an RPG). What I do is have the player pass stats (STR, CON, WIS, etc.) to the weapon, which does the attack. \$\endgroup\$
    – mklingen
    Mar 26, 2015 at 20:11
  • \$\begingroup\$ See, its that unless scenario that I'm interested in. Would you pass the player to the weapon to get the relevant stats.. then its if myplayer.hasability then activate ability? \$\endgroup\$ Mar 26, 2015 at 20:38
  • \$\begingroup\$ @CalvinSmith at that point you've left the realm of design patterns, and we can't help much without more detailed information. Just spitballing here: What I would do here is, rather than implement a concrete class for each type of weapon, have a WeaponType class that loads relevant weapon data, and then a HeldWeapon class that combines the attacker's abilities and skills with the weapon's stats to give the actual values you'd use when calculating hit chance and damage and the like. \$\endgroup\$
    – Chris Bode
    Mar 27, 2015 at 1:39
  • \$\begingroup\$ I still have nightmares about "traditional" ECS. If you take this route, I suggest a modified version, such as Unity's component system. \$\endgroup\$
    – Evorlor
    Mar 27, 2015 at 16:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .