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.

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