diff options
author | BoredGuy <osome3717@gmail.com> | 2025-07-31 12:35:53 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-07-31 12:35:53 +0300 |
commit | 4740fe22c3fa9f9b029fc1f965d7240f9be40ccb (patch) | |
tree | 43520dc2ab874afb6d99b6a82eb8c7f1fc9bc616 | |
parent | c05e188ef6a4b715848f9f0b401351a43c80168d (diff) |
Further Refactoring
-rw-r--r-- | src/assets.c | 3 | ||||
-rw-r--r-- | src/game.c | 58 | ||||
-rw-r--r-- | src/physics.c | 27 |
3 files changed, 41 insertions, 47 deletions
diff --git a/src/assets.c b/src/assets.c index 073c748..498c801 100644 --- a/src/assets.c +++ b/src/assets.c @@ -43,8 +43,7 @@ Asset* GetMatchingAssetWithType(const char* targetName, AssetType targetType) { return c; } - printf("Failed to load asset with name: %s, exitting!\n", targetName); - exit(EXIT_FAILURE); + return NULL; } void UnloadAssets() { @@ -1,9 +1,9 @@ #include <raylib.h> #include <string.h> -#include "game.h" -#include "assets.h" #include "stdio.h" +#include "game.h" +#include "assets.h" #include "player.h" #include "background.h" @@ -27,43 +27,46 @@ static inline Rectangle GetDrawDestinationRectGlobal(const Entity* e) { 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])) { + e->id = nextId++; + e->flags |= ENTITY_ALLOCATED; game.entities[i] = *e; break; } } + +#ifdef BEATEMUP_DEBUG + TraceLog(LOG_WARNING, "Entity pool full, cannot add new entity!"); +#endif } void InitGame() { game.paused = false; - #ifdef BEATEMUP_DEBUG - game.enableDebugOverlay = false; - #endif +#ifdef BEATEMUP_DEBUG + game.enableDebugOverlay = false; +#endif memset(game.entities, 0, sizeof(game.entities)); } void UpdateEntity(Entity* e, float deltaTime) { switch (e->type) { - case Player_Entity: - UpdatePlayer(e, deltaTime); - break; + case Player_Entity: + UpdatePlayer(e, deltaTime); + break; - default: - break; + default: + break; } } void UpdateGame(float deltaTime) { - #ifdef BEATEMUP_DEBUG - if (IsKeyPressed(KEY_D) && IsKeyDown(KEY_ENTER)) { - game.enableDebugOverlay = !game.enableDebugOverlay; - } - #endif +#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]; @@ -85,8 +88,8 @@ void DefaultDrawEntity(const Entity* e) { void DrawEntity(const Entity* e) { switch (e->type) { - default: - DefaultDrawEntity(e); + default: + DefaultDrawEntity(e); } } @@ -103,12 +106,13 @@ void DrawGame() { } } - #ifdef BEATEMUP_DEBUG - for (int i = 0; i < MAX_ENTITY_COUNT; i++) { - if (game.enableDebugOverlay) +#ifdef BEATEMUP_DEBUG + if (game.enableDebugOverlay) { + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { DebugHighlights(&game.entities[i]); + } } - #endif +#endif EndDrawing(); } @@ -122,9 +126,9 @@ void AddWall(float xpos, float ypos, float width, float height) { wall.position = (Vector2) {xpos, ypos}; wall.physicsCollider = (Rectangle) {0, 0, width, height}; - #ifdef BEATEMUP_DEBUG - wall.physicsColliderColor = BLUE; - #endif +#ifdef BEATEMUP_DEBUG + wall.physicsColliderColor = BLUE; +#endif AddEntity(&wall); } diff --git a/src/physics.c b/src/physics.c index 6567dc4..048d65d 100644 --- a/src/physics.c +++ b/src/physics.c @@ -7,16 +7,6 @@ static inline bool PhysicsEnabled(const Entity* e) { return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE); } -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; @@ -31,12 +21,12 @@ void MoveAndSlide(Entity* e, float deltaTime) { physicsCollider.x += e->velocity.x * deltaTime; for (int i = 0; i < MAX_ENTITY_COUNT; i++) { - Entity* c = &game.entities[i]; + Entity* other = &game.entities[i]; - if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; - Rectangle otherCollider = GetPhysicsColliderGlobal(c); + if(!PhysicsEnabled(other) || SameEntity(other, e)) continue; + Rectangle otherCollider = GetPhysicsColliderGlobal(other); - if(IsColliding(&physicsCollider, &otherCollider)) { + if(CheckCollisionRecs(physicsCollider, otherCollider)) { if (velocity.x > 0) { physicsCollider.x = otherCollider.x - physicsCollider.width; } else { @@ -47,12 +37,13 @@ void MoveAndSlide(Entity* e, float deltaTime) { physicsCollider.y += e->velocity.y * deltaTime; for (int i = 0; i < MAX_ENTITY_COUNT; i++) { - Entity* c = &game.entities[i]; + Entity* other = &game.entities[i]; + + if(!PhysicsEnabled(other) || SameEntity(other, e)) continue; - if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; - Rectangle otherCollider = GetPhysicsColliderGlobal(c); + Rectangle otherCollider = GetPhysicsColliderGlobal(other); - if(IsColliding(&physicsCollider, &otherCollider)) { + if(CheckCollisionRecs(physicsCollider, otherCollider)) { if (velocity.y > 0) { physicsCollider.y = otherCollider.y - physicsCollider.height; } else { |