2022-06-12 17:49:42 +00:00
|
|
|
/*
|
|
|
|
- Avoid ~ a game by Canneddonuts
|
|
|
|
- Filename ~ Gameplay.c
|
|
|
|
- Author ~ Return0ne
|
|
|
|
- 2022
|
|
|
|
- *no license*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../include/raylib.h"
|
|
|
|
|
|
|
|
#include "Screens.h"
|
|
|
|
#include "Controls.h"
|
|
|
|
#include "Gameplay.h"
|
|
|
|
#include "Score.h"
|
2022-06-17 02:17:26 +00:00
|
|
|
#include "Textures.h"
|
2022-06-12 17:49:42 +00:00
|
|
|
|
|
|
|
int score = 0, bestscore = 0;
|
|
|
|
|
|
|
|
void InitGameplayScreen(void)
|
|
|
|
{
|
|
|
|
fxbounce = LoadSound("assets/sfx/boing.wav");
|
|
|
|
|
|
|
|
SetMasterVolume(0.2);
|
|
|
|
|
2022-06-18 16:10:36 +00:00
|
|
|
player_sprite = LoadTexture("assets/gfx/player.png");
|
2022-06-12 17:49:42 +00:00
|
|
|
player.currentframe = 0;
|
|
|
|
player.speed = 300.0f;
|
|
|
|
player.hp = 30;
|
|
|
|
player.frameRec = (Rectangle) {
|
|
|
|
player.hitbox.x,
|
|
|
|
player.hitbox.y,
|
2022-06-18 16:10:36 +00:00
|
|
|
(float) player_sprite.width/3,
|
|
|
|
(float) player_sprite.height
|
2022-06-12 17:49:42 +00:00
|
|
|
};
|
|
|
|
player.hitbox = (Rectangle) {
|
2022-06-19 21:36:26 +00:00
|
|
|
0,
|
|
|
|
50,
|
2022-06-18 16:10:36 +00:00
|
|
|
(float) player_sprite.width/3,
|
|
|
|
(float) player_sprite.height
|
2022-06-12 17:49:42 +00:00
|
|
|
};
|
|
|
|
|
2022-06-18 16:10:36 +00:00
|
|
|
enemy_sprite = LoadTexture("assets/gfx/enemy.png");
|
2022-06-17 16:32:35 +00:00
|
|
|
enemy.currentframe = 0;
|
|
|
|
enemy.speed = 100.0f;
|
|
|
|
enemy.hp = 30;
|
|
|
|
enemy.hitbox = (Rectangle) {
|
2022-06-19 21:36:26 +00:00
|
|
|
690,
|
2022-06-17 16:32:35 +00:00
|
|
|
10,
|
2022-06-18 16:10:36 +00:00
|
|
|
(float) enemy_sprite.width,
|
|
|
|
(float) enemy_sprite.height
|
2022-06-17 16:32:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-06-18 16:10:36 +00:00
|
|
|
heart_sprite = LoadTexture("assets/gfx/health.png");
|
2022-06-17 02:17:26 +00:00
|
|
|
heart.hitbox = (Rectangle) {
|
2022-06-18 16:10:36 +00:00
|
|
|
GetRandomValue(0, GetScreenWidth() - heart_sprite.width),
|
|
|
|
GetRandomValue(0, GetScreenHeight() - heart_sprite.height),
|
|
|
|
(float) heart_sprite.width,
|
|
|
|
(float) heart_sprite.height
|
2022-06-12 17:49:42 +00:00
|
|
|
};
|
2022-06-14 00:33:35 +00:00
|
|
|
heart.active = true;
|
2022-06-12 17:49:42 +00:00
|
|
|
|
2022-06-18 16:10:36 +00:00
|
|
|
firework_sprite = LoadTexture("assets/gfx/firework.png");
|
|
|
|
for (int i = 0; i < MAX_FIREWORKS; i++) {
|
|
|
|
fireworks[i].currentframe = 0;
|
2022-06-19 21:36:26 +00:00
|
|
|
fireworks[i].speed = 300.0f;
|
|
|
|
fireworks[i].hp = 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-18 16:10:36 +00:00
|
|
|
(float) firework_sprite.width,
|
|
|
|
(float) firework_sprite.height
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-12 17:49:42 +00:00
|
|
|
pause = 0;
|
2022-06-17 02:17:26 +00:00
|
|
|
mute = true;
|
2022-06-12 17:49:42 +00:00
|
|
|
DebugMode = 0;
|
|
|
|
pauseTimer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResetGameplayScreen(void)
|
|
|
|
{
|
|
|
|
// code to reset all variables without reloading assets
|
|
|
|
player.currentframe = 0;
|
|
|
|
player.speed = 300.0f;
|
|
|
|
player.hp = 30;
|
|
|
|
player.hitbox = (Rectangle) {
|
2022-06-19 21:36:26 +00:00
|
|
|
0,
|
|
|
|
50,
|
2022-06-18 16:10:36 +00:00
|
|
|
(float) player_sprite.width/3,
|
|
|
|
(float) player_sprite.height
|
2022-06-12 17:49:42 +00:00
|
|
|
};
|
|
|
|
|
2022-06-17 16:32:35 +00:00
|
|
|
enemy.currentframe = 0;
|
|
|
|
enemy.speed = 100.0f;
|
|
|
|
enemy.hp = 30;
|
|
|
|
enemy.hitbox = (Rectangle) {
|
2022-06-19 21:36:26 +00:00
|
|
|
690,
|
2022-06-17 16:32:35 +00:00
|
|
|
10,
|
2022-06-18 16:10:36 +00:00
|
|
|
(float) enemy_sprite.width,
|
|
|
|
(float) enemy_sprite.height
|
2022-06-17 16:32:35 +00:00
|
|
|
};
|
|
|
|
|
2022-06-12 17:49:42 +00:00
|
|
|
heart.hitbox = (Rectangle) {
|
2022-06-18 16:10:36 +00:00
|
|
|
GetRandomValue(0, GetScreenWidth() - heart_sprite.width),
|
|
|
|
GetRandomValue(0, GetScreenHeight() - heart_sprite.height),
|
|
|
|
(float) heart_sprite.width,
|
|
|
|
(float) heart_sprite.height
|
2022-06-17 02:17:26 +00:00
|
|
|
};
|
|
|
|
heart.active = true;
|
2022-06-12 17:49:42 +00:00
|
|
|
|
2022-06-18 16:10:36 +00:00
|
|
|
for (int i = 0; i < MAX_FIREWORKS; i++) {
|
|
|
|
fireworks[i].currentframe = 0;
|
2022-06-19 21:36:26 +00:00
|
|
|
fireworks[i].speed = 300.0f;
|
|
|
|
fireworks[i].hp = 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-18 16:10:36 +00:00
|
|
|
(float) firework_sprite.width,
|
|
|
|
(float) firework_sprite.height
|
|
|
|
};
|
|
|
|
}
|
2022-06-12 17:49:42 +00:00
|
|
|
|
|
|
|
DebugMode = 0;
|
|
|
|
|
|
|
|
pauseTimer = 0;
|
|
|
|
score = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateGameplayScreen(void)
|
|
|
|
{
|
2022-06-14 00:33:35 +00:00
|
|
|
if (IsKeyPressed(KEY_M)) mute = !mute;
|
2022-06-12 17:49:42 +00:00
|
|
|
|
2022-06-14 00:33:35 +00:00
|
|
|
if (INPUT_OPTION_PRESSED) pause = !pause;
|
2022-06-12 17:49:42 +00:00
|
|
|
|
2022-06-14 00:33:35 +00:00
|
|
|
if (!pause) {
|
2022-06-17 16:32:35 +00:00
|
|
|
|
2022-06-14 00:33:35 +00:00
|
|
|
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-06-12 17:49:42 +00:00
|
|
|
|
|
|
|
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-06-12 17:49:42 +00:00
|
|
|
|
|
|
|
heart.sprite_pos = (Vector2){ heart.hitbox.x, heart.hitbox.y };
|
2022-06-17 16:32:35 +00:00
|
|
|
enemy.sprite_pos = (Vector2){ enemy.hitbox.x, enemy.hitbox.y };
|
2022-06-12 17:49:42 +00:00
|
|
|
|
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-06-14 00:33:35 +00:00
|
|
|
if (score % 1000 == 0) heart.active = true;
|
2022-06-12 17:49:42 +00:00
|
|
|
|
|
|
|
// Player to da wallz collies
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (IsKeyPressed(KEY_D)) DebugMode = !DebugMode;
|
|
|
|
|
|
|
|
if (IsKeyPressed(KEY_R)) {
|
|
|
|
gameReset();
|
|
|
|
currentScreen = TITLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (player.hp <= 0) {
|
|
|
|
gameReset();
|
|
|
|
currentScreen = GAMEOVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (heart.active) {
|
|
|
|
if (CheckCollisionRecs(player.hitbox, heart.hitbox)) {
|
|
|
|
player.hp = 30;
|
2022-06-18 16:10:36 +00:00
|
|
|
heart.hitbox.x = GetRandomValue(0, GetScreenWidth() - heart_sprite.width);
|
|
|
|
heart.hitbox.y = GetRandomValue(0, GetScreenHeight() - heart_sprite.height);
|
2022-06-12 17:49:42 +00:00
|
|
|
heart.active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 16:32:35 +00:00
|
|
|
if (enemy.hp != 0) {
|
|
|
|
score++;
|
|
|
|
if (score >= bestscore) bestscore = score;
|
2022-06-17 02:17:26 +00:00
|
|
|
|
2022-06-17 16:32:35 +00:00
|
|
|
enemy.hitbox.y += GetFrameTime() * enemy.speed;
|
2022-06-17 02:17:26 +00:00
|
|
|
|
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-06-12 17:49:42 +00:00
|
|
|
|
2022-06-17 16:32:35 +00:00
|
|
|
if (CheckCollisionRecs(player.hitbox, enemy.hitbox)) {
|
|
|
|
player.hp -= GetFrameTime() * 3.0f;
|
|
|
|
player.currentframe = 1;
|
|
|
|
} else player.currentframe = 0;
|
|
|
|
}
|
2022-06-12 17:49:42 +00:00
|
|
|
|
2022-06-19 21:36:26 +00:00
|
|
|
for (int i = 0; i < MAX_FIREWORKS; i++) {
|
2022-06-18 16:10:36 +00:00
|
|
|
if (CheckCollisionRecs(player.hitbox, fireworks[i].hitbox)) {
|
|
|
|
player.hp -= GetFrameTime() * 3.0f;
|
|
|
|
player.currentframe = 1;
|
|
|
|
}
|
2022-06-19 21:36:26 +00:00
|
|
|
|
|
|
|
switch (fireworks[i].hp) {
|
|
|
|
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].hp = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
fireworks[i].hitbox.x += GetFrameTime() * -fireworks[i].speed;
|
|
|
|
if (((fireworks[i].hitbox.x + -firework_sprite.width) > GetScreenWidth()
|
|
|
|
|| (fireworks[i].hitbox.x <= -firework_sprite.width))) fireworks[i].hp = 0;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-18 16:10:36 +00:00
|
|
|
}
|
2022-06-12 17:49:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else pauseTimer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawGameplayScreen(void)
|
|
|
|
{
|
2022-06-17 02:17:26 +00:00
|
|
|
DrawTexture(background, 0, 0, RAYWHITE);
|
2022-06-12 17:49:42 +00:00
|
|
|
DrawFPS(10, 430);
|
|
|
|
if (DebugMode) {
|
|
|
|
DrawRectangleRec(player.hitbox, BLUE);
|
|
|
|
DrawRectangleRec(heart.hitbox, GREEN);
|
2022-06-19 21:36:26 +00:00
|
|
|
DrawText(TextFormat("enemy.hitbox.y: %f", enemy.hitbox.y), 10, 200, 20, RED);
|
|
|
|
DrawText(TextFormat("player.hitbox.y: %f", player.hitbox.y), 10, 230, 20, RED);
|
2022-06-17 16:32:35 +00:00
|
|
|
DrawRectangleRec(enemy.hitbox, BLACK);
|
2022-06-19 21:36:26 +00:00
|
|
|
DrawText(TextFormat("firework_sprite.width: %d", firework_sprite.width), 10, 270, 20, RED);
|
2022-06-18 16:10:36 +00:00
|
|
|
for (int i = 0; i < MAX_FIREWORKS; i++) {
|
2022-06-19 21:36:26 +00:00
|
|
|
DrawText(TextFormat("fireworks[i].hp: %d", fireworks[i].hp), 10, 250, 20, RED);
|
2022-06-18 16:10:36 +00:00
|
|
|
DrawRectangleRec(fireworks[i].hitbox, BLACK);
|
|
|
|
}
|
2022-06-12 17:49:42 +00:00
|
|
|
}
|
2022-06-18 16:10:36 +00:00
|
|
|
if (heart.active) DrawTexture(heart_sprite, heart.sprite_pos.x, heart.sprite_pos.y, RAYWHITE);
|
|
|
|
DrawTexture(enemy_sprite, enemy.sprite_pos.x, enemy.sprite_pos.y, RAYWHITE);
|
|
|
|
for (int i = 0; i < MAX_FIREWORKS; i++) {
|
|
|
|
DrawTexture(firework_sprite, fireworks[i].sprite_pos.x, fireworks[i].sprite_pos.y, RAYWHITE);
|
|
|
|
}
|
|
|
|
DrawTextureRec(player_sprite, player.frameRec, player.sprite_pos, RAYWHITE);
|
|
|
|
DrawText(TextFormat("HP: %i", player.hp), 10, 10, 20, RED);
|
|
|
|
DrawText(TextFormat("SCORE: %i", score), 10, 30, 20, BLUE);
|
2022-06-12 17:49:42 +00:00
|
|
|
if (pause && ((pauseTimer/30)%2)) DrawText("PAUSED", 330, 190, 30, PURPLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnloadGameplayScreen()
|
|
|
|
{
|
|
|
|
UnloadSound(fxbounce);
|
2022-06-18 16:10:36 +00:00
|
|
|
UnloadTexture(player_sprite);
|
|
|
|
UnloadTexture(heart_sprite);
|
|
|
|
UnloadTexture(enemy_sprite);
|
|
|
|
UnloadTexture(firework_sprite);
|
2022-06-12 17:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gameReset(void)
|
|
|
|
{
|
|
|
|
ResetGameplayScreen();
|
|
|
|
InitGameoverScreen();
|
|
|
|
}
|