aboutsummaryrefslogtreecommitdiff
path: root/plugins/recharge/recharge.c
blob: 37d0207695279170ba50a0c4eb9a4b7d9d7357f9 (plain)
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
#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,
	});
}