aboutsummaryrefslogtreecommitdiff
path: root/09-september/player.c
diff options
context:
space:
mode:
authorThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:02:32 +0100
committerThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:02:32 +0100
commit6b8af9cf83851c075c6c9514b1deaa931c2b19a4 (patch)
tree428986b49c32e21d3f7a3c2dfa41858ae0153209 /09-september/player.c
Initial commit
Diffstat (limited to '09-september/player.c')
-rw-r--r--09-september/player.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/09-september/player.c b/09-september/player.c
new file mode 100644
index 0000000..9258058
--- /dev/null
+++ b/09-september/player.c
@@ -0,0 +1,66 @@
+#include "player.h"
+#include "tomcat/util/util_time.h"
+#include "tomcat/input.h"
+#include "tomcat/util/util.h"
+
+#define MAX_MOVEMENT_SPEED 10
+#define MAX_ROTATION_SPEED 100
+#define GRAVITY -15
+#define JUMP_POWER 7
+
+static void jump(Player *player)
+{
+ player->verticalSpeed = JUMP_POWER;
+}
+
+static void check_input(Player *player)
+{
+ if(Input_isKeyPressed(SDL_SCANCODE_W)) {
+ player->speed = MAX_MOVEMENT_SPEED;
+ } else if(Input_isKeyPressed(SDL_SCANCODE_S)) {
+ player->speed = -MAX_MOVEMENT_SPEED;
+ } else {
+ player->speed = 0.0f;
+ }
+
+ if(Input_isKeyPressed(SDL_SCANCODE_A)) {
+ player->turnSpeed = MAX_ROTATION_SPEED;
+ } else if(Input_isKeyPressed(SDL_SCANCODE_D)) {
+ player->turnSpeed = -MAX_ROTATION_SPEED;
+ } else {
+ player->turnSpeed = 0.0f;
+ }
+
+ if(Input_isKeyPressed(SDL_SCANCODE_SPACE)) {
+ jump(player);
+ }
+}
+
+void Player_Init(Player *player)
+{
+ Entity *e = &player->entity;
+ memset(player, 0, sizeof(Player));
+ e->position = (Vec3){ 0.0f, 35.0f, 0.0f };
+ e->scale[0] = 1.0f;
+ e->scale[1] = 1.0f;
+ e->scale[2] = 1.0f;
+}
+
+void Player_Update(Player *player, Terrain *terrain)
+{
+ check_input(player);
+ player->entity.rotY += player->turnSpeed * Time_GetFrameTime();
+ player->entity.position.x += SDL_sinf(toRadians(player->entity.rotY)) * player->speed * Time_GetFrameTime();
+ player->entity.position.z += SDL_cosf(toRadians(player->entity.rotY)) * player->speed * Time_GetFrameTime();
+
+ /*
+ player->verticalSpeed += GRAVITY * Time_GetFrameTime();
+ player->entity.position.y += player->verticalSpeed * Time_GetFrameTime();
+ */
+
+ GLfloat terrainHeight = Terrain_GetHeightOfTerrain(terrain, player->entity.position.x, player->entity.position.z);
+ if(player->entity.position.y - 1.0f < terrainHeight) {
+ //player->entity.position.y = terrainHeight + 1.0f;
+ //player->verticalSpeed = 0.0f;
+ }
+}