Luninariel

Monsters - Entity

Dec 6th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. package edu.missouriwestern.cpozo.csc254.monsters;
  2.  
  3. /**
  4. * Entity is intended to be an abstract class, although it is not in its current form
  5. * <p>
  6. * The following recommendations are for all entities:
  7. * - strength: values should be 0.00 through 9.99
  8. * any value over 2.0 is likely to be an instant kill.
  9. * 1.0 would be a full strength human with a sword.
  10. * Something like a plant would have a strength of 0.0
  11. * A bunny might have a strength of .05 unless it is a Monty Python bunny
  12. * A T-Rex might have a strength of 5.5
  13. * - aggressiveness: Must be between 0 and 1.0
  14. * 1.0 would mean attacks at every opportunity.
  15. * 0.7 would attack 70% of the time and not attack 30%
  16. * 0 would be for something like a plant.
  17. * - health: Health also represents stamina
  18. * Value should be > 0.0 through 1.00
  19. * Anything below .10 is considered unconscious
  20. * 0 is dead.
  21. * 1.0 is full health (100%)
  22. * <p>
  23. * Symbols are taken from unicode or just txt.
  24. * For an alien you could use 👽
  25. * A good source is https://emojipedia.org
  26. * Using emoji will mess up printing because they are not standard width.
  27. * - stamina: Stamina is greater than 0.
  28. * Stamina is roughly approximated by size. A good start is
  29. * 1 KG = .01 point of stamina
  30. * .80 would be about 180 pound human.
  31. * 1. would be about a 250 pound human (maybe a warrior)
  32. */
  33. public abstract class Entity implements Comparable<Entity> {
  34.  
  35. String symbol = "❇️";// https://emojipedia.org sparkle
  36. static private int nextID = 10001;
  37. static double strength = 0.0; // 1. is normal human strength
  38. static double aggressiveness = 0.0; //1.0 is human in combat situation. Must not be > 1.0
  39. protected String attackMessage = "attacks generically";
  40. protected String passiveMessage = "stares into space";
  41. //individual characteristics
  42. private int id;
  43. private double health;
  44. private double stamina = 0.0;
  45.  
  46. public Entity() {
  47. id = nextID;
  48. nextID++;
  49. }
  50.  
  51.  
  52.  
  53. public int getId(){
  54. return id;
  55. }
  56.  
  57. public void setHealth(double health) {
  58. this.health = health;
  59. if (health > 1.0)
  60. this.health = 1.0;
  61. if (health < 0.)
  62. this.health = 0;
  63. }
  64.  
  65. public void setStamina(double stamina) {
  66. this.stamina = (stamina > 0.10) ? stamina : 0.10;
  67. }
  68. public String getSymbol(){
  69. return symbol;
  70. }
  71.  
  72.  
  73. public double getHealth() {
  74. return health;
  75. }
  76.  
  77. public void subtractHealth(double delta) {
  78. double delta2 = Math.abs(delta); //prevents adding negative health;
  79. health -= delta2;
  80. }
  81.  
  82. public void subtractStamina(double delta) {
  83. double delta2 = Math.abs(delta);
  84. stamina = (stamina - delta2 > 0.10) ? (stamina - delta) : 0.10;
  85. }
  86.  
  87. public String getAttackMessage() {
  88. return attackMessage;
  89. }
  90.  
  91. public String getPassiveMessage() {
  92. return passiveMessage;
  93. }
  94.  
  95. public double getAggressiveness() {
  96. return aggressiveness;
  97. }
  98.  
  99. public double getStamina() {
  100. return stamina;
  101. }
  102.  
  103. private int round(double x) {
  104. return (int) Math.round(x * 100);
  105. }
  106.  
  107. public String toString() {
  108. return String.format("%8s %s %6d (%3d%%/%3d/%3d)", symbol, getName(), id,
  109. round(getHealth()), round(stamina), round(aggressiveness));
  110. }
  111.  
  112. public boolean isDead() {
  113. return health <= 0.0;
  114. }
  115.  
  116. public double getStrength() {
  117. return strength;
  118. }
  119.  
  120. public String getName() {
  121. String fullName = String.format("%s", getClass());
  122. int lastPeriod = fullName.lastIndexOf(".");
  123. String shortName = fullName.substring(lastPeriod + 1);
  124.  
  125. return shortName;
  126. }
  127.  
  128. //equals and hasCode were generated automatically, using only the id field.
  129. @Override
  130. public boolean equals(Object o) {
  131. if (this == o) return true;
  132. if (!(o instanceof Entity)) return false;
  133.  
  134. Entity that = (Entity) o;
  135.  
  136. return id == that.id;
  137.  
  138. }
  139.  
  140. @Override
  141. public int hashCode() {
  142. return id;
  143. }
  144.  
  145.  
  146. public int compareTo(Entity other) {
  147. return this.id - other.id;
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment