Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Armor Interface for each piece of armor?

Discussion in 'Scripting' started by littlepigboy5, Nov 20, 2017.

  1. littlepigboy5

    littlepigboy5

    Joined:
    Sep 2, 2015
    Posts:
    28
    I am designing a 2d rpg and am trying to decide how to program my armor system. I began by making an enum for armor types (.head, .shoulders, etc), and then an armor interface with functions like
    Code (CSharp):
    1. takeDamage(int d);
    that would reduce the durability of the piece of armor. However, I then began to realize that I would have to manually write a class that implements this interface for every piece of armor in the game, which feels like a waste of time. What is the best way of going about programming an armor system? Should I write an abstract class for each piece? or should I manually program a class for each piece of armor?
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Think about what the differences between armor pieces are. Is it just where on the character to put it? If so, you only need a single class with an enum.

    Or you can make an interface, with an abstract class implementing the common functions, and concrete classes implementing unique behaviour.
     
  3. littlepigboy5

    littlepigboy5

    Joined:
    Sep 2, 2015
    Posts:
    28
    I get how from a functional perspective, but does this mean, structurally, I will be writing a class for each piece of armor that implements or is a child of the class/interface mentioned above?
     
  4. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    I would make an interface IDamageable with the following members

    Code (CSharp):
    1. public interface IDamageable
    2. {
    3.     void TakeDamage(int damage);
    4.     int Durability { get; }
    5. }
    And rest depends on if your armour types are the same or not. If they all share the same members, make one class Armour implementing IDamageable. If the armour types will differ, make HeadArmour, ShouldesArmour etc., all implementing IDamageable. Maybe they could be derived from common abstract class ArmourBase.
     
  5. littlepigboy5

    littlepigboy5

    Joined:
    Sep 2, 2015
    Posts:
    28
    I think this solution may be fitting. Just to be clear, you are suggesting, for example,
    Code (CSharp):
    1. Class HeadArmor : baseArmour, IDamageable {
    2. //CODE
    3. }
    right?
     
  6. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    Right, but IDamageable could be probably on baseArmour and HeadArmor will inherit it implicitly.
     
    Last edited: Nov 20, 2017