summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2023-05-11 22:04:13 -0700
committerSchark <jordan@schark.online>2023-05-11 22:04:13 -0700
commit622604ab99f53a891700919268e34ab679c96330 (patch)
tree48774ea2333c55b2cc1391e49116e12579022909 /src
parent1d21cae59d8f5670eefee331c91b5a3250f40fb2 (diff)
downloadgamedev-622604ab99f53a891700919268e34ab679c96330.tar.gz
gamedev-622604ab99f53a891700919268e34ab679c96330.zip
Created player files for easier management of movement
Diffstat (limited to 'src')
-rw-r--r--src/main.c44
-rw-r--r--src/player.c34
-rw-r--r--src/player.h14
3 files changed, 52 insertions, 40 deletions
diff --git a/src/main.c b/src/main.c
index 51fb50a..180c955 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <math.h>
+#include "player.h"
+
// TODO: move to implicit map definition with level editor in future
#define MAP_HEIGHT 20
#define MAP_WIDTH 20
@@ -28,16 +30,6 @@ int world_map[MAP_HEIGHT][MAP_WIDTH] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
-// TODO: move player code to dedicated file after complication demands it
-// TODO: implement y-shearing to allow players to look vertically
-typedef struct {
- float pos_x, pos_y;
- float dir_x, dir_y;
- float plane_x, plane_y;
- float move_speed;
- float rot_speed;
-} Player;
-
typedef struct {
unsigned char r, g, b;
} ColorRGB;
@@ -221,36 +213,7 @@ int main(int argc, char *argv[]) {
// main raycast function
raycast(&player);
-
- // movement and camera rotation
- // TODO: eventually translate movement from a header
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
- player.pos_x += player.dir_x * player.move_speed;
- player.pos_y += player.dir_y * player.move_speed;
- }
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
- player.pos_x -= player.dir_x * player.move_speed;
- player.pos_y -= player.dir_y * player.move_speed;
- }
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
- // this changes where we're casting our rays
- double old_dir_x = player.dir_x;
- player.dir_x = player.dir_x * cos(-player.rot_speed) - player.dir_y * sin(-player.rot_speed);
- player.dir_y = old_dir_x * sin(-player.rot_speed) + player.dir_y * cos(-player.rot_speed);
- // this updates perspective
- double old_plane_x = player.plane_x;
- player.plane_x = player.plane_x * cos(-player.rot_speed) - player.plane_y * sin(-player.rot_speed);
- player.plane_y = old_plane_x * sin(-player.rot_speed) + player.plane_y * cos(-player.rot_speed);
- }
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
- double old_dir_x = player.dir_x;
- player.dir_x = player.dir_x * cos(player.rot_speed) - player.dir_y * sin(player.rot_speed);
- player.dir_y = old_dir_x * sin(player.rot_speed) + player.dir_y * cos(player.rot_speed);
- double old_plane_x = player.plane_x;
- player.plane_x = player.plane_x * cos(player.rot_speed) - player.plane_y * sin(player.rot_speed);
- player.plane_y = old_plane_x * sin(player.rot_speed) + player.plane_y * cos(player.rot_speed);
- }
-
+ move_player(&player, window);
glfwSwapBuffers(window);
glfwPollEvents();
@@ -260,4 +223,5 @@ int main(int argc, char *argv[]) {
glfwTerminate();
printf("Terminating program...\n");
return 0;
+
}
diff --git a/src/player.c b/src/player.c
new file mode 100644
index 0000000..91c4520
--- /dev/null
+++ b/src/player.c
@@ -0,0 +1,34 @@
+#include <GLFW/glfw3.h>
+#include <math.h>
+
+#include "player.h"
+
+void move_player(Player *player, GLFWwindow* window) {
+ if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
+ player->pos_x += player->dir_x * player->move_speed;
+ player->pos_y += player->dir_y * player->move_speed;
+ }
+ if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
+ player->pos_x -= player->dir_x * player->move_speed;
+ player->pos_y -= player->dir_y * player->move_speed;
+ }
+ if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
+ // this changes where we're casting our rays
+ double old_dir_x = player->dir_x;
+ player->dir_x = player->dir_x * cos(-player->rot_speed) - player->dir_y * sin(-player->rot_speed);
+ player->dir_y = old_dir_x * sin(-player->rot_speed) + player->dir_y * cos(-player->rot_speed);
+ // this updates perspective
+ double old_plane_x = player->plane_x;
+ player->plane_x = player->plane_x * cos(-player->rot_speed) - player->plane_y * sin(-player->rot_speed);
+ player->plane_y = old_plane_x * sin(-player->rot_speed) + player->plane_y * cos(-player->rot_speed);
+ }
+ if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
+ double old_dir_x = player->dir_x;
+ player->dir_x = player->dir_x * cos(player->rot_speed) - player->dir_y * sin(player->rot_speed);
+ player->dir_y = old_dir_x * sin(player->rot_speed) + player->dir_y * cos(player->rot_speed);
+ double old_plane_x = player->plane_x;
+ player->plane_x = player->plane_x * cos(player->rot_speed) - player->plane_y * sin(player->rot_speed);
+ player->plane_y = old_plane_x * sin(player->rot_speed) + player->plane_y * cos(player->rot_speed);
+ }
+
+}
diff --git a/src/player.h b/src/player.h
new file mode 100644
index 0000000..1ee8154
--- /dev/null
+++ b/src/player.h
@@ -0,0 +1,14 @@
+#ifndef PLAYER_H
+#define PLAYER_H
+
+typedef struct Player {
+ float pos_x, pos_y;
+ float dir_x, dir_y;
+ float plane_x, plane_y;
+ float move_speed;
+ float rot_speed;
+} Player;
+
+void move_player(Player *player, GLFWwindow* window);
+
+#endif