Avoid/src/Gameplay.c

375 lines
12 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 "Score.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"
2022-07-19 15:04:05 +00:00
int score = 0, bestscore = 0, finishfromGameplayScreen = 0;
2022-07-25 19:21:52 +00:00
Levels level = 0;
2022-08-07 20:24:55 +00:00
Music music = { 0 };
2022-07-26 23:12:36 +00:00
void SetEnemyLevel(void)
2022-07-25 19:21:52 +00:00
{
switch (level) {
case LEVEL1: enemy.speed *= 1.0f; break;
case LEVEL2: enemy.speed *= 2.0f; break;
2022-07-28 16:02:47 +00:00
case LEVEL3: enemy.speed *= 2.0f; break;
2022-07-25 19:21:52 +00:00
}
}
void LoadGamplayScreen(void)
{
fxhit = LoadSound("assets/sfx/hit.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");
fxboom = LoadSound("assets/sfx/boom.wav");
2022-08-07 20:24:55 +00:00
music = LoadMusicStream("assets/bgm/03-Boss.ogg");
PlayMusicStream(music);
}
void InitGameplayScreen(void)
{
2022-07-19 15:04:05 +00:00
finishfromGameplayScreen = 0;
2022-07-25 19:21:52 +00:00
level = LEVEL1;
2022-07-28 16:02:47 +00:00
globalTimer = 0;
player.currentframe = 0;
player.speed = 300.0f;
2022-06-29 16:11:37 +00:00
player.hp = PLAYER_HP;
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-07-25 19:21:52 +00:00
enemy.speed = 2.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-06-17 16:32:35 +00:00
10,
(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-06-29 16:11:37 +00:00
GetRandomValue(0, GetScreenWidth() - feather_sprite.width),
GetRandomValue(0, GetScreenHeight() - feather_sprite.height),
(float) feather_sprite.width,
(float) feather_sprite.height
};
2022-07-08 20:00:38 +00:00
feather.active = true;
feather.power = 0;
2022-06-18 16:10:36 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
2022-07-08 20:00:38 +00:00
fireworks[i].active = 0;
2022-06-18 16:10:36 +00:00
fireworks[i].hitbox = (Rectangle) {
2022-06-19 21:36:26 +00:00
630,
GetRandomValue(0, GetScreenHeight()),
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
};
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;
}
ammo = 0;
pause = 0;
DebugMode = 0;
pauseTimer = 0;
score = 0;
GI_callcount++;
}
2022-06-26 23:43:39 +00:00
void DamagePlayer(void)
{
2022-08-02 13:38:36 +00:00
if (!player.in) {
2022-06-26 23:43:39 +00:00
player.hp--;
2022-07-04 01:08:45 +00:00
if (!mute) PlaySoundMulti(fxhit);
2022-08-02 13:38:36 +00:00
player.in = true;
2022-06-26 23:43:39 +00:00
}
player.currentframe = 1;
}
2022-08-02 13:38:36 +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) {
actor->iframetimer++;
actor->currentframe = 1;
if (globalTimer % 2 == 0) actor->color = BLANK;
else actor->color = RAYWHITE;
if (actor->iframetimer >= 60) {
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
}
void UpdateGameplayScreen(void)
{
if (INPUT_OPTION_PRESSED) pause = !pause;
2022-08-02 13:38:36 +00:00
// code to end the game
2022-08-07 20:24:55 +00:00
if (level > 2) { StopMusicStream(music); finishfromGameplayScreen = 3; }
if (!mute) UpdateMusicStream(music);
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
score++;
globalTimer++;
// pass the address of each struct to the UpdateiFrameTimer function
UpdateiFrameTimer(&player);
UpdateiFrameTimer(&enemy);
2022-07-26 23:12:36 +00:00
if (score >= bestscore) bestscore = score;
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-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-08-02 13:38:36 +00:00
// call gameover when killed
2022-08-07 20:24:55 +00:00
if (player.hp <= 0) { StopMusicStream(music); 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
}
if (CheckCollisionRecs(shoot[i].hitbox, enemy.hitbox) && shoot[i].active) {
2022-08-02 13:38:36 +00:00
if (!enemy.in) enemy.hp--;
enemy.in = true;
2022-07-08 20:00:38 +00:00
score += 300;
if (!mute) PlaySoundMulti(fxboom);
shoot[i].active = false;
}
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
switch (feather.power) {
2022-07-08 20:00:38 +00:00
case 0: feather.color = GREEN; break;
case 1: feather.color = RED; break;
}
if (CheckCollisionRecs(player.hitbox, feather.hitbox)) {
switch (feather.power) {
case 0: player.hp++; feather.power = 1; break;
2022-07-25 19:21:52 +00:00
case 1: ammo++; if (player.hp < 5) feather.power = 0; else feather.power = 1; break;
2022-07-08 20:00:38 +00:00
}
2022-07-28 16:02:47 +00:00
if (!mute && player.hp < 5) PlaySoundMulti(fxfeather);
feather.hitbox.x = GetRandomValue(0, 600);
2022-07-08 20:00:38 +00:00
feather.hitbox.y = GetRandomValue(0, GetScreenHeight() - feather_sprite.height);
}
2022-08-02 13:38:36 +00:00
// Enemy logic
2022-07-26 23:12:36 +00:00
if (level < 3) {
2022-06-17 16:32:35 +00:00
if (((enemy.hitbox.y + enemy.hitbox.height) >= GetScreenHeight()
|| (enemy.hitbox.y <= 0))) enemy.speed *= -1.0f;
2022-07-25 19:21:52 +00:00
enemy.hitbox.y += enemy.speed;
2022-06-26 23:43:39 +00:00
if (CheckCollisionRecs(player.hitbox, enemy.hitbox)) DamagePlayer();
2022-07-25 19:21:52 +00:00
2022-07-26 23:12:36 +00:00
if (enemy.hp < 1) { level++; enemy.hp = 5; SetEnemyLevel(); }
}
2022-07-25 19:21:52 +00:00
2022-08-02 13:38:36 +00:00
// Firework logic
2022-06-19 21:36:26 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
2022-07-04 01:08:45 +00:00
if (CheckCollisionRecs(player.hitbox, fireworks[i].hitbox)) {
DamagePlayer();
2022-07-08 20:00:38 +00:00
fireworks[i].active = 0;
2022-07-04 01:08:45 +00:00
}
2022-07-08 20:00:38 +00:00
switch (fireworks[i].active) {
2022-07-25 19:21:52 +00:00
case 0:
fireworks[i].hitbox.x = enemy.hitbox.x - 20;
fireworks[i].hitbox.y = enemy.hitbox.y - 20;
if (GetRandomValue(0, 50) == 50) {
fireworks[i].active = 1;
fireworks[i].hitbox.y += enemy.hitbox.height/2;
}
break;
case 1:
fireworks[i].hitbox.x += GetFrameTime() * -fireworks[i].speed.x;
2022-08-02 13:38:36 +00:00
// Firework wall collision
2022-07-25 19:21:52 +00:00
if (((fireworks[i].hitbox.x + -firework_sprite.width) > GetScreenWidth()
|| (fireworks[i].hitbox.x <= -firework_sprite.width))) fireworks[i].active = 0;
break;
}
switch (level) {
case LEVEL1: fireworks[i].speed.x = 300.0f; break;
case LEVEL2: fireworks[i].speed.x = 600.0f; break;
2022-07-28 16:02:47 +00:00
case LEVEL3: fireworks[i].speed.x = 900.0f; break;
2022-06-19 21:36:26 +00:00
}
2022-06-18 16:10:36 +00:00
}
2022-07-25 19:21:52 +00:00
} else pauseTimer++;
}
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) {
DrawRectangleRec(player.hitbox, BLUE);
2022-07-08 20:00:38 +00:00
DrawRectangleRec(feather.hitbox, WHITE);
2022-06-17 16:32:35 +00:00
DrawRectangleRec(enemy.hitbox, BLACK);
2022-06-18 16:10:36 +00:00
for (int i = 0; i < MAX_FIREWORKS; i++) {
DrawRectangleRec(fireworks[i].hitbox, BLACK);
}
2022-07-08 20:00:38 +00:00
for (int i = 0; i < MAX_SHOOTS; i++) {
DrawRectangleRec(shoot[i].hitbox, 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-07-28 16:02:47 +00:00
DrawText(TextFormat("globalTimer: %i", 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-02 13:38:36 +00:00
DrawText(TextFormat("player.iframetimer: %d", player.iframetimer), 10, 280, 20, GREEN);
DrawText(TextFormat("player.in: %d", player.in), 10, 300, 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);
DrawTextureRec(enemy_sprite, enemy.frameRec, enemy.sprite_pos, enemy.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-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-07 20:24:55 +00:00
DrawTextEx(ZadoBold, TextFormat("= %i", player.hp), (Vector2){ 30, 30 }, 30, 2, GREEN);
DrawTexture(feather_sprite, 80, 0, RED);
DrawTextEx(ZadoBold, TextFormat("= %i", ammo), (Vector2){ 110, 30 }, 30, 2, RED);
DrawTextEx(ZadoBold, TextFormat("ENEMY HP: %i", enemy.hp), (Vector2){ GetScreenWidth() - 200, 0 }, 30, 2, RED);
if (score >= 10000) DrawTextEx(ZadoBold, TextFormat("SCORE: %i", score), (Vector2){ 10, 65 }, 30, 2, (Color){ 222, 181, 0, 255 });
else DrawTextEx(ZadoBold, TextFormat("SCORE: %i", score), (Vector2){ 10, 65 }, 30, 2, BLUE);
if (pause && ((pauseTimer/30)%2)) DrawTextEx(ZadoBold, "PAUSED", (Vector2){ 290, 160 }, 60, 2, WHITE);
}
void UnloadGameplayScreen()
{
2022-07-04 01:08:45 +00:00
UnloadSound(fxhit);
UnloadSound(fxfeather);
2022-07-08 20:00:38 +00:00
UnloadSound(fxboom);
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-07 20:24:55 +00:00
UnloadMusicStream(music);
}
2022-07-19 15:04:05 +00:00
int FinishGameplayScreen(void)
{
2022-07-19 15:04:05 +00:00
return finishfromGameplayScreen;
}