]> git.mcshandy.xyz Git - barrow_crawler/commitdiff
Movement and camera refinements
authorRandy McShandy <randy@mcshandy.xyz>
Sat, 4 May 2024 20:51:54 +0000 (15:51 -0500)
committerRandy McShandy <randy@mcshandy.xyz>
Sat, 4 May 2024 20:51:54 +0000 (15:51 -0500)
src/render_raylib.c

index d2017e76ff11b45344630a3e2bb4b0f1a31e7296..19ebeaeec69cb3d328e52015d052f416d5db18a6 100644 (file)
@@ -1,4 +1,5 @@
 #include <raylib.h>
+#include <raymath.h>
 #include "structs.h"
 
 Camera3D cam;
@@ -12,6 +13,50 @@ Material barrow_material;
 Matrix barrow_transform;
 Vector3 barrow_position;
 
+Vector3 player_velocity;
+Vector3 player_rotation;
+
+float forward_speed = 0.075f;
+float forward_speed_decay = 0.05f;
+float rotate_speed = 0.5f;
+float rotate_speed_decay = 0.05f;
+
+void process_inputs()
+{
+       /* I guess GetRayCollisionMesh is the best solution here
+                but cameras don't know what they're touching so I'll
+                have to roll my own movement */
+
+       KeyboardKey key = GetKeyPressed();
+       do
+       {
+               if (IsKeyDown(KEY_W) || IsKeyDown(KEY_K))
+               {
+                       player_velocity.x = forward_speed;
+               }
+               else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_J))
+               {
+                       player_velocity.x = -forward_speed;
+               }
+
+               if (IsKeyDown(KEY_A) || IsKeyDown(KEY_H))
+               {
+                       player_rotation.x = -rotate_speed;
+               }
+               else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_L))
+               {
+                       player_rotation.x = rotate_speed;
+               }
+
+               switch(key)
+               {
+                       default:
+                       {} break;
+               };
+       }
+       while((key = GetKeyPressed()));
+}
+
 void initialize_renderer()
 {
        barrow_position = (Vector3){-8.0f, 0.0f, -8.0f};
@@ -29,12 +74,14 @@ void initialize_renderer()
        barrow_model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = diffuse_texture;
 
        cam = (Camera3D){0};
-       cam.position    = barrow_position; //(Vector3){18.0f, 21.0f, 18.0f};
-       cam.position.y += 1.0f;
-       cam.target              = (Vector3){0.0f, 0.0f, 0.0f};
+       cam.position    = barrow_position;
+       cam.position.y += 3.0f;
+       cam.target              = (Vector3){0.0f, cam.position.y * 0.66f, 0.0f};
        cam.up                          = (Vector3){0.0f, 1.0f, 0.0f};
        cam.fovy                        = 45.0f;
        cam.projection= CAMERA_PERSPECTIVE;
+
+       DisableCursor();
 }
 
 void start_render_loop()
@@ -45,7 +92,9 @@ void start_render_loop()
        SetTargetFPS(target_fps);
        while (!WindowShouldClose())
        {
-               UpdateCamera(&cam, CAMERA_CUSTOM);
+               process_inputs();
+
+               UpdateCameraPro(&cam, player_velocity, player_rotation, 0.0f);
                BeginDrawing();
                        ClearBackground(LIGHTGRAY);
 
@@ -56,6 +105,10 @@ void start_render_loop()
                        DrawTexture(barrow_texture, 64, 64, RAYWHITE);
                        DrawFPS(10, 10);
                EndDrawing();
+
+               /* Decay forward velocity and rotation */
+               player_velocity.x = Lerp(player_velocity.x, 0.0f, forward_speed_decay);
+               player_rotation.x = Lerp(player_rotation.x, 0.0f, rotate_speed_decay);
        }
 
        UnloadImage(barrow_image);