diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0b6ede8 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +ColumnLimit: 120 +BreakBeforeBraces: Allman \ No newline at end of file diff --git a/Makefile b/Makefile index fc4c862..10c0cad 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,11 @@ +all: build/game + build/game: src/* mkdir -p build gcc -o build/game src/* \ -I./raylib-6.0_linux_amd64/include \ -L./raylib-6.0_linux_amd64/lib \ -l:libraylib.a -lm -lX11 + +format: + find src/ -type f | xargs clang-format -i diff --git a/src/cube.c b/src/cube.c index 91c85bf..564b4ef 100644 --- a/src/cube.c +++ b/src/cube.c @@ -2,18 +2,21 @@ void MyDrawCube(Cube *cube) { - if (!cube->grabbed && cube->position.y > cube->edge/2.0f) + // Gravity + if (!cube->grabbed && cube->position.y > cube->edge / 2.0f) { float delta = GetFrameTime(); - cube->yvel -= delta*32.0f; - cube->position.y += cube->yvel*delta; - if (cube->position.y <= cube->edge/2.0f) + cube->yvel -= delta * 32.0f; + cube->position.y += cube->yvel * delta; + if (cube->position.y <= cube->edge / 2.0f) { - cube->position.y = cube->edge/2.0f; + cube->position.y = cube->edge / 2.0f; cube->yvel = 0.0f; } } - DrawModelEx(cube->model, cube->position, (Vector3){ 0.0f, 1.0f, 0.0f }, cube->hRotation, (Vector3){ 1.0f, 1.0f, 1.0f }, cube->color); - DrawModelWiresEx(cube->model, cube->position, (Vector3){ 0.0f, 1.0f, 0.0f }, cube->hRotation, (Vector3){ 1.0f, 1.0f, 1.0f }, cube->wiresColor); + DrawModelEx(cube->model, cube->position, (Vector3){0.0f, 1.0f, 0.0f}, cube->hRotation, (Vector3){1.0f, 1.0f, 1.0f}, + cube->color); + DrawModelWiresEx(cube->model, cube->position, (Vector3){0.0f, 1.0f, 0.0f}, cube->hRotation, + (Vector3){1.0f, 1.0f, 1.0f}, cube->wiresColor); } diff --git a/src/cube.h b/src/cube.h index 7230c3c..b74e3c8 100644 --- a/src/cube.h +++ b/src/cube.h @@ -3,7 +3,8 @@ #include "raylib.h" -typedef struct { +typedef struct +{ bool initialized; Model model; Vector3 position; diff --git a/src/level00.c b/src/level00.c index aca5a13..6724f4c 100644 --- a/src/level00.c +++ b/src/level00.c @@ -1,13 +1,13 @@ -#include -#include -#include "raylib.h" #include "cube.h" #include "player.h" +#include "raylib.h" #include "screen.h" +#include +#include Cube cube = { .initialized = false, - .position = (Vector3){ 0.0f, 0.5f, 0.0f }, + .position = (Vector3){0.0f, 0.5f, 0.0f}, .edge = 1.0f, .color = RED, .wiresColor = BLACK, @@ -32,16 +32,16 @@ void DrawLevel00(Player *player) { if ((y & 1) ^ (x & 1)) { - DrawPlane((Vector3){ x*tileSize, 0.0f, y*tileSize}, (Vector2){ tileSize, tileSize }, LIGHTGRAY); + DrawPlane((Vector3){x * tileSize, 0.0f, y * tileSize}, (Vector2){tileSize, tileSize}, LIGHTGRAY); } } } - DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall - DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, GREEN); // Draw a green wall - DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, YELLOW); // Draw a yellow wall + DrawCube((Vector3){-16.0f, 2.5f, 0.0f}, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall + DrawCube((Vector3){16.0f, 2.5f, 0.0f}, 1.0f, 5.0f, 32.0f, GREEN); // Draw a green wall + DrawCube((Vector3){0.0f, 2.5f, 16.0f}, 32.0f, 5.0f, 1.0f, YELLOW); // Draw a yellow wall - UpdateGrab(player, &cube); // Check if we grabbed a cube + UpdateGrab(player, &cube); // Check if we grabbed a cube - MyDrawCube(&cube); // Draw a cube + MyDrawCube(&cube); // Draw a cube } diff --git a/src/main.c b/src/main.c index a9b3912..0735465 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,8 @@ -#include +#include "player.h" #include "raylib.h" #include "raymath.h" #include "screen.h" -#include "player.h" +#include extern Vector2 lookRotation; extern Vector2 lean; @@ -15,20 +15,20 @@ static Color colors[MAX_COLUMNS] = {0}; int main() { - InitWindow(800, 600, "Super cool game"); + InitWindow(1600, 900, "Super cool game"); Player player = {0}; Camera *camera = &player.camera; Body *body = &player.body; player.grab = NULL; - Vector2 sensitivity = { 0.001f, 0.001f }; + Vector2 sensitivity = {0.001f, 0.001f}; float headLerp = STAND_HEIGHT; camera->fovy = 75.0f; camera->projection = CAMERA_PERSPECTIVE; - camera->position = (Vector3) { + camera->position = (Vector3){ body->position.x, body->position.y + (BOTTOM_HEIGHT + headLerp), body->position.z, @@ -39,10 +39,11 @@ int main() DisableCursor(); SetTargetFPS(GAME_FPS); - while (!WindowShouldClose()) { + while (!WindowShouldClose()) + { Vector2 mouseDelta = GetMouseDelta(); - lookRotation.x -= mouseDelta.x*sensitivity.x; - lookRotation.y += mouseDelta.y*sensitivity.y; + lookRotation.x -= mouseDelta.x * sensitivity.x; + lookRotation.y += mouseDelta.y * sensitivity.y; char sideway = (IsKeyDown(KEY_D) - IsKeyDown(KEY_A)); char forward = (IsKeyDown(KEY_W) - IsKeyDown(KEY_S)); @@ -51,36 +52,38 @@ int main() UpdateBody(&player, lookRotation.x, sideway, forward, IsKeyPressed(KEY_SPACE), crouching); float delta = GetFrameTime(); - headLerp = Lerp(headLerp, (crouching ? CROUCH_HEIGHT : STAND_HEIGHT), 20.0f*delta); + headLerp = Lerp(headLerp, (crouching ? CROUCH_HEIGHT : STAND_HEIGHT), 20.0f * delta); camera->position = (Vector3){ body->position.x, body->position.y + (BOTTOM_HEIGHT + headLerp), body->position.z, }; - if (body->grounded && ((forward != 0) || (sideway != 0))) { - headTimer += delta*3.0f; - walkLerp = Lerp(walkLerp, 1.0f, 10.0f*delta); - camera->fovy = Lerp(camera->fovy, 55.0f, 5.0f*delta); + if (body->grounded && ((forward != 0) || (sideway != 0))) + { + headTimer += delta * 3.0f; + walkLerp = Lerp(walkLerp, 1.0f, 10.0f * delta); + camera->fovy = Lerp(camera->fovy, 55.0f, 5.0f * delta); } - else { - walkLerp = Lerp(walkLerp, 0.0f, 10.0f*delta); - camera->fovy = Lerp(camera->fovy, 60.0f, 5.0f*delta); + else + { + walkLerp = Lerp(walkLerp, 0.0f, 10.0f * delta); + camera->fovy = Lerp(camera->fovy, 60.0f, 5.0f * delta); } - lean.x = Lerp(lean.x, sideway*0.02f, 10.0f*delta); - lean.y = Lerp(lean.y, forward*0.015f, 10.0f*delta); + lean.x = Lerp(lean.x, sideway * 0.02f, 10.0f * delta); + lean.y = Lerp(lean.y, forward * 0.015f, 10.0f * delta); UpdateFpsCamera(&player); BeginDrawing(); - ClearBackground(RAYWHITE); + ClearBackground(RAYWHITE); - BeginMode3D(*camera); - DrawLevel00(&player); - EndMode3D(); + BeginMode3D(*camera); + DrawLevel00(&player); + EndMode3D(); - DrawFPS(10, 10); + DrawFPS(10, 10); EndDrawing(); } diff --git a/src/player.c b/src/player.c index af5a6bf..a107777 100644 --- a/src/player.c +++ b/src/player.c @@ -1,75 +1,81 @@ -#include +#include "player.h" #include "raylib.h" #include "raymath.h" -#include "player.h" +#include Vector2 lookRotation = {0}; Vector2 lean = {0}; float headTimer = 0.0f; float walkLerp = 0.0f; - void UpdateFpsCamera(Player *player) { Camera *camera = &player->camera; - const Vector3 up = (Vector3){ 0.0f, 1.0f, 0.0f }; - const Vector3 targetOffset = (Vector3){ 0.0f, 0.0f, -1.0f }; + const Vector3 up = (Vector3){0.0f, 1.0f, 0.0f}; + const Vector3 targetOffset = (Vector3){0.0f, 0.0f, -1.0f}; Vector3 yaw = Vector3RotateByAxisAngle(targetOffset, up, lookRotation.x); float maxAngleUp = Vector3Angle(up, yaw); maxAngleUp -= 0.001f; - if ( -(lookRotation.y) > maxAngleUp) { lookRotation.y = -maxAngleUp; } + if (-(lookRotation.y) > maxAngleUp) + { + lookRotation.y = -maxAngleUp; + } Vector3 right = Vector3Normalize(Vector3CrossProduct(yaw, up)); float pitchAngle = -lookRotation.y - lean.y; - pitchAngle = Clamp(pitchAngle, -PI/2 + 0.0001f, PI/2 - 0.0001f); + pitchAngle = Clamp(pitchAngle, -PI / 2 + 0.0001f, PI / 2 - 0.0001f); Vector3 pitch = Vector3RotateByAxisAngle(yaw, right, pitchAngle); - float headSin = sinf(headTimer*PI); - float headCos = cosf(headTimer*PI); + float headSin = sinf(headTimer * PI); + float headCos = cosf(headTimer * PI); const float stepRotation = 0.01f; - camera->up = Vector3RotateByAxisAngle(up, pitch, headSin*stepRotation + lean.x); + camera->up = Vector3RotateByAxisAngle(up, pitch, headSin * stepRotation + lean.x); const float bobSide = 0.1f; const float bobUp = 0.15f; - Vector3 bobbing = Vector3Scale(right, headSin*bobSide); - bobbing.y = fabsf(headCos*bobUp); + Vector3 bobbing = Vector3Scale(right, headSin * bobSide); + bobbing.y = fabsf(headCos * bobUp); camera->position = Vector3Add(camera->position, Vector3Scale(bobbing, walkLerp)); camera->target = Vector3Add(camera->position, pitch); if (player->grab) { + // Move cube where your camera looks Vector3 forward = Vector3Subtract(camera->target, camera->position); forward = Vector3Normalize(forward); Vector3 cubePosition = Vector3Add(camera->target, forward); player->grab->position = cubePosition; - if (player->grab->position.y <= player->grab->edge/2.0f) - { - player->grab->position.y = player->grab->edge/2.0f; - } - player->grab->hRotation = lookRotation.x * RAD2DEG; - } + // Don't let putting grab under the floor + if (player->grab->position.y <= player->grab->edge / 2.0f) + { + player->grab->position.y = player->grab->edge / 2.0f; + } + player->grab->hRotation = lookRotation.x * RAD2DEG; // Rottate cube to be perpendicular to camera + } } void UpdateBody(Player *player, float rot, char side, char forward, bool jumpPressed, bool crouchHold) { Body *body = &player->body; - Vector2 input = (Vector2){ (float)side, (float)-forward }; + Vector2 input = (Vector2){(float)side, (float)-forward}; // Slow down diagonal movement #if defined(NORMALIZE_INPUT) - if ((side != 0) && (forward != 0)) input = Vector2Normalize(input); + if ((side != 0) && (forward != 0)) + input = Vector2Normalize(input); #endif float delta = GetFrameTime(); - if (!body->grounded) body->velocity.y -= GRAVITY*delta; + if (!body->grounded) + body->velocity.y -= GRAVITY * delta; if (body->grounded && jumpPressed) { @@ -81,37 +87,43 @@ void UpdateBody(Player *player, float rot, char side, char forward, bool jumpPre // PlaySound(fxJump); } - Vector3 front = (Vector3){ sinf(rot), 0.f, cosf(rot) }; - Vector3 right = (Vector3){ cosf(-rot), 0.f, sinf(-rot) }; + Vector3 front = (Vector3){sinf(rot), 0.f, cosf(rot)}; + Vector3 right = (Vector3){cosf(-rot), 0.f, sinf(-rot)}; - Vector3 desiredDir = (Vector3){ input.x*right.x + input.y*front.x, 0.0f, input.x*right.z + input.y*front.z, }; - body->dir = Vector3Lerp(body->dir, desiredDir, CONTROL*delta); + Vector3 desiredDir = (Vector3){ + input.x * right.x + input.y * front.x, + 0.0f, + input.x * right.z + input.y * front.z, + }; + body->dir = Vector3Lerp(body->dir, desiredDir, CONTROL * delta); float decel = (body->grounded ? FRICTION : AIR_DRAG); - Vector3 hvel = (Vector3){ body->velocity.x*decel, 0.0f, body->velocity.z*decel }; + Vector3 hvel = (Vector3){body->velocity.x * decel, 0.0f, body->velocity.z * decel}; float hvelLength = Vector3Length(hvel); // Magnitude - if (hvelLength < (MAX_SPEED*0.01f)) hvel = (Vector3){ 0 }; + if (hvelLength < (MAX_SPEED * 0.01f)) + hvel = (Vector3){0}; // This is what creates strafing float speed = Vector3DotProduct(hvel, body->dir); - float maxSpeed = (crouchHold? CROUCH_SPEED : MAX_SPEED); - float accel = Clamp(maxSpeed - speed, 0.f, MAX_ACCEL*delta); - hvel.x += body->dir.x*accel; - hvel.z += body->dir.z*accel; + float maxSpeed = (crouchHold ? CROUCH_SPEED : MAX_SPEED); + float accel = Clamp(maxSpeed - speed, 0.f, MAX_ACCEL * delta); + hvel.x += body->dir.x * accel; + hvel.z += body->dir.z * accel; body->velocity.x = hvel.x; body->velocity.z = hvel.z; - float diffx = body->velocity.x*delta; - float diffy = body->velocity.y*delta; - float diffz = body->velocity.z*delta; + float diffx = body->velocity.x * delta; + float diffy = body->velocity.y * delta; + float diffz = body->velocity.z * delta; body->position.x += diffx; body->position.y += diffy; body->position.z += diffz; + // Move grab with the player if (player->grab) { Cube *cube = player->grab; @@ -147,13 +159,12 @@ void UpdateGrab(Player *player, Cube *cube) return; } - float halfEdge = cube->edge/2; + float halfEdge = cube->edge / 2; BoundingBox cubeBox = { - (Vector3){ cube->position.x - halfEdge, cube->position.y - halfEdge, cube->position.z - halfEdge }, - (Vector3){ cube->position.x + halfEdge, cube->position.y + halfEdge, cube->position.z + halfEdge } - }; + (Vector3){cube->position.x - halfEdge, cube->position.y - halfEdge, cube->position.z - halfEdge}, + (Vector3){cube->position.x + halfEdge, cube->position.y + halfEdge, cube->position.z + halfEdge}}; - Vector2 screenCenter = { (float)GetScreenWidth()/2.0f, (float)GetScreenHeight()/2.0f}; + Vector2 screenCenter = {(float)GetScreenWidth() / 2.0f, (float)GetScreenHeight() / 2.0f}; Ray lookRay = GetScreenToWorldRay(screenCenter, player->camera); RayCollision collision = GetRayCollisionBox(lookRay, cubeBox); diff --git a/src/player.h b/src/player.h index 13aa963..470835d 100644 --- a/src/player.h +++ b/src/player.h @@ -1,30 +1,32 @@ #ifndef PLAYER_H #define PLAYER_H -#include -#include "raylib.h" #include "cube.h" +#include "raylib.h" +#include -#define GRAVITY 32.0f -#define MAX_SPEED 20.0f -#define CROUCH_SPEED 5.0f -#define JUMP_FORCE 12.0f -#define CONTROL 15.0f -#define MAX_ACCEL 150.0f -#define FRICTION 0.86f -#define AIR_DRAG 0.98f +#define GRAVITY 32.0f +#define MAX_SPEED 20.0f +#define CROUCH_SPEED 5.0f +#define JUMP_FORCE 12.0f +#define CONTROL 15.0f +#define MAX_ACCEL 150.0f +#define FRICTION 0.86f +#define AIR_DRAG 0.98f #define BOTTOM_HEIGHT 0.5f #define CROUCH_HEIGHT 0.4f -#define STAND_HEIGHT 1.0f +#define STAND_HEIGHT 1.0f -typedef struct { +typedef struct +{ Vector3 position; Vector3 velocity; Vector3 dir; bool grounded; } Body; -typedef struct { +typedef struct +{ Body body; Camera camera; Cube *grab;