| 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 |
#include "health_gauge.h" |
| 30 |
|
| 31 |
#include "gamedata.h" |
| 32 |
#include "gauge.h" |
| 33 |
|
| 34 |
#define WAIT_MS 1000 |
| 35 |
#define FLASH_PERIOD_MS 100 |
| 36 |
|
| 37 |
void HealthGaugeInit(HealthGauge *h) |
| 38 |
{ |
| 39 |
memset(h, 0, sizeof *h); |
| 40 |
} |
| 41 |
|
| 42 |
void HealthGaugeUpdate(HealthGauge *h, const TActor *a, const int ms) |
| 43 |
{ |
| 44 |
|
| 45 |
if (h->health == 0) |
| 46 |
{ |
| 47 |
h->health = a->health; |
| 48 |
h->waitHealth = a->health; |
| 49 |
} |
| 50 |
h->waitMs = MAX(0, h->waitMs - ms); |
| 51 |
if (h->waitHealth != a->health) |
| 52 |
{ |
| 53 |
|
| 54 |
h->waitHealth = a->health; |
| 55 |
h->waitMs = WAIT_MS; |
| 56 |
} |
| 57 |
else if (h->waitMs == 0) |
| 58 |
{ |
| 59 |
|
| 60 |
if (h->health != a->health) |
| 61 |
{ |
| 62 |
const int d = SIGN(a->health - h->health); |
| 63 |
h->health += d; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
void HealthGaugeDraw( |
| 69 |
const HealthGauge *h, GraphicsDevice *device, const TActor *actor, |
| 70 |
const struct vec2i pos, const int width, const FontOpts opts, |
| 71 |
const color_t mask) |
| 72 |
{ |
| 73 |
|
| 74 |
HUDDrawGauge(device, &gPicManager, pos, width, 0, colorTransparent, mask); |
| 75 |
if (actor == NULL) |
| 76 |
{ |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
HSV hsv = {0.0, 1.0, 1.0}; |
| 81 |
const int maxHealth = ActorGetMaxHeal(actor, false); |
| 82 |
const int health = MIN(actor->health, maxHealth); |
| 83 |
if (actor->poisoned) |
| 84 |
{ |
| 85 |
hsv.h = 120.0; |
| 86 |
hsv.v = 0.5; |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
double maxHealthHue = 50.0; |
| 91 |
double minHealthHue = 0.0; |
| 92 |
hsv.h = |
| 93 |
(maxHealthHue - minHealthHue) * health / maxHealth + minHealthHue; |
| 94 |
} |
| 95 |
|
| 96 |
if (h->waitMs > 0 && (h->waitMs % FLASH_PERIOD_MS) < FLASH_PERIOD_MS / 2) |
| 97 |
{ |
| 98 |
hsv.s = 0.0; |
| 99 |
hsv.v = 1.0; |
| 100 |
} |
| 101 |
color_t barColor; |
| 102 |
if (h->health != health) |
| 103 |
{ |
| 104 |
|
| 105 |
const int higherHealth = MIN(MAX(h->health, health), maxHealth); |
| 106 |
const int innerWidthUpdate = MAX(1, width * higherHealth / maxHealth); |
| 107 |
barColor = h->health > health ? colorMaroon : colorGreen; |
| 108 |
barColor.a = opts.Mask.a; |
| 109 |
HUDDrawGaugeInner( |
| 110 |
device, &gPicManager, pos, innerWidthUpdate, barColor); |
| 111 |
} |
| 112 |
const int lowerHealth = MIN(h->health, health); |
| 113 |
const int innerWidth = MAX(1, width * lowerHealth / maxHealth); |
| 114 |
barColor = ColorTint(colorWhite, hsv); |
| 115 |
barColor.a = opts.Mask.a; |
| 116 |
HUDDrawGaugeInner(device, &gPicManager, pos, innerWidth, barColor); |
| 117 |
|
| 118 |
|
| 119 |
if (actor->health > maxHealth) |
| 120 |
{ |
| 121 |
const int healthOverMax = actor->health - maxHealth; |
| 122 |
const int excessHealth = ActorGetMaxHeal(actor, true); |
| 123 |
const int excessHealthRange = |
| 124 |
MAX(excessHealth - maxHealth, healthOverMax); |
| 125 |
const int innerWidth2 = |
| 126 |
MAX(1, width * healthOverMax / excessHealthRange); |
| 127 |
hsv.h = 120.0; |
| 128 |
hsv.v = 1.0; |
| 129 |
barColor = ColorTint(colorWhite, hsv); |
| 130 |
barColor.a = opts.Mask.a; |
| 131 |
HUDDrawGaugeInner(device, &gPicManager, pos, innerWidth2, barColor); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
char s[50]; |
| 136 |
sprintf(s, "%d", actor->health); |
| 137 |
FontOpts fOpts = opts; |
| 138 |
fOpts.Area = svec2i(width, 11); |
| 139 |
fOpts.Pad = svec2i(2, 1); |
| 140 |
|
| 141 |
if (ActorIsLowHealth(actor)) |
| 142 |
{ |
| 143 |
|
| 144 |
const int fps = ConfigGetInt(&gConfig, "Game.FPS"); |
| 145 |
const int pulsePeriod = fps / 4; |
| 146 |
if ((gMission.time % pulsePeriod) < (pulsePeriod / 2)) |
| 147 |
{ |
| 148 |
fOpts.Mask = colorRed; |
| 149 |
} |
| 150 |
} |
| 151 |
FontStrOpt(s, pos, fOpts); |
| 152 |
} |