aboutsummaryrefslogtreecommitdiff
path: root/09-september/player.c
blob: 9258058f6be5f94afe110c3efe206ea274e16b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;
    }
}