diff options
Diffstat (limited to 'plugins/recharge')
-rw-r--r-- | plugins/recharge/Makefile | 4 | ||||
-rw-r--r-- | plugins/recharge/dependencies.txt | 1 | ||||
-rw-r--r-- | plugins/recharge/recharge.c | 72 | ||||
-rw-r--r-- | plugins/recharge/recharge.h | 9 |
4 files changed, 86 insertions, 0 deletions
diff --git a/plugins/recharge/Makefile b/plugins/recharge/Makefile new file mode 100644 index 0000000..cb6e05e --- /dev/null +++ b/plugins/recharge/Makefile @@ -0,0 +1,4 @@ +plugins/recharge/recharge.so: plugins/recharge/recharge.c plugins/recharge/recharge.h plugins/game/game.h + cc -g -shared -fpic -o plugins/recharge/recharge.so plugins/recharge/recharge.c + +PLUGINS := ${PLUGINS} plugins/recharge/recharge.so diff --git a/plugins/recharge/dependencies.txt b/plugins/recharge/dependencies.txt new file mode 100644 index 0000000..dc22e61 --- /dev/null +++ b/plugins/recharge/dependencies.txt @@ -0,0 +1 @@ +game diff --git a/plugins/recharge/recharge.c b/plugins/recharge/recharge.c new file mode 100644 index 0000000..37d0207 --- /dev/null +++ b/plugins/recharge/recharge.c @@ -0,0 +1,72 @@ +#include <stdio.h> +#include "recharge.h" +#include "../game/game.h" + +static double recharge_full_timer = 0.0; +static double recharge_timer = 0.0; + +static const char *recharge_icon; + +bool is_charged() +{ + return recharge_timer <= 0; +} + +void recharge(double timer, const char *icon) +{ + recharge_full_timer = recharge_timer = timer; + recharge_icon = icon; +} + +static void render_recharge_meter(struct winsize ws) +{ + int y = ws.ws_row - 1; + int x = ws.ws_col - 14; + + if (recharge_timer <= 0.0) + return; + + double frac = (recharge_full_timer - recharge_timer) / recharge_full_timer; + + printf("\e[%d;%dH", y, x); + + printf("%s[", recharge_icon); + + struct color color = { + (1.0 - frac) * 255, + frac * 255, + 0, + }; + + set_color(color, true); + + char bar[11]; + sprintf(bar, "%9d%%", (int) (frac * 100)); + + int bars = frac * 10; + + for (int i = 0; i < 10; i++) { + if (i == bars) + set_color(black, true); + printf("%c", bar[i]); + } + + set_color(black, true); + + printf("]"); +} + +static void recharge_globalstep(double dtime) +{ + if (recharge_timer > 0.0) + recharge_timer -= dtime; +} + +__attribute__ ((constructor)) static void init() +{ + register_render_component(&render_recharge_meter); + register_globalstep((struct globalstep) { + .run_if_dead = false, + .callback = &recharge_globalstep, + }); +} diff --git a/plugins/recharge/recharge.h b/plugins/recharge/recharge.h new file mode 100644 index 0000000..7c0dc88 --- /dev/null +++ b/plugins/recharge/recharge.h @@ -0,0 +1,9 @@ +#ifndef _RECHARGE_H_ +#define _RECHARGE_H_ + +#include <stdbool.h> + +bool is_charged(); +void recharge(double timer, const char *icon); + +#endif |