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.

155 lines
5.4 KiB

  1. /**********************************************************************************************
  2. *
  3. * raylib - Standard Game template
  4. *
  5. * Level06 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. //----------------------------------------------------------------------------------
  28. // Global Variables Definition (local to this module)
  29. //----------------------------------------------------------------------------------
  30. // Level06 screen global variables
  31. static int framesCounter;
  32. static int finishScreen;
  33. static Rectangle centerRec;
  34. static Rectangle movingRecs[4];
  35. static int speedRecs[4];
  36. static bool stoppedRec[4];
  37. static int mouseOverNum = -1;
  38. static bool done = false;
  39. static int levelTimeSec = 0;
  40. static bool levelFinished = false;
  41. //----------------------------------------------------------------------------------
  42. // Level06 Screen Functions Definition
  43. //----------------------------------------------------------------------------------
  44. // Level06 Screen Initialization logic
  45. void InitLevel06Screen(void)
  46. {
  47. // Initialize Level06 screen variables here!
  48. framesCounter = 0;
  49. finishScreen = 0;
  50. centerRec = (Rectangle){ GetScreenWidth()/2 - 100, 0, 200, GetScreenHeight() };
  51. for (int i = 0; i < 4; i++)
  52. {
  53. movingRecs[i] = (Rectangle){ GetRandomValue(0, 5)*150, (i*150) + 90, 100, 100 };
  54. stoppedRec[i] = false;
  55. speedRecs[i] = GetRandomValue(4, 8);
  56. }
  57. }
  58. // Level06 Screen Update logic
  59. void UpdateLevel06Screen(void)
  60. {
  61. // Update Level06 screen variables here!
  62. framesCounter++;
  63. if (!done)
  64. {
  65. for (int i = 0; i < 4; i++)
  66. {
  67. if (!stoppedRec[i]) movingRecs[i].x += speedRecs[i];
  68. if (movingRecs[i].x >= GetScreenWidth()) movingRecs[i].x = -movingRecs[i].width;
  69. if (CheckCollisionPointRec(GetMousePosition(), movingRecs[i]))
  70. {
  71. mouseOverNum = i;
  72. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  73. {
  74. if (i == 0) stoppedRec[3] = !stoppedRec[3];
  75. else if (i == 1) stoppedRec[2] = !stoppedRec[2];
  76. else if (i == 2) stoppedRec[0] = !stoppedRec[0];
  77. else if (i == 3) stoppedRec[1] = !stoppedRec[1];
  78. }
  79. }
  80. }
  81. // Check if all boxes are aligned
  82. if (((movingRecs[0].x > centerRec.x) && ((movingRecs[0].x + movingRecs[0].width) < (centerRec.x + centerRec.width))) &&
  83. ((movingRecs[1].x > centerRec.x) && ((movingRecs[1].x + movingRecs[1].width) < (centerRec.x + centerRec.width))) &&
  84. ((movingRecs[2].x > centerRec.x) && ((movingRecs[2].x + movingRecs[2].width) < (centerRec.x + centerRec.width))) &&
  85. ((movingRecs[3].x > centerRec.x) && ((movingRecs[3].x + movingRecs[3].width) < (centerRec.x + centerRec.width))))
  86. {
  87. done = true;
  88. PlaySound(levelWin);
  89. }
  90. }
  91. if (done && !levelFinished)
  92. {
  93. levelTimeSec = framesCounter/60;
  94. levelFinished = true;
  95. framesCounter = 0;
  96. }
  97. if (levelFinished)
  98. {
  99. framesCounter++;
  100. if ((framesCounter > 90) && (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) finishScreen = true;
  101. }
  102. }
  103. // Level06 Screen Draw logic
  104. void DrawLevel06Screen(void)
  105. {
  106. // Draw Level06 screen
  107. DrawRectangleRec(centerRec, LIGHTGRAY);
  108. for (int i = 0; i < 4; i++)
  109. {
  110. DrawRectangleRec(movingRecs[i], GRAY);
  111. }
  112. if (!done && (mouseOverNum >= 0)) DrawRectangleLines(movingRecs[mouseOverNum].x - 5, movingRecs[mouseOverNum].y - 5, movingRecs[mouseOverNum].width + 10, movingRecs[mouseOverNum].height + 10, Fade(LIGHTGRAY, 0.8f));
  113. if (levelFinished)
  114. {
  115. DrawRectangleBordersRec((Rectangle){0, 0, GetScreenWidth(), GetScreenHeight()}, 0, 0, 60, Fade(LIGHTGRAY, 0.6f));
  116. DrawText("LEVEL 06", GetScreenWidth()/2 - MeasureText("LEVEL 06", 30)/2, 20, 30, GRAY);
  117. DrawText(FormatText("DONE! (Seconds: %03i)", levelTimeSec), GetScreenWidth()/2 - MeasureText("DONE! (Seconds: 000)", 30)/2, GetScreenHeight() - 40, 30, GRAY);
  118. }
  119. else DrawText("LEVEL 06", GetScreenWidth()/2 - MeasureText("LEVEL 06", 30)/2, 20, 30, LIGHTGRAY);
  120. }
  121. // Level06 Screen Unload logic
  122. void UnloadLevel06Screen(void)
  123. {
  124. // TODO: Unload Level06 screen variables here!
  125. }
  126. // Level06 Screen should finish?
  127. int FinishLevel06Screen(void)
  128. {
  129. return finishScreen;
  130. }