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.

198 lines
6.2 KiB

  1. /**********************************************************************************************
  2. *
  3. * raylib - Standard Game template
  4. *
  5. * Level09 Screen Functions Definitions (Init, Update, Draw, Unload)
  6. *
  7. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  8. *
  9. * This software is provided "as-is", without any express or implied warranty. In no event
  10. * will the authors be held liable for any damages arising from the use of this software.
  11. *
  12. * Permission is granted to anyone to use this software for any purpose, including commercial
  13. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not claim that you
  16. * wrote the original software. If you use this software in a product, an acknowledgment
  17. * in the product documentation would be appreciated but is not required.
  18. *
  19. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  20. * as being the original software.
  21. *
  22. * 3. This notice may not be removed or altered from any source distribution.
  23. *
  24. **********************************************************************************************/
  25. #include "raylib.h"
  26. #include "screens.h"
  27. #define NUM_BOXES 21
  28. //----------------------------------------------------------------------------------
  29. // Global Variables Definition (local to this module)
  30. //----------------------------------------------------------------------------------
  31. // Level09 screen global variables
  32. static int framesCounter;
  33. static int finishScreen;
  34. static Rectangle bwRecs[NUM_BOXES];
  35. static Color bwColors[NUM_BOXES];
  36. static bool activated[NUM_BOXES];
  37. static int resetCounter = 0;
  38. static bool enableCounter = 0;
  39. static bool done = false;
  40. static int levelTimeSec = 0;
  41. static bool levelFinished = false;
  42. //----------------------------------------------------------------------------------
  43. // Level09 Screen Functions Definition
  44. //----------------------------------------------------------------------------------
  45. static bool CheckColor(Color col1, Color col2);
  46. // Level09 Screen Initialization logic
  47. void InitLevel09Screen(void)
  48. {
  49. // Initialize Level09 screen variables here!
  50. framesCounter = 0;
  51. finishScreen = 0;
  52. for (int i = 0; i < NUM_BOXES; i++)
  53. {
  54. bwRecs[i].x = GetScreenWidth()/7*(i%7);
  55. bwRecs[i].y = GetScreenHeight()/3*(i/7);
  56. bwRecs[i].width = GetScreenWidth()/7;
  57. bwRecs[i].height = GetScreenHeight()/3;
  58. activated[i] = false;
  59. if (i%2 == 0) bwColors[i] = LIGHTGRAY;
  60. else bwColors[i] = GRAY;
  61. }
  62. bwColors[10] = RAYWHITE;
  63. }
  64. // Level09 Screen Update logic
  65. void UpdateLevel09Screen(void)
  66. {
  67. // Update Level09 screen variables here!
  68. framesCounter++;
  69. if (enableCounter) resetCounter++;
  70. if (!done)
  71. {
  72. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  73. {
  74. for (int i = 0; i < NUM_BOXES; i++)
  75. {
  76. if (CheckCollisionPointRec(GetMousePosition(), bwRecs[i]))
  77. {
  78. if (i == 10)
  79. {
  80. if (CheckColor(bwColors[i], RAYWHITE))
  81. {
  82. bwColors[i] = LIGHTGRAY;
  83. enableCounter = true;
  84. resetCounter = 0;
  85. activated[1] = true;
  86. }
  87. else
  88. {
  89. bwColors[i] = RAYWHITE;
  90. enableCounter = false;
  91. resetCounter = 5*60;
  92. for (int i = 0; i < NUM_BOXES; i++) activated[i] = false;
  93. }
  94. }
  95. else if ((i%2 == 1) && enableCounter)
  96. {
  97. if (activated[i])
  98. {
  99. bwColors[i] = LIGHTGRAY;
  100. if (i != 19) activated[i + 2] = true;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. if (resetCounter > (4*60 + 10))
  107. {
  108. for (int i = 0; i < NUM_BOXES; i++)
  109. {
  110. if (i%2 == 0) bwColors[i] = LIGHTGRAY;
  111. else bwColors[i] = GRAY;
  112. activated[i] = false;
  113. }
  114. bwColors[10] = RAYWHITE;
  115. enableCounter = false;
  116. resetCounter = 0;
  117. }
  118. for (int i = 0; i < NUM_BOXES; i++)
  119. {
  120. done = true;
  121. if (!CheckColor(bwColors[i], LIGHTGRAY))
  122. {
  123. done = false;
  124. return;
  125. }
  126. //if (done) PlaySound(levelWin);
  127. }
  128. }
  129. if (done && !levelFinished)
  130. {
  131. levelTimeSec = framesCounter/60;
  132. levelFinished = true;
  133. framesCounter = 0;
  134. }
  135. if (levelFinished)
  136. {
  137. framesCounter++;
  138. if ((framesCounter > 90) && (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) finishScreen = true;
  139. }
  140. }
  141. // Level09 Screen Draw logic
  142. void DrawLevel09Screen(void)
  143. {
  144. // Draw Level09 screen
  145. for (int i = 0; i < NUM_BOXES; i++)
  146. {
  147. DrawRectangleRec(bwRecs[i], bwColors[i]);
  148. }
  149. if (levelFinished)
  150. {
  151. DrawRectangleBordersRec((Rectangle){0, 0, GetScreenWidth(), GetScreenHeight()}, 0, 0, 60, Fade(RAYWHITE, 0.6f));
  152. DrawText("LEVEL 09", GetScreenWidth()/2 - MeasureText("LEVEL 09", 30)/2, 20, 30, GRAY);
  153. DrawText(FormatText("DONE! (Seconds: %03i)", levelTimeSec), GetScreenWidth()/2 - MeasureText("DONE! (Seconds: 000)", 30)/2, GetScreenHeight() - 40, 30, GRAY);
  154. }
  155. else DrawText("LEVEL 09", GetScreenWidth()/2 - MeasureText("LEVEL 09", 30)/2, 20, 30, LIGHTGRAY);
  156. }
  157. // Level09 Screen Unload logic
  158. void UnloadLevel09Screen(void)
  159. {
  160. // TODO: Unload Level09 screen variables here!
  161. }
  162. // Level09 Screen should finish?
  163. int FinishLevel09Screen(void)
  164. {
  165. return finishScreen;
  166. }
  167. static bool CheckColor(Color col1, Color col2)
  168. {
  169. return ((col1.r == col2.r) && (col1.g == col2.g) && (col1.b == col2.b) && (col1.a == col2.a));
  170. }