summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/barrel.h2
-rw-r--r--include/barrel_data.h20
-rw-r--r--include/constants.h2
-rw-r--r--include/game.h6
-rw-r--r--include/physics.h1
-rw-r--r--src/background.c1
-rw-r--r--src/barrel.c45
-rw-r--r--src/game.c10
-rw-r--r--src/physics.c5
-rw-r--r--src/player.c4
10 files changed, 81 insertions, 15 deletions
diff --git a/include/barrel.h b/include/barrel.h
index d905004..04f179d 100644
--- a/include/barrel.h
+++ b/include/barrel.h
@@ -3,5 +3,5 @@
#include "game.h"
void AddBarrel(float xpos, float ypos);
-void UpdateBarrel(float dt);
+void UpdateBarrel(Entity* barrel, float dt);
void BarrelHandleCollision(Entity* barrel);
diff --git a/include/barrel_data.h b/include/barrel_data.h
index 97a13db..8dbfd5e 100644
--- a/include/barrel_data.h
+++ b/include/barrel_data.h
@@ -1,15 +1,31 @@
#include "barrel.h"
-static const Rectangle physicsCollider = (Rectangle) {
+static Rectangle physicsCollider = (Rectangle) {
.x = NO_OFFSET,
.y = NO_OFFSET,
.width = 100,
.height = 120
};
-static const Rectangle destRect = (Rectangle) {
+static Rectangle destRect = (Rectangle) {
.x = -65,
.y = -140,
.width = 230,
.height = 300
};
+
+static Rectangle srcRect = (Rectangle) {
+ .x = 0,
+ .y = 0,
+ .width = 32,
+ .height = 32
+};
+
+static Rectangle srcRectDamaged = (Rectangle) {
+ .x = 32,
+ .y = 0,
+ .width = 32,
+ .height = 32
+};
+
+#define BARREL_REMOVE_TIME 1.0f
diff --git a/include/constants.h b/include/constants.h
index 366da35..7e4a6fe 100644
--- a/include/constants.h
+++ b/include/constants.h
@@ -17,3 +17,5 @@
#define FPS_POS_Y 10
#define COLLIDER_ALPHA 170
+
+#define GRAVITY 700.0f
diff --git a/include/game.h b/include/game.h
index d79c790..558f96f 100644
--- a/include/game.h
+++ b/include/game.h
@@ -38,6 +38,7 @@ typedef struct Sprite {
DrawLayer layer;
Rectangle destRect; //Destination rectangle relative to player position
Rectangle srcRect; //For un-animated sprites only
+ float alpha;
bool flipX;
bool flipY;
@@ -51,6 +52,7 @@ typedef struct Entity {
int id;
EntityType type;
uint16_t flags;
+ int state;
Vector2 position;
Vector2 velocity;
@@ -74,7 +76,9 @@ typedef struct Entity {
//Human entity information
int bodySpriteIndex;
- int state;
+
+ //Barrel entity information
+ float timeSinceLastHit;
} Entity;
typedef struct Game {
diff --git a/include/physics.h b/include/physics.h
index 5079293..29852fb 100644
--- a/include/physics.h
+++ b/include/physics.h
@@ -3,3 +3,4 @@
#include "game.h"
void MoveAndStop(Entity* e, float dt);
+void MoveWithoutPhysics(Entity* e, float dt);
diff --git a/src/background.c b/src/background.c
index a2b3334..6816dbf 100644
--- a/src/background.c
+++ b/src/background.c
@@ -26,6 +26,7 @@ void AddBackground(const char* backgroundTextureName) {
.texture = backgroundTexture,
.layer = Background_Layer,
.numAnimations = 0,
+ .alpha = 1.0,
.srcRect = {0, 0, backgroundTexture.width, backgroundTexture.height},
.destRect = {NO_OFFSET, NO_OFFSET, backgroundBounds.x, backgroundBounds.y}
diff --git a/src/barrel.c b/src/barrel.c
index df1e29e..474b151 100644
--- a/src/barrel.c
+++ b/src/barrel.c
@@ -2,6 +2,14 @@
#include "barrel_data.h"
#include "utils.h"
#include "assets.h"
+#include "physics.h"
+#include "constants.h"
+#include <raymath.h>
+
+enum Barrel_State {
+ BARREL_NEUTRAL,
+ BARREL_DAMAGED
+};
void AddBarrel(float xpos, float ypos) {
Entity barrel = {0};
@@ -15,14 +23,10 @@ void AddBarrel(float xpos, float ypos) {
Sprite barrelSprite = (Sprite) {
.texture = barrelTexture->texture,
.layer = Foreground_Layer,
+ .alpha = 1.0,
.destRect = destRect,
- .srcRect = {
- .x = 0,
- .y = 0,
- .width = barrelTexture->texture.width / 2,
- .height = barrelTexture->texture.height
- }
+ .srcRect = srcRect
};
AddSpriteToEntity(&barrel, barrelSprite);
@@ -31,6 +35,8 @@ void AddBarrel(float xpos, float ypos) {
barrel.hurtBoxes[0] = physicsCollider;
barrel.numHurtBoxes = 1;
+ barrel.state = BARREL_NEUTRAL;
+
#ifdef BEATEMUP_DEBUG
barrel.physicsColliderColor = RED;
barrel.hurtboxColors[0] = BLUE;
@@ -39,10 +45,31 @@ void AddBarrel(float xpos, float ypos) {
AddEntity(&barrel);
}
-void UpdateBarrel(float dt) {
- UNUSED(dt);
+void UpdateBarrel(Entity* barrel, float dt) {
+ if (barrel->state == BARREL_DAMAGED) {
+ barrel->velocity.y += GRAVITY * dt * 0.5;
+ MoveWithoutPhysics(barrel, dt);
+ barrel->velocity.y += GRAVITY * dt * 0.5;
+
+ barrel->sprites[0].alpha =
+ 1.0 - (barrel->timeSinceLastHit / BARREL_REMOVE_TIME);
+
+ barrel->timeSinceLastHit += dt;
+ if (barrel->timeSinceLastHit > BARREL_REMOVE_TIME)
+ barrel->flags &= ~ENTITY_ALLOCATED;
+ }
+}
+
+void GetDamaged(Entity* barrel) {
+ barrel->sprites[0].srcRect = srcRectDamaged;
+
+ barrel->flags &= ~ENTITY_PHYSICS_ACTIVE;
+ barrel->state = BARREL_DAMAGED;
+
+ barrel->velocity = (Vector2) {100.0f, -400.0f};
+ barrel->timeSinceLastHit = 0.0f;
}
void BarrelHandleCollision(Entity* barrel) {
- barrel->flags &= !ENTITY_ALLOCATED;
+ GetDamaged(barrel);
}
diff --git a/src/game.c b/src/game.c
index 6e72b97..a2516b7 100644
--- a/src/game.c
+++ b/src/game.c
@@ -60,6 +60,10 @@ void UpdateEntity(Entity* e, float deltaTime) {
UpdatePlayer(e, deltaTime);
break;
+ case Barrel_Entity:
+ UpdateBarrel(e, deltaTime);
+ break;
+
default:
break;
}
@@ -147,7 +151,11 @@ void DrawEntitySprite(const Entity* e, int spriteIndex) {
if (drawnSprite->flipY)
srcRect.height = -srcRect.height;
- DrawTexturePro(drawnSprite->texture, srcRect, destRect, origin, rotation, WHITE);
+ Vector4 colorMaskV = ColorNormalize(WHITE);
+ colorMaskV.w = drawnSprite->alpha;
+ Color colorMask = ColorFromNormalized(colorMaskV);
+
+ DrawTexturePro(drawnSprite->texture, srcRect, destRect, origin, rotation, colorMask);
}
diff --git a/src/physics.c b/src/physics.c
index 72d9410..27b21bd 100644
--- a/src/physics.c
+++ b/src/physics.c
@@ -5,6 +5,11 @@
extern Game game;
+void MoveWithoutPhysics(Entity* e, float dt) {
+ e->position.x += e->velocity.x * dt;
+ e->position.y += e->velocity.y * dt;
+}
+
void MoveAndStop(Entity* e, float deltaTime) {
Vector2 velocity = e->velocity;
Rectangle physicsCollider = GetPhysicsColliderGlobal(e);
diff --git a/src/player.c b/src/player.c
index 7d41b57..2404bd5 100644
--- a/src/player.c
+++ b/src/player.c
@@ -162,6 +162,7 @@ static void AddPlayerSprites(Entity* player) {
.texture = shadowTexture->texture,
.layer = Foreground_Layer,
.destRect = shadowDestRect,
+ .alpha = 1.0,
.srcRect = {
0,
@@ -179,6 +180,7 @@ static void AddPlayerSprites(Entity* player) {
Sprite bodySprite = (Sprite) {
.texture = bodyTexture->texture,
.layer = Foreground_Layer,
+ .alpha = 1.0,
.destRect = bodyDestRect,
.numAnimations = 0
@@ -227,7 +229,7 @@ static void AddPlayerSprites(Entity* player) {
.frameTimes = (float[]) {0.05f, 0.05f, 0.05f, 0.05f}
});
bodySprite.animations[PLAYER_ATTACK] = punchAnimation;
-
bodySprite.numAnimations = 2;
+
player->bodySpriteIndex = AddSpriteToEntity(player, bodySprite);
}