summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 37eefe16de017c7134d4c99c6973b5c8a4384535 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <raylib.h>
#include <string.h>
#include "game.h"
#include "assets.h"
#include "stdio.h"

#include "player.h"
#include "background.h"

Game game;

static inline bool EntityAllocated(const Entity* e) {
    return (e->flags & ENTITY_ALLOCATED);
}

static inline bool PhysicsEnabled(const Entity* e) {
    return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE);
}

static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) {
    return EntityAllocated(e)
        && (e->flags & ENTITY_VISIBLE)
        && (layerDrawing == e->drawLayer);
}

static inline bool SameEntity(const Entity* a, const Entity* b) {
    return a->id == b->id;
}

static inline bool IsColliding(const Rectangle* a, const Rectangle* b) {
    bool collisionX = (a->x <= b->x && a->x + a->width > b->x)
        || (b->x <= a->x && b->x + b->width > a->x);

    bool collisionY = (a->y <= b->y && a->y + a->height > b->y)
        || (b->y <= a->y && b->y + b->height > a->y);

    return collisionX && collisionY;
}

static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) {
    Rectangle physicsColliderGlobal = e->physicsCollider;
    physicsColliderGlobal.x += e->position.x;
    physicsColliderGlobal.y += e->position.y;

    return physicsColliderGlobal;
}

static inline Rectangle GetDrawDestinationRectGlobal(const Entity* e) {
    Rectangle destRectGlobal = e->destRect;

    destRectGlobal.x += e->position.x;
    destRectGlobal.y += e->position.y;

    return destRectGlobal;
}

void MoveAndSlide(Entity* e, float deltaTime) {
    Vector2 velocity = e->velocity;
    Rectangle physicsCollider = GetPhysicsColliderGlobal(e);

    physicsCollider.x += e->velocity.x * deltaTime;
    for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
        Entity* c = &game.entities[i];

        if(!PhysicsEnabled(c) || SameEntity(c, e)) continue;
        Rectangle otherCollider = GetPhysicsColliderGlobal(c);

        if(IsColliding(&physicsCollider, &otherCollider)) {
            if (velocity.x > 0) { 
                physicsCollider.x = otherCollider.x - physicsCollider.width;
            } else {
                physicsCollider.x = otherCollider.x + otherCollider.width;
            }
        }
    }

    physicsCollider.y += e->velocity.y * deltaTime;
    for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
        Entity* c = &game.entities[i];

        if(!PhysicsEnabled(c) || SameEntity(c, e)) continue;
        Rectangle otherCollider = GetPhysicsColliderGlobal(c);

        if(IsColliding(&physicsCollider, &otherCollider)) {
            if (velocity.y > 0) { 
                physicsCollider.y = otherCollider.y - physicsCollider.height;
            } else {
                physicsCollider.y = otherCollider.y + otherCollider.height;
            }
        }
    }

    e->position.x = physicsCollider.x;
    e->position.y = physicsCollider.y;
}


void AddEntity(Entity* e) {
    static int nextId = 0;

    e->id = nextId++;
    e->flags |= ENTITY_ALLOCATED;

    for(int i = 0; i < MAX_ENTITY_COUNT; i++) {
        if (!EntityAllocated(&game.entities[i])) {
            game.entities[i] = *e;
            break;
        }
    }
}

void InitGame() {
    game.paused = false;
    LoadAssets();
    
    memset(game.entities, 0, sizeof(game.entities));
}

void UpdateEntity(Entity* e, float deltaTime) {
    switch (e->type) {
        case Player_Entity:
            UpdatePlayer(e, deltaTime);
            break;

        case Background_Entity:
            UpdateBackground(e, deltaTime);
            break;
        
        default:
            break;
    }
}

void UpdateGame(float deltaTime) {
    #ifdef BEATEMUP_DEBUG
        if (IsKeyPressed(KEY_D) && IsKeyDown(KEY_ENTER)) {
            game.enableDebugOverlay = !game.enableDebugOverlay;
        }
    #endif

    for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
        Entity* e = &game.entities[i];

        if (EntityAllocated(e))
            UpdateEntity(e, deltaTime);
    }
}

#ifdef BEATEMUP_DEBUG
void DebugHighlights(const Entity* e) {
    if (!PhysicsEnabled(e))
        return;

    Rectangle dstRect = GetPhysicsColliderGlobal(e);
    DrawRectangle(dstRect.x, dstRect.y, dstRect.width, dstRect.height, e->physicsColliderColor);
}
#endif

void DefaultDrawEntity(const Entity* e) {
    Rectangle srcRect = {0, 0, e->texture.width, e->texture.height};
    Vector2 origin = {0.0f, 0.0f};
    float rotation = 0.0f;
    Rectangle destRect = GetDrawDestinationRectGlobal(e);

    DrawTexturePro(e->texture, srcRect, destRect, origin, rotation, WHITE);
}

void DrawEntity(const Entity* e) {
    switch (e->type) {

        default:
            DefaultDrawEntity(e);
    }
}

void DrawGame() {
    BeginDrawing();
    ClearBackground(RAYWHITE);

    for (DrawLayer layer = Background_Layer; layer <= Foreground_Layer; layer++) {
        for (int j = 0; j < MAX_ENTITY_COUNT; j++) {
            Entity* currentEntity = &game.entities[j];

            if (ShouldDrawEntity(currentEntity, layer))
                DrawEntity(currentEntity);
        }
    }

    #ifdef BEATEMUP_DEBUG
    for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
        if (game.enableDebugOverlay)
            DebugHighlights(&game.entities[i]);
    }
    #endif

    EndDrawing();
}

void AddWall(float xpos, float ypos, float width, float height) {
    Entity wall = {0};
    wall.type = Wall_Entity;

    wall.flags |= ENTITY_PHYSICS_ACTIVE;

    wall.position = (Vector2) {xpos, ypos};
    wall.physicsCollider = (Rectangle) {0, 0, width, height};

    #ifdef BEATEMUP_DEBUG
        wall.physicsColliderColor = BLUE;
    #endif
    
    AddEntity(&wall);
}