summaryrefslogtreecommitdiff
path: root/src/player.c
blob: 67f971f60723ab82b9e757657ecb29a838e797a8 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdlib.h>
#include <math.h>
#include "constants.h"
#include "game.h"
#include "player.h"
#include "physics.h"
#include "assets.h"

extern Game game;

#define PLAYER_SPEED 300.0f

const Rectangle shadowDestRect = (Rectangle) {
    .x = 0,
    .y = 100,
    .width = 100,
    .height = 40
};

const Rectangle physicsCollider = (Rectangle) {
    .x = 0,
    .y = 0,
    .width = 100,
    .height = 100
};

void UpdatePlayer(Entity* player, float deltaTime) {
    player->velocity = (Vector2) {0.0f, 0.0f};

    if (IsKeyDown(KEY_W)) { 
        player->velocity.y = -1.0f;
    } else if (IsKeyDown(KEY_S)) {
        player->velocity.y = 1.0f;
    }

    if (IsKeyDown(KEY_A)) {
        player->velocity.x = -1.0f;
    } else if (IsKeyDown(KEY_D)) {
        player->velocity.x = 1.0f;
    }

    if (Vector2Length(player->velocity) > 0.0f) {
        player->velocity = Vector2Normalize(player->velocity);
    }

    player->velocity = Vector2Scale(player->velocity, PLAYER_SPEED);
    MoveAndSlide(player, deltaTime);

    game.camera.target =
        (Vector2) {fmaxf(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f};
}

void AddPlayer(float xpos, float ypos) {
    Entity player = {0};
    player.type = Player_Entity;

    player.position = (Vector2) {xpos, ypos};
    player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE);

    player.physicsCollider = physicsCollider;

    #ifdef BEATEMUP_DEBUG
        player.physicsColliderColor = RED;
    #endif

    Asset* shadowTexture = GetMatchingAssetWithType("human-shadow", Texture_Asset);

    if (shadowTexture == NULL) {
        TraceLog(LOG_ERROR, "Failed to find texture asset human-shadow, exitting!");
        exit(EXIT_FAILURE);
    }

    AddSpriteToEntity(&player, (Sprite){
            .texture = shadowTexture->texture,
            .layer = Foreground_Layer,
            .destRect = shadowDestRect
        });

    AddEntity(&player);
}