Skip to content

Commit

Permalink
Stats screen
Browse files Browse the repository at this point in the history
  • Loading branch information
bit69tream committed Jan 4, 2024
1 parent 686e475 commit 3a6c512
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 64 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ _TODO: Show your game to the world, animated GIFs recommended!._
- `shot03.wav` - [Shapeforms Audio Free Sound Effects](https://shapeforms.itch.io/shapeforms-audio-free-sfx)
- `border.wav` - [Shapeforms Audio Free Sound Effects](https://shapeforms.itch.io/shapeforms-audio-free-sfx)
- `beep.wav` - [Shapeforms Audio Free Sound Effects](https://shapeforms.itch.io/shapeforms-audio-free-sfx)
- `dead.wav` - [Shapeforms Audio Free Sound Effects](https://shapeforms.itch.io/shapeforms-audio-free-sfx)
- `drozerix_-_stardust_jam.mod` - [Drozerix - Stardust Jam](https://modarchive.org/module.php?201039)
- `once_is_not_enough.mod` - [once is not enough](https://modarchive.org/module.php?170002) [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

Expand Down
Binary file added src/resources/dead.wav
Binary file not shown.
241 changes: 177 additions & 64 deletions src/stribun.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef enum {
GAME_BOSS,
GAME_BOSS_DEAD,
GAME_PLAYER_DEAD,
GAME_STATS,
} GameState;

static Vector2 cameraIntroductionTarget = {0};
Expand Down Expand Up @@ -104,6 +105,13 @@ typedef struct {
bool isInvincible;
} Player;

typedef struct {
float time;
int kills;
} PlayerStats;

static PlayerStats playerStats = {0};

#define MAX_BOUNDING_CIRCLES 3

typedef struct {
Expand Down Expand Up @@ -317,6 +325,8 @@ static Player player = {0};

static float time = 0;

static Sound playerDeathSound = {0};

static Sound dashSoundEffect = {0};
static Sound playerShot = {0};
static Sound beep = {0};
Expand Down Expand Up @@ -724,6 +734,8 @@ void bossMarineAttack(void) {

void updateBossMarine(void) {
if (bossMarine.health <= 0) {
PauseMusicStream(bossMarineMusic);
playerStats.kills += 1;
gameState = GAME_BOSS_DEAD;

for (int i = 0; i < PROJECTILES_MAX; i++) {
Expand Down Expand Up @@ -777,7 +789,9 @@ void updateMouse(void) {
}

mouseCursor = GetScreenToWorld2D(screenMouseLocation, camera);
if (gameState == GAME_MAIN_MENU) {
if (gameState == GAME_MAIN_MENU ||
gameState == GAME_STATS ||
gameState == GAME_TUTORIAL) {
mouseCursor = screenMouseLocation;
}

Expand Down Expand Up @@ -1519,17 +1533,17 @@ void renderPhase1(void) {
renderBackgroundAsteroid();

if (gameState == GAME_BOSS ||
(gameState == GAME_BOSS_INTRODUCTION && introductionStage != BOSS_INTRODUCTION_BEGINNING)) {
(gameState == GAME_BOSS_INTRODUCTION &&
introductionStage != BOSS_INTRODUCTION_BEGINNING)) {
renderArenaBorder();
}

renderAsteroids();

if (gameState == GAME_BOSS_DEAD) {
if (gameState == GAME_BOSS_DEAD || gameState == GAME_PLAYER_DEAD) {
DrawRectangleV(Vector2Zero(),
level,
ColorAlpha(BLACK,
blackBackgroundAlpha));
ColorAlpha(BLACK, blackBackgroundAlpha));
}

renderBoss();
Expand Down Expand Up @@ -1759,6 +1773,12 @@ void checkRegularProjectileCollision(int i) {
CheckCollisionCircles(proj, radius, player.position, PLAYER_HITBOX_RADIUS)) {
projectiles[i].willBeDestroyed = true;
player.health -= projectiles[i].damage;

if (player.health == 0) {
gameState = GAME_PLAYER_DEAD;
PauseMusicStream(bossMarineMusic);
PlaySound(playerDeathSound);
}
return;
}

Expand Down Expand Up @@ -1996,6 +2016,63 @@ void initRaylib() {
#endif
}

void initBackgroundAsteroid(void) {
bigAssAsteroidPosition.x = (float) GetRandomValue(0, LEVEL_WIDTH);
bigAssAsteroidPosition.y = (float) GetRandomValue(0, LEVEL_HEIGHT);

bigAssAsteroidAngle = (float) GetRandomValue(0, 360);

bigAssAsteroidPositionDelta.x = (float)GetRandomValue(-1, 1) * 0.05f;
bigAssAsteroidPositionDelta.y = (float)GetRandomValue(-1, 1) * 0.05f;

bigAssAsteroidAngleDelta = (float)GetRandomValue(-1, 1) * 0.005f;
}

void initBossMarine(void) {
memset(&bossMarine, 0, sizeof(bossMarine));

bossMarine = (BossMarine) {
.position = {
.x = (float)LEVEL_WIDTH / 2,
.y = (float)LEVEL_HEIGHT / 2,
},
.health = BOSS_MARINE_MAX_HEALTH,
.boundingCircles = {
{{4, -25}, 12},
{{25, -14}, 5},
{{2, 22}, 15},
{{0, 12}, 14},
{{7, 10}, 12},
{{13, 0}, 7},
{{-14, 2}, 8},
{{-23, -9}, 12},
{{-10, -8}, 14},
{{6, -8}, 8},
{{17, -11}, 9},
{{27, -5}, 8},
},
.weaponAngle = 0,
.horizontalFlip = 1,
.walkingDirection = 1,
.weaponOffset = bossMarineInitialWeaponOffset,
};

for (int i = 0; i < BOSS_MARINE_BOUNDING_CIRCLES; i++) {
bossMarine.boundingCircles[i].position =
Vector2Scale(bossMarine.boundingCircles[i].position,
SPRITES_SCALE);
bossMarine.boundingCircles[i].radius *= SPRITES_SCALE;
}
};

void initMusic(void) {
mainMenuMusic = LoadMusicStream("resources/drozerix_-_stardust_jam.mod");
SetMusicVolume(mainMenuMusic, 0.8);

bossMarineMusic = LoadMusicStream("resources/once_is_not_enough.mod");
SetMusicVolume(bossMarineMusic, 0.5);
}

void initMouse(void) {
DisableCursor();

Expand All @@ -2007,6 +2084,7 @@ void initMouse(void) {

void initPlayer(void) {
memset(&player, 0, sizeof(player));
memset(&playerStats, 0, sizeof(playerStats));

player = (Player) {
.position = (Vector2) {
Expand Down Expand Up @@ -2065,6 +2143,8 @@ void initSoundEffects(void) {

bossMarineShotgunSound = LoadSound("resources/shot02.wav");
bossMarineGunshotSound = LoadSound("resources/shot03.wav");

playerDeathSound = LoadSound("resources/dead.wav");
}

void initShaders(void) {
Expand Down Expand Up @@ -2347,6 +2427,7 @@ void updateButtons(void) {
gameState = GAME_TUTORIAL;
} return;
case BUTTON_ACTION_TOGGLE_CONTROLS: {
PlaySound(beep);
esdf = !esdf;
} break;
case BUTTON_ACTION_QUIT: exit(0);
Expand Down Expand Up @@ -2439,7 +2520,6 @@ void renderMainMenu(void) {
} EndDrawing();
}

static float mainMenuMusicVolume = 0.8f;
void updateMainMenuMusic(void) {
if (!IsMusicStreamPlaying(mainMenuMusic)) {
PlayMusicStream(mainMenuMusic);
Expand Down Expand Up @@ -2554,6 +2634,8 @@ void updateAndRenderTutorial(void) {
return;
}

updateMouse();

float mul = camera.zoom * 2;
Font f = GetFontDefault();
float fontSize = 25 * mul;
Expand Down Expand Up @@ -2591,6 +2673,7 @@ void updateAndRenderTutorial(void) {

DrawTextPro(f, text1, pos, Vector2Scale(size, 0.5f), 0, fontSize, spacing, WHITE);

renderMouseCursor();
} EndDrawing();
}

Expand All @@ -2613,7 +2696,17 @@ void updateDeadBoss(void) {
if (Vector2Distance(bossMarine.weaponOffset, headshotWeaponOffset) < 0.5 &&
fabsf(bossMarine.weaponAngle) < 0.1f) {
PlaySound(bossMarineShotgunSound);
gameState = GAME_MAIN_MENU;
ResumeMusicStream(bossMarineMusic);
gameState = GAME_STATS;
}
}

static float deadPlayerTime = 2.0f;

void updateDeadPlayer(void) {
if (deadPlayerTime <= 0.0f) {
ResumeMusicStream(bossMarineMusic);
gameState = GAME_STATS;
}
}

Expand All @@ -2633,6 +2726,77 @@ void updateAndRenderBossDead(void) {
renderFinal();
}

void updateAndRenderPlayerDead(void) {
blackBackgroundAlpha = Lerp(blackBackgroundAlpha,
1.0f,
0.2f);
blackBackgroundAlpha = Clamp(blackBackgroundAlpha,
0, 1);

deadPlayerTime -= GetFrameTime();

updateMouse();
updateCamera();
updateProjectiles();
updateDeadPlayer();

renderPhase1();
renderFinal();
}

void updateAndRenderStats(void) {
if (GetKeyPressed() != KEY_NULL ||
IsMouseButtonPressed(MOUSE_BUTTON_LEFT) ||
IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
gameState = GAME_MAIN_MENU;
initPlayer();
initBossMarine();
initAsteroids();
initBackgroundAsteroid();
initProjectiles();

for (int i = 0; i < THRUSTER_TRAILS_MAX; i++) {
thrusterTrail[i].alpha = 0.0f;
thrusterTrail[i].origin = Vector2Zero();
thrusterTrail[i].angle = 0.0f;
}

return;
}

UpdateMusicStream(bossMarineMusic);
updateMouse();

float mul = camera.zoom * 2;
Font f = GetFontDefault();
float fontSize = 25 * mul;
float spacing = 2 * mul;

BeginDrawing(); {
ClearBackground(BLACK);

const char *time_stat = TextFormat("TIME: %.2f", playerStats.time);
Vector2 pos = {
.x = ((float)GetScreenWidth() / 2.0f),
.y = ((float)GetScreenHeight() / 2.0f),
};

Vector2 size = MeasureTextEx(f, time_stat, fontSize, spacing);

pos.y -= size.y / 2;

DrawTextPro(f, time_stat, pos, Vector2Scale(size, 0.5f), 0, fontSize, spacing, WHITE);
pos.y += size.y;

const char *kills_stat = TextFormat("KILLS: %d", playerStats.kills);
size = MeasureTextEx(f, kills_stat, fontSize, spacing);

DrawTextPro(f, kills_stat, pos, Vector2Scale(size, 0.5f), 0, fontSize, spacing, WHITE);

renderMouseCursor();
} EndDrawing();
}

void UpdateDrawFrame(void) {
switch (gameState) {
case GAME_MAIN_MENU:
Expand All @@ -2646,14 +2810,18 @@ void UpdateDrawFrame(void) {
break;
case GAME_BOSS:
updateAndRenderBossFight();
playerStats.time += GetFrameTime();
break;
case GAME_BOSS_DEAD: {
updateAndRenderBossDead();
} break;
case GAME_PLAYER_DEAD:
exit(1);
updateAndRenderPlayerDead();
break;
}
case GAME_STATS:
updateAndRenderStats();
break;
}

time += GetFrameTime();
}
Expand All @@ -2662,61 +2830,6 @@ float randomFloat(void) {
return (float)rand() / (float)RAND_MAX;
}

void initBackgroundAsteroid(void) {
bigAssAsteroidPosition.x = (float) GetRandomValue(0, LEVEL_WIDTH);
bigAssAsteroidPosition.y = (float) GetRandomValue(0, LEVEL_HEIGHT);

bigAssAsteroidAngle = (float) GetRandomValue(0, 360);

bigAssAsteroidPositionDelta.x = (float)GetRandomValue(-1, 1) * 0.05f;
bigAssAsteroidPositionDelta.y = (float)GetRandomValue(-1, 1) * 0.05f;

bigAssAsteroidAngleDelta = (float)GetRandomValue(-1, 1) * 0.005f;
}

void initBossMarine(void) {
bossMarine = (BossMarine) {
.position = {
.x = (float)LEVEL_WIDTH / 2,
.y = (float)LEVEL_HEIGHT / 2,
},
.health = BOSS_MARINE_MAX_HEALTH,
.boundingCircles = {
{{4, -25}, 12},
{{25, -14}, 5},
{{2, 22}, 15},
{{0, 12}, 14},
{{7, 10}, 12},
{{13, 0}, 7},
{{-14, 2}, 8},
{{-23, -9}, 12},
{{-10, -8}, 14},
{{6, -8}, 8},
{{17, -11}, 9},
{{27, -5}, 8},
},
.weaponAngle = 0,
.horizontalFlip = 1,
.walkingDirection = 1,
.weaponOffset = bossMarineInitialWeaponOffset,
};

for (int i = 0; i < BOSS_MARINE_BOUNDING_CIRCLES; i++) {
bossMarine.boundingCircles[i].position =
Vector2Scale(bossMarine.boundingCircles[i].position,
SPRITES_SCALE);
bossMarine.boundingCircles[i].radius *= SPRITES_SCALE;
}
};

void initMusic(void) {
mainMenuMusic = LoadMusicStream("resources/drozerix_-_stardust_jam.mod");
SetMusicVolume(mainMenuMusic, mainMenuMusicVolume);

bossMarineMusic = LoadMusicStream("resources/once_is_not_enough.mod");
SetMusicVolume(bossMarineMusic, 0.5);
}

int main(void) {
initRaylib();
initMouse();
Expand Down

0 comments on commit 3a6c512

Please sign in to comment.