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.

156 lines
5.4 KiB

  1. /**********************************************************************************************
  2. *
  3. * raylib - Standard Game template
  4. *
  5. * Level02 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. #include <math.h>
  28. //----------------------------------------------------------------------------------
  29. // Global Variables Definition (local to this module)
  30. //----------------------------------------------------------------------------------
  31. // Level02 screen global variables
  32. static int framesCounter;
  33. static int finishScreen;
  34. static Vector2 bouncingBallPos;
  35. static float bouncingBallRadius = 40;
  36. static Vector2 bouncingBallSpeed;
  37. static Vector2 holeCirclePos;
  38. static float holeCircleRadius = 50;
  39. static bool ballOnHole = false;
  40. static int levelTimeSec = 0;
  41. static bool levelFinished = false;
  42. //----------------------------------------------------------------------------------
  43. // Level02 Screen Functions Definition
  44. //----------------------------------------------------------------------------------
  45. float Vector2Distance(Vector2 v1, Vector2 v2);
  46. // Level02 Screen Initialization logic
  47. void InitLevel02Screen(void)
  48. {
  49. // TODO: Initialize Level02 screen variables here!
  50. framesCounter = 0;
  51. finishScreen = 0;
  52. bouncingBallPos = (Vector2){ 120, 80 };
  53. bouncingBallSpeed = (Vector2){ 6, 8 };
  54. holeCirclePos = (Vector2){ GetScreenWidth()/2, GetScreenHeight()/2 };
  55. }
  56. // Level02 Screen Update logic
  57. void UpdateLevel02Screen(void)
  58. {
  59. // Update Level02 screen
  60. framesCounter++;
  61. if (!ballOnHole)
  62. {
  63. bouncingBallPos.x += bouncingBallSpeed.x;
  64. bouncingBallPos.y += bouncingBallSpeed.y;
  65. if (((bouncingBallPos.x - bouncingBallRadius) <= 0) || ((bouncingBallPos.x + bouncingBallRadius) >= GetScreenWidth())) bouncingBallSpeed.x *= -1;
  66. if (((bouncingBallPos.y - bouncingBallRadius) <= 0) || ((bouncingBallPos.y + bouncingBallRadius) >= GetScreenHeight())) bouncingBallSpeed.y *= -1;
  67. Vector2 mousePos = GetMousePosition();
  68. if (CheckCollisionPointCircle(mousePos, bouncingBallPos, 120))
  69. {
  70. bouncingBallPos.x = GetRandomValue(80, 1200);
  71. bouncingBallPos.y = GetRandomValue(80, 650);
  72. }
  73. if (CheckCollisionPointCircle(mousePos, holeCirclePos, 120))
  74. {
  75. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  76. {
  77. holeCirclePos = mousePos;
  78. if ((holeCirclePos.x - holeCircleRadius) <= 0) holeCirclePos.x = holeCircleRadius;
  79. else if ((holeCirclePos.x + holeCircleRadius) >= GetScreenWidth()) holeCirclePos.x = GetScreenWidth() - holeCircleRadius;
  80. if ((holeCirclePos.y - holeCircleRadius) <= 0) holeCirclePos.y = holeCircleRadius;
  81. else if ((holeCirclePos.y + holeCircleRadius) >= GetScreenHeight()) holeCirclePos.y = GetScreenHeight() - holeCircleRadius;
  82. }
  83. }
  84. if (Vector2Distance(bouncingBallPos, holeCirclePos) < 20)
  85. {
  86. ballOnHole = true;
  87. PlaySound(levelWin);
  88. }
  89. }
  90. if (ballOnHole && !levelFinished)
  91. {
  92. levelTimeSec = framesCounter/60;
  93. levelFinished = true;
  94. framesCounter = 0;
  95. }
  96. if (levelFinished)
  97. {
  98. framesCounter++;
  99. if ((framesCounter > 90) && (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) finishScreen = true;
  100. }
  101. }
  102. // Level02 Screen Draw logic
  103. void DrawLevel02Screen(void)
  104. {
  105. // Draw Level02 screen
  106. DrawCircleV(holeCirclePos, holeCircleRadius, LIGHTGRAY);
  107. DrawCircleV(bouncingBallPos, bouncingBallRadius, DARKGRAY);
  108. DrawCircleLines(bouncingBallPos.x, bouncingBallPos.y, 120, Fade(LIGHTGRAY, 0.8f));
  109. if (levelFinished)
  110. {
  111. DrawRectangleBordersRec((Rectangle){0, 0, GetScreenWidth(), GetScreenHeight()}, 0, 0, 60, Fade(LIGHTGRAY, 0.6f));
  112. DrawText("LEVEL 02", GetScreenWidth()/2 - MeasureText("LEVEL 02", 30)/2, 20, 30, GRAY);
  113. DrawText(FormatText("DONE! (Seconds: %03i)", levelTimeSec), GetScreenWidth()/2 - MeasureText("DONE! (Seconds: 000)", 30)/2, GetScreenHeight() - 40, 30, GRAY);
  114. }
  115. else DrawText("LEVEL 02", GetScreenWidth()/2 - MeasureText("LEVEL 02", 30)/2, 20, 30, LIGHTGRAY);
  116. }
  117. // Level02 Screen Unload logic
  118. void UnloadLevel02Screen(void)
  119. {
  120. // TODO: Unload Level02 screen variables here!
  121. }
  122. // Level02 Screen should finish?
  123. int FinishLevel02Screen(void)
  124. {
  125. return finishScreen;
  126. }