All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dcutils.rpg.Vital Maven / Gradle / Ivy

package dcutils.rpg;

/**
 * The Vital class represents things like hit points, magic points.
* E.g.:
Vital hp = new Vital(50);<br/>
if(hp.reduce(num)) {<br/>
   System.out.println("still alive");<br/>
} else {<br/>
   System.out.println("dead");<br/>
} // END if/else
* @author dca */ public class Vital { /** The maximum value this vital can be set to.
*/ private int max; /** The value which this vital is currently set.
*/ private int cur; /** * The constructor initializes the max and cur values to match the supplied maximum value.
* @param max The supplied maximum value.
*/ public Vital(int max) { max(max); cur(max); } // END constructor /** * The constructor initializes the max and cur values to match the supplied maximum and current values respectively.
* @param max The supplied maximum value.
* @param cur the supplied current value.
*/ public Vital(int max, int cur) { max(max); cur(max); } // END constructor /** * Returns the maximum value of this vital.
* @return max The maximum value of this vital.
*/ public int max() { return max; } // END max getter /** * Sets the maximum value of this vital.
* @param num The supplied maximum value.
* @return Vital A reference to this vital.
* @see #check() */ public Vital max(int num) { this.max = num; check(); return this; } // END max setter /** * Returns the current value of this vital.
* @return cur The current value of this vital.
*/ public int cur() { return cur; } // END cur getter /** * Sets the current value of this vital.
* @param num The supplied current value.
* @return Vital A reference to this vital.
* @see #check() */ public Vital cur(int num) { this.cur = num; check(); return this; } // END cur setter /** * Sets the current value of this vital to the maximum value.
* @return Vital A reference to this vital.
*/ public Vital refresh() { cur = max; return this; } // END refresh /** * Adds the supplied amount to the current value of this vital, as long as the amount is positive.
* Otherwise, nothing happens.
* @param amt The supplied amount. Cannot be negative.
* @return Vital A reference to this vital.
* @see #check() */ public Vital refresh(int amt) { // disallow negative param and overflow if(0 < amt && 0 < cur + amt) { cur += amt; check(); } // END if return this; } // END refresh /** * Subtracts the supplied amount to the current value of this vital, as long as the amount is positive.
* Otherwise, nothing happens.
* @param amt The supplied amount. Cannot be negative.
* @return Vital A reference to this vital.
* @see #check() */ public boolean reduce(int amt) { // disallow negative param if(0 < amt) { cur -= amt; check(); } // END if return 0 < cur; } // END reduce /** * Adds the supplied amount to the maximum value of this vital, as long as the amount is positive.
* Otherwise, nothing happens.
* @param amt The supplied amount. Cannot be negative.
* @return Vital A reference to this vital.
*/ public Vital increase(int amt) { // disallow negative param and overflow if(0 < amt && 0 < max + amt) { max += amt; } // END if return this; } // END increase /** * Checks current and maximum values of this vital to make sure they are both positive and the current value is <= to the maximum value.
* Called by other methods of this class, usually after a change to either cur or max has occurred.
* If either cur or max goes negative, they will be set to zero.
* If cur is found to be greater than max, then cur will be set to max.
* Generally, none of these cases should occur since negative parameter values are checked in their respective methods.
*/ private void check() { if(cur < 0) cur = 0; if(max < 0) max = 0; if(cur > max) cur = max; } // END check /** * Returns a string containing the current value over the maximum value.
* I.e.: cur/max
* @return String cur/max
*/ @Override public String toString() { return String.format("%d/%d", cur, max); } // END toString /** * Creates a hash code for this vital object.
* The hash code uses prime numbers and max and cur
* @return integer The hash code.
*/ @Override public int hashCode() { int result = 1; result = 151 * result + max; result = 151 * result + cur; return result; } // END hashCode /** * Decides if an object is equal to this vital.
* In order for an object to be equal to this vital:
* the object must also be a vital, must not be null, and cur and max must be the same as this vital's.
* @param obj The item to check for value equality.
* @return True or false
*/ @Override public boolean equals(Object obj) { if(this == obj) return true; if(null == obj) return false; if(this.getClass() != obj.getClass()) return false; Vital v = (Vital)obj; return (this.max == v.max && this.cur == v.cur); } // END equals } // END class Vital




© 2015 - 2025 Weber Informatics LLC | Privacy Policy