PlayerStat.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package cz.davza.studentadventure.objects;
  2. /**
  3. * A single bounded numeric stat for the player (e.g. energy, stress, intelligence).
  4. *
  5. * <p>Values are always clamped to the range [{@value #MIN}, {@value #MAX}].
  6. * Use {@link #increase(int)} and {@link #decrease(int)} to modify the value;
  7. * both internally call the same clamp so negative amounts are handled safely
  8. * by whichever direction is semantically appropriate.</p>
  9. */
  10. public class PlayerStat {
  11. private final String name;
  12. private int value;
  13. private static final int MIN = 0;
  14. private static final int MAX = 100;
  15. /**
  16. * Creates a new stat with the given name and initial value.
  17. * The initial value is clamped to [{@value #MIN}, {@value #MAX}].
  18. *
  19. * @param name the stat's display name (e.g. {@code "Energy"})
  20. * @param initialValue the starting value
  21. */
  22. public PlayerStat(String name, int initialValue) {
  23. this.name = name;
  24. this.value = clamp(initialValue);
  25. }
  26. /**
  27. * Increases the stat by {@code amount}, clamped at {@value #MAX}.
  28. *
  29. * @param amount the amount to add (should be positive)
  30. */
  31. public void increase(int amount) {
  32. this.value = clamp(this.value + amount);
  33. }
  34. /**
  35. * Decreases the stat by {@code amount}, clamped at {@value #MIN}.
  36. *
  37. * @param amount the amount to subtract (should be positive)
  38. */
  39. public void decrease(int amount) {
  40. this.value = clamp(this.value - amount);
  41. }
  42. /**
  43. * Returns the current value of this stat.
  44. *
  45. * @return a value in the range [{@value #MIN}, {@value #MAX}]
  46. */
  47. public int getValue() { return value; }
  48. /**
  49. * Returns the display name of this stat.
  50. *
  51. * @return the stat name
  52. */
  53. public String getName() { return name; }
  54. private int clamp(int val) {
  55. return Math.max(MIN, Math.min(MAX, val));
  56. }
  57. /**
  58. * Returns a human-readable representation in the form {@code "Name: value/100"}.
  59. *
  60. * @return formatted stat string
  61. */
  62. @Override
  63. public String toString() {
  64. return name + ": " + value + "/100";
  65. }
  66. }