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

IMPL_INIT(player, 0) {
	IMPL_UNUSED;
	this->height = this->width = 5;
}

IMPL(update) {
	IMPL_UNUSED;
	const int kleft = TZR_IsKeyDown(SDL_SCANCODE_A) ||
	                  TZR_IsKeyDown(SDL_SCANCODE_LEFT);
	const int kright = TZR_IsKeyDown(SDL_SCANCODE_D) ||
	                   TZR_IsKeyDown(SDL_SCANCODE_RIGHT);
	const int kpjump = TZR_IsKeyPressed(SDL_SCANCODE_W) ||
	                   TZR_IsKeyPressed(SDL_SCANCODE_UP) ||
	                   TZR_IsKeyPressed(SDL_SCANCODE_SPACE);
	const int dirx = kright - kleft;
	const int on_ground = collide(0, 1);

	this->vel[0] = dirx;
	if (on_ground && kpjump)
		this->vel[1] = -2;
	else
		this->vel[1] += 0.1;

	move();
	camera_update(this->vel);
}

IMPL(draw) {
	IMPL_UNUSED;
	extern const PxSpr spr_player;
	const int dx = this->pos[0] - 2 + camera_x(1);
	const int dy = this->pos[1] - 2 + camera_y(1);
	pxSpr(&spr_player, dx, dy, 0, 0, -1, -1, false, false);
}