Debian Codegraph @

src/cdogs/hud/health_gauge.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2017, 2019, 2021, 2023-2024 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
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 // Initialise health to actor's health
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 // Health has changed; start waiting
54 h->waitHealth = a->health;
55 h->waitMs = WAIT_MS;
56 }
57 else if (h->waitMs == 0)
58 {
59 // End of wait; start moving health towards real health
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 // Draw gauge background
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 // Flash bar white if just took damage
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 // Draw different-coloured health gauge representing health change
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 // Draw a second bar if health is over max
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 // Draw health number label
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 // If low health, draw text with different colours, flashing
141 if (ActorIsLowHealth(actor))
142 {
143 // Fast flashing
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 }