You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
8.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*******************************************************************************************
  2. *
  3. * CAT VS ROOMBA [GLOBAL GAME JAM 2019]
  4. *
  5. * Ah! Home, sweet home! Time for some automatic cleaning...
  6. * if the worst enemy of Roomba allows it... be careful with Cat!
  7. *
  8. * This game has been created using raylib 2.0 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2019 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "screens/screens.h" // NOTE: Defines global variable: currentScreen
  16. #if defined(PLATFORM_WEB)
  17. #include <emscripten/emscripten.h>
  18. #endif
  19. //----------------------------------------------------------------------------------
  20. // Global Variables Definition (local to this module)
  21. //----------------------------------------------------------------------------------
  22. const int screenWidth = 1280;
  23. #if defined(TILE_VIEWER_MODE)
  24. const int screenHeight = 1080;
  25. #else
  26. const int screenHeight = 720;
  27. #endif
  28. // Required variables to manage screen transitions (fade-in, fade-out)
  29. static float transAlpha = 0.0f;
  30. static bool onTransition = false;
  31. static bool transFadeOut = false;
  32. static int transFromScreen = -1;
  33. static int transToScreen = -1;
  34. // NOTE: Some global variables that require to be visible for all screens,
  35. // are defined in screens.h (i.e. currentScreen)
  36. //----------------------------------------------------------------------------------
  37. // Local Functions Declaration
  38. //----------------------------------------------------------------------------------
  39. static void ChangeToScreen(int screen); // No transition effect
  40. static void TransitionToScreen(int screen);
  41. static void UpdateTransition(void);
  42. static void DrawTransition(void);
  43. static void UpdateDrawFrame(void); // Update and Draw one frame
  44. //----------------------------------------------------------------------------------
  45. // Main entry point
  46. //----------------------------------------------------------------------------------
  47. int main(void)
  48. {
  49. // Initialization (Note windowTitle is unused on Android)
  50. //---------------------------------------------------------
  51. InitWindow(screenWidth, screenHeight, "CAT VS ROOMBA [GGJ19]");
  52. // Global data loading (assets that must be available in all screens, i.e. fonts)
  53. InitAudioDevice();
  54. font = LoadFont("resources/star.fnt");
  55. font2 = LoadFont("resources/star2.fnt");
  56. music = LoadMusicStream("resources/cat_mouse.mod");
  57. fxCoin = LoadSound("resources/coin.wav");
  58. SetMusicVolume(music, 1.0f);
  59. PlayMusicStream(music);
  60. // Setup and Init first screen
  61. currentScreen = LOGO;
  62. InitLogoScreen();
  63. #if defined(PLATFORM_WEB)
  64. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  65. #else
  66. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  67. //--------------------------------------------------------------------------------------
  68. // Main game loop
  69. while (!WindowShouldClose()) // Detect window close button or ESC key
  70. {
  71. UpdateDrawFrame();
  72. }
  73. #endif
  74. // De-Initialization
  75. //--------------------------------------------------------------------------------------
  76. // Unload current screen data before closing
  77. switch (currentScreen)
  78. {
  79. case LOGO: UnloadLogoScreen(); break;
  80. case TITLE: UnloadTitleScreen(); break;
  81. case GAMEPLAY: UnloadGameplayScreen(); break;
  82. case ENDING: UnloadEndingScreen(); break;
  83. default: break;
  84. }
  85. // Unload all global loaded data (i.e. fonts) here!
  86. UnloadFont(font);
  87. UnloadFont(font2);
  88. UnloadMusicStream(music);
  89. UnloadSound(fxCoin);
  90. CloseAudioDevice(); // Close audio context
  91. CloseWindow(); // Close window and OpenGL context
  92. //--------------------------------------------------------------------------------------
  93. return 0;
  94. }
  95. //----------------------------------------------------------------------------------
  96. // Module specific Functions Definition
  97. //----------------------------------------------------------------------------------
  98. // Change to next screen, no transition
  99. static void ChangeToScreen(int screen)
  100. {
  101. // Unload current screen
  102. switch (currentScreen)
  103. {
  104. case LOGO: UnloadLogoScreen(); break;
  105. case TITLE: UnloadTitleScreen(); break;
  106. case GAMEPLAY: UnloadGameplayScreen(); break;
  107. case ENDING: UnloadEndingScreen(); break;
  108. default: break;
  109. }
  110. // Init next screen
  111. switch (screen)
  112. {
  113. case LOGO: InitLogoScreen(); break;
  114. case TITLE: InitTitleScreen(); break;
  115. case GAMEPLAY: InitGameplayScreen(); break;
  116. case ENDING: InitEndingScreen(); break;
  117. default: break;
  118. }
  119. currentScreen = screen;
  120. }
  121. // Define transition to next screen
  122. static void TransitionToScreen(int screen)
  123. {
  124. onTransition = true;
  125. transFadeOut = false;
  126. transFromScreen = currentScreen;
  127. transToScreen = screen;
  128. transAlpha = 0.0f;
  129. }
  130. // Update transition effect
  131. static void UpdateTransition(void)
  132. {
  133. if (!transFadeOut)
  134. {
  135. transAlpha += 0.02f;
  136. // NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
  137. // For that reason we compare against 1.01f, to avoid last frame loading stop
  138. if (transAlpha > 1.01f)
  139. {
  140. transAlpha = 1.0f;
  141. // Unload current screen
  142. switch (transFromScreen)
  143. {
  144. case LOGO: UnloadLogoScreen(); break;
  145. case TITLE: UnloadTitleScreen(); break;
  146. case GAMEPLAY: UnloadGameplayScreen(); break;
  147. case ENDING: UnloadEndingScreen(); break;
  148. default: break;
  149. }
  150. // Load next screen
  151. switch (transToScreen)
  152. {
  153. case LOGO: InitLogoScreen(); break;
  154. case TITLE: InitTitleScreen(); break;
  155. case GAMEPLAY: InitGameplayScreen(); break;
  156. case ENDING: InitEndingScreen(); break;
  157. default: break;
  158. }
  159. currentScreen = transToScreen;
  160. // Activate fade out effect to next loaded screen
  161. transFadeOut = true;
  162. }
  163. }
  164. else // Transition fade out logic
  165. {
  166. transAlpha -= 0.02f;
  167. if (transAlpha < -0.01f)
  168. {
  169. transAlpha = 0.0f;
  170. transFadeOut = false;
  171. onTransition = false;
  172. transFromScreen = -1;
  173. transToScreen = -1;
  174. }
  175. }
  176. }
  177. // Draw transition effect (full-screen rectangle)
  178. static void DrawTransition(void)
  179. {
  180. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
  181. }
  182. // Update and draw game frame
  183. static void UpdateDrawFrame(void)
  184. {
  185. // Update
  186. //----------------------------------------------------------------------------------
  187. UpdateMusicStream(music); // NOTE: Music keeps playing between screens
  188. if (!onTransition)
  189. {
  190. switch(currentScreen)
  191. {
  192. case LOGO:
  193. {
  194. UpdateLogoScreen();
  195. if (FinishLogoScreen()) TransitionToScreen(TITLE);
  196. } break;
  197. case TITLE:
  198. {
  199. UpdateTitleScreen();
  200. if (FinishTitleScreen() == 1) TransitionToScreen(OPTIONS);
  201. else if (FinishTitleScreen() == 2) TransitionToScreen(GAMEPLAY);
  202. } break;
  203. case GAMEPLAY:
  204. {
  205. UpdateGameplayScreen();
  206. if (FinishGameplayScreen() == 1) TransitionToScreen(ENDING);
  207. //else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
  208. } break;
  209. case ENDING:
  210. {
  211. UpdateEndingScreen();
  212. if (FinishEndingScreen() == 1) TransitionToScreen(TITLE);
  213. } break;
  214. default: break;
  215. }
  216. }
  217. else UpdateTransition(); // Update transition (fade-in, fade-out)
  218. //----------------------------------------------------------------------------------
  219. // Draw
  220. //----------------------------------------------------------------------------------
  221. BeginDrawing();
  222. ClearBackground(RAYWHITE);
  223. switch(currentScreen)
  224. {
  225. case LOGO: DrawLogoScreen(); break;
  226. case TITLE: DrawTitleScreen(); break;
  227. case GAMEPLAY: DrawGameplayScreen(); break;
  228. case ENDING: DrawEndingScreen(); break;
  229. default: break;
  230. }
  231. // Draw full screen rectangle in front of everything
  232. if (onTransition) DrawTransition();
  233. //DrawFPS(10, 10);
  234. EndDrawing();
  235. //----------------------------------------------------------------------------------
  236. }