Nimbin2git.christianimmanuel.de / Games / strichmaennchen / human.c

strichmaennchen git · main

git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gz
human.c 2.2 KB · 73 lines raw
#include <SDL3/SDL.h>
#include <stdlib.h>
#include <stdio.h>

#include "human.h"
#include "mesh.h"
#include "object.h"

float cosf(float x);
float tanf(float x);
float sinf(float x);
float sqrtf(float x);
float atan2f(float y, float x);

#define DEG2RAD(x) ((x) * (3.14159265358979323846f / 180.0f))
#define M_PI 3.14159265358979323846
#define M_PI_2 6.28318530717958647692

Human* Human_create(Vec3 position, VertexPreset vertex, Vec3 size, bool is_player, float weight) {
    Human *human = malloc(sizeof(Human));
    if (!human) return NULL;

    human->object = Object_create(is_player ? OBJ_HUMAN_PLAYER : OBJ_HUMAN, vertex, position, size, weight);
    return human;
}

void Human_destroy(Human* h) {
    free(h->object);
    free(h);
}

void Player_update(Human *player, Userinput *input, Camera *cam) {
    // Flatten the camera's front vector so movement is only in XZ plane
    Vec3 flatFront = cam->front;
    flatFront.y = 0.0f;
    flatFront = vec3_normalize(flatFront);

    // Compute right vector from camera
    Vec3 right = vec3_normalize(vec3_cross(flatFront, cam->up));

    // Base speed
    float baseSpeed   = 5.0f;
    float sprintSpeed = 20.0f;
    float moveSpeed   = input->sprint ? sprintSpeed : baseSpeed;

    // Build movement vector
    Vec3 move = vec3_add(
        vec3_scale(flatFront, input->move_forward),
        vec3_scale(right,     input->move_sideway)
    );

    // Apply speed
    if (vec3_length(move) > 0.0f) {
        move = vec3_scale(vec3_normalize(move), moveSpeed);
        player->object->movement = move;
    } else {
        player->object->movement = (Vec3){0,0,0};
    }

    // Jump
    if (input->jump > 0 && player->object->is_grounded) {
        //float jump_vel = input->jump == 1 ? 5.0 : 8.0;
        float jump_vel = input->jump == 1 ? 5.0 : 10.0;
        player->object->velocity.y = jump_vel; // tweak jump strength
        player->object->is_grounded = false;
        input->jump = 0;
    }

    //player->object->position = vec3_add(player->object->position, vec3_scale(move, dt));
    // Rotate player to face camera direction
    float yaw = atan2f(flatFront.z, flatFront.x) + M_PI/2; // x = right, z = forward
    player->object->rotation.y = yaw;
}