-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealth.java
More file actions
79 lines (67 loc) · 2.09 KB
/
Copy pathHealth.java
File metadata and controls
79 lines (67 loc) · 2.09 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
78
79
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Health class that manages the player's health.
*
* @author Jacky161
* @version v1.0-public
*/
public class Health extends Actor
{
/** Internal Variables */
private Counter healthCount = new Counter(6, 0, 6, 10, false, false);
private GreenfootImage[] healthSprites = new GreenfootImage[7];
private GreenfootImage powerupSprite;
private int regenTime = 0;
private boolean powerup = false;
/** Constructor for Health Tracker */
public Health() {
// Prepare Health Sprites
prepareImages();
}
/** Import all images for health */
private void prepareImages() {
int index = 0;
for (double i = 0.0; i < 3.1; i += 0.5) {
healthSprites[index] = new GreenfootImage("heart" + i + ".png");
index++;
}
powerupSprite = new GreenfootImage("heartPowerup.png");
setImage(healthSprites[6]);
}
/**
* Act - For health, we regenerate after enough time has passed
*/
public void act() {
// Handle pausing
UndeadWorld world = (UndeadWorld) getWorld();
if (world.getGameState() == 1) return;
regenTime++;
healthCount.act();
if (regenTime > 120 || powerup) {
healthCount.increment();
updateImage();
}
}
/** Decrements health by 1 */
public void decrement() {
regenTime = 0;
healthCount.decrement();
updateImage();
}
/** Powerup makes it so health increments every frame (invincible basically) */
public void powerup() {
powerup = powerup ? false : true;
}
/** Gets the current value of health */
public int getValue() {
return healthCount.getValue();
}
/** Updates the image displayed on screen */
private void updateImage() {
if (powerup) {
setImage(powerupSprite);
return;
}
setImage(healthSprites[healthCount.getValue()]);
}
}