summaryrefslogtreecommitdiff
path: root/src/player.c
blob: b7dcde567710d6adbf02e29d8188e0f1b72d2db9 (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
#include "player.h"

static int2 pos = I2(0, 0);
static int2 dir = I2(0, 0);

static bool collide(int tile, int2 p);

void
player_init(void)
{
	pos = world_find(2);
	pos.x *= cfg.tile_width;
	pos.x += cfg.tile_width / 2;
	pos.y *= cfg.tile_height;
	pos.y += cfg.tile_height / 2;
	dir = I2R(0);
}

void
player_update(void)
{
	// rotate counterclockwise
	if (input_pressed("action")) {
		if (dir.x + dir.y)
			dir = I2(dir.y, -dir.x);
		else
			dir = I2(1, 0);
	}

	if (collide(3, pos)) {
		g_world.x += 1;
		return player_init();
	}

	// deth
	if (collide(1, pos))
		return player_init();

	// you read mi code??
	const auto next_pos = int2_add(pos, dir);
	// thx and sry i guess
	if (!collide(0, next_pos))
		pos = next_pos;
}

void
player_draw(void)
{
	TZR_DrawSetColor(0, 0, 0);
	TZR_DrawRectangle(UNPACK(pos), 3, 4, true, true);
}

static bool
collide(int tile, int2 p)
{
	return world_get(p.x, p.y) == tile;
}