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

#include <stdio.h>

void player_accelerate(struct Player *player, int dir_x, int dir_y, float delta)
{
	double vel_x = player->vel_x + dir_x * delta * player->accel_x;
	double vel_y = player->vel_y + dir_y * delta * player->accel_y;

	if (vel_x > player->max_vel_x)
		vel_x = player->max_vel_x;
	if (vel_x < -player->max_vel_x)
		vel_x = -player->max_vel_x;

	if (vel_y > player->max_vel_y)
		vel_y = player->max_vel_y;
	if (vel_y < -player->max_vel_y)
		vel_y = -player->max_vel_y;

	player->vel_x = vel_x;
	player->vel_y = vel_y;

}

// TODO: field boundaries
void player_move(struct Player *player, enum Direction direction, float delta)
{
	if (direction == UP) {
		player->pos_y -= player->vel_y * delta;
		for (int i = 0; i < player->geometry_len; i++) {
			player->geometry[i].position.y -= player->vel_y * delta;
		}
	}
	if (direction == DOWN) {
		player->pos_y += player->vel_y * delta;
		for (int i = 0; i < player->geometry_len; i++) {
			player->geometry[i].position.y += player->vel_y * delta;
		}
	}
	if (direction == LEFT) {
		player->pos_x -= player->vel_x * delta;
		for (int i = 0; i < player->geometry_len; i++) {
			player->geometry[i].position.x -= player->vel_x * delta;
		}
	}
	if (direction == RIGHT) {
		player->pos_x += player->vel_x * delta;
		for (int i = 0; i < player->geometry_len; i++) {
			player->geometry[i].position.x += player->vel_x * delta;
		}
	}

}