summaryrefslogtreecommitdiff
path: root/player.c
diff options
context:
space:
mode:
Diffstat (limited to 'player.c')
-rw-r--r--player.c54
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;
+ }
+ }
+
+}