creatures.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // This file contains the definitions of objects used along in the game.
  3. //
  4. // This file is part of Linux Legends.
  5. //
  6. // Linux Legends is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. //
  19. #include <iostream>
  20. #include <string>
  21. #include "creatures.h"
  22. // The creature is the basic fighting unit in the game.
  23. Creature::Creature(int atk, int hp, int def, int xp):
  24. max_attack(atk), health(hp), defense(def), experience(xp)
  25. { }
  26. int Creature::attack()
  27. {
  28. return max_attack;
  29. }
  30. bool Creature::is_dead()
  31. {
  32. return (health < 0);
  33. }
  34. // Monster definitions:
  35. Monster::Monster(int atk, int hp, int def, int xp, std::string nm):
  36. Creature(atk, hp, def, xp), name(nm)
  37. {
  38. std::cout << "A " << name << " appears!\n";
  39. }
  40. void Monster::defend(int incoming)
  41. {
  42. if (incoming >= defense) {
  43. int damage = incoming - defense;
  44. health -= damage;
  45. std::cout << name << " loses " << damage << " hp.\n";
  46. }
  47. else {
  48. std::cout << name << " defends your attack!\n";
  49. }
  50. }
  51. int Monster::yield_xp()
  52. {
  53. return experience;
  54. }
  55. // Player definitions
  56. Player::Player(int atk, int hp, int def, int xp, std::string eq):
  57. Creature(atk, hp, def, xp), weapon(eq)
  58. {
  59. std::cout << "A new player steps in the field.\n";
  60. }
  61. void Player::defend(int incoming)
  62. {
  63. if (incoming >= defense) {
  64. int damage = incoming - defense;
  65. health -= damage;
  66. std::cout << "You lose " << damage << " hp.\n";
  67. }
  68. else {
  69. std::cout << "You defended the attack!\n";
  70. }
  71. }
  72. void Player::get_xp(int xp)
  73. {
  74. experience += xp;
  75. std::cout << "You receive " << xp << "xp, and now have "
  76. << experience << " experience.\n";
  77. }