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);
|
|
|
|
|
|
|
|
player.sprite = LoadTexture("assets/gfx/player.png");
|
|
|
|
player.currentframe = 0;
|
|
|
|
player.speed = 300.0f;
|
|
|
|
player.hp = 30;
|
|
|
|
player.frameRec = (Rectangle) {
|
|
|
|
player.hitbox.x,
|
|
|
|
player.hitbox.y,
|
|
|
|
(float) player.sprite.width/3,
|
|
|
|
(float) player.sprite.height
|
|
|
|
};
|
|
|
|
player.hitbox = (Rectangle) {
|
|
|
|
GetScreenWidth()/2.0f - 30,
|
|
|
|
GetScreenHeight()/2.0f - 30,
|
|
|
|
(float) player.sprite.width/3,
|
|
|
|
(float) player.sprite.height
|
|
|
|
};
|
|
|
|
|
2022-06-17 16:32:35 +00:00
|
|
|
enemy.sprite = LoadTexture("assets/gfx/enemy.png");
|
|
|
|
enemy.currentframe = 0;
|
|
|
|
enemy.speed = 100.0f;
|
|
|
|
enemy.hp = 30;
|
|
|
|
enemy.hitbox = (Rectangle) {
|
|
|
|
740,
|
|
|
|
10,
|
|
|
|
(float) enemy.sprite.width,
|
|
|
|
(float) enemy.sprite.height
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-06-17 02:17:26 +00:00
|
|
|
heart.sprite = LoadTexture("assets/gfx/health.png");
|
|
|
|
heart.hitbox = (Rectangle) {
|
|
|
|
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
|
|
|
|
|
|
|
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) {
|
|
|
|
GetScreenWidth()/2.0f - 30,
|
|
|
|
GetScreenHeight()/2.0f - 30,
|
|
|
|
(float) player.sprite.width/3,
|
|
|
|
(float) player.sprite.height
|
|
|
|
};
|
|
|
|
|
2022-06-17 16:32:35 +00:00
|
|
|
enemy.currentframe = 0;
|
|
|
|
enemy.speed = 100.0f;
|
|
|
|
enemy.hp = 30;
|
|
|
|
enemy.hitbox = (Rectangle) {
|
|
|
|
740,
|
|
|
|
10,
|
|
|
|
(float) enemy.sprite.width,
|
|
|
|
(float) enemy.sprite.height
|
|
|
|
};
|
|
|
|
|
2022-06-12 17:49:42 +00:00
|
|
|
heart.hitbox = (Rectangle) {
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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 };
|
|
|
|
player.frameRec.x = (float)player.currentframe*(float)player.sprite.width/3;
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
heart.hitbox.x = GetRandomValue(0, GetScreenWidth() - heart.sprite.width);
|
|
|
|
heart.hitbox.y = GetRandomValue(0, GetScreenHeight() - heart.sprite.height);
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
DrawText(TextFormat("HP: %i", player.hp), 10, 10, 20, RED);
|
|
|
|
DrawText(TextFormat("SCORE: %i", score), 10, 30, 20, BLUE);
|
|
|
|
if (DebugMode) {
|
|
|
|
DrawRectangleRec(player.hitbox, BLUE);
|
|
|
|
DrawRectangleRec(heart.hitbox, GREEN);
|
2022-06-17 16:32:35 +00:00
|
|
|
DrawRectangleRec(enemy.hitbox, BLACK);
|
2022-06-12 17:49:42 +00:00
|
|
|
}
|
|
|
|
if (heart.active) DrawTexture(heart.sprite, heart.sprite_pos.x, heart.sprite_pos.y, RAYWHITE);
|
2022-06-17 16:32:35 +00:00
|
|
|
DrawTexture(enemy.sprite, enemy.sprite_pos.x, enemy.sprite_pos.y, RAYWHITE);
|
2022-06-17 02:17:26 +00:00
|
|
|
DrawTextureRec(player.sprite, player.frameRec, player.sprite_pos, RAYWHITE);
|
2022-06-12 17:49:42 +00:00
|
|
|
if (pause && ((pauseTimer/30)%2)) DrawText("PAUSED", 330, 190, 30, PURPLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnloadGameplayScreen()
|
|
|
|
{
|
|
|
|
UnloadSound(fxbounce);
|
|
|
|
UnloadTexture(player.sprite);
|
|
|
|
UnloadTexture(heart.sprite);
|
2022-06-17 16:32:35 +00:00
|
|
|
UnloadTexture(enemy.sprite);
|
2022-06-12 17:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gameReset(void)
|
|
|
|
{
|
|
|
|
ResetGameplayScreen();
|
|
|
|
InitGameoverScreen();
|
|
|
|
}
|