summaryrefslogtreecommitdiff
path: root/src/game.c
blob: d6e8ce368ff91c9ff42e0260005cff1e60e94ee1 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <raylib.h>
#include <string.h>
#include "stdio.h"

#include "constants.h"
#include "game.h"
#include "assets.h"
#include "player.h"
#include "background.h"
#include <math.h>
#include "utils.h"

Game game;

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

    for(int i = 0; i < MAX_ENTITY_COUNT; i++) {
        if (!EntityAllocated(&game.entities[i])) {
            e->id = nextId++;
            e->flags |= ENTITY_ALLOCATED;
            game.entities[i] = *e;
            return;
        }
    }

#ifdef BEATEMUP_DEBUG
    TraceLog(LOG_WARNING, "Entity pool full, cannot add new entity!");
#endif
}

void InitGame() {
    memset(&game, 0, sizeof(Game));

    game.camera = (Camera2D) {
        .offset = (Vector2) {WINDOW_WIDTH / 2.0f, 0.0f},
        .rotation = 0.0f,
        .zoom = 1.0f
    };
}

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

    default:
        break;
    }
}

void UpdateGame(float deltaTime) {
#ifdef BEATEMUP_DEBUG
    if (IsKeyPressed(KEY_F3)) {
        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);
    }
}

void DrawEntitySprite(const Entity* e, int spriteIndex) {
    const Sprite* drawnSprite = &e->sprites[spriteIndex];

    Rectangle srcRect;
    if (!IsAnimated(drawnSprite)) {
        srcRect = (Rectangle) {
            .x = 0,
            .y = 0,
            .width = drawnSprite->texture.width,
            .height = drawnSprite->texture.height
        };
    } else {
        const Animation* currentAnimation =
            &drawnSprite->animations[drawnSprite->currentAnimation];

        srcRect = GetCurrentSourceRectangle(currentAnimation);
    }

    Vector2 origin = {0.0f, 0.0f};
    float rotation = 0.0f;
    Rectangle destRect = GetSpriteDrawDestinationRectGlobal(e, spriteIndex);

    if (drawnSprite->flipX)
        srcRect.width = -srcRect.width;

    if (drawnSprite->flipY)
        srcRect.height = -srcRect.width;

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


void DefaultDrawEntity(const Entity* e, DrawLayer toLayer) {
    for (int i = 0; i < e->numSprites; i++) {
        if (e->sprites[i].layer == toLayer)
            DrawEntitySprite(e, i);
    }
}

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

    default:
        DefaultDrawEntity(e, toLayer);
    }
}

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

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

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

    BeginMode2D(game.camera);

    for (DrawLayer layer = Background_Layer; layer <= Foreground_Layer; layer++)
        DrawEntitiesInLayer(layer);

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

    EndMode2D();

#ifdef BEATEMUP_DEBUG
    if (game.enableDebugOverlay) {
        const int posX = 10, posY = 10;
        DrawFPS(posX, posY);
    }
#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);
}

static inline float GetCurrentFrameTime(const Animation* animation) {
    return animation->frameTimes[animation->currentFrame];
}

void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt) {
    if (!IsAnimated(sprite))
        return;

    Animation* currentAnimation = GetCurrentAnimation(sprite);

    if (currentAnimation->isComplete)
        return;

    currentAnimation->currentFrameTimer += dt;

    if (currentAnimation->currentFrameTimer >= GetCurrentFrameTime(currentAnimation)) {
        currentAnimation->currentFrame++;

        currentAnimation->currentFrameTimer =
            currentAnimation->currentFrameTimer - GetCurrentFrameTime(currentAnimation);
    }

    if (currentAnimation->currentFrame >= currentAnimation->numFrames) {
        if (currentAnimation->isLooping) {
            currentAnimation->currentFrame = 0;
            currentAnimation->currentFrameTimer = 0.0f;
        }
        else {
            currentAnimation->currentFrame = currentAnimation->numFrames - 1;
            currentAnimation->isComplete = true;
        }
    }
}

Rectangle GetSrcRectFromIndex(Texture texture, int framesX, int framesY, int index) {
    const float frameWidth = (float)texture.width / framesX;
    const float frameHeight = (float)texture.height / framesY;

    return (Rectangle) {
        .x = (index % framesY) * frameWidth,
        .y = (index / framesY) * frameHeight,
        .width = frameWidth,
        .height = frameHeight
    };
}

Animation AnimationFromIndices(AnimationFromIndicesParams params) {
    int numFrames = params.numFrames;
    bool isLooping = params.isLooping;
    Texture texture = params.texture;
    int framesX = params.framesX;
    int framesY = params.framesY;
    int* indices = params.indices;
    float* frameTimes = params.frameTimes;

    Animation animation = (Animation) {
        .numFrames = numFrames,
        .isLooping = isLooping,

        .currentFrame = 0
    };

    for (int i = 0; i < numFrames; i++) {
        animation.srcRects[i] = GetSrcRectFromIndex(texture, framesX, framesY, indices[i]);
        animation.frameTimes[i] = frameTimes[i];
    }

    return animation;
}