player struct and cool fucking button

This commit is contained in:
speckitor
2026-07-07 14:28:31 +07:00
parent d70dd97f1d
commit 5ab9bac6b0
6 changed files with 102 additions and 34 deletions
+47
View File
@@ -0,0 +1,47 @@
#include "button.h"
#include "cube.h"
#include "raylib.h"
void DrawButton(Button *button, Cube *cube)
{
bool pressed = !cube->grabbed && CheckCollisionBoxes(
(BoundingBox){
(Vector3){
button->position.x - button->length / 2.0f,
button->position.y - button->height / 2.0f,
button->position.z - button->length / 2.0f,
},
(Vector3){
button->position.x + button->length / 2.0f,
button->position.y + button->height / 2.0f,
button->position.z + button->length / 2.0f,
},
},
(BoundingBox){
(Vector3){
cube->position.x - cube->edge / 2.0f,
cube->position.y - cube->edge / 2.0f,
cube->position.z - cube->edge / 2.0f,
},
(Vector3){
cube->position.x + cube->edge / 2.0f,
cube->position.y + cube->edge / 2.0f,
cube->position.z + cube->edge / 2.0f,
},
});
if (pressed)
{
button->height = cube->position.y - cube->edge / 2.0f;
button->color = RED;
button->pressed = true;
}
else
{
button->height = 0.2f;
button->color = BLUE;
button->pressed = false;
}
DrawCube(button->position, button->length, button->height, button->length, button->color);
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef BUTTON_H
#define BUTTON_H
#include "cube.h"
typedef struct
{
Vector3 position;
float length;
float height;
Color color;
bool pressed;
} Button;
void DrawButton(Button *button, Cube *cube);
#endif // BUTTON_H
+12 -2
View File
@@ -1,3 +1,4 @@
#include "button.h"
#include "cube.h" #include "cube.h"
#include "player.h" #include "player.h"
#include "raylib.h" #include "raylib.h"
@@ -5,10 +6,18 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
Button button = {
.position = (Vector3){0.0f, 0.0f, 0.0f},
.length = 1.0f,
.height = 0.1f,
.color = BLUE,
.pressed = false,
};
Cube cube = { Cube cube = {
.initialized = false, .initialized = false,
.position = (Vector3){0.0f, 0.5f, 0.0f}, .position = (Vector3){0.0f, 0.5f, 0.0f},
.edge = 1.0f, .edge = 0.8f,
.color = RED, .color = RED,
.wiresColor = BLACK, .wiresColor = BLACK,
.grabbed = false, .grabbed = false,
@@ -18,7 +27,7 @@ void DrawLevel00(Player *player)
{ {
if (!cube.initialized) if (!cube.initialized)
{ {
cube.model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); cube.model = LoadModelFromMesh(GenMeshCube(cube.edge, cube.edge, cube.edge));
cube.initialized = true; cube.initialized = true;
} }
@@ -44,4 +53,5 @@ void DrawLevel00(Player *player)
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
DrawButton(&button, &cube); // Draw a button
} }
+8 -17
View File
@@ -4,15 +4,6 @@
#include "screen.h" #include "screen.h"
#include <stddef.h> #include <stddef.h>
extern Vector2 lookRotation;
extern Vector2 lean;
extern float headTimer;
extern float walkLerp;
static float heights[MAX_COLUMNS] = {0};
static Vector3 positions[MAX_COLUMNS] = {0};
static Color colors[MAX_COLUMNS] = {0};
int main() int main()
{ {
InitWindow(1600, 900, "Super cool game"); InitWindow(1600, 900, "Super cool game");
@@ -42,14 +33,14 @@ int main()
while (!WindowShouldClose()) while (!WindowShouldClose())
{ {
Vector2 mouseDelta = GetMouseDelta(); Vector2 mouseDelta = GetMouseDelta();
lookRotation.x -= mouseDelta.x * sensitivity.x; player.lookRotation.x -= mouseDelta.x * sensitivity.x;
lookRotation.y += mouseDelta.y * sensitivity.y; player.lookRotation.y += mouseDelta.y * sensitivity.y;
char sideway = (IsKeyDown(KEY_D) - IsKeyDown(KEY_A)); char sideway = (IsKeyDown(KEY_D) - IsKeyDown(KEY_A));
char forward = (IsKeyDown(KEY_W) - IsKeyDown(KEY_S)); char forward = (IsKeyDown(KEY_W) - IsKeyDown(KEY_S));
bool crouching = IsKeyDown(KEY_LEFT_CONTROL); bool crouching = IsKeyDown(KEY_LEFT_CONTROL);
UpdateBody(&player, lookRotation.x, sideway, forward, IsKeyPressed(KEY_SPACE), crouching); UpdateBody(&player, player.lookRotation.x, sideway, forward, IsKeyPressed(KEY_SPACE), crouching);
float delta = GetFrameTime(); 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);
@@ -61,18 +52,18 @@ int main()
if (body->grounded && ((forward != 0) || (sideway != 0))) if (body->grounded && ((forward != 0) || (sideway != 0)))
{ {
headTimer += delta * 3.0f; player.headTimer += delta * 3.0f;
walkLerp = Lerp(walkLerp, 1.0f, 10.0f * delta); player.walkLerp = Lerp(player.walkLerp, 1.0f, 10.0f * delta);
camera->fovy = Lerp(camera->fovy, 55.0f, 5.0f * delta); camera->fovy = Lerp(camera->fovy, 55.0f, 5.0f * delta);
} }
else else
{ {
walkLerp = Lerp(walkLerp, 0.0f, 10.0f * delta); player.walkLerp = Lerp(player.walkLerp, 0.0f, 10.0f * delta);
camera->fovy = Lerp(camera->fovy, 60.0f, 5.0f * delta); camera->fovy = Lerp(camera->fovy, 60.0f, 5.0f * delta);
} }
lean.x = Lerp(lean.x, sideway * 0.02f, 10.0f * delta); player.lean.x = Lerp(player.lean.x, sideway * 0.02f, 10.0f * delta);
lean.y = Lerp(lean.y, forward * 0.015f, 10.0f * delta); player.lean.y = Lerp(player.lean.y, forward * 0.015f, 10.0f * delta);
UpdateFpsCamera(&player); UpdateFpsCamera(&player);
+9 -14
View File
@@ -3,11 +3,6 @@
#include "raymath.h" #include "raymath.h"
#include <stddef.h> #include <stddef.h>
Vector2 lookRotation = {0};
Vector2 lean = {0};
float headTimer = 0.0f;
float walkLerp = 0.0f;
void UpdateFpsCamera(Player *player) void UpdateFpsCamera(Player *player)
{ {
Camera *camera = &player->camera; Camera *camera = &player->camera;
@@ -15,32 +10,32 @@ void UpdateFpsCamera(Player *player)
const Vector3 up = (Vector3){0.0f, 1.0f, 0.0f}; const Vector3 up = (Vector3){0.0f, 1.0f, 0.0f};
const Vector3 targetOffset = (Vector3){0.0f, 0.0f, -1.0f}; const Vector3 targetOffset = (Vector3){0.0f, 0.0f, -1.0f};
Vector3 yaw = Vector3RotateByAxisAngle(targetOffset, up, lookRotation.x); Vector3 yaw = Vector3RotateByAxisAngle(targetOffset, up, player->lookRotation.x);
float maxAngleUp = Vector3Angle(up, yaw); float maxAngleUp = Vector3Angle(up, yaw);
maxAngleUp -= 0.001f; maxAngleUp -= 0.001f;
if (-(lookRotation.y) > maxAngleUp) if (-(player->lookRotation.y) > maxAngleUp)
{ {
lookRotation.y = -maxAngleUp; player->lookRotation.y = -maxAngleUp;
} }
Vector3 right = Vector3Normalize(Vector3CrossProduct(yaw, up)); Vector3 right = Vector3Normalize(Vector3CrossProduct(yaw, up));
float pitchAngle = -lookRotation.y - lean.y; float pitchAngle = -player->lookRotation.y - player->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); Vector3 pitch = Vector3RotateByAxisAngle(yaw, right, pitchAngle);
float headSin = sinf(headTimer * PI); float headSin = sinf(player->headTimer * PI);
float headCos = cosf(headTimer * PI); float headCos = cosf(player->headTimer * PI);
const float stepRotation = 0.01f; const float stepRotation = 0.01f;
camera->up = Vector3RotateByAxisAngle(up, pitch, headSin * stepRotation + lean.x); camera->up = Vector3RotateByAxisAngle(up, pitch, headSin * stepRotation + player->lean.x);
const float bobSide = 0.1f; const float bobSide = 0.1f;
const float bobUp = 0.15f; const float bobUp = 0.15f;
Vector3 bobbing = Vector3Scale(right, headSin * bobSide); Vector3 bobbing = Vector3Scale(right, headSin * bobSide);
bobbing.y = fabsf(headCos * bobUp); bobbing.y = fabsf(headCos * bobUp);
camera->position = Vector3Add(camera->position, Vector3Scale(bobbing, walkLerp)); camera->position = Vector3Add(camera->position, Vector3Scale(bobbing, player->walkLerp));
camera->target = Vector3Add(camera->position, pitch); camera->target = Vector3Add(camera->position, pitch);
if (player->grab) if (player->grab)
@@ -56,7 +51,7 @@ void UpdateFpsCamera(Player *player)
{ {
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 player->grab->hRotation = player->lookRotation.x * RAD2DEG; // Rottate cube to be perpendicular to camera
} }
} }
+8
View File
@@ -5,6 +5,7 @@
#include "raylib.h" #include "raylib.h"
#include <stdbool.h> #include <stdbool.h>
// Some constants
#define GRAVITY 32.0f #define GRAVITY 32.0f
#define MAX_SPEED 20.0f #define MAX_SPEED 20.0f
#define CROUCH_SPEED 5.0f #define CROUCH_SPEED 5.0f
@@ -28,7 +29,14 @@ typedef struct
typedef struct typedef struct
{ {
Body body; Body body;
// Camera stuff
Camera camera; Camera camera;
Vector2 lookRotation;
Vector2 lean;
float headTimer;
float walkLerp;
Cube *grab; Cube *grab;
} Player; } Player;