summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2025-07-29 11:50:57 +0300
committerBoredGuy <osome3717@gmail.com>2025-07-29 11:50:57 +0300
commit815aec62f8ae3a403e913559d5fe6138c8825007 (patch)
tree3041d4b8a843c05a4da02e66d5e2f63e1e713b72 /src/game.c
parent9e1627c229d8d094c7b55751d82db9d3579a16e1 (diff)
Added background entity and asset system
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c71
1 files changed, 58 insertions, 13 deletions
diff --git a/src/game.c b/src/game.c
index 104effa..37eefe1 100644
--- a/src/game.c
+++ b/src/game.c
@@ -1,8 +1,11 @@
#include <raylib.h>
#include <string.h>
#include "game.h"
+#include "assets.h"
+#include "stdio.h"
#include "player.h"
+#include "background.h"
Game game;
@@ -14,8 +17,10 @@ static inline bool PhysicsEnabled(const Entity* e) {
return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE);
}
-static inline bool ShouldDrawEntity(const Entity* e) {
- return EntityAllocated(e) && (e->flags & ENTITY_VISIBLE);
+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) {
@@ -26,7 +31,7 @@ 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->height)
+ 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;
@@ -40,6 +45,15 @@ static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) {
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);
@@ -97,6 +111,7 @@ void AddEntity(Entity* e) {
void InitGame() {
game.paused = false;
+ LoadAssets();
memset(game.entities, 0, sizeof(game.entities));
}
@@ -106,6 +121,10 @@ void UpdateEntity(Entity* e, float deltaTime) {
case Player_Entity:
UpdatePlayer(e, deltaTime);
break;
+
+ case Background_Entity:
+ UpdateBackground(e, deltaTime);
+ break;
default:
break;
@@ -113,7 +132,11 @@ void UpdateEntity(Entity* e, float deltaTime) {
}
void UpdateGame(float deltaTime) {
- (void)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];
@@ -125,34 +148,56 @@ void UpdateGame(float 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 (int i = 0; i < MAX_ENTITY_COUNT; i++) {
- Entity* e = &game.entities[i];
+ for (DrawLayer layer = Background_Layer; layer <= Foreground_Layer; layer++) {
+ for (int j = 0; j < MAX_ENTITY_COUNT; j++) {
+ Entity* currentEntity = &game.entities[j];
- #ifdef BEATEMUP_DEBUG
- DebugHighlights(e);
- #endif
+ if (ShouldDrawEntity(currentEntity, layer))
+ DrawEntity(currentEntity);
+ }
+ }
- if (ShouldDrawEntity(e))
- DrawEntity(e);
+ #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;
+ Entity wall = {0};
wall.type = Wall_Entity;
wall.flags |= ENTITY_PHYSICS_ACTIVE;
@@ -165,4 +210,4 @@ void AddWall(float xpos, float ypos, float width, float height) {
#endif
AddEntity(&wall);
-} \ No newline at end of file
+}