diff --git a/Makefile b/Makefile index e00d4fe..15a299b 100644 --- a/Makefile +++ b/Makefile @@ -381,7 +381,8 @@ PROJECT_SOURCE_FILES ?= \ $(PROJECT_SOURCE_PATH)/Gameplay.c \ $(PROJECT_SOURCE_PATH)/Title.c \ $(PROJECT_SOURCE_PATH)/Credits.c \ - $(PROJECT_SOURCE_PATH)/Gameover.c + $(PROJECT_SOURCE_PATH)/Gameover.c \ + $(PROJECT_SOURCE_PATH)/Options.c # Define all object files from source files OBJS = $(patsubst %.c, %.o, $(PROJECT_SOURCE_FILES)) diff --git a/README.md b/README.md index a41b781..d381f3b 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,10 @@ A dumb raylib test which you can play [here](https://canneddonuts.itch.io/avoid- ## To-do - a build guide - multiple stages -- an options menu - a tutorial +## Note +This games code more specifically 'Main.c' is a retyped version of this [repo](https://github.com/raysan5/raylib-game-template) which is code under the zlib license. + ## Preview ![Alt Text](./doc-assets/preview.png) diff --git a/src/Credits.c b/src/Credits.c index 72d2eb9..24c1257 100644 --- a/src/Credits.c +++ b/src/Credits.c @@ -12,9 +12,16 @@ #include "Controls.h" #include "Textures.h" +int finishfromCreditsScreen = 0; + +void InitCreditsScreen(void) +{ + finishfromCreditsScreen = 0; +} + void UpdateCreditsScreen(void) { - if (INPUT_OPTION_PRESSED) currentScreen = TITLE; + if (INPUT_OPTION_PRESSED) finishfromCreditsScreen = 1; } void DrawCreditsScreen(void) @@ -27,3 +34,13 @@ void DrawCreditsScreen(void) DrawText(TextFormat("Build compiled on %s", __DATE__), 10, 310, 30, GREEN); DrawText("Press 'ENTER' ", 10, 350, 20, WHITE); } + +int FinishCreditsScreen(void) +{ + return finishfromCreditsScreen; +} + +void UnloadCreditsScreen(void) +{ + +} diff --git a/src/Gameover.c b/src/Gameover.c index 646a3ec..f2ba277 100644 --- a/src/Gameover.c +++ b/src/Gameover.c @@ -11,11 +11,12 @@ #include "Screens.h" #include "Controls.h" -int gameoverSelected = 0; +int gameoverSelected = 0, finishfromGameoverScreen = 0; void InitGameoverScreen(void) { gameoverSelected = 0; + finishfromGameoverScreen = 0; } void UpdateGameoverScreen(void) @@ -26,10 +27,10 @@ void UpdateGameoverScreen(void) if (gameoverSelected < -1) gameoverSelected++; if ((gameoverSelected == 0) && (INPUT_OPTION_PRESSED)) - currentScreen = GAMEPLAY; + finishfromGameoverScreen = 2; if ((gameoverSelected == -1) && (INPUT_OPTION_PRESSED)) - currentScreen = TITLE; + finishfromGameoverScreen = 1; } void DrawGameoverScreen(void) @@ -42,3 +43,13 @@ void DrawGameoverScreen(void) if (gameoverSelected == -1) DrawText("TITLE", 352, 230, 20, WHITE); else DrawText("TITLE", 352, 230, 20, RED); } + +int FinishGameoverScreen(void) +{ + return finishfromGameoverScreen; +} + +void UnloadGameoverScreen(void) +{ + +} diff --git a/src/Gameplay.c b/src/Gameplay.c index f8c692c..69196a3 100644 --- a/src/Gameplay.c +++ b/src/Gameplay.c @@ -10,12 +10,13 @@ #include "Screens.h" #include "Controls.h" +#include "Options.h" #include "Gameplay.h" #include "Score.h" #include "Timers.h" #include "Textures.h" -int score = 0, bestscore = 0; +int score = 0, bestscore = 0, finishfromGameplayScreen = 0; void LoadGamplayScreen(void) { @@ -31,6 +32,8 @@ void LoadGamplayScreen(void) void InitGameplayScreen(void) { + finishfromGameplayScreen = 0; + SetMasterVolume(0.5); player.currentframe = 0; @@ -155,8 +158,6 @@ void UpdateiFrameTimer(void) void UpdateGameplayScreen(void) { - if (IsKeyPressed(KEY_M)) mute = !mute; - if (INPUT_OPTION_PRESSED) pause = !pause; if (!pause) { @@ -220,13 +221,11 @@ void UpdateGameplayScreen(void) if (IsKeyPressed(KEY_R)) { - gameReset(); - currentScreen = TITLE; + finishfromGameplayScreen = 2; } if (player.hp <= 0) { - gameReset(); - currentScreen = GAMEOVER; + finishfromGameplayScreen = 1; } for (int i = 0; i < MAX_SHOOTS; i++) { @@ -357,8 +356,7 @@ void UnloadGameplayScreen() UnloadTexture(attack_sprite); } -void gameReset(void) +int FinishGameplayScreen(void) { - InitGameplayScreen(); - InitGameoverScreen(); + return finishfromGameplayScreen; } diff --git a/src/Gameplay.h b/src/Gameplay.h index 8b5a542..49e20ee 100644 --- a/src/Gameplay.h +++ b/src/Gameplay.h @@ -48,7 +48,6 @@ Sound fxhit = { 0 }; Sound fxfeather = { 0 }; Sound fxboom = { 0 }; bool pause; -bool mute = false; bool player_in; bool enemy_hurt; int ammo = 0; diff --git a/src/Main.c b/src/Main.c index 2501900..450ee3f 100644 --- a/src/Main.c +++ b/src/Main.c @@ -9,6 +9,7 @@ #include "../include/raylib.h" #include "Screens.h" +#include "Options.h" #if defined(PLATFORM_WEB) #include @@ -18,6 +19,12 @@ static const int screenWidth = 800; static const int screenHeight = 450; +static float transAlpha = 0.0f; +static bool onTransition = false; +static bool transFadeOut = false; +static int transFromScreen = -1; +static int transToScreen = -1; + GameScreen currentScreen = 0; Texture2D background; @@ -29,10 +36,11 @@ Texture2D attack_sprite; // Game functions static void gameSetup(void); -static void updateGame(void); -static void drawGame(void); -static void gameLoop(void); +static void update_draw_frame(void); static void unloadGame(void); +static void transition_to_screen(int screen); +static void update_transition(void); +static void draw_transition(void); int main(void) { @@ -43,11 +51,11 @@ int main(void) gameSetup(); #if defined(PLATFORM_WEB) - emscripten_set_main_loop(gameLoop, 60, 1); + emscripten_set_main_loop(update_draw_frame, 60, 1); #else SetTargetFPS(60); - while (!WindowShouldClose()) gameLoop(); + while (!WindowShouldClose()) update_draw_frame(); #endif unloadGame(); @@ -63,52 +71,134 @@ void gameSetup(void) { // asset loading & setting of variable values currentScreen = TITLE; - - LoadGamplayScreen(); - InitGameplayScreen(); - InitTitleScreen(); + background = LoadTexture("assets/gfx/background.png"); } -void updateGame(void) -{ - // code that runs as long as the program is running - if ((IsKeyDown(KEY_LEFT_ALT)) && (IsKeyPressed(KEY_F))) ToggleFullscreen(); - switch (currentScreen) { - case TITLE: UpdateTitleScreen(); break; - case GAMEPLAY: UpdateGameplayScreen(); break; - case GAMEOVER: UpdateGameoverScreen(); break; - case CREDITS: UpdateCreditsScreen(); break; - default: break; +static void transition_to_screen(int screen) +{ + onTransition = true; + transFadeOut = false; + transFromScreen = currentScreen; + transToScreen = screen; + transAlpha = 0.0f; +} + +static void update_transition(void) +{ + if (!transFadeOut) { + transAlpha += 0.05f; + + if (transAlpha > 1.01f) { + transAlpha = 1.0f; + + switch (transFromScreen) { + case TITLE: UnloadTitleScreen(); break; + case GAMEPLAY: UnloadGameplayScreen(); break; + case GAMEOVER: UnloadGameoverScreen(); break; + case CREDITS: UnloadCreditsScreen(); break; + case OPTIONS: UnloadOptionsScreen(); break; + default: break; + } + + switch (transToScreen) { + case TITLE: InitTitleScreen(); break; + case GAMEPLAY: LoadGamplayScreen(); InitGameplayScreen(); break; + case GAMEOVER: InitGameoverScreen(); break; + case CREDITS: InitCreditsScreen(); break; + case OPTIONS: InitOptionsScreen(); break; + default: break; + } + + currentScreen = transToScreen; + + transFadeOut = true; + } + } else { + transAlpha -= 0.02f; + + if (transAlpha < -0.01f) { + transAlpha = 0.0f; + transFadeOut = false; + onTransition = false; + transFromScreen = -1; + transToScreen = -1; + } } } -void drawGame(void) +static void draw_transition(void) { - // code to render the game to the game window + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha)); +} + + +static void update_draw_frame(void) +{ + if (IsKeyPressed(KEY_M)) mute = !mute; + if ((IsKeyDown(KEY_LEFT_ALT)) && (IsKeyPressed(KEY_F))) { ToggleFullscreen(); fullscreen = !fullscreen; } + + if (!onTransition) { + switch (currentScreen) { + case TITLE: { + UpdateTitleScreen(); + + if (FinishTitleScreen() == 1) transition_to_screen(CREDITS); + else if (FinishTitleScreen() == 2) transition_to_screen(GAMEPLAY); + else if (FinishTitleScreen() == 3) transition_to_screen(OPTIONS); + } break; + case CREDITS: { + UpdateCreditsScreen(); + + if (FinishCreditsScreen() == 1) transition_to_screen(TITLE); + } break; + case GAMEPLAY: { + UpdateGameplayScreen(); + + if (FinishGameplayScreen() == 1) transition_to_screen(GAMEOVER); + else if (FinishGameplayScreen() == 2) transition_to_screen(TITLE); + } break; + case GAMEOVER: { + UpdateGameoverScreen(); + + if (FinishGameoverScreen() == 1) transition_to_screen(TITLE); + else if (FinishGameoverScreen() == 2) transition_to_screen(GAMEPLAY); + } break; + case OPTIONS: { + UpdateOptionsScreen(); + + if (FinishOptionsScreen() == 1) transition_to_screen(TITLE); + } break; + default: break; + } + } else update_transition(); + BeginDrawing(); - ClearBackground(RAYWHITE); + ClearBackground(RAYWHITE); - switch (currentScreen) { - case TITLE: DrawTitleScreen(); break; - case GAMEPLAY: DrawGameplayScreen(); break; - case GAMEOVER: DrawGameoverScreen(); break; - case CREDITS: DrawCreditsScreen(); break; - default: break; - } + switch (currentScreen) { + case TITLE: DrawTitleScreen(); break; + case CREDITS: DrawCreditsScreen(); break; + case GAMEPLAY: DrawGameplayScreen(); break; + case GAMEOVER: DrawGameoverScreen(); break; + case OPTIONS: DrawOptionsScreen(); break; + default: break; + } + + if (onTransition) draw_transition(); EndDrawing(); } -void gameLoop(void) +static void unloadGame(void) { - updateGame(); - drawGame(); -} - -void unloadGame(void) -{ - UnloadGameplayScreen(); - UnloadTitleScreen(); + switch (currentScreen) { + case TITLE: UnloadTitleScreen(); break; + case GAMEPLAY: UnloadGameplayScreen(); break; + case GAMEOVER: UnloadGameoverScreen(); break; + case CREDITS: UnloadCreditsScreen(); break; + case OPTIONS: UnloadOptionsScreen(); break; + default: break; + } } diff --git a/src/Options.c b/src/Options.c new file mode 100644 index 0000000..680f59a --- /dev/null +++ b/src/Options.c @@ -0,0 +1,68 @@ +/* +- Avoid ~ a game by Canneddonuts +- Filename ~ Options.c +- Author ~ Return0ne +- 2022 +- *no license* +*/ + +#include "../include/raylib.h" + +#include "Screens.h" +#include "Textures.h" +#include "Controls.h" +#include "Options.h" + +int optionsSelected = 0, finishfromOptionsScreen = 0; + +void InitOptionsScreen(void) +{ + finishfromOptionsScreen = 0; + optionsSelected = 0; +} + +void UpdateOptionsScreen(void) +{ + if (INPUT_UP_PRESSED) optionsSelected++; + if (INPUT_DOWN_PRESSED) optionsSelected--; + if (optionsSelected > 0) optionsSelected--; + if (optionsSelected < -2) optionsSelected++; + + if ((optionsSelected == 0) && (INPUT_OPTION_PRESSED)) finishfromOptionsScreen = 1; + if ((optionsSelected == -1) && (INPUT_OPTION_PRESSED)) mute = !mute; + if ((optionsSelected == -2) && (INPUT_OPTION_PRESSED)) { ToggleFullscreen(); fullscreen = !fullscreen; } +} + +void DrawOptionsScreen(void) +{ + DrawTexture(background, 0, 0, DARKGRAY); + DrawText("OPTIONS", 300, 20, 50, BLUE); + if (optionsSelected == 0) DrawText("Back", 20, 200, 20, WHITE); + else DrawText("Back", 20, 200, 20, BLUE); + if (optionsSelected == -1) { + DrawText("Mute", 20, 220, 20, WHITE); + DrawText(TextFormat("<%i>", mute), 200, 220, 20, WHITE); + } + else { + DrawText("Mute", 20, 220, 20, BLUE); + DrawText(TextFormat("<%i>", mute), 200, 220, 20, BLUE); + } + if (optionsSelected == -2) { + DrawText("Fullscreen", 20, 240, 20, WHITE); + DrawText(TextFormat("<%i>", fullscreen), 200, 240, 20, WHITE); + } + else { + DrawText("Fullscreen", 20, 240, 20, BLUE); + DrawText(TextFormat("<%i>", fullscreen), 200, 240, 20, BLUE); + } +} + +void UnloadOptionsScreen(void) +{ + +} + +int FinishOptionsScreen(void) +{ + return finishfromOptionsScreen; +} diff --git a/src/Options.h b/src/Options.h new file mode 100644 index 0000000..de75d98 --- /dev/null +++ b/src/Options.h @@ -0,0 +1,15 @@ +/* +- Avoid ~ a game by Canneddonuts +- Filename ~ Options.h +- Author ~ Return0ne +- 2022 +- *no license* +*/ + +#ifndef OPTIONS_HEADER +#define OPTIONS_HEADER + +bool mute; +bool fullscreen; + +#endif diff --git a/src/Screens.h b/src/Screens.h index 46f7f57..4a1b79f 100644 --- a/src/Screens.h +++ b/src/Screens.h @@ -9,31 +9,39 @@ #ifndef SCREENS_HEADER #define SCREENS_HEADER -typedef enum GameScreen { TITLE = 0, GAMEPLAY, GAMEOVER, CREDITS } GameScreen; +typedef enum GameScreen { TITLE = 0, GAMEPLAY, GAMEOVER, CREDITS, OPTIONS } GameScreen; extern GameScreen currentScreen; -void gameReset(void); - void InitTitleScreen(void); void UpdateTitleScreen(void); void DrawTitleScreen(void); void UnloadTitleScreen(void); - +int FinishTitleScreen(void); void InitGameplayScreen(void); void UpdateGameplayScreen(void); void DrawGameplayScreen(void); void UnloadGameplayScreen(void); void LoadGamplayScreen(void); +int FinishGameplayScreen(void); - +void InitCreditsScreen(void); void UpdateCreditsScreen(void); void DrawCreditsScreen(void); +void UnloadCreditsScreen(void); +int FinishCreditsScreen(void); void InitGameoverScreen(void); void UpdateGameoverScreen(void); void DrawGameoverScreen(void); +void UnloadGameoverScreen(void); +int FinishGameoverScreen(void); +void InitOptionsScreen(void); +void UpdateOptionsScreen(void); +void DrawOptionsScreen(void); +void UnloadOptionsScreen(void); +int FinishOptionsScreen(void); #endif diff --git a/src/Title.c b/src/Title.c index 599307d..29eca53 100644 --- a/src/Title.c +++ b/src/Title.c @@ -1,6 +1,6 @@ /* - Avoid ~ a game by Canneddonuts -- Filename ~ Title.h +- Filename ~ Title.c - Author ~ Return0ne - 2022 - *no license* @@ -13,7 +13,7 @@ #include "Score.h" #include "Textures.h" -int titleSelected = 0; +int titleSelected = 0, finishfromTitleScreen = 0; void DrawScore(void) { @@ -27,9 +27,10 @@ void DrawScore(void) DrawText(TextFormat("BEST: %i", bestscore), 600, 0, 30, BLUE); } + void InitTitleScreen(void) { - background = LoadTexture("assets/gfx/background.png"); + finishfromTitleScreen = 0; } void UpdateTitleScreen(void) @@ -39,36 +40,41 @@ void UpdateTitleScreen(void) if (titleSelected > 0) titleSelected--; if (titleSelected < -2) titleSelected++; - if ((titleSelected == 0) && (INPUT_OPTION_PRESSED)) currentScreen = GAMEPLAY; - if ((titleSelected == -1) && (INPUT_OPTION_PRESSED)) currentScreen = CREDITS; - if ((titleSelected == -2) && (INPUT_OPTION_PRESSED)) OpenURL("https://canneddonuts.itch.io/"); + if ((titleSelected == 0) && (INPUT_OPTION_PRESSED)) finishfromTitleScreen = 2; + if ((titleSelected == -1) && (INPUT_OPTION_PRESSED)) finishfromTitleScreen = 1; + if ((titleSelected == -2) && (INPUT_OPTION_PRESSED)) finishfromTitleScreen = 3; } void DrawTitleScreen(void) { DrawTexture(background, 0, 0, GRAY); - DrawText("Controls", 10, 10, 30, BLUE); - DrawScore(); - DrawText("Press the arrow keys or 'DPAD' to move and 'X' to dash", 10, 40, 10, WHITE); - DrawText("Press 'ENTER' or 'START' to pause", 10, 60, 10, WHITE); - DrawText("Press 'M' to mute", 10, 80, 10, WHITE); - DrawText("Press 'Left-ALT' + 'F' for full screen", 10, 100, 10, WHITE); - DrawText("Press 'R' to restart", 10, 120, 10, WHITE); - DrawText("Press 'ENTER' or 'START' to select an option", 10, 140, 10, WHITE); - DrawText("Press 'X' or 'A' on a gamepad to shoot", 10, 160, 10, WHITE); DrawText("Avoid", 330, 20, 50, BLUE); + DrawText("Controls", 10, 10, 30, BLUE); + DrawScore(); + DrawText("Press the arrow keys or 'DPAD' to move and 'X' to dash", 10, 40, 10, WHITE); + DrawText("Press 'ENTER' or 'START' to pause", 10, 60, 10, WHITE); + DrawText("Press 'M' to mute", 10, 80, 10, WHITE); + DrawText("Press 'Left-ALT' + 'F' for full screen", 10, 100, 10, WHITE); + DrawText("Press 'R' to restart", 10, 120, 10, WHITE); + DrawText("Press 'ENTER' or 'START' to select an option", 10, 140, 10, WHITE); + DrawText("Press 'X' or 'A' on a gamepad to shoot", 10, 160, 10, WHITE); // DrawText("Ver: 0.1", 680, 420, 30, WHITE); - if (titleSelected == 0) DrawText("PLAY", 360, 220, 20, WHITE); - else DrawText("PLAY", 360, 220, 20, BLUE); + if (titleSelected == 0) DrawText("PLAY", 360, 220, 20, WHITE); + else DrawText("PLAY", 360, 220, 20, BLUE); - if (titleSelected == -1) DrawText("CREDITS", 340, 240, 20, WHITE); - else DrawText("CREDITS", 340, 240, 20, BLUE); + if (titleSelected == -1) DrawText("CREDITS", 340, 240, 20, WHITE); + else DrawText("CREDITS", 340, 240, 20, BLUE); - if (titleSelected == -2) DrawText("MORE GAMES", 320, 260, 20, WHITE); - else DrawText("MORE GAMES", 320, 260, 20, BLUE); + if (titleSelected == -2) DrawText("OPTIONS", 340, 260, 20, WHITE); + else DrawText("OPTIONS", 340, 260, 20, BLUE); } void UnloadTitleScreen(void) { - UnloadTexture(background); + +} + +int FinishTitleScreen(void) +{ + return finishfromTitleScreen; }