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
|
#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;
if (g_world.cells[g_world.x + g_world.y * g_world.width] == nullptr)
g_world.x = 0;
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;
}
|