Avoid/src/Gameplay.c

418 lines
15 KiB
C
Raw Normal View History

/*
- Avoid ~ a game by Canneddonuts
- Filename ~ Gameplay.c
- Author ~ Return0ne
- 2022
- *no license*
*/
#include "../include/raylib.h"
#include "Screens.h"
#include "Controls.h"
2022-07-19 15:04:05 +00:00
#include "Options.h"
#include "Gameplay.h"
#include "Stats.h"
2022-06-26 23:43:39 +00:00
#include "Timers.h"
2022-08-07 20:24:55 +00:00
#include "Music.h"
2022-08-05 15:31:55 +00:00
#include "Gfx.h"
int score = 0, bestscore = 0, finishfromGameplayScreen = 0, redfeathers = 0, greenfeathers = 0;
2022-07-25 19:21:52 +00:00
2022-08-22 19:16:28 +00:00
Music Gameplaysong = { 0 };
2022-08-07 20:24:55 +00:00
bool CheckFireworkActivity(void)
{
int matches = 0, val = 0;
for (int i = 0; i < MAX_FIREWORKS; i++) {
if (fireworks[i].active == val) matches++;
}
if (matches == MAX_FIREWORKS) return true;
else return false;
}
void LoadGamplayScreen(void)
{
2022-08-27 18:08:36 +00:00
player.fxhit = LoadSound("assets/sfx/hit.wav");
enemy.fxhit = LoadSound("assets/sfx/boom.wav");
player_sprite = LoadTexture("assets/gfx/player.png");
enemy_sprite = LoadTexture("assets/gfx/enemy.png");
fxfeather = LoadSound("assets/sfx/feather.wav");
feather_sprite = LoadTexture("assets/gfx/feather.png");
attack_sprite = LoadTexture("assets/gfx/attack.png");
firework_sprite = LoadTexture("assets/gfx/firework.png");
2022-08-22 19:16:28 +00:00
Gameplaysong = LoadMusicStream("assets/bgm/03-Boss.ogg");
}
void InitGameplayScreen(void)
{
2022-08-22 19:16:28 +00:00
PlayMusicStream(Gameplaysong);
2022-07-19 15:04:05 +00:00
finishfromGameplayScreen = 0;
2022-07-28 16:02:47 +00:00
globalTimer = 0;
2022-09-20 23:28:31 +00:00
if (player.hp < 1) player.hp = 1;
player.currentframe = 0;
player.speed = 300.0f;
if (GI_callcount < 1) {
player.frameRec = (Rectangle) {
player.hitbox.x,
player.hitbox.y,
(float) player_sprite.width/3,
(float) player_sprite.height
};
}
player.hitbox = (Rectangle) {
2022-06-19 21:36:26 +00:00
0,
2022-07-08 20:00:38 +00:00
100,
2022-06-18 16:10:36 +00:00
(float) player_sprite.width/3,
(float) player_sprite.height/2 +5
};
2022-08-02 13:38:36 +00:00
player.iframetimer = 0;
player.in = false;
2022-06-26 23:43:39 +00:00
player.color = RAYWHITE;
2022-06-17 16:32:35 +00:00
enemy.currentframe = 0;
2022-07-26 23:12:36 +00:00
enemy.hp = 5;
2022-08-22 19:16:28 +00:00
enemy.speed = 200.0f;
if (GI_callcount < 1) {
enemy.frameRec = (Rectangle) {
enemy.hitbox.x,
enemy.hitbox.y,
(float) enemy_sprite.width/2,
(float) enemy_sprite.height
};
}
2022-06-17 16:32:35 +00:00
enemy.hitbox = (Rectangle) {
2022-06-19 21:36:26 +00:00
690,
2022-08-22 19:16:28 +00:00
20,
(float) enemy_sprite.width/2,
2022-06-18 16:10:36 +00:00
(float) enemy_sprite.height
2022-06-17 16:32:35 +00:00
};
2022-06-26 23:43:39 +00:00
enemy.color = RAYWHITE;
2022-08-02 13:38:36 +00:00
enemy.in = false;
enemy.iframetimer = 0;
2022-07-08 20:00:38 +00:00
feather.hitbox = (Rectangle) {
2022-08-26 16:24:33 +00:00
GetScreenWidth() - feather_sprite.width,
2022-06-29 16:11:37 +00:00
GetRandomValue(0, GetScreenHeight() - feather_sprite.height),
(float) feather_sprite.width,
(float) feather_sprite.height
};
2022-08-26 16:24:33 +00:00
feather.active = false;
2022-07-08 20:00:38 +00:00
feather.power = 0;
2022-06-18 16:10:36 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
fireworks[i].active = 1;
2022-06-18 16:10:36 +00:00
fireworks[i].hitbox = (Rectangle) {
GetScreenWidth() + firework_sprite.width,
0,
2022-06-26 23:43:39 +00:00
(float) firework_sprite.width/2 + 10,
2022-06-18 16:10:36 +00:00
(float) firework_sprite.height
};
fireworks[i].hitbox.y = GetRandomValue(0, GetScreenHeight() - firework_sprite.height);
switch (level) {
case LEVEL1: fireworks[i].speed.x = GetRandomValue(100, 300); break;
case LEVEL2: fireworks[i].speed.x = GetRandomValue(400, 600); break;
case LEVEL3: fireworks[i].speed.x = GetRandomValue(800, 1000); break;
}
2022-06-26 23:43:39 +00:00
fireworks[i].color = RAYWHITE;
2022-06-18 16:10:36 +00:00
}
2022-07-08 20:00:38 +00:00
for (int i = 0; i < MAX_SHOOTS; i++) {
shoot[i].hitbox = (Rectangle) {
player.hitbox.x,
player.hitbox.y,
(float) attack_sprite.width,
(float) attack_sprite.height
};
2022-07-25 19:21:52 +00:00
shoot[i].speed.x = 500.f;
2022-07-08 20:00:38 +00:00
shoot[i].speed.y = 0;
shoot[i].active = false;
shoot[i].color = RED;
}
switch (level) {
case LEVEL1: fireworkAmount = 50; break;
case LEVEL2: fireworkAmount = 200; break;
case LEVEL3: fireworkAmount = 500; break;
}
2022-07-08 20:00:38 +00:00
pause = 0;
DebugMode = 0;
pauseTimer = 0;
GI_callcount++;
}
2022-08-27 18:08:36 +00:00
void DamageActor(struct Actor *actor)
2022-06-26 23:43:39 +00:00
{
2022-08-27 18:08:36 +00:00
if (!actor->in) {
actor->hp--;
if (!mute) PlaySoundMulti(actor->fxhit);
actor->in = true;
2022-06-26 23:43:39 +00:00
}
2022-08-27 18:08:36 +00:00
actor->currentframe = 1;
2022-06-26 23:43:39 +00:00
}
2022-08-22 19:16:28 +00:00
void UpdateiFrameTimer(struct Actor *actor)
2022-06-26 23:43:39 +00:00
{
2022-08-02 13:38:36 +00:00
// here we use pointers to avoid duplicating code
if (actor->in) {
2022-08-22 19:16:28 +00:00
actor->iframetimer += GetFrameTime();
2022-08-02 13:38:36 +00:00
actor->currentframe = 1;
2022-08-22 19:16:28 +00:00
if ((int)globalTimer % 2 == 0) actor->color = GRAY;
2022-08-02 13:38:36 +00:00
else actor->color = RAYWHITE;
2022-08-22 19:16:28 +00:00
if (actor->iframetimer > 2) {
2022-08-02 13:38:36 +00:00
actor->in = false;
actor->iframetimer = 0;
2022-06-26 23:43:39 +00:00
}
2022-08-02 13:38:36 +00:00
} else { actor->color = RAYWHITE; actor->currentframe = 0; }
2022-06-26 23:43:39 +00:00
}
2022-08-26 16:24:33 +00:00
void ResetFeather(void)
{
2022-08-31 18:08:31 +00:00
if (player.hp < 2) {
2022-08-26 16:24:33 +00:00
feather.power = 0;
2022-08-31 18:08:31 +00:00
} else if (ammo < 2) {
2022-08-26 16:24:33 +00:00
feather.power = 1;
2022-08-31 18:08:31 +00:00
} else {
feather.power = GetRandomValue(0, 1);
2022-08-26 16:24:33 +00:00
}
2022-08-31 18:08:31 +00:00
feather.hitbox.x = GetScreenWidth() + feather_sprite.width;
2022-08-27 18:08:36 +00:00
feather.hitbox.y = GetRandomValue(0, GetScreenHeight() - feather_sprite.height);
2022-08-26 16:24:33 +00:00
feather.active = false;
}
void UpdateGameplayScreen(void)
{
if (INPUT_OPTION_PRESSED) pause = !pause;
2022-08-02 13:38:36 +00:00
// code to end the game
2022-08-22 19:16:28 +00:00
if (level > 2) { StopMusicStream(Gameplaysong); finishfromGameplayScreen = 3; }
2022-10-02 19:58:18 +00:00
if (CheckFireworkActivity() && level < 2) { StopMusicStream(Gameplaysong); levelunlocked[nextlevel] = true; nextlevel++; finishfromGameplayScreen = 4; }
2022-08-07 20:24:55 +00:00
2022-08-22 19:16:28 +00:00
if (!mute) UpdateMusicStream(Gameplaysong);
2022-07-28 16:02:47 +00:00
if (!pause) {
2022-06-17 16:32:35 +00:00
2022-08-02 13:38:36 +00:00
// Controls
if (INPUT_LEFT_DOWN) player.hitbox.x -= GetFrameTime() * player.speed;
if (INPUT_RIGHT_DOWN) player.hitbox.x += GetFrameTime() * player.speed;
if (INPUT_UP_DOWN) player.hitbox.y -= GetFrameTime() * player.speed;
if (INPUT_DOWN_DOWN) player.hitbox.y += GetFrameTime() * player.speed;
if (INPUT_DASH_DOWN) {
player.speed = 600.0f;
if (player.currentframe != 1) player.currentframe = 2;
} else player.speed = 300.0f;
2022-07-08 20:00:38 +00:00
if (INPUT_FIRE_PRESSED) {
if (ammo > 0) {
for (int i = 0; i < MAX_SHOOTS; i++) {
if (!shoot[i].active) {
ammo--;
shoot[i].hitbox.x = player.hitbox.x;
shoot[i].hitbox.y = player.hitbox.y + player.hitbox.height/4;
shoot[i].active = true;
break;
}
}
}
}
2022-08-02 13:38:36 +00:00
// Update sprite positions
player.sprite_pos = (Vector2){ player.hitbox.x, player.hitbox.y };
2022-06-18 16:10:36 +00:00
player.frameRec.x = (float)player.currentframe*(float)player_sprite.width/3;
2022-07-08 20:00:38 +00:00
feather.sprite_pos = (Vector2){ feather.hitbox.x, feather.hitbox.y };
2022-06-17 16:32:35 +00:00
enemy.sprite_pos = (Vector2){ enemy.hitbox.x, enemy.hitbox.y };
enemy.frameRec.x = (float)enemy.currentframe*(float)enemy_sprite.width/2;
2022-06-18 16:10:36 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
fireworks[i].sprite_pos = (Vector2){ fireworks[i].hitbox.x, fireworks[i].hitbox.y };
}
2022-07-08 20:00:38 +00:00
for (int i = 0; i < MAX_SHOOTS; i++) {
shoot[i].sprite_pos = (Vector2){ shoot[i].hitbox.x, shoot[i].hitbox.y };
}
2022-08-02 13:38:36 +00:00
// Player wall collision
if ((player.hitbox.x + player.hitbox.width) >= GetScreenWidth()) player.hitbox.x = GetScreenWidth() - player.hitbox.width;
else if (player.hitbox.x <= 0) player.hitbox.x = 0;
if ((player.hitbox.y + player.hitbox.height) >= GetScreenHeight()) player.hitbox.y = GetScreenHeight() - player.hitbox.height;
else if (player.hitbox.y <= 0) player.hitbox.y = 0;
2022-08-02 13:38:36 +00:00
// Update Timers
2022-08-22 19:16:28 +00:00
scoreTimer += 60 * GetFrameTime();
score = (int)scoreTimer;
globalTimer += 10 * GetFrameTime();
2022-08-02 13:38:36 +00:00
// pass the address of each struct to the UpdateiFrameTimer function
UpdateiFrameTimer(&player);
UpdateiFrameTimer(&enemy);
greenfeathers = player.hp;
redfeathers = ammo;
2022-06-26 23:43:39 +00:00
2022-08-02 13:38:36 +00:00
// Debug stuff
if (IsKeyPressed(KEY_D)) DebugMode = !DebugMode;
2022-07-08 20:00:38 +00:00
if (IsKeyPressed(KEY_NINE)) ammo = 99;
if (IsKeyPressed(KEY_ZERO)) ammo = 0;
2022-08-31 19:15:32 +00:00
if (IsKeyPressed(KEY_G)) finishfromGameplayScreen = 1;
2022-07-25 19:21:52 +00:00
if (IsKeyPressed(KEY_R)) finishfromGameplayScreen = 2;
2022-07-28 16:02:47 +00:00
if (IsKeyPressed(KEY_W)) finishfromGameplayScreen = 3;
2022-09-04 15:15:22 +00:00
if (IsKeyPressed(KEY_EQUAL)) level++;
if (IsKeyPressed(KEY_MINUS)) level--;
2022-08-02 13:38:36 +00:00
// call gameover when killed
2022-08-31 19:15:32 +00:00
if (player.hp < 1) { StopMusicStream(Gameplaysong); finishfromGameplayScreen = 1; }
2022-08-02 13:38:36 +00:00
// Red feather logic
2022-07-08 20:00:38 +00:00
for (int i = 0; i < MAX_SHOOTS; i++) {
if (shoot[i].active) {
2022-07-25 19:21:52 +00:00
shoot[i].hitbox.x += shoot[i].speed.x * GetFrameTime();
2022-07-08 20:00:38 +00:00
}
2022-07-25 19:21:52 +00:00
if (shoot[i].hitbox.x + shoot[i].hitbox.width >= GetScreenWidth() + attack_sprite.width) shoot[i].active = false;
2022-07-08 20:00:38 +00:00
}
2022-08-02 13:38:36 +00:00
// Feather spawn logic
2022-08-26 16:24:33 +00:00
if (level == LEVEL3) { if ((int) globalTimer % 10 == 0) feather.active = true; }
else { if ((int) globalTimer % 50 == 0) feather.active = true; }
2022-08-02 13:38:36 +00:00
switch (feather.power) {
2022-07-08 20:00:38 +00:00
case 0: feather.color = GREEN; break;
case 1: feather.color = RED; break;
}
2022-08-26 16:24:33 +00:00
if (feather.active) {
if (((feather.hitbox.x + -feather_sprite.width) > GetScreenWidth()
|| (feather.hitbox.x <= -feather_sprite.width))) ResetFeather();
2022-07-08 20:00:38 +00:00
if (CheckCollisionRecs(player.hitbox, feather.hitbox)) {
switch (feather.power) {
2022-08-26 16:24:33 +00:00
case 0: player.hp++; break;
case 1: ammo++; break;
2022-07-08 20:00:38 +00:00
}
2022-08-31 19:15:32 +00:00
if (!mute) PlaySoundMulti(fxfeather);
2022-08-26 16:24:33 +00:00
ResetFeather();
}
2022-08-26 16:24:33 +00:00
feather.hitbox.x -= 300.0f * GetFrameTime();
}
2022-08-02 13:38:36 +00:00
// Enemy logic
2022-09-04 15:15:22 +00:00
if (level == 2) {
2022-08-31 19:15:32 +00:00
if ((int)globalTimer % 40 == 0) enemy.hitbox.y = GetRandomValue(0, GetScreenHeight() - enemy_sprite.height);
2022-07-25 19:21:52 +00:00
2022-08-27 18:08:36 +00:00
if (CheckCollisionRecs(player.hitbox, enemy.hitbox)) DamageActor(&player);
2022-07-25 19:21:52 +00:00
2022-09-04 15:15:22 +00:00
for (int i = 0; i < MAX_SHOOTS; i++) {
if (CheckCollisionRecs(shoot[i].hitbox, enemy.hitbox) && shoot[i].active) {
DamageActor(&enemy);
scoreTimer += 300;
enemy.hitbox.y = GetRandomValue(0, GetScreenHeight());
shoot[i].active = false;
}
}
2022-08-31 19:15:32 +00:00
if (enemy.hp < 1) { level++; enemy.hp = 5; }
2022-07-26 23:12:36 +00:00
}
2022-07-25 19:21:52 +00:00
2022-08-02 13:38:36 +00:00
// Firework logic
2022-09-20 01:28:10 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
if (CheckCollisionRecs(player.hitbox, fireworks[i].hitbox)) {
DamageActor(&player);
fireworks[i].active = 0;
}
/* for (int j = 0; j < MAX_SHOOTS; j++) {
if (CheckCollisionRecs(shoot[j].hitbox, fireworks[i].hitbox) && shoot[j].active) {
if (!mute) PlaySoundMulti(enemy.fxhit);
fireworks[i].active = 0;
fireworkAmount--;
shoot[j].active = 0;
}
} */
switch (fireworks[i].active) {
case 0:
fireworks[i].hitbox.x = GetScreenWidth() + firework_sprite.width;
if (fireworkAmount > 0) { fireworkAmount--; fireworks[i].active = 1; }
2022-09-20 01:28:10 +00:00
fireworks[i].hitbox.y = GetRandomValue(0, GetScreenHeight() - firework_sprite.height);
switch (level) {
case LEVEL1: fireworks[i].speed.x = GetRandomValue(100, 300); break;
case LEVEL2: fireworks[i].speed.x = GetRandomValue(400, 600); break;
case LEVEL3: fireworks[i].speed.x = GetRandomValue(800, 1000); break;
}
break;
case 1:
fireworks[i].hitbox.x += GetFrameTime() * -fireworks[i].speed.x;
// Firework wall collision
if (((fireworks[i].hitbox.x + -firework_sprite.width) > GetScreenWidth()
|| (fireworks[i].hitbox.x <= -firework_sprite.width))) fireworks[i].active = 0;
2022-07-25 19:21:52 +00:00
break;
2022-09-20 01:28:10 +00:00
}
2022-06-18 16:10:36 +00:00
}
2022-08-22 19:16:28 +00:00
} else pauseTimer += 60 * GetFrameTime();
}
void DrawGameplayScreen(void)
{
2022-07-25 19:21:52 +00:00
switch (level) {
case LEVEL1: DrawTexture(background, 0, 0, RAYWHITE); break;
2022-07-28 16:02:47 +00:00
case LEVEL2: DrawTexture(background, 0, 0, ORANGE); break;
2022-07-25 19:21:52 +00:00
case LEVEL3: DrawTexture(background, 0, 0, RED); break;
}
DrawFPS(10, 430);
if (DebugMode) {
2022-08-22 19:16:28 +00:00
DrawRectangleLines(player.hitbox.x, player.hitbox.y, player.hitbox.width, player.hitbox.height, BLUE);
DrawRectangleLines(feather.hitbox.x, feather.hitbox.y, feather.hitbox.width, feather.hitbox.height, WHITE);
DrawRectangleLines(enemy.hitbox.x, enemy.hitbox.y, enemy.hitbox.width, enemy.hitbox.height, BLACK);
2022-06-18 16:10:36 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
2022-08-22 19:16:28 +00:00
DrawRectangleLines(fireworks[i].hitbox.x, fireworks[i].hitbox.y, fireworks[i].hitbox.width, fireworks[i].hitbox.height, BLACK);
2022-06-18 16:10:36 +00:00
}
2022-07-08 20:00:38 +00:00
for (int i = 0; i < MAX_SHOOTS; i++) {
2022-08-22 19:16:28 +00:00
DrawRectangleLines(shoot[i].hitbox.x, shoot[i].hitbox.y, shoot[i].hitbox.width, shoot[i].hitbox.height, GREEN);
}
2022-07-26 23:12:36 +00:00
DrawText(TextFormat("enemy.hitbox.y: %f", enemy.hitbox.y), 10, 200, 20, GREEN);
DrawText(TextFormat("enemy.speed: %f", enemy.speed), 10, 220, 20, GREEN);
2022-08-22 19:16:28 +00:00
DrawText(TextFormat("globalTimer: %f", globalTimer), 10, 240, 20, GREEN);
2022-07-26 23:12:36 +00:00
DrawText(TextFormat("firework_sprite.width: %d", firework_sprite.width), 10, 260, 20, GREEN);
2022-08-22 19:16:28 +00:00
DrawText(TextFormat("player.iframetimer: %f", player.iframetimer), 10, 280, 20, GREEN);
2022-08-02 13:38:36 +00:00
DrawText(TextFormat("player.in: %d", player.in), 10, 300, 20, GREEN);
2022-08-26 16:24:33 +00:00
DrawText(TextFormat("feather.active: %d", feather.active), 10, 320, 20, GREEN);
2022-08-31 19:15:32 +00:00
DrawText(TextFormat("GetTime(): %f", GetTime()), 10, 340, 20, GREEN);
2022-09-20 01:28:10 +00:00
DrawText(TextFormat("fireworkAmount: %d", fireworkAmount), 10, 360, 20, GREEN);
}
2022-07-08 20:00:38 +00:00
if (feather.active) DrawTexture(feather_sprite, feather.sprite_pos.x, feather.sprite_pos.y, feather.color);
2022-06-18 16:10:36 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
2022-06-26 23:43:39 +00:00
DrawTexture(firework_sprite, fireworks[i].sprite_pos.x, fireworks[i].sprite_pos.y, fireworks[i].color);
2022-06-18 16:10:36 +00:00
}
2022-07-08 20:00:38 +00:00
for (int i = 0; i < MAX_SHOOTS; i++) {
if (shoot[i].active) DrawTexture(attack_sprite, shoot[i].sprite_pos.x, shoot[i].sprite_pos.y, shoot[i].color);
}
2022-09-04 15:15:22 +00:00
if (level == 2) DrawTextureRec(enemy_sprite, enemy.frameRec, enemy.sprite_pos, enemy.color);
2022-06-26 23:43:39 +00:00
DrawTextureRec(player_sprite, player.frameRec, player.sprite_pos, player.color);
2022-07-08 20:00:38 +00:00
DrawTexture(feather_sprite, 0, 0, GREEN);
2022-08-22 19:16:28 +00:00
DrawText(TextFormat("= %i", player.hp), 30, 30, 30, GREEN);
2022-08-07 20:24:55 +00:00
DrawTexture(feather_sprite, 80, 0, RED);
2022-08-22 19:16:28 +00:00
DrawText(TextFormat("= %i", ammo), 110, 30, 30, RED);
if (level == 2) DrawText(TextFormat("ENEMY HP: %i", enemy.hp), GetScreenWidth() - 380, 0, 20, RED);
DrawText(TextFormat("FIREWORKS LEFT: %i", fireworkAmount), GetScreenWidth() - 240, 0, 20, GREEN);
2022-08-22 19:16:28 +00:00
if (score >= 10000) DrawText(TextFormat("SCORE: %i", score), 10, 65, 30, (Color){ 222, 181, 0, 255 });
else DrawText(TextFormat("SCORE: %i", score), 10, 65, 30, BLUE);
if (pause && (((int)pauseTimer/30)%2)) DrawTextEx(ZadoBold, "PAUSED", (Vector2){ 280, 160 }, 60, 2, WHITE);
}
2022-09-20 01:28:10 +00:00
void UnloadGameplayScreen(void)
{
2022-08-27 18:08:36 +00:00
UnloadSound(player.fxhit);
UnloadSound(enemy.fxhit);
2022-07-04 01:08:45 +00:00
UnloadSound(fxfeather);
2022-06-18 16:10:36 +00:00
UnloadTexture(player_sprite);
2022-06-29 16:11:37 +00:00
UnloadTexture(feather_sprite);
2022-06-18 16:10:36 +00:00
UnloadTexture(enemy_sprite);
UnloadTexture(firework_sprite);
2022-07-08 20:00:38 +00:00
UnloadTexture(attack_sprite);
2022-08-22 19:16:28 +00:00
UnloadMusicStream(Gameplaysong);
}
2022-07-19 15:04:05 +00:00
int FinishGameplayScreen(void)
{
2022-07-19 15:04:05 +00:00
return finishfromGameplayScreen;
}