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.

332 lines
9.9 KiB

  1. /**********************************************************************************************
  2. *
  3. * raylib - Advance Game template
  4. *
  5. * Gameplay 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 "../player.h"
  28. #include "../monster.h"
  29. #include <string.h>
  30. //----------------------------------------------------------------------------------
  31. // Global Variables Definition (local to this module)
  32. //----------------------------------------------------------------------------------
  33. // Gameplay screen global variables
  34. static int framesCounter;
  35. static int finishScreen;
  36. static Texture2D background;
  37. // Declare doors
  38. static Door doorRight;
  39. // Decalre monsters
  40. static Monster lamp;
  41. static Monster arc;
  42. static bool monsterHover;
  43. static int monsterCheck; // Identify checking monster
  44. static const char message[256] = "YOUR PARENTS ARE GONE! TIME TO ESCAPE!\nTHE DOOR IS LOCKED... TURN ON THE LIGHTS! ;)";
  45. static int msgPosX = 100;
  46. static int msgState = 0; // 0-writting, 1-wait, 2-choose
  47. static int lettersCounter = 0;
  48. static char msgBuffer[256] = { '\0' };
  49. static int msgCounter = 0;
  50. static bool searching = false;
  51. //----------------------------------------------------------------------------------
  52. // Gameplay Screen Functions Definition
  53. //----------------------------------------------------------------------------------
  54. // Gameplay Screen Initialization logic
  55. void InitAtticScreen(void)
  56. {
  57. ResetPlayer();
  58. // Reset Screen variables
  59. monsterHover = false;
  60. monsterCheck = -1;
  61. msgState = 0;
  62. msgCounter = 0;
  63. lettersCounter = 0;
  64. for (int i = 0; i < 256; i++) msgBuffer[i] = '\0';
  65. framesCounter = 0;
  66. finishScreen = 0;
  67. background = LoadTexture("resources/textures/background_attic.png");
  68. // Initialize doors
  69. doorRight.position = (Vector2) { 1074, 140 };
  70. doorRight.facing = 2;
  71. doorRight.locked = true;
  72. doorRight.frameRec =(Rectangle) {((doors.width/3)*doorRight.facing), doors.height/2, doors.width/3, doors.height/2};
  73. doorRight.bound = (Rectangle) { doorRight.position.x, doorRight.position.y, doors.width/3, doors.height/2};
  74. doorRight.selected = false;
  75. // Monster init: lamp
  76. lamp.position = (Vector2){ 50, 316 };
  77. lamp.texture = LoadTexture("resources/textures/monster_lamp_left.png");
  78. lamp.currentFrame = 0;
  79. lamp.framesCounter = 0;
  80. lamp.numFrames = 4;
  81. lamp.bounds = (Rectangle){ lamp.position.x + 20, lamp.position.y, 90, 380 };
  82. lamp.frameRec = (Rectangle) { 0, 0, lamp.texture.width/lamp.numFrames, lamp.texture.height };
  83. lamp.selected = false;
  84. lamp.active = false;
  85. lamp.spooky = false;
  86. // Monster init: arc
  87. arc.position = (Vector2){ 760, 430 };
  88. arc.texture = LoadTexture("resources/textures/monster_arc.png");
  89. arc.currentFrame = 0;
  90. arc.framesCounter = 0;
  91. arc.numFrames = 4;
  92. arc.bounds = (Rectangle){ arc.position.x + 44, arc.position.y + 70, 220, 120 };
  93. arc.frameRec = (Rectangle) { 0, 0, arc.texture.width/arc.numFrames, arc.texture.height };
  94. arc.selected = false;
  95. arc.active = false;
  96. arc.spooky = true;
  97. }
  98. // Gameplay Screen Update logic
  99. void UpdateAtticScreen(void)
  100. {
  101. if (player.key)
  102. {
  103. // Door: right
  104. if ((CheckCollisionPointRec(GetMousePosition(), doorRight.bound)) ||
  105. (CheckCollisionRecs(player.bounds, doorRight.bound))) doorRight.selected = true;
  106. else doorRight.selected = false;
  107. if ((doorRight.selected) && (CheckCollisionRecs(player.bounds, doorRight.bound)))
  108. {
  109. if (((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && CheckCollisionPointRec(GetMousePosition(), doorRight.bound)) || (IsKeyPressed(KEY_SPACE)))
  110. {
  111. if (doorRight.locked)
  112. {
  113. doorRight.frameRec.y = 0;
  114. doorRight.locked = false;
  115. PlaySound(sndDoor);
  116. }
  117. else finishScreen = 1;
  118. }
  119. }
  120. }
  121. if (msgState > 2)
  122. {
  123. UpdatePlayer();
  124. // Monsters logic
  125. UpdateMonster(&lamp);
  126. UpdateMonster(&arc);
  127. }
  128. // Check player hover monsters to interact
  129. if (((CheckCollisionRecs(player.bounds, lamp.bounds)) && !lamp.active) ||
  130. ((CheckCollisionRecs(player.bounds, arc.bounds)) && !arc.active)) monsterHover = true;
  131. else monsterHover = false;
  132. // Monters logic: lamp
  133. if ((CheckCollisionRecs(player.bounds, lamp.bounds)) && !lamp.active)
  134. {
  135. lamp.selected = true;
  136. if ((IsKeyPressed(KEY_SPACE)) ||
  137. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), lamp.bounds))))
  138. {
  139. SearchKeyPlayer();
  140. searching = true;
  141. framesCounter = 0;
  142. monsterCheck = 1;
  143. }
  144. }
  145. else lamp.selected = false;
  146. // Monters logic: arc
  147. if ((CheckCollisionRecs(player.bounds, arc.bounds)) && !arc.active)
  148. {
  149. arc.selected = true;
  150. if ((IsKeyPressed(KEY_SPACE)) ||
  151. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), arc.bounds))))
  152. {
  153. SearchKeyPlayer();
  154. searching = true;
  155. framesCounter = 0;
  156. monsterCheck = 2;
  157. }
  158. }
  159. else arc.selected = false;
  160. if (searching)
  161. {
  162. framesCounter++;
  163. if (framesCounter > 180)
  164. {
  165. if (monsterCheck == 1)
  166. {
  167. if (lamp.spooky)
  168. {
  169. ScarePlayer();
  170. PlaySound(sndScream);
  171. }
  172. else FindKeyPlayer();
  173. lamp.active = true;
  174. lamp.selected = false;
  175. }
  176. else if (monsterCheck == 2)
  177. {
  178. if (arc.spooky)
  179. {
  180. ScarePlayer();
  181. PlaySound(sndScream);
  182. }
  183. else FindKeyPlayer();
  184. arc.active = true;
  185. arc.selected = false;
  186. }
  187. searching = false;
  188. framesCounter = 0;
  189. }
  190. }
  191. // Text animation
  192. framesCounter++;
  193. if ((framesCounter%2) == 0) lettersCounter++;
  194. if (msgState == 0)
  195. {
  196. if (lettersCounter <= (int)strlen(message)) strncpy(msgBuffer, message, lettersCounter);
  197. else
  198. {
  199. for (int i = 0; i < (int)strlen(msgBuffer); i++) msgBuffer[i] = '\0';
  200. lettersCounter = 0;
  201. msgState = 1;
  202. }
  203. if (IsKeyPressed(KEY_ENTER)) msgState = 1;
  204. }
  205. else if (msgState == 1)
  206. {
  207. msgCounter++;
  208. if ((IsKeyPressed(KEY_ENTER)) || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)))
  209. {
  210. msgState = 2;
  211. msgCounter = 0;
  212. }
  213. }
  214. else if (msgState == 2)
  215. {
  216. msgCounter++;
  217. if (msgCounter > 180) msgState = 3;
  218. }
  219. else msgCounter++;
  220. if (IsKeyPressed('M'))
  221. {
  222. finishScreen = 1;
  223. }
  224. }
  225. // Gameplay Screen Draw logic
  226. void DrawAtticScreen(void)
  227. {
  228. DrawTexture(background, 0, 0, WHITE);
  229. // Draw monsters
  230. DrawMonster(lamp, 0);
  231. DrawMonster(arc, 0);
  232. // Draw door
  233. if (doorRight.selected) DrawTextureRec(doors, doorRight.frameRec, doorRight.position, GREEN);
  234. else DrawTextureRec(doors, doorRight.frameRec, doorRight.position, WHITE);
  235. // Draw messsages
  236. if (msgState < 2) DrawRectangle(0, 40, GetScreenWidth(), 200, Fade(LIGHTGRAY, 0.5f));
  237. else if (msgState == 2) DrawRectangle(0, 80, GetScreenWidth(), 100, Fade(LIGHTGRAY, 0.5f));
  238. if (msgState == 0)
  239. {
  240. DrawTextEx(font, msgBuffer, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  241. }
  242. else if (msgState == 1)
  243. {
  244. DrawTextEx(font, message, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  245. if ((msgCounter/30)%2) DrawText("PRESS ENTER or CLICK", GetScreenWidth() - 280, 200, 20, BLACK);
  246. }
  247. else if (msgState == 2)
  248. {
  249. if ((msgCounter/30)%2)
  250. {
  251. DrawTextEx(font, "CHOOSE WISELY!", (Vector2){ 300, 95 }, font.baseSize*2, 2, WHITE);
  252. DrawRectangleRec(lamp.bounds, Fade(RED, 0.6f));
  253. DrawRectangleRec(arc.bounds, Fade(RED, 0.6f));
  254. }
  255. }
  256. else
  257. {
  258. if ((monsterHover) && ((msgCounter/30)%2))
  259. {
  260. DrawRectangle(0, 0, GetScreenWidth(), 50, Fade(LIGHTGRAY, 0.5f));
  261. DrawText("PRESS SPACE or CLICK to INTERACT", 420, 15, 20, BLACK);
  262. }
  263. }
  264. DrawPlayer(); // NOTE: Also draws mouse pointer!
  265. }
  266. // Gameplay Screen Unload logic
  267. void UnloadAtticScreen(void)
  268. {
  269. // TODO: Unload GAMEPLAY screen variables here!
  270. UnloadTexture(background);
  271. UnloadMonster(lamp);
  272. UnloadMonster(arc);
  273. }
  274. // Gameplay Screen should finish?
  275. int FinishAtticScreen(void)
  276. {
  277. return finishScreen;
  278. }