#include #include #include "stdio.h" #include "constants.h" #include "game.h" #include "assets.h" #include "player.h" #include "background.h" #include Game game; static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) { return EntityAllocated(e) && (e->flags & ENTITY_VISIBLE); } static inline Rectangle GetSpriteDrawDestinationRectGlobal(const Entity* e, int spriteIndex) { Rectangle destRectGlobal = e->sprites[spriteIndex].destRect; destRectGlobal.x += e->position.x; destRectGlobal.y += e->position.y; return destRectGlobal; } 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 = {0, 0, drawnSprite->texture.width, drawnSprite->texture.height}; Vector2 origin = {0.0f, 0.0f}; float rotation = 0.0f; Rectangle destRect = GetSpriteDrawDestinationRectGlobal(e, spriteIndex); 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); }