Component.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Component.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../constants/VariantIdentifier.h"
  12. #include "../constants/EntityIdentifiers.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. enum class ComponentType : int8_t
  15. {
  16. NONE = -1,
  17. PRIM_SKILL,
  18. SEC_SKILL,
  19. RESOURCE,
  20. RESOURCE_PER_DAY,
  21. CREATURE,
  22. ARTIFACT,
  23. SPELL_SCROLL,
  24. MANA,
  25. EXPERIENCE,
  26. LEVEL,
  27. SPELL,
  28. MORALE,
  29. LUCK,
  30. BUILDING,
  31. HERO_PORTRAIT,
  32. FLAG
  33. };
  34. using ComponentSubType = VariantIdentifier<PrimarySkill, SecondarySkill, GameResID, CreatureID, ArtifactID, SpellID, BuildingTypeUniqueID, HeroTypeID, PlayerColor>;
  35. struct Component
  36. {
  37. ComponentType type = ComponentType::NONE;
  38. ComponentSubType subType;
  39. std::optional<int32_t> value; // + give; - take
  40. template <typename Handler> void serialize(Handler &h)
  41. {
  42. h & type;
  43. h & subType;
  44. h & value;
  45. }
  46. Component() = default;
  47. template<typename Numeric, std::enable_if_t<std::is_arithmetic_v<Numeric>, bool> = true>
  48. Component(ComponentType type, Numeric value)
  49. : type(type)
  50. , value(value)
  51. {
  52. }
  53. template<typename IdentifierType, std::enable_if_t<std::is_base_of_v<IdentifierBase, IdentifierType>, bool> = true>
  54. Component(ComponentType type, IdentifierType subType)
  55. : type(type)
  56. , subType(subType)
  57. {
  58. }
  59. Component(ComponentType type, ComponentSubType subType, int32_t value)
  60. : type(type)
  61. , subType(subType)
  62. , value(value)
  63. {
  64. }
  65. };
  66. VCMI_LIB_NAMESPACE_END