Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #ifndef RPGGAME_ENTITY_H_
  2. #define RPGGAME_ENTITY_H_
  3.  
  4. #include <string>
  5. #include "spectype.h"
  6.  
  7. /// <summary>
  8. /// A base class that represents an entity, and implements
  9. /// some base attributes, like health or name, and a few helper
  10. /// methods for apply damage.
  11. /// </summary>
  12. class Entity
  13. {
  14. public:
  15. std::string name;
  16. SpecType health;
  17.  
  18. Entity(const std::string& name, SpecType health);
  19. std::string toString();
  20. };
  21.  
  22. #endif
  23.  
  24. #include <string>
  25. #include <random>
  26. #include "entity.h"
  27.  
  28. /// <summary>
  29. /// Constructor for the Entity class.
  30. /// </summary>
  31. /// <param name="name">The name of the entity.</param>
  32. /// <param name="health">The health of the entity.</param>
  33. Entity::Entity(const std::string& name, SpecType health):
  34. name(name),
  35. health(health)
  36. {}
  37.  
  38. /// <summary>
  39. /// Return a string value representing the entity.
  40. /// </summary>
  41. /// <remarks>
  42. /// This is mostly not used unless a raw Entity is being
  43. /// used by itself, which should be very uncommon, or not
  44. /// happen at all.
  45. /// </remarks>
  46. std::string Entity::toString()
  47. {
  48. return "Name: " + name + ", " +
  49. "Health: " + std::to_string(health.specValue) + "n";
  50. }
  51.  
  52. #ifndef RPGGAME_SPECTYPE_H_
  53. #define RPGGAME_SPECTYPE_H_
  54.  
  55. /// <summary>
  56. /// The SpecType class describes a non-constant unsigned
  57. /// integer spec that can be used in any entity-derived class.
  58. /// or structure.
  59. /// </summary>
  60. class SpecType
  61. {
  62. public:
  63. int specValue;
  64.  
  65. SpecType(int specValue);
  66. void changeValue(int value);
  67. };
  68.  
  69. #endif
  70.  
  71. #include "spectype.h"
  72.  
  73. /// <summary>
  74. /// Constructor for the SpecType class.
  75. /// </summary>
  76. /// <param name="specValue">The value that the SpecType contains.</param>
  77. SpecType::SpecType(int specValue):
  78. specValue(specValue)
  79. {}
  80.  
  81. /// <summary>
  82. /// Change the Spec's value by a certain amount.
  83. /// <summary>
  84. /// <param name="value">The value to change specValue by.</param>
  85. void SpecType::changeValue(int value)
  86. {
  87. if (specValue + value >= 0) { specValue += value; }
  88. else { specValue = 0; }
  89. }
  90.  
  91. #include <iostream>
  92. #include "spectype.h"
  93. #include "entity.h"
  94.  
  95. int main()
  96. {
  97. SpecType testSpec1(0);
  98. testSpec1.changeValue(10);
  99. testSpec1.changeValue(-11);
  100. std::cout << testSpec1.specValue << "n";
  101.  
  102. SpecType testSpec2(51);
  103. testSpec2.changeValue(-2143);
  104. testSpec2.changeValue(10);
  105. std::cout << testSpec2.specValue << "n";
  106.  
  107. std::cin.get();
  108.  
  109. Entity testEntity1("FooBarBlahName", 30);
  110. std::cout << testEntity1.toString();
  111.  
  112. Entity testEntity2("Goku", 9001);
  113. std::cout << testEntity2.toString();
  114.  
  115. std::cin.get();
  116. }
  117.  
  118. std::string toString() const;
  119. ^^^^^
  120.  
  121. /// <summary>
  122. /// Constructor for the SpecType class.
  123. /// </summary>
  124. /// <param name="specValue">The value that the SpecType contains.</param>
  125. SpecType::SpecType(int specValue):
  126. specValue(specValue)
  127. {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement