From 3422a87bdb087b6d927203e266beb71027779dbe Mon Sep 17 00:00:00 2001 From: "Dobbertin, Niclas" Date: Sat, 24 Jun 2023 23:04:23 +0200 Subject: init code --- player.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 player.c (limited to 'player.c') 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 + +#include + +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; + } + } + +} -- cgit v1.2.3