diff options
author | BoredGuy <osome3717@gmail.com> | 2025-08-31 19:24:24 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-08-31 19:24:24 +0300 |
commit | 1562dd67d16d328e919db28cfd48206c8c868fe4 (patch) | |
tree | fe8f78e9d94ae55ff3868739dc359dd4629f8441 | |
parent | 8a7c25827624348ccd7a9ac8bf978e71117f8b75 (diff) |
Added hitbox/hurtbox system
- Made the entity struct much smaller
-rw-r--r-- | include/barrel_data.h | 2 | ||||
-rw-r--r-- | include/constants.h | 5 | ||||
-rw-r--r-- | include/physics.h | 2 | ||||
-rw-r--r-- | include/utils.h | 17 | ||||
-rw-r--r-- | src/background.c | 12 | ||||
-rw-r--r-- | src/game.c | 45 |
6 files changed, 67 insertions, 16 deletions
diff --git a/include/barrel_data.h b/include/barrel_data.h index 0db5fdd..97a13db 100644 --- a/include/barrel_data.h +++ b/include/barrel_data.h @@ -4,7 +4,7 @@ static const Rectangle physicsCollider = (Rectangle) { .x = NO_OFFSET, .y = NO_OFFSET, .width = 100, - .height = 100 + .height = 120 }; static const Rectangle destRect = (Rectangle) { diff --git a/include/constants.h b/include/constants.h index 2fd6ace..a2ea961 100644 --- a/include/constants.h +++ b/include/constants.h @@ -3,7 +3,7 @@ #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 -#define MAX_ENTITY_COUNT 2048 +#define MAX_ENTITY_COUNT 128 #define MAX_AREA_COUNT 4 #define MAX_SPRITE_COUNT 4 //Maximum number of sprites per entity @@ -12,3 +12,6 @@ #define MAX_FRAMES 30 #define MAX_ANIMATIONS 20 + +#define FPS_POS_X 10 +#define FPS_POS_Y 10 diff --git a/include/physics.h b/include/physics.h index b393d08..8c50fb4 100644 --- a/include/physics.h +++ b/include/physics.h @@ -2,4 +2,4 @@ #include "game.h" -void MoveAndSlide(Entity* e, float dt);
\ No newline at end of file +void MoveAndSlide(Entity* e, float dt); diff --git a/include/utils.h b/include/utils.h index 5a4a4ac..cb1770e 100644 --- a/include/utils.h +++ b/include/utils.h @@ -60,4 +60,21 @@ static inline Rectangle GetCurrentSourceRectangle(const Animation* animation) { return animation->srcRects[animation->currentFrame]; } +static inline Rectangle GetEntityHitboxGlobal(const Entity* e, int hbIndex) { + Rectangle hitBoxGlobal = e->hitBoxes[hbIndex]; + hitBoxGlobal.x += e->position.x; + hitBoxGlobal.y += e->position.y; + + return hitBoxGlobal; +} + +static inline Rectangle GetEntityHurtboxGlobal(const Entity* e, int hbIndex) { + Rectangle hurtBoxGlobal = e->hurtBoxes[hbIndex]; + hurtBoxGlobal.x += e->position.x; + hurtBoxGlobal.y += e->position.y; + + return hurtBoxGlobal; +} + + #endif // UTILS_H_ diff --git a/src/background.c b/src/background.c index 7929fa1..a2b3334 100644 --- a/src/background.c +++ b/src/background.c @@ -14,17 +14,7 @@ void AddBackground(const char* backgroundTextureName) { e.position = (Vector2) {0.0f, 0.0f}; Asset* backgroundTextureAsset = - GetMatchingAssetWithType(backgroundTextureName, Texture_Asset); - - if (backgroundTextureAsset == NULL) { - - TraceLog(LOG_ERROR, - "Failed to create background from texture asset: %s, asset not found", - backgroundTextureName - ); - exit(EXIT_FAILURE); - - } + GetMatchingAssetExitOnFail(backgroundTextureName, Texture_Asset); Texture2D backgroundTexture = backgroundTextureAsset->texture; const float backgroundSizeScale = (float)WINDOW_HEIGHT / backgroundTexture.height; @@ -63,6 +63,46 @@ void UpdateEntity(Entity* e, float deltaTime) { } } +static bool IsHitting(const Entity* a, const Entity* b) { + if (!PhysicsEnabled(a) || !PhysicsEnabled(b)) + return false; + + for (int i = 0; i < a->numHitBoxes; i++) { + for (int j = 0; j < b->numHurtBoxes; j++) { + Rectangle hitbox = GetEntityHitboxGlobal(a, i); + Rectangle hurtbox = GetEntityHurtboxGlobal(b, j); + + if (CheckCollisionRecs(hitbox, hurtbox)) + return true; + } + } + + return false; +} + +void EntityHandleHit(Entity* a, Entity* b) { + switch(a->type) { + + default: + break; + } +} + +void HandleHitboxInteractions() { + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + for (int j = i + 1; j < MAX_ENTITY_COUNT; j++) { + Entity* a = &game.entities[i]; + Entity* b = &game.entities[j]; + + if (IsHitting(a, b)) + EntityHandleHit(a, b); + + if (IsHitting(b, a)) + EntityHandleHit(b, a); + } + } +} + void UpdateGame(float deltaTime) { #ifdef BEATEMUP_DEBUG if (IsKeyPressed(KEY_F3)) { @@ -75,6 +115,8 @@ void UpdateGame(float deltaTime) { if (EntityAllocated(e)) UpdateEntity(e, deltaTime); } + + HandleHitboxInteractions(); } void DrawEntitySprite(const Entity* e, int spriteIndex) { @@ -160,8 +202,7 @@ void DrawGame() { #ifdef BEATEMUP_DEBUG if (game.enableDebugOverlay) { - const int posX = 10, posY = 10; - DrawFPS(posX, posY); + DrawFPS(FPS_POS_X, FPS_POS_Y); } #endif |