]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/recharge/recharge.c
Add charged hits
[dungeon_game.git] / plugins / recharge / recharge.c
1 #include <stdio.h>
2 #include "recharge.h"
3 #include "../game/game.h"
4
5 static double recharge_full_timer = 0.0;
6 static double recharge_timer = 0.0;
7
8 static const char *recharge_icon;
9
10 bool is_charged()
11 {
12         return recharge_timer <= 0;
13 }
14
15 void recharge(double timer, const char *icon)
16 {
17         recharge_full_timer = recharge_timer = timer;
18         recharge_icon = icon;
19 }
20
21 static void render_recharge_meter(struct winsize ws)
22 {
23         int y = ws.ws_row - 1;
24         int x = ws.ws_col - 14;
25
26         if (recharge_timer <= 0.0)
27                 return;
28
29         double frac = (recharge_full_timer - recharge_timer) / recharge_full_timer;
30
31         printf("\e[%d;%dH", y, x);
32
33         printf("%s[", recharge_icon);
34
35         struct color color = {
36                 (1.0 - frac) * 255,
37                 frac * 255,
38                 0,
39         };
40
41         set_color(color, true);
42
43         char bar[11];
44         sprintf(bar, "%9d%%", (int) (frac * 100));
45
46         int bars = frac * 10;
47
48         for (int i = 0; i < 10; i++) {
49                 if (i == bars)
50                         set_color(black, true);
51                 printf("%c", bar[i]);
52         }
53
54         set_color(black, true);
55
56         printf("]");
57 }
58
59 static void recharge_globalstep(double dtime)
60 {
61         if (recharge_timer > 0.0)
62                 recharge_timer -= dtime;
63 }
64
65 __attribute__ ((constructor)) static void init()
66 {
67         register_render_component(&render_recharge_meter);
68         register_globalstep((struct globalstep) {
69                 .run_if_dead = false,
70                 .callback = &recharge_globalstep,
71         });
72 }