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.

293 lines
8.1 KiB

  1. /**********************************************************************************************
  2. *
  3. * raylib - transmission mission
  4. *
  5. *
  6. * Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
  7. *
  8. * This software is provided "as-is", without any express or implied warranty. In no event
  9. * will the authors be held liable for any damages arising from the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose, including commercial
  12. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must not claim that you
  15. * wrote the original software. If you use this software in a product, an acknowledgment
  16. * in the product documentation would be appreciated but is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  19. * as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. *
  23. **********************************************************************************************/
  24. #include "raylib.h"
  25. #include "screens.h"
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #define MISSION_MAX_LENGTH 256
  29. #define KEYWORD_MAX_LENGTH 32
  30. #define MAX_LINE_CHAR 75
  31. //----------------------------------------------------------------------------------
  32. // Global Variables Definition (local to this module)
  33. //----------------------------------------------------------------------------------
  34. // Mission screen global variables
  35. static int framesCounter;
  36. static int finishScreen;
  37. static Texture2D texBackground;
  38. static Texture2D texBackline; //mission_backline
  39. static Rectangle sourceRecBackLine;
  40. static Rectangle destRecBackLine;
  41. static float fadeBackLine;
  42. static Vector2 numberPosition;
  43. static Color numberColor;
  44. //static char textMission[MISSION_MAX_LENGTH];
  45. static Vector2 missionPosition;
  46. static int missionSize;
  47. static Color missionColor;
  48. static int missionLenght;
  49. static bool missionMaxLength;
  50. static int missionSpeed;
  51. //static char textKeyword[KEYWORD_MAX_LENGTH];
  52. static Vector2 keywordPosition;
  53. static Color keywordColor;
  54. static int showMissionWaitFrames;
  55. static int showNumberWaitFrames;
  56. static int showKeywordWaitFrames;
  57. static bool startWritting;
  58. static bool writeMission;
  59. static bool writeNumber;
  60. static bool writeKeyword;
  61. static bool writeEnd;
  62. static bool writtingMission;
  63. static int blinkFrames;
  64. static bool blinkKeyWord = true;
  65. static bool showButton = false;
  66. static Mission *missions = NULL;
  67. static Sound fxTransmit;
  68. static Music musMission;
  69. //----------------------------------------------------------------------------------
  70. // Mission Screen Functions Definition
  71. //----------------------------------------------------------------------------------
  72. static void WriteMissionText();
  73. static void EndWritting();
  74. static void BlinkKeyword();
  75. // Mission Screen Initialization logic
  76. void InitMissionScreen(void)
  77. {
  78. framesCounter = 0;
  79. finishScreen = 0;
  80. fadeButton = 0.80f;
  81. texBackground = LoadTexture("resources/textures/mission_background.png");
  82. texBackline = LoadTexture("resources/textures/mission_backline.png");
  83. sourceRecBackLine = (Rectangle){0,0,GetScreenWidth(), texBackline.height};
  84. destRecBackLine = (Rectangle){0,0,sourceRecBackLine.width, sourceRecBackLine.height};
  85. fadeBackLine = 0;
  86. fxTransmit = LoadSound("resources/audio/fx_message.ogg");
  87. musMission = LoadMusicStream("resources/audio/music_mission.ogg");
  88. PlayMusicStream(musMission);
  89. // Initialize missions
  90. missions = LoadMissions("resources/missions.txt");
  91. missionMaxLength = strlen(missions[currentMission].brief);
  92. // Insert line breaks every MAX_LINE_CHAR
  93. int currentLine = 1;
  94. int i = currentLine * MAX_LINE_CHAR;
  95. while (i < missionMaxLength)
  96. {
  97. if (missions[currentMission].brief[i] == ' ')
  98. {
  99. missions[currentMission].brief[i] = '\n';
  100. currentLine++;
  101. i = currentLine*MAX_LINE_CHAR;
  102. }
  103. else i++;
  104. }
  105. missionSize = 30;
  106. missionLenght = 0;
  107. missionSpeed = 1;
  108. numberColor = RAYWHITE;
  109. missionColor = LIGHTGRAY;
  110. keywordColor = (Color){198, 49, 60, 255}; //RED
  111. numberPosition = (Vector2){150, 185};
  112. missionPosition = (Vector2){numberPosition.x, numberPosition.y + 60};
  113. keywordPosition = (Vector2){missionPosition.x, missionPosition.y + MeasureTextEx(fontMission, missions[currentMission].brief, missionSize, 0).y + 60};
  114. startWritting = false;
  115. writeNumber = false;
  116. writeMission = false;
  117. writeKeyword = false;
  118. writeEnd = false;
  119. writtingMission = false;
  120. showNumberWaitFrames = 30;
  121. showMissionWaitFrames = 60;
  122. showKeywordWaitFrames = 60;
  123. blinkKeyWord = true;
  124. blinkFrames = 15;
  125. PlaySound(fxTransmit);
  126. }
  127. // Mission Screen Update logic
  128. void UpdateMissionScreen(void)
  129. {
  130. UpdateMusicStream(musMission);
  131. if (!writeEnd) WriteMissionText();
  132. else BlinkKeyword();
  133. if (showButton)
  134. {
  135. if (IsKeyPressed(KEY_ENTER) || IsButtonPressed())
  136. {
  137. if (!writeEnd) EndWritting();
  138. else
  139. {
  140. finishScreen = true;
  141. showButton = false;
  142. }
  143. }
  144. }
  145. }
  146. // Mission Screen Draw logic
  147. void DrawMissionScreen(void)
  148. {
  149. // TODO: Draw MISSION screen here!
  150. DrawTexture(texBackground, 0,0, WHITE);
  151. DrawTexturePro(texBackline, sourceRecBackLine, destRecBackLine, (Vector2){0,0},0, Fade(WHITE, fadeBackLine));
  152. if (writeNumber) DrawTextEx(fontMission, FormatText("Filtración #%02i ", currentMission + 1), numberPosition, missionSize + 10, 0, numberColor);
  153. DrawTextEx(fontMission, SubText(missions[currentMission].brief, 0, missionLenght), missionPosition, missionSize, 0, missionColor);
  154. if (writeKeyword && blinkKeyWord) DrawTextEx(fontMission, FormatText("Keyword: %s", missions[currentMission].key), keywordPosition, missionSize + 10, 0, keywordColor);
  155. if (showButton)
  156. {
  157. if (!writeEnd) DrawButton("saltar");
  158. else DrawButton("codificar");
  159. }
  160. }
  161. // Mission Screen Unload logic
  162. void UnloadMissionScreen(void)
  163. {
  164. // TODO: Unload MISSION screen variables here!
  165. UnloadTexture(texBackground);
  166. UnloadTexture(texBackline);
  167. UnloadSound(fxTransmit);
  168. UnloadMusicStream(musMission);
  169. free(missions);
  170. }
  171. // Mission Screen should finish?
  172. int FinishMissionScreen(void)
  173. {
  174. return finishScreen;
  175. }
  176. static void WriteMissionText()
  177. {
  178. if(!startWritting)
  179. {
  180. framesCounter++;
  181. if(framesCounter % 60 == 0)
  182. {
  183. framesCounter = 0;
  184. startWritting = true;
  185. }
  186. }
  187. else if(!writeNumber)
  188. {
  189. framesCounter++;
  190. fadeBackLine += 0.020f;
  191. if(framesCounter % showNumberWaitFrames == 0)
  192. {
  193. framesCounter = 0;
  194. writeNumber = true;
  195. showButton = true;
  196. }
  197. }
  198. else if(!writeMission)
  199. {
  200. framesCounter ++;
  201. if(framesCounter % showMissionWaitFrames == 0)
  202. {
  203. framesCounter = 0;
  204. writeMission = true;
  205. writtingMission = true;
  206. }
  207. }
  208. else if(writeMission && writtingMission)
  209. {
  210. framesCounter++;
  211. if(framesCounter % missionSpeed == 0)
  212. {
  213. framesCounter = 0;
  214. missionLenght++;
  215. if(missionLenght == missionMaxLength)
  216. {
  217. writtingMission = false;
  218. }
  219. }
  220. }
  221. else if(!writeKeyword)
  222. {
  223. framesCounter++;
  224. if(framesCounter % showKeywordWaitFrames == 0)
  225. {
  226. framesCounter = 0;
  227. writeKeyword = true;
  228. writeEnd = true;
  229. }
  230. }
  231. }
  232. static void EndWritting()
  233. {
  234. writeEnd = true;
  235. writeKeyword = true;
  236. writeNumber = true;
  237. missionLenght = missionMaxLength;
  238. }
  239. static void BlinkKeyword()
  240. {
  241. framesCounter++;
  242. if(framesCounter % blinkFrames == 0)
  243. {
  244. framesCounter = 0;
  245. blinkKeyWord = !blinkKeyWord;
  246. }
  247. }