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.

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