diff options
author | Dobbertin, Niclas <niclas.dobbertin@gmx.de> | 2023-06-24 23:04:23 +0200 |
---|---|---|
committer | Dobbertin, Niclas <niclas.dobbertin@gmx.de> | 2023-06-24 23:04:23 +0200 |
commit | 3422a87bdb087b6d927203e266beb71027779dbe (patch) | |
tree | 3d8381b27c94529c45ad7287695befe3eaa241c5 /player.c |
Diffstat (limited to 'player.c')
-rw-r--r-- | player.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/player.c b/player.c new file mode 100644 index 0000000..22e22f0 --- /dev/null +++ b/player.c @@ -0,0 +1,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; + } + } + +} |