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.

383 lines
12 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 monst
  40. static Monster lamp;
  41. static Monster chair;
  42. static Monster mirror;
  43. static bool monsterHover = false;
  44. static int monsterCheck = -1; // Identify checking monster
  45. static const char message[256] = "TRICK OR TREAT! WHO IS THE MOST BEAUTIFUL\nSKELETON IN THE WORLD?";
  46. static int msgPosX = 100;
  47. static int msgState = 0; // 0-writting, 1-wait, 2-choose
  48. static int lettersCounter = 0;
  49. static char msgBuffer[256] = { '\0' };
  50. static int msgCounter = 0;
  51. static bool searching = false;
  52. //----------------------------------------------------------------------------------
  53. // Gameplay Screen Functions Definition
  54. //----------------------------------------------------------------------------------
  55. // Gameplay Screen Initialization logic
  56. void InitBathroomScreen(void)
  57. {
  58. ResetPlayer();
  59. // Reset Screen variables
  60. monsterHover = false;
  61. monsterCheck = -1;
  62. msgState = 0;
  63. msgCounter = 0;
  64. lettersCounter = 0;
  65. for (int i = 0; i < 256; i++) msgBuffer[i] = '\0';
  66. framesCounter = 0;
  67. finishScreen = 0;
  68. background = LoadTexture("resources/textures/background_bathroom.png");
  69. // Reset Screen variables
  70. monsterHover = false;
  71. monsterCheck = -1;
  72. msgState = 0;
  73. msgCounter = 0;
  74. lettersCounter = 0;
  75. for (int i = 0; i < 256; i++) msgBuffer[i] = '\0';
  76. // Initialize doors
  77. doorRight.position = (Vector2) { 1070, 135 };
  78. doorRight.facing = 2;
  79. doorRight.locked = true;
  80. doorRight.frameRec =(Rectangle) {((doors.width/3)*doorRight.facing), doors.height/2, doors.width/3, doors.height/2};
  81. doorRight.bound = (Rectangle) { doorRight.position.x, doorRight.position.y, doors.width/3, doors.height/2};
  82. doorRight.selected = false;
  83. // Monster init: lamp
  84. lamp.position = (Vector2){ 35, 334 };
  85. lamp.texture = LoadTexture("resources/textures/monster_lamp_left.png");
  86. lamp.currentFrame = 0;
  87. lamp.framesCounter = 0;
  88. lamp.numFrames = 4;
  89. lamp.bounds = (Rectangle){ lamp.position.x + 20, lamp.position.y + 0, 90, 380};
  90. lamp.frameRec = (Rectangle) { 0, 0, lamp.texture.width/lamp.numFrames, lamp.texture.height };
  91. lamp.selected = false;
  92. lamp.active = false;
  93. lamp.spooky = true;
  94. // Monster init: mirror
  95. mirror.position = (Vector2){ 300, 200 };
  96. mirror.texture = LoadTexture("resources/textures/monster_mirror.png");
  97. mirror.currentFrame = 0;
  98. mirror.framesCounter = 0;
  99. mirror.numFrames = 4;
  100. mirror.bounds = (Rectangle){ mirror.position.x + 40, mirror.position.y + 20, 190, 200 };
  101. mirror.frameRec = (Rectangle) { 0, 0, mirror.texture.width/mirror.numFrames, mirror.texture.height };
  102. mirror.selected = false;
  103. mirror.active = false;
  104. mirror.spooky = false;
  105. // Monster init: chair
  106. chair.position = (Vector2){ 760, 430 };
  107. chair.texture = LoadTexture("resources/textures/monster_chair_right.png");
  108. chair.currentFrame = 0;
  109. chair.framesCounter = 0;
  110. chair.numFrames = 4;
  111. chair.bounds = (Rectangle){ chair.position.x + 30, chair.position.y + 30, 120, 190 };
  112. chair.frameRec = (Rectangle) { 0, 0, chair.texture.width/chair.numFrames, chair.texture.height };
  113. chair.selected = false;
  114. chair.active = false;
  115. chair.spooky = true;
  116. }
  117. // Gameplay Screen Update logic
  118. void UpdateBathroomScreen(void)
  119. {
  120. if (player.key)
  121. {
  122. // Door: right
  123. if ((CheckCollisionPointRec(GetMousePosition(), doorRight.bound)) ||
  124. (CheckCollisionRecs(player.bounds, doorRight.bound))) doorRight.selected = true;
  125. else doorRight.selected = false;
  126. if ((doorRight.selected) && (CheckCollisionRecs(player.bounds, doorRight.bound)))
  127. {
  128. if (((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && CheckCollisionPointRec(GetMousePosition(), doorRight.bound)) || (IsKeyPressed(KEY_SPACE)))
  129. {
  130. if (doorRight.locked)
  131. {
  132. doorRight.frameRec.y = 0;
  133. doorRight.locked = false;
  134. PlaySound(sndDoor);
  135. }
  136. else finishScreen = 1;
  137. }
  138. }
  139. }
  140. if (msgState > 2)
  141. {
  142. UpdatePlayer();
  143. // Monsters logic
  144. UpdateMonster(&lamp);
  145. UpdateMonster(&mirror);
  146. UpdateMonster(&chair);
  147. }
  148. // Check player hover monsters to interact
  149. if (((CheckCollisionRecs(player.bounds, lamp.bounds)) && !lamp.active) ||
  150. ((CheckCollisionRecs(player.bounds, mirror.bounds)) && !mirror.active) ||
  151. ((CheckCollisionRecs(player.bounds, chair.bounds)) && !chair.active)) monsterHover = true;
  152. else monsterHover = false;
  153. // Monters logic: lamp
  154. if ((CheckCollisionRecs(player.bounds, lamp.bounds)) && !lamp.active)
  155. {
  156. lamp.selected = true;
  157. if ((IsKeyPressed(KEY_SPACE)) ||
  158. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), lamp.bounds))))
  159. {
  160. SearchKeyPlayer();
  161. searching = true;
  162. framesCounter = 0;
  163. monsterCheck = 1;
  164. }
  165. }
  166. else lamp.selected = false;
  167. // Monters logic: mirror
  168. if ((CheckCollisionRecs(player.bounds, mirror.bounds)) && !mirror.active)
  169. {
  170. mirror.selected = true;
  171. if ((IsKeyPressed(KEY_SPACE)) ||
  172. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), mirror.bounds))))
  173. {
  174. SearchKeyPlayer();
  175. searching = true;
  176. framesCounter = 0;
  177. monsterCheck = 2;
  178. }
  179. }
  180. else mirror.selected = false;
  181. // Monters logic: chair
  182. if ((CheckCollisionRecs(player.bounds, chair.bounds)) && !chair.active)
  183. {
  184. chair.selected = true;
  185. if ((IsKeyPressed(KEY_SPACE)) ||
  186. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), chair.bounds))))
  187. {
  188. SearchKeyPlayer();
  189. searching = true;
  190. framesCounter = 0;
  191. monsterCheck = 3;
  192. }
  193. }
  194. else chair.selected = false;
  195. if (searching)
  196. {
  197. framesCounter++;
  198. if (framesCounter > 180)
  199. {
  200. if (monsterCheck == 1)
  201. {
  202. if (lamp.spooky)
  203. {
  204. ScarePlayer();
  205. PlaySound(sndScream);
  206. }
  207. else FindKeyPlayer();
  208. lamp.active = true;
  209. lamp.selected = false;
  210. }
  211. else if (monsterCheck == 2)
  212. {
  213. if (mirror.spooky)
  214. {
  215. ScarePlayer();
  216. PlaySound(sndScream);
  217. }
  218. else FindKeyPlayer();
  219. mirror.active = true;
  220. mirror.selected = false;
  221. }
  222. else if (monsterCheck == 3)
  223. {
  224. if (chair.spooky)
  225. {
  226. ScarePlayer();
  227. PlaySound(sndScream);
  228. }
  229. else FindKeyPlayer();
  230. chair.active = true;
  231. chair.selected = false;
  232. }
  233. searching = false;
  234. framesCounter = 0;
  235. }
  236. }
  237. // Text animation
  238. framesCounter++;
  239. if ((framesCounter%2) == 0) lettersCounter++;
  240. if (msgState == 0)
  241. {
  242. if (lettersCounter <= (int)strlen(message)) strncpy(msgBuffer, message, lettersCounter);
  243. else
  244. {
  245. for (int i = 0; i < (int)strlen(msgBuffer); i++) msgBuffer[i] = '\0';
  246. lettersCounter = 0;
  247. msgState = 1;
  248. }
  249. if (IsKeyPressed(KEY_ENTER)) msgState = 1;
  250. }
  251. else if (msgState == 1)
  252. {
  253. msgCounter++;
  254. if ((IsKeyPressed(KEY_ENTER)) || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)))
  255. {
  256. msgState = 2;
  257. msgCounter = 0;
  258. }
  259. }
  260. else if (msgState == 2)
  261. {
  262. msgCounter++;
  263. if (msgCounter > 180) msgState = 3;
  264. }
  265. else msgCounter++;
  266. }
  267. // Gameplay Screen Draw logic
  268. void DrawBathroomScreen(void)
  269. {
  270. DrawTexture(background, 0, 0, WHITE);
  271. // Draw monsters
  272. DrawMonster(lamp, 0);
  273. DrawMonster(mirror, 0);
  274. DrawMonster(chair, 0);
  275. // Draw door
  276. if (doorRight.selected) DrawTextureRec(doors, doorRight.frameRec, doorRight.position, GREEN);
  277. else DrawTextureRec(doors, doorRight.frameRec, doorRight.position, WHITE);
  278. // Draw messsages
  279. if (msgState < 2) DrawRectangle(0, 40, GetScreenWidth(), 200, Fade(LIGHTGRAY, 0.5f));
  280. else if (msgState == 2) DrawRectangle(0, 80, GetScreenWidth(), 100, Fade(LIGHTGRAY, 0.5f));
  281. if (msgState == 0)
  282. {
  283. DrawTextEx(font, msgBuffer, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  284. }
  285. else if (msgState == 1)
  286. {
  287. DrawTextEx(font, message, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  288. if ((msgCounter/30)%2) DrawText("PRESS ENTER or CLICK", GetScreenWidth() - 280, 200, 20, BLACK);
  289. }
  290. else if (msgState == 2)
  291. {
  292. if ((msgCounter/30)%2)
  293. {
  294. DrawTextEx(font, "CHOOSE WISELY!", (Vector2){ 300, 95 }, font.baseSize*2, 2, WHITE);
  295. DrawRectangleRec(lamp.bounds, Fade(RED, 0.6f));
  296. DrawRectangleRec(mirror.bounds, Fade(RED, 0.6f));
  297. DrawRectangleRec(chair.bounds, Fade(RED, 0.6f));
  298. }
  299. }
  300. else
  301. {
  302. if ((monsterHover) && ((msgCounter/30)%2))
  303. {
  304. DrawRectangle(0, 0, GetScreenWidth(), 50, Fade(LIGHTGRAY, 0.5f));
  305. DrawText("PRESS SPACE or CLICK to INTERACT", 420, 15, 20, BLACK);
  306. }
  307. }
  308. DrawPlayer(); // NOTE: Also draws mouse pointer!
  309. }
  310. // Gameplay Screen Unload logic
  311. void UnloadBathroomScreen(void)
  312. {
  313. // TODO: Unload GAMEPLAY screen variables here!
  314. UnloadTexture(background);
  315. UnloadMonster(lamp);
  316. UnloadMonster(chair);
  317. UnloadMonster(mirror);
  318. }
  319. // Gameplay Screen should finish?
  320. int FinishBathroomScreen(void)
  321. {
  322. return finishScreen;
  323. }