diff options
| -rw-r--r-- | include/barrel.h | 1 | ||||
| -rw-r--r-- | include/constants.h | 2 | ||||
| -rw-r--r-- | include/game.h | 20 | ||||
| -rw-r--r-- | include/player_data.h | 16 | ||||
| -rw-r--r-- | src/barrel.c | 7 | ||||
| -rw-r--r-- | src/game.c | 19 | ||||
| -rw-r--r-- | src/physics.c | 21 | ||||
| -rw-r--r-- | src/player.c | 17 |
8 files changed, 79 insertions, 24 deletions
diff --git a/include/barrel.h b/include/barrel.h index 7cc3ea0..d905004 100644 --- a/include/barrel.h +++ b/include/barrel.h @@ -4,3 +4,4 @@ void AddBarrel(float xpos, float ypos); void UpdateBarrel(float dt); +void BarrelHandleCollision(Entity* barrel); diff --git a/include/constants.h b/include/constants.h index a2ea961..366da35 100644 --- a/include/constants.h +++ b/include/constants.h @@ -15,3 +15,5 @@ #define FPS_POS_X 10 #define FPS_POS_Y 10 + +#define COLLIDER_ALPHA 170 diff --git a/include/game.h b/include/game.h index b9df2ce..d79c790 100644 --- a/include/game.h +++ b/include/game.h @@ -65,10 +65,12 @@ typedef struct Entity { Sprite sprites[MAX_SPRITE_COUNT]; int numSprites; - #ifdef BEATEMUP_DEBUG - //Debug information - Color physicsColliderColor; - #endif +#ifdef BEATEMUP_DEBUG + //Debug information + Color physicsColliderColor; + Color hitboxColors[MAX_AREA_COUNT]; + Color hurtboxColors[MAX_AREA_COUNT]; +#endif //Human entity information int bodySpriteIndex; @@ -80,10 +82,10 @@ typedef struct Game { Entity entities[MAX_ENTITY_COUNT]; Camera2D camera; - #ifdef BEATEMUP_DEBUG - //Debug information - bool enableDebugOverlay; - #endif +#ifdef BEATEMUP_DEBUG + //Debug information + bool enableDebugOverlay; +#endif } Game; //Entity Stuff @@ -104,7 +106,7 @@ Rectangle GetSrcRectFromIndex int framesX, int framesY, int index -); + ); typedef struct { Texture texture; diff --git a/include/player_data.h b/include/player_data.h index f8e937a..7df3c77 100644 --- a/include/player_data.h +++ b/include/player_data.h @@ -5,27 +5,37 @@ #define PLAYER_SPEED 300.0f -static const Rectangle shadowDestRect = (Rectangle) { +Rectangle shadowDestRect = (Rectangle) { .x = -20, .y = 115, .width = 128, .height = 20 }; -static const Rectangle physicsCollider = (Rectangle) { +Rectangle physicsCollider = (Rectangle) { .x = NO_OFFSET, .y = NO_OFFSET, .width = 100, .height = 100 }; -static const Rectangle bodyDestRect = (Rectangle) { +Rectangle bodyDestRect = (Rectangle) { .x = -160, .y = -280, .width = 400, .height = 400 }; +#define HITBOX_XPOS 70 +#define HITBOX_XPOS_FLIPPED -90 + +Rectangle punchHitBox = (Rectangle) { + .x = HITBOX_XPOS, + .y = -5, + .width = 100, + .height = 40 +}; + #define IDLE_FTIME 0.1f #endif // PLAYER_DATA_H_ diff --git a/src/barrel.c b/src/barrel.c index b0ce9a7..df1e29e 100644 --- a/src/barrel.c +++ b/src/barrel.c @@ -28,9 +28,12 @@ void AddBarrel(float xpos, float ypos) { AddSpriteToEntity(&barrel, barrelSprite); barrel.physicsCollider = physicsCollider; + barrel.hurtBoxes[0] = physicsCollider; + barrel.numHurtBoxes = 1; #ifdef BEATEMUP_DEBUG barrel.physicsColliderColor = RED; + barrel.hurtboxColors[0] = BLUE; #endif AddEntity(&barrel); @@ -39,3 +42,7 @@ void AddBarrel(float xpos, float ypos) { void UpdateBarrel(float dt) { UNUSED(dt); } + +void BarrelHandleCollision(Entity* barrel) { + barrel->flags &= !ENTITY_ALLOCATED; +} @@ -11,6 +11,8 @@ #include <math.h> #include "utils.h" +#include "barrel.h" + Game game; int compare(const void* a, const void* b) { @@ -63,14 +65,14 @@ void UpdateEntity(Entity* e, float deltaTime) { } } -static bool IsHitting(const Entity* a, const Entity* b) { +static bool IsBeingHit(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); + for (int i = 0; i < a->numHurtBoxes; i++) { + for (int j = 0; j < b->numHitBoxes; j++) { + Rectangle hitbox = GetEntityHurtboxGlobal(a, i); + Rectangle hurtbox = GetEntityHitboxGlobal(b, j); if (CheckCollisionRecs(hitbox, hurtbox)) return true; @@ -82,6 +84,9 @@ static bool IsHitting(const Entity* a, const Entity* b) { void EntityHandleHit(Entity* a, Entity* b) { switch(a->type) { + case Barrel_Entity: + BarrelHandleCollision(a); + break; default: break; @@ -94,10 +99,10 @@ void HandleHitboxInteractions() { Entity* a = &game.entities[i]; Entity* b = &game.entities[j]; - if (IsHitting(a, b)) + if (IsBeingHit(a, b)) EntityHandleHit(a, b); - if (IsHitting(b, a)) + if (IsBeingHit(b, a)) EntityHandleHit(b, a); } } diff --git a/src/physics.c b/src/physics.c index 6781c41..72d9410 100644 --- a/src/physics.c +++ b/src/physics.c @@ -1,6 +1,7 @@ #include "physics.h" #include "game.h" #include "utils.h" +#include "constants.h" extern Game game; @@ -54,8 +55,24 @@ void DebugHighlights(const Entity* e) { Rectangle dstRect = GetPhysicsColliderGlobal(e); Color colliderDrawColor = e->physicsColliderColor; - colliderDrawColor.a = 170; + colliderDrawColor.a = COLLIDER_ALPHA; - DrawRectangle(dstRect.x, dstRect.y, dstRect.width, dstRect.height, colliderDrawColor); + DrawRectangleRec(dstRect, colliderDrawColor); + + for (int i = 0; i < e->numHitBoxes; i++) { + Color hitboxDrawColor = e->hitboxColors[i]; + hitboxDrawColor.a = COLLIDER_ALPHA; + + Rectangle dstRect = GetEntityHitboxGlobal(e, i); + DrawRectangleRec(dstRect, hitboxDrawColor); + } + + for (int i = 0; i < e->numHurtBoxes; i++) { + Color hurtboxDrawColor = e->hurtboxColors[i]; + hurtboxDrawColor.a = COLLIDER_ALPHA; + + Rectangle dstRect = GetEntityHurtboxGlobal(e, i); + DrawRectangleRec(dstRect, hurtboxDrawColor); + } } #endif diff --git a/src/player.c b/src/player.c index 05d3546..7d41b57 100644 --- a/src/player.c +++ b/src/player.c @@ -80,6 +80,7 @@ void HandleAttack(Entity* player) { } Sprite* bodySprite = &player->sprites[player->bodySpriteIndex]; + player->numHitBoxes = 1; if (bodySprite->animations[PLAYER_ATTACK].isComplete) { player->state = PLAYER_IDLE; @@ -110,9 +111,16 @@ void FlipBody(Entity* player) { Sprite* bodySprite = &player->sprites[player->bodySpriteIndex]; if (player->velocity.x < 0) { + bodySprite->flipX = true; + player->hitBoxes[0].x = HITBOX_XPOS_FLIPPED; + + } else if (player->velocity.x > 0) { + bodySprite->flipX = false; + player->hitBoxes[0].x = HITBOX_XPOS; + } } @@ -135,10 +143,13 @@ void AddPlayer(float xpos, float ypos) { player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE); player.physicsCollider = physicsCollider; + player.hitBoxes[0] = punchHitBox; + player.numHitBoxes = 1; - #ifdef BEATEMUP_DEBUG - player.physicsColliderColor = RED; - #endif +#ifdef BEATEMUP_DEBUG + player.physicsColliderColor = RED; + player.hitboxColors[0] = BLUE; +#endif AddPlayerSprites(&player); AddEntity(&player); |
