Advertisement
Luninariel

textGame - Entity

Dec 11th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. package sample.edu.missouriwestern.cpozo.monsters;
  2.  
  3. public abstract class Entity implements Comparable<Entity> {
  4.  
  5. String symbol = "❇️";// https://emojipedia.org sparkle
  6. static private int nextID = 10001;
  7. double strength = 0.0; // 1. is normal human strength
  8. double aggressiveness = 0.0; //1.0 is human in combat situation. Must not be > 1.0
  9. protected String attackMessage = "attacks generically";
  10. protected String passiveMessage = "stares into space";
  11. //individual characteristics
  12. private int id;
  13. private double health;
  14. private double stamina = 0.0;
  15.  
  16. public Entity() {
  17. id = nextID;
  18. nextID++;
  19. }
  20.  
  21.  
  22.  
  23. public int getId(){
  24. return id;
  25. }
  26.  
  27. public void setHealth(double health) {
  28. this.health = health;
  29. if (health > 1.0)
  30. this.health = 1.0;
  31. if (health < 0.)
  32. this.health = 0;
  33. }
  34.  
  35. public void setStamina(double stamina) {
  36. this.stamina = (stamina > 0.10) ? stamina : 0.10;
  37. }
  38. public String getSymbol(){
  39. return symbol;
  40. }
  41.  
  42.  
  43. public double getHealth() {
  44. return health;
  45. }
  46.  
  47. public void subtractHealth(double delta) {
  48. double delta2 = Math.abs(delta); //prevents adding negative health;
  49. health -= delta2;
  50. }
  51.  
  52. public void subtractStamina(double delta) {
  53. double delta2 = Math.abs(delta);
  54. stamina = (stamina - delta2 > 0.10) ? (stamina - delta) : 0.10;
  55. }
  56.  
  57. public String getAttackMessage() {
  58. return attackMessage;
  59. }
  60.  
  61. public String getPassiveMessage() {
  62. return passiveMessage;
  63. }
  64.  
  65. public double getAggressiveness() {
  66. return aggressiveness;
  67. }
  68.  
  69. public double getStamina() {
  70. return stamina;
  71. }
  72.  
  73. private int round(double x) {
  74. return (int) Math.round(x * 100);
  75. }
  76.  
  77. public String toString() {
  78. return String.format("%8s %s %6d (%3d%%/%3d/%3d)", symbol, getName(), id,
  79. round(getHealth()), round(stamina), round(aggressiveness));
  80. }
  81.  
  82. public boolean isDead() {
  83. return health <= 0.0;
  84. }
  85.  
  86. public double getStrength() {
  87. return strength;
  88. }
  89.  
  90. public String getName() {
  91. String fullName = String.format("%s", getClass());
  92. int lastPeriod = fullName.lastIndexOf(".");
  93. String shortName = fullName.substring(lastPeriod + 1);
  94.  
  95. return shortName;
  96. }
  97.  
  98. //equals and hasCode were generated automatically, using only the id field.
  99. @Override
  100. public boolean equals(Object o) {
  101. if (this == o) return true;
  102. if (!(o instanceof Entity)) return false;
  103.  
  104. Entity that = (Entity) o;
  105.  
  106. return id == that.id;
  107.  
  108. }
  109.  
  110. @Override
  111. public int hashCode() {
  112. return id;
  113. }
  114.  
  115.  
  116. public int compareTo(Entity other) {
  117. return this.id - other.id;
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement