import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Show (approximately) how much health the corresponding player has. Actually, just show one out of 100 different * health bar graphics based on two variables. * * @author Martin Schend, Jannis Andrija Schnitzer * @version 2011-01-21 */ public class HealthBar extends Actor { /** * Current health. */ protected int health; protected int max_health; protected int side; public HealthBar() { health = 580; max_health = 1000; draw(); } public void setHealth (int newHealth) { health = newHealth; draw(); } public void setSide(int newSide) { side = newSide; draw(); } public void setMaxHealth(int new_max_health) { max_health = new_max_health; } /** * Set the health bar's sprite to the image that corresponds to the current quotient health/max_health. */ protected void draw() { int percentage = (int) (100*health/max_health); /* removeHitpoints() in Spaceship classes doesn't check if it goes beyond zero, because that would be * slow and annoying. So the health bar checks. */ if (percentage < 0) percentage = 0; // don't check for percentage > 100 -- that way we notice programming errors in spaceship health code String side_string = "health"; if (side == InvadersWorld.WHITE) side_string = "WhiteHealth"; else if (side == InvadersWorld.BLACK) side_string = "BlackHealth"; this.setImage(side_string+"/"+percentage+".png"); } }