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.

455 lines
14 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - transmission mission
  4. *
  5. * Code and transmit the right message
  6. *
  7. * This game has been created using raylib (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include "screens/screens.h" // NOTE: Defines global variable: currentScreen
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #if defined(PLATFORM_WEB)
  18. #include <emscripten/emscripten.h>
  19. #endif
  20. //----------------------------------------------------------------------------------
  21. // Global Variables Definition (local to this module)
  22. //----------------------------------------------------------------------------------
  23. const int screenWidth = 1280;
  24. const int screenHeight = 720;
  25. // Required variables to manage screen transitions (fade-in, fade-out)
  26. static float transAlpha = 0.0f;
  27. static bool onTransition = false;
  28. static bool transFadeOut = false;
  29. static int transFromScreen = -1;
  30. static int transToScreen = -1;
  31. // NOTE: Some global variables that require to be visible for all screens,
  32. // are defined in screens.h (i.e. currentScreen)
  33. //----------------------------------------------------------------------------------
  34. // Local Functions Declaration
  35. //----------------------------------------------------------------------------------
  36. static void ChangeToScreen(int screen); // No transition effect
  37. static void TransitionToScreen(int screen);
  38. static void UpdateTransition(void);
  39. static void DrawTransition(void);
  40. static void UpdateDrawFrame(void); // Update and Draw one frame
  41. //----------------------------------------------------------------------------------
  42. // Main entry point
  43. //----------------------------------------------------------------------------------
  44. int main(void)
  45. {
  46. // Initialization
  47. //---------------------------------------------------------
  48. #ifndef PLATFORM_ANDROID
  49. SetConfigFlags(FLAG_SHOW_LOGO); // | FLAG_FULLSCREEN_MODE);
  50. #endif
  51. // Note windowTitle is unused on Android
  52. InitWindow(screenWidth, screenHeight, "raylib game - transmission mission");
  53. // Global data loading (assets that must be available in all screens, i.e. fonts)
  54. InitAudioDevice();
  55. music = LoadMusicStream("resources/audio/music_title.ogg");
  56. fxButton = LoadSound("resources/audio/fx_newspaper.ogg");
  57. SetMusicVolume(music, 1.0f);
  58. PlayMusicStream(music);
  59. fontMission = LoadSpriteFontEx("resources/fonts/traveling_typewriter.ttf", 64, 250, 0);
  60. texButton = LoadTexture("resources/textures/title_ribbon.png");
  61. // UI BUTTON
  62. recButton.width = texButton.width;
  63. recButton.height = texButton.height;
  64. recButton.x = screenWidth - recButton.width;
  65. recButton.y = screenHeight - recButton.height - 50;
  66. fadeButton = 0.8f;
  67. colorButton = RED;
  68. textPositionButton = (Vector2){recButton.x + recButton.width/2, recButton.y + recButton.height/2};
  69. fontSizeButton = 30;
  70. textColorButton = WHITE;
  71. currentMission = 0;
  72. totalMissions = 4;
  73. // Setup and Init first screen
  74. currentScreen = LOGO;
  75. InitLogoScreen();
  76. #if defined(PLATFORM_WEB)
  77. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  78. #else
  79. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  80. //--------------------------------------------------------------------------------------
  81. // Main game loop
  82. while (!WindowShouldClose()) // Detect window close button or ESC key
  83. {
  84. UpdateDrawFrame();
  85. }
  86. #endif
  87. // De-Initialization
  88. //--------------------------------------------------------------------------------------
  89. // Unload current screen data before closing
  90. switch (currentScreen)
  91. {
  92. case LOGO: UnloadLogoScreen(); break;
  93. case TITLE: UnloadTitleScreen(); break;
  94. case MISSION: UnloadMissionScreen(); break;
  95. case GAMEPLAY: UnloadGameplayScreen(); break;
  96. case ENDING: UnloadEndingScreen(); break;
  97. default: break;
  98. }
  99. // Unload all global loaded data (i.e. fonts) here!
  100. UnloadMusicStream(music);
  101. UnloadSound(fxButton);
  102. UnloadSpriteFont(fontMission);
  103. UnloadTexture(texButton);
  104. CloseAudioDevice(); // Close audio context
  105. CloseWindow(); // Close window and OpenGL context
  106. //--------------------------------------------------------------------------------------
  107. return 0;
  108. }
  109. //----------------------------------------------------------------------------------
  110. // Module specific Functions Definition
  111. //----------------------------------------------------------------------------------
  112. // Change to next screen, no transition
  113. static void ChangeToScreen(int screen)
  114. {
  115. // Unload current screen
  116. switch (currentScreen)
  117. {
  118. case LOGO: UnloadLogoScreen(); break;
  119. case TITLE: UnloadTitleScreen(); break;
  120. case MISSION: UnloadMissionScreen(); break;
  121. case GAMEPLAY: UnloadGameplayScreen(); break;
  122. case ENDING: UnloadEndingScreen(); break;
  123. default: break;
  124. }
  125. // Init next screen
  126. switch (screen)
  127. {
  128. case LOGO: InitLogoScreen(); break;
  129. case TITLE: InitTitleScreen(); break;
  130. case MISSION: InitMissionScreen(); break;
  131. case GAMEPLAY: InitGameplayScreen(); break;
  132. case ENDING: InitEndingScreen(); break;
  133. default: break;
  134. }
  135. currentScreen = screen;
  136. }
  137. // Define transition to next screen
  138. static void TransitionToScreen(int screen)
  139. {
  140. onTransition = true;
  141. transFadeOut = false;
  142. transFromScreen = currentScreen;
  143. transToScreen = screen;
  144. transAlpha = 0.0f;
  145. }
  146. // Update transition effect
  147. static void UpdateTransition(void)
  148. {
  149. if (!transFadeOut)
  150. {
  151. transAlpha += 0.02f;
  152. // NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
  153. // For that reason we compare against 1.01f, to avoid last frame loading stop
  154. if (transAlpha > 1.01f)
  155. {
  156. transAlpha = 1.0f;
  157. // Unload current screen
  158. switch (transFromScreen)
  159. {
  160. case LOGO: UnloadLogoScreen(); break;
  161. case TITLE: UnloadTitleScreen(); break;
  162. case MISSION: UnloadMissionScreen(); break;
  163. case GAMEPLAY: UnloadGameplayScreen(); break;
  164. case ENDING: UnloadEndingScreen(); break;
  165. default: break;
  166. }
  167. // Load next screen
  168. switch (transToScreen)
  169. {
  170. case LOGO: InitLogoScreen(); break;
  171. case TITLE: InitTitleScreen(); break;
  172. case MISSION: InitMissionScreen(); break;
  173. case GAMEPLAY: InitGameplayScreen(); break;
  174. case ENDING: InitEndingScreen(); break;
  175. default: break;
  176. }
  177. currentScreen = transToScreen;
  178. // Activate fade out effect to next loaded screen
  179. transFadeOut = true;
  180. }
  181. }
  182. else // Transition fade out logic
  183. {
  184. transAlpha -= 0.02f;
  185. if (transAlpha < -0.01f)
  186. {
  187. transAlpha = 0.0f;
  188. transFadeOut = false;
  189. onTransition = false;
  190. transFromScreen = -1;
  191. transToScreen = -1;
  192. }
  193. }
  194. }
  195. // Draw transition effect (full-screen rectangle)
  196. static void DrawTransition(void)
  197. {
  198. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
  199. }
  200. // Update and draw game frame
  201. static void UpdateDrawFrame(void)
  202. {
  203. // Update
  204. //----------------------------------------------------------------------------------
  205. UpdateMusicStream(music); // NOTE: Music keeps playing between screens
  206. if (!onTransition)
  207. {
  208. switch(currentScreen)
  209. {
  210. case LOGO:
  211. {
  212. UpdateLogoScreen();
  213. if (FinishLogoScreen()) TransitionToScreen(TITLE);
  214. } break;
  215. case TITLE:
  216. {
  217. UpdateTitleScreen();
  218. if (FinishTitleScreen())
  219. {
  220. StopMusicStream(music);
  221. TransitionToScreen(MISSION);
  222. }
  223. } break;
  224. case MISSION:
  225. {
  226. UpdateMissionScreen();
  227. if (FinishMissionScreen())
  228. {
  229. StopMusicStream(music);
  230. TransitionToScreen(GAMEPLAY);
  231. }
  232. } break;
  233. case GAMEPLAY:
  234. {
  235. UpdateGameplayScreen();
  236. if (FinishGameplayScreen() == 1) TransitionToScreen(ENDING);
  237. //else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
  238. } break;
  239. case ENDING:
  240. {
  241. UpdateEndingScreen();
  242. if (FinishEndingScreen() == 1) // Continue to next mission
  243. {
  244. TransitionToScreen(MISSION);
  245. }
  246. else if (FinishEndingScreen() == 2) // Replay current mission
  247. {
  248. PlayMusicStream(music);
  249. TransitionToScreen(TITLE);
  250. }
  251. } break;
  252. default: break;
  253. }
  254. }
  255. else UpdateTransition(); // Update transition (fade-in, fade-out)
  256. //----------------------------------------------------------------------------------
  257. // Draw
  258. //----------------------------------------------------------------------------------
  259. BeginDrawing();
  260. ClearBackground(RAYWHITE);
  261. switch(currentScreen)
  262. {
  263. case LOGO: DrawLogoScreen(); break;
  264. case TITLE: DrawTitleScreen(); break;
  265. case MISSION: DrawMissionScreen(); break;
  266. case GAMEPLAY: DrawGameplayScreen(); break;
  267. case ENDING: DrawEndingScreen(); break;
  268. default: break;
  269. }
  270. // Draw full screen rectangle in front of everything
  271. if (onTransition) DrawTransition();
  272. //DrawFPS(10, 10);
  273. EndDrawing();
  274. //----------------------------------------------------------------------------------
  275. }
  276. // Load missions from text file
  277. Mission *LoadMissions(const char *fileName)
  278. {
  279. Mission *missions = NULL;
  280. char buffer[512];
  281. int missionsCount = 0;
  282. FILE *misFile = fopen(fileName, "rt");
  283. if (misFile == NULL) printf("[%s] Missions file could not be opened\n", fileName);
  284. else
  285. {
  286. // First pass to get total missions count
  287. while (!feof(misFile))
  288. {
  289. fgets(buffer, 512, misFile);
  290. switch (buffer[0])
  291. {
  292. case 't': sscanf(buffer, "t %i", &missionsCount); break;
  293. default: break;
  294. }
  295. }
  296. if (missionsCount > 0) missions = (Mission *)malloc(missionsCount*sizeof(Mission));
  297. else return NULL;
  298. rewind(misFile); // Return to the beginning of the file, to read again
  299. int missionNum = 0;
  300. while (!feof(misFile))
  301. {
  302. fgets(buffer, 512, misFile);
  303. if (missionNum < missionsCount)
  304. {
  305. switch (buffer[0])
  306. {
  307. case 'b':
  308. {
  309. // New mission brief starts!
  310. missions[missionNum].id = missionNum;
  311. sscanf(buffer, "b %[^\n]s", missions[missionNum].brief);
  312. } break;
  313. case 'k': sscanf(buffer, "k %[^\n]s", missions[missionNum].key); break;
  314. case 'm':
  315. {
  316. // NOTE: Message is loaded as is, needs to be processed!
  317. sscanf(buffer, "m %[^\n]s", missions[missionNum].msg);
  318. } break;
  319. case 's':
  320. {
  321. sscanf(buffer, "s %i %i %i %i %i %i %i %i",
  322. &missions[missionNum].sols[0],
  323. &missions[missionNum].sols[1],
  324. &missions[missionNum].sols[2],
  325. &missions[missionNum].sols[3],
  326. &missions[missionNum].sols[4],
  327. &missions[missionNum].sols[5],
  328. &missions[missionNum].sols[6],
  329. &missions[missionNum].sols[7]);
  330. missions[missionNum].wordsCount = 0;
  331. for (int i = 0; i < 8; i++)
  332. {
  333. if (missions[missionNum].sols[i] > -1)
  334. {
  335. missions[missionNum].wordsCount++;
  336. }
  337. }
  338. TraceLog(LOG_WARNING, "Mission %i - Words count %i", missionNum, missions[missionNum].wordsCount);
  339. missionNum++;
  340. } break;
  341. default: break;
  342. }
  343. }
  344. }
  345. if (missionsCount != missionNum) TraceLog(LOG_WARNING, "Missions count and loaded missions don't match!");
  346. }
  347. fclose(misFile);
  348. if (missions != NULL)
  349. {
  350. TraceLog(LOG_INFO, "Missions loaded: %i", missionsCount);
  351. TraceLog(LOG_INFO, "Missions loaded successfully!");
  352. }
  353. return missions;
  354. }
  355. bool IsButtonPressed()
  356. {
  357. if (CheckCollisionPointRec(GetMousePosition(), recButton))
  358. {
  359. fadeButton = 1.0f;
  360. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsGestureDetected(GESTURE_TAP))
  361. {
  362. PlaySound(fxButton);
  363. return true;
  364. }
  365. }
  366. else fadeButton = 0.80f;
  367. return false;
  368. }
  369. void DrawButton(const char *text)
  370. {
  371. //DrawRectangleRec(recButton, Fade(colorButton, fadeButton));
  372. DrawTexturePro(texButton, (Rectangle){0,0,texButton.width, texButton.height}, recButton, (Vector2){0,0},0, Fade(WHITE, fadeButton));
  373. Vector2 measure = MeasureTextEx(fontMission, text, fontSizeButton, 0);
  374. Vector2 textPos = {textPositionButton.x - measure.x/2 + 10, textPositionButton.y - measure.y/2 - 10};
  375. DrawTextEx(fontMission, text, textPos , fontSizeButton, 0, textColorButton);
  376. }