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.

444 lines
14 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. // Decalre monsters
  40. static Monster lamp;
  41. static Monster chair;
  42. static Monster picture;
  43. static Monster arc;
  44. static bool monsterHover = false;
  45. static int monsterCheck = -1; // Identify checking monster
  46. static const char message[256] = "HAS LEGS BUT CAN NOT WALK...\nSEARCH FOR IT TO OPEN THE DOOR!";
  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 InitAisle02Screen(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_aisle02.png");
  71. scroll = player.position.x - 200;
  72. // Initialize doors
  73. doorLeft.position = (Vector2) { -10, 136 };
  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. // Monster init: lamp
  80. lamp.position = (Vector2){ 1520, 300 };
  81. lamp.texture = LoadTexture("resources/textures/monster_lamp_right.png");
  82. lamp.currentFrame = 0;
  83. lamp.framesCounter = 0;
  84. lamp.numFrames = 4;
  85. lamp.bounds = (Rectangle){ lamp.position.x + 200, lamp.position.y, 90, 380 };
  86. lamp.frameRec = (Rectangle) { 0, 0, lamp.texture.width/lamp.numFrames, lamp.texture.height };
  87. lamp.selected = false;
  88. lamp.active = false;
  89. lamp.spooky = true;
  90. // Monster init: chair
  91. chair.position = (Vector2){ 1400, 404 };
  92. chair.texture = LoadTexture("resources/textures/monster_chair_right.png");
  93. chair.currentFrame = 0;
  94. chair.framesCounter = 0;
  95. chair.numFrames = 4;
  96. chair.bounds = (Rectangle){ chair.position.x + 50, chair.position.y + 30, 120, 190 };
  97. chair.frameRec = (Rectangle) { 0, 0, chair.texture.width/chair.numFrames, chair.texture.height };
  98. chair.selected = false;
  99. chair.active = false;
  100. chair.spooky = false;
  101. // Monster init: picture
  102. picture.position = (Vector2){ 837, 162 };
  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, 264 };
  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 = true;
  112. // Monster init: arc
  113. arc.position = (Vector2){ 388, 423 };
  114. arc.texture = LoadTexture("resources/textures/monster_arc.png");
  115. arc.currentFrame = 0;
  116. arc.framesCounter = 0;
  117. arc.numFrames = 4;
  118. arc.bounds = (Rectangle){ arc.position.x + 44, arc.position.y + 70, 220, 120 };
  119. arc.frameRec = (Rectangle) { 0, 0, arc.texture.width/arc.numFrames, arc.texture.height };
  120. arc.selected = false;
  121. arc.active = false;
  122. arc.spooky = true;
  123. }
  124. // Gameplay Screen Update logic
  125. void UpdateAisle02Screen(void)
  126. {
  127. // Update doors bounds
  128. doorLeft.bound.x = doorLeft.position.x - scroll;
  129. if (player.key)
  130. {
  131. // Door: left
  132. if ((CheckCollisionPointRec(GetMousePosition(), doorLeft.bound)) ||
  133. (CheckCollisionRecs(player.bounds, doorLeft.bound))) doorLeft.selected = true;
  134. else doorLeft.selected = false;
  135. if ((doorLeft.selected) && (CheckCollisionRecs(player.bounds, doorLeft.bound)))
  136. {
  137. if (((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && CheckCollisionPointRec(GetMousePosition(), doorLeft.bound)) || (IsKeyPressed(KEY_SPACE)))
  138. {
  139. if (doorLeft.locked)
  140. {
  141. doorLeft.frameRec.y = 0;
  142. doorLeft.locked = false;
  143. PlaySound(sndDoor);
  144. }
  145. else finishScreen = 1;
  146. }
  147. }
  148. }
  149. if (msgState > 2)
  150. {
  151. UpdatePlayer();
  152. // Monsters logic
  153. UpdateMonster(&lamp);
  154. UpdateMonster(&chair);
  155. UpdateMonster(&picture);
  156. UpdateMonster(&arc);
  157. }
  158. // Update monster bounds
  159. lamp.bounds.x = lamp.position.x + 200 - scroll;
  160. chair.bounds.x = chair.position.x + 50 - scroll;
  161. picture.bounds.x = picture.position.x + 44 - scroll;
  162. arc.bounds.x = arc.position.x + 44 - scroll;
  163. // Check player hover monsters to interact
  164. if (((CheckCollisionRecs(player.bounds, lamp.bounds)) && !lamp.active) ||
  165. ((CheckCollisionRecs(player.bounds, chair.bounds)) && !chair.active) ||
  166. ((CheckCollisionRecs(player.bounds, picture.bounds)) && !picture.active) ||
  167. ((CheckCollisionRecs(player.bounds, arc.bounds)) && !arc.active)) monsterHover = true;
  168. else monsterHover = false;
  169. // Monters logic: lamp
  170. if ((CheckCollisionRecs(player.bounds, lamp.bounds)) && !lamp.active)
  171. {
  172. lamp.selected = true;
  173. if ((IsKeyPressed(KEY_SPACE)) ||
  174. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), lamp.bounds))))
  175. {
  176. SearchKeyPlayer();
  177. searching = true;
  178. framesCounter = 0;
  179. monsterCheck = 1;
  180. }
  181. }
  182. else lamp.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: picture
  198. if ((CheckCollisionRecs(player.bounds, picture.bounds)) && !picture.active)
  199. {
  200. picture.selected = true;
  201. if ((IsKeyPressed(KEY_SPACE)) ||
  202. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), picture.bounds))))
  203. {
  204. SearchKeyPlayer();
  205. searching = true;
  206. framesCounter = 0;
  207. monsterCheck = 3;
  208. }
  209. }
  210. else picture.selected = false;
  211. // Monters logic: arc
  212. if ((CheckCollisionRecs(player.bounds, arc.bounds)) && !arc.active)
  213. {
  214. arc.selected = true;
  215. if ((IsKeyPressed(KEY_SPACE)) ||
  216. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), arc.bounds))))
  217. {
  218. SearchKeyPlayer();
  219. searching = true;
  220. framesCounter = 0;
  221. monsterCheck = 4;
  222. }
  223. }
  224. else arc.selected = false;
  225. if (searching)
  226. {
  227. framesCounter++;
  228. if (framesCounter > 180)
  229. {
  230. if (monsterCheck == 1)
  231. {
  232. if (lamp.spooky)
  233. {
  234. ScarePlayer();
  235. PlaySound(sndScream);
  236. }
  237. else FindKeyPlayer();
  238. lamp.active = true;
  239. lamp.selected = false;
  240. }
  241. else if (monsterCheck == 2)
  242. {
  243. if (chair.spooky)
  244. {
  245. ScarePlayer();
  246. PlaySound(sndScream);
  247. }
  248. else FindKeyPlayer();
  249. chair.active = true;
  250. chair.selected = false;
  251. }
  252. else if (monsterCheck == 3)
  253. {
  254. if (picture.spooky)
  255. {
  256. ScarePlayer();
  257. PlaySound(sndScream);
  258. }
  259. else FindKeyPlayer();
  260. picture.active = true;
  261. picture.selected = false;
  262. }
  263. else if (monsterCheck == 4)
  264. {
  265. if (arc.spooky)
  266. {
  267. ScarePlayer();
  268. PlaySound(sndScream);
  269. }
  270. else FindKeyPlayer();
  271. arc.active = true;
  272. arc.selected = false;
  273. }
  274. searching = false;
  275. framesCounter = 0;
  276. }
  277. }
  278. // Text animation
  279. framesCounter++;
  280. if ((framesCounter%2) == 0) lettersCounter++;
  281. if (msgState == 0)
  282. {
  283. if (lettersCounter <= (int)strlen(message)) strncpy(msgBuffer, message, lettersCounter);
  284. else
  285. {
  286. for (int i = 0; i < (int)strlen(msgBuffer); i++) msgBuffer[i] = '\0';
  287. lettersCounter = 0;
  288. msgState = 1;
  289. }
  290. if (IsKeyPressed(KEY_ENTER)) msgState = 1;
  291. }
  292. else if (msgState == 1)
  293. {
  294. msgCounter++;
  295. if ((IsKeyPressed(KEY_ENTER)) || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)))
  296. {
  297. msgState = 2;
  298. msgCounter = 0;
  299. }
  300. }
  301. else if (msgState == 2)
  302. {
  303. msgCounter++;
  304. if (msgCounter > 180) msgState = 3;
  305. }
  306. else msgCounter++;
  307. if (player.position.x > 200)
  308. {
  309. scroll = player.position.x - 200;
  310. if (scroll > 620) scroll = 620;
  311. }
  312. }
  313. // Gameplay Screen Draw logic
  314. void DrawAisle02Screen(void)
  315. {
  316. DrawTexture(background, -scroll, 0, WHITE);
  317. // Draw monsters
  318. DrawMonster(lamp, scroll);
  319. DrawMonster(arc, scroll);
  320. DrawMonster(picture, scroll);
  321. DrawMonster(chair, scroll);
  322. // Draw door
  323. Vector2 doorScrollPos = { doorLeft.position.x - scroll, doorLeft.position.y };
  324. if (doorLeft.selected) DrawTextureRec(doors, doorLeft.frameRec, doorScrollPos, GREEN);
  325. else DrawTextureRec(doors, doorLeft.frameRec, doorScrollPos, WHITE);
  326. // Draw messsages
  327. if (msgState < 2) DrawRectangle(0, 40, GetScreenWidth(), 200, Fade(LIGHTGRAY, 0.5f));
  328. else if (msgState == 2) DrawRectangle(0, 80, GetScreenWidth(), 100, Fade(LIGHTGRAY, 0.5f));
  329. if (msgState == 0)
  330. {
  331. DrawTextEx(font, msgBuffer, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  332. }
  333. else if (msgState == 1)
  334. {
  335. DrawTextEx(font, message, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  336. if ((msgCounter/30)%2) DrawText("PRESS ENTER or CLICK", GetScreenWidth() - 280, 200, 20, BLACK);
  337. }
  338. else if (msgState == 2)
  339. {
  340. if ((msgCounter/30)%2)
  341. {
  342. DrawTextEx(font, "CHOOSE WISELY!", (Vector2){ 300, 95 }, font.baseSize*2, 2, WHITE);
  343. DrawRectangleRec(lamp.bounds, Fade(RED, 0.6f));
  344. DrawRectangleRec(arc.bounds, Fade(RED, 0.6f));
  345. DrawRectangleRec(chair.bounds, Fade(RED, 0.6f));
  346. DrawRectangleRec(picture.bounds, Fade(RED, 0.6f));
  347. }
  348. }
  349. else
  350. {
  351. if ((monsterHover) && ((msgCounter/30)%2))
  352. {
  353. DrawRectangle(0, 0, GetScreenWidth(), 50, Fade(LIGHTGRAY, 0.5f));
  354. DrawText("PRESS SPACE or CLICK to INTERACT", 420, 15, 20, BLACK);
  355. }
  356. }
  357. DrawPlayer(); // NOTE: Also draws mouse pointer!
  358. }
  359. // Gameplay Screen Unload logic
  360. void UnloadAisle02Screen(void)
  361. {
  362. // TODO: Unload GAMEPLAY screen variables here!
  363. UnloadTexture(background);
  364. UnloadMonster(lamp);
  365. UnloadMonster(chair);
  366. UnloadMonster(picture);
  367. UnloadMonster(arc);
  368. }
  369. // Gameplay Screen should finish?
  370. int FinishAisle02Screen(void)
  371. {
  372. return finishScreen;
  373. }