im making a modular healthbar class that changes the display color based on the health of the player/target. if the bar health is above 75% it is green, between 35 and 74 its orange and if its below 35 then its red. It automatically sets to whichever color is set in the constructor instead of following my update method here is the code so far pls help
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The Health Bar class is one that helps the user track the
* overall hp of the given target
*
* @author Prayan Jegathees
* @version March 2020
*/
public class PJHpBar extends Actor
{
// instance variables
private int hp;
//
private int value;
private int minimumVal = 0;
private int maximumVal;
private int minValue;
private int maxValue;
// HP bar dimensions
private int hpBarWidth = 140;
private int hpBarHeight = 24;
// image variable names
private GreenfootImage bar;
private GreenfootImage border;
// bar colors
private Color healthyColor = Color.GREEN;
private Color midColor = Color.ORANGE;
private Color dangerColor = Color.RED;
// chunk removed from HP bar when health is decreased
private int hpPerHit;
// target for HP bar to follow & how far above to attach the bar
private Actor target;
private int offset;
/**
* Default Constructor for the PJHpBar class, creating a functioning HP Bar
*
* @param hp takes in a value for the amount of HP the target has and what to display on the HP Bar
* @param maximumVal takes in a value for the maximum value of the bar
*
*/
public PJHpBar(int hp,int maximumVal)
{
bar = new GreenfootImage(hpBarWidth, hpBarHeight);
bar.setColor(healthyColor);
bar.fill();
bar.setColor (Color.BLACK);
bar.drawRect (0, 0, hpBarWidth - 2, hpBarHeight -2);
this.setImage(bar);
minValue = minimumVal;
maxValue = maximumVal;
this.hp = hp;
hpPerHit = (int) ((double)hpBarWidth / hp);
}
/**
* Act - do whatever the PJHealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
update();
}
/**
* Update - adjust bar hp color based on the current hp of the target
* <p>
* - Green if the target's health is between 100% and 75%
* <p>
* - Orange if the target's health is between 35% and 74%
* <p>
* - Red if the target's health is below 35%
*/
public void update()
{
int percentHealth = ((hp / maxValue) * 100);
if (percentHealth >= 75)
{
bar.setColor(healthyColor);
}
if (35 <= percentHealth && percentHealth <= 74)
{
bar.setColor(midColor);
}
if (0 <= percentHealth && percentHealth <= 34)
{
bar.setColor(dangerColor);
}
}
/**
* Add - add an amount to the value of the bar, checks to make sure the new value is between minimumValue and maximumValue,
* then, calls 'newImage' to build and set the new image for the bar.
*
* @param amount - the amount to add (if not negative) or subtract (if negative) to the current value of the bar
*/
public void add(int amount)
{
value += amount;
protectValue();
}
/**
* Subtract - subtracts an amount from the value of the bar, checks to make sure the new value does not overstep its bounds,
* then, calls 'updBarImage' to build and set the new image for the bar.
*
* @param amount - the amount to subtract (if positive) or add (if negative) to the current value of the bar
*/
public void loseHP(int amount)
{
value -= amount;
protectValue();
}
/**
* Method protectValue - ensures that the new value in between the minimum value and the maximum value for the bar
*/
private void protectValue()
{
if (value < minValue)
{
value = minValue;
}
if (value > maxValue)
{
value = maxValue;
}
}
}
