Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef RPGGAME_ENTITY_H_
- #define RPGGAME_ENTITY_H_
- #include <string>
- #include "spectype.h"
- /// <summary>
- /// A base class that represents an entity, and implements
- /// some base attributes, like health or name, and a few helper
- /// methods for apply damage.
- /// </summary>
- class Entity
- {
- public:
- std::string name;
- SpecType health;
- Entity(const std::string& name, SpecType health);
- std::string toString();
- };
- #endif
- #include <string>
- #include <random>
- #include "entity.h"
- /// <summary>
- /// Constructor for the Entity class.
- /// </summary>
- /// <param name="name">The name of the entity.</param>
- /// <param name="health">The health of the entity.</param>
- Entity::Entity(const std::string& name, SpecType health):
- name(name),
- health(health)
- {}
- /// <summary>
- /// Return a string value representing the entity.
- /// </summary>
- /// <remarks>
- /// This is mostly not used unless a raw Entity is being
- /// used by itself, which should be very uncommon, or not
- /// happen at all.
- /// </remarks>
- std::string Entity::toString()
- {
- return "Name: " + name + ", " +
- "Health: " + std::to_string(health.specValue) + "n";
- }
- #ifndef RPGGAME_SPECTYPE_H_
- #define RPGGAME_SPECTYPE_H_
- /// <summary>
- /// The SpecType class describes a non-constant unsigned
- /// integer spec that can be used in any entity-derived class.
- /// or structure.
- /// </summary>
- class SpecType
- {
- public:
- int specValue;
- SpecType(int specValue);
- void changeValue(int value);
- };
- #endif
- #include "spectype.h"
- /// <summary>
- /// Constructor for the SpecType class.
- /// </summary>
- /// <param name="specValue">The value that the SpecType contains.</param>
- SpecType::SpecType(int specValue):
- specValue(specValue)
- {}
- /// <summary>
- /// Change the Spec's value by a certain amount.
- /// <summary>
- /// <param name="value">The value to change specValue by.</param>
- void SpecType::changeValue(int value)
- {
- if (specValue + value >= 0) { specValue += value; }
- else { specValue = 0; }
- }
- #include <iostream>
- #include "spectype.h"
- #include "entity.h"
- int main()
- {
- SpecType testSpec1(0);
- testSpec1.changeValue(10);
- testSpec1.changeValue(-11);
- std::cout << testSpec1.specValue << "n";
- SpecType testSpec2(51);
- testSpec2.changeValue(-2143);
- testSpec2.changeValue(10);
- std::cout << testSpec2.specValue << "n";
- std::cin.get();
- Entity testEntity1("FooBarBlahName", 30);
- std::cout << testEntity1.toString();
- Entity testEntity2("Goku", 9001);
- std::cout << testEntity2.toString();
- std::cin.get();
- }
- std::string toString() const;
- ^^^^^
- /// <summary>
- /// Constructor for the SpecType class.
- /// </summary>
- /// <param name="specValue">The value that the SpecType contains.</param>
- SpecType::SpecType(int specValue):
- specValue(specValue)
- {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement