i have made a class that creates a healthbar for the spawning units, but i have a few problems with it.
a) it doesn't fill in the bar till the unit is hit
b) it doesn't fill it proportionally
trying to get it to show how much they've damaged the unit when the tower hits.
here's the class for the healthbar:
and here's the class i'm implementing it in:
public class healthbar extends Actor
{
/**
* Act - do whatever the healthbar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
GreenfootImage bar = new GreenfootImage(50, 10);//width and height
public healthbar(int health)
{
bar.setColor(new Color(0,0,255));
bar.fill();
bar.setColor(new Color(0, 255, 0));
bar.drawRect(0,0, 48, 8);
bar.fillRect(0, 0, health, 8);
setImage(bar);
}
// public void act()
// {
// // Add your action code here.
// //imageUpdate();
// }
public void imageUpdate(int health)
{
bar.clear();
bar.setColor(new Color(0,0,255));
bar.fill();
bar.setColor(new Color(0, 255, 0));
bar.fillRect(0, 0, health, 8);
setImage(bar);
}
}public class enemies extends Actor
{
/**
* Act - do whatever the enemies wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int x = 0;
int y = 0;
private int wave = 1; //get from wave class
public int health;
public int mins;
public int lifeLost;
private int input = 0;
private int i = 1;
private int j = 0;
private int slow = 0;
private healthbar HPbar;
public enemies()
{
//getWorld().addObject(new healthbar(health), this.getX(), this.getY() - 10);
HPbar = new healthbar(health);
}
public void act()
{
// Add your action code here.
if(slowStat() && slow < 100)
{
slow++;
}
else
{
move();
getWorld().addObject(HPbar, this.getX(), this.getY() - 20);
HPbar.setLocation(this.getX(), this.getY() - 10);
if(slow == 100)
{
((tdpath)getWorld()).removeSlowMatter();
slow = 0;
}
}
// slowMove();
checkHealth();
checkWave();
if(((tdpath)getWorld()).waveNum() > 5 && ((tdpath)getWorld()).spawnsDone() && ((tdpath)getWorld()).getSpawnsAlive() == 0)
{
input = 0;
}
if(j == 5)
{
i++;
j = 0;
}
}
public int getHealth()
{
return health;
}
public void decHealth(int damage)
{
health = health - damage;
HPbar.imageUpdate(health);
checkHealth();
}
public void setHealth(int hp)
{
health = hp;
}
public void checkHealth()
{
if (health <= 0)
{
((tdpath)getWorld()).mineralsAdd(mins);
((tdpath)getWorld()).killSpawn();
getWorld().removeObject(HPbar);
((tdpath)getWorld()).removeObject(this);
}
}
public void move()
{
if(getWorld().getColorAt(getX()+1, getY()).getRed() < 10 && getWorld().getColorAt(getX()+1, getY()).getGreen() < 10 && x !=-1)
{
setLocation(getX() + 1,getY());
x = 1;
y = 0;
}
else if(getWorld().getColorAt(getX()-1, getY()).getRed() < 10 && getWorld().getColorAt(getX()-1, getY()).getGreen() < 10 && x !=1)
{
setLocation(getX()-1, getY());
x = -1;
y = 0;
}
else if(getWorld().getColorAt(getX(), getY()+1).getRed() < 10 && getWorld().getColorAt(getX(), getY()+1).getGreen() < 10 && y != -1)
{
setLocation(getX(), getY()+1);
x = 0;
y = 1;
}
else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 10 && getWorld().getColorAt(getX(), getY()-1).getGreen() < 10 && y !=1)
{
setLocation(getX(), getY()-1);
x = 0;
y = -1;
}
}
public void slowMove()
{
}
public int getLivesLost()
{
return lifeLost;
}
public int getSlow()
{
return slow;
}
public void checkWave()
{
if((((tdpath)getWorld()).waveNum() > 5) && input < 1)//&& (((tdpath)getWorld()).waveNum())%5 >= 1 && input < 1)
{
healthInc();
input++;
j++;
}
}
public void healthInc()
{
setHealth(health*(2*(((tdpath)getWorld()).waveNum()/5)));
}
public boolean slowStat()
{
if(!getObjectsInRange(50, slowMatter.class).isEmpty())
{
return true;
}
return false;
}
public void setSlow(int SL)
{
slow = SL;
}
}
