-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthBar.java
More file actions
77 lines (67 loc) · 1.81 KB
/
HealthBar.java
File metadata and controls
77 lines (67 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* The health bar for the enemy
*
* @author James Lu
* @version 0.01
*/
public class HealthBar extends Actor
{
private GreenfootImage bg;
private int maxHp;
private int currentHp;
private int length, width;
public HealthBar(int maxhp, int currenthp){
this.maxHp = maxhp;
this.currentHp = currenthp;
length = 20;
width = 6;
bg = new GreenfootImage(length+2, width+2);
refresh();
}
public HealthBar(int maxhp, int currenthp, int length, int width){
this (maxhp, currenthp);
this.length = length;
}
/**
* refreshes the picture after every change
*/
private void refresh(){
int scale = Math.round ( (float)currentHp/ (float)maxHp * length);
bg.clear();
if (currentHp != maxHp){
bg.setColor (Color.RED);
bg.fill();
}
bg.setColor (Color.GREEN);
bg.fillRect (1,1, scale, width);
//bg.fill();
bg.setColor (Color.BLACK);
bg.drawRect (0,0,length+1, width+1);
this.setImage (bg);
}
/**
* changes the hp of the health bar
* called after the mob it belongs to takes damage
*/
public void changeHp (int hp){
currentHp = hp;
refresh();
}
/**
* changes the coordinates of the health bar
* called after the mob it belongs to moves
*/
public void changeLocation (int x, int y){
setLocation (x, y-15);
}
/**
* Changes the maxHp of the bar
*/
public void changeMaxHp (int maxHp, int currentHp){
this.maxHp = maxHp;
this.currentHp = currentHp;
refresh();
}
}