| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package cz.davza.studentadventure.objects;
- /**
- * A single bounded numeric stat for the player (e.g. energy, stress, intelligence).
- *
- * <p>Values are always clamped to the range [{@value #MIN}, {@value #MAX}].
- * Use {@link #increase(int)} and {@link #decrease(int)} to modify the value;
- * both internally call the same clamp so negative amounts are handled safely
- * by whichever direction is semantically appropriate.</p>
- */
- public class PlayerStat {
- private final String name;
- private int value;
- private static final int MIN = 0;
- private static final int MAX = 100;
- /**
- * Creates a new stat with the given name and initial value.
- * The initial value is clamped to [{@value #MIN}, {@value #MAX}].
- *
- * @param name the stat's display name (e.g. {@code "Energy"})
- * @param initialValue the starting value
- */
- public PlayerStat(String name, int initialValue) {
- this.name = name;
- this.value = clamp(initialValue);
- }
- /**
- * Increases the stat by {@code amount}, clamped at {@value #MAX}.
- *
- * @param amount the amount to add (should be positive)
- */
- public void increase(int amount) {
- this.value = clamp(this.value + amount);
- }
- /**
- * Decreases the stat by {@code amount}, clamped at {@value #MIN}.
- *
- * @param amount the amount to subtract (should be positive)
- */
- public void decrease(int amount) {
- this.value = clamp(this.value - amount);
- }
- /**
- * Returns the current value of this stat.
- *
- * @return a value in the range [{@value #MIN}, {@value #MAX}]
- */
- public int getValue() { return value; }
- /**
- * Returns the display name of this stat.
- *
- * @return the stat name
- */
- public String getName() { return name; }
- private int clamp(int val) {
- return Math.max(MIN, Math.min(MAX, val));
- }
- /**
- * Returns a human-readable representation in the form {@code "Name: value/100"}.
- *
- * @return formatted stat string
- */
- @Override
- public String toString() {
- return name + ": " + value + "/100";
- }
- }
|