aboutsummaryrefslogtreecommitdiff
path: root/plugins/score/score.c
blob: c310d3b5696935cf2cc22ea22a193969e7f3d94c (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "../game/game.h"

static int score = 0;
static int needed_score = 5;
static int level = 0;
static char *level_symbol = NULL;
static size_t level_symbol_len = 0;
static double score_timer = 0.0;
static double level_timer = 0.0;

static void level_up()
{
	level += 1;
	needed_score = (level + 1) * 5;

	if (level_symbol)
		free(level_symbol);

	get_roman_numeral(level, &level_symbol, &level_symbol_len);

	level_timer = 2.0;
}

void add_score(int s)
{
	score += s;

	if (score >= needed_score) {
		score -= needed_score;
		level_up();
	} else {
		score_timer = 2.0;
	}
}

int get_score()
{
	return score;
}

int get_level()
{
	return level;
}

static void render_score(struct winsize ws)
{
	int bar_flash = clamp(score_timer * 255, 0, 255);
	set_color((struct color) {bar_flash, 255, bar_flash}, true);

	int level_flash = clamp(level_timer * 255, 0, 255);
	set_color((struct color) {255, 255, 255 - level_flash}, false);

	size_t level_len = level_symbol_len > 0 ? 6 + level_symbol_len + 1 : 0;
	char level_display[level_len];

	if (level_len > 0)
		sprintf(level_display, "Level %s", level_symbol);

	int bar = (double) score / (double) needed_score * ws.ws_col;
	int level_start = (ws.ws_col - level_len) / 2;
	int level_end = level_start + level_len - 1;

	for (int i = 0; i < ws.ws_col; i++) {
		if (i == bar)
			printf("\e[49m");

		if (i >= level_start && i < level_end)
			printf("%c", level_display[i - level_start]);
		else
			printf(" ");
	}

	printf("\n");
}

static void score_globalstep(double dtime)
{
	if (level_timer > 0.0)
		level_timer -= dtime;

	if (score_timer > 0.0)
		score_timer -= dtime * 3.0;
}

__attribute__ ((constructor)) static void init()
{
	register_render_component(&render_score);
	register_globalstep((struct globalstep) {
		.run_if_dead = true,
		.callback = &score_globalstep,
	});
}