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.

404 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 blazon01;
  42. static Monster blazon02;
  43. static Monster blazon03;
  44. static bool monsterHover = false;
  45. static int monsterCheck = -1; // Identify checking monster
  46. static const char message[256] = "NO MORE TIPS...\nFOLLOW YOUR INSTINCT!";
  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 InitArmoryScreen(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_armory.png");
  70. // Initialize doors
  71. doorLeft.position = (Vector2) { -50, 145 };
  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) { 1074, 140 };
  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: blazon01
  84. blazon01.position = (Vector2){ 300, 260 };
  85. blazon01.texture = LoadTexture("resources/textures/monster_blazon01.png");
  86. blazon01.currentFrame = 0;
  87. blazon01.framesCounter = 0;
  88. blazon01.numFrames = 4;
  89. blazon01.bounds = (Rectangle){ blazon01.position.x, blazon01.position.y + 20, 160, 230 };
  90. blazon01.frameRec = (Rectangle) { 0, 0, blazon01.texture.width/blazon01.numFrames, blazon01.texture.height };
  91. blazon01.selected = false;
  92. blazon01.active = false;
  93. blazon01.spooky = true;
  94. // Monster init: blazon02
  95. blazon02.position = (Vector2){ 550, 260 };
  96. blazon02.texture = LoadTexture("resources/textures/monster_blazon02.png");
  97. blazon02.currentFrame = 0;
  98. blazon02.framesCounter = 0;
  99. blazon02.numFrames = 4;
  100. blazon02.bounds = (Rectangle){ blazon02.position.x, blazon02.position.y + 20, 160, 230 };
  101. blazon02.frameRec = (Rectangle) { 0, 0, blazon02.texture.width/blazon02.numFrames, blazon02.texture.height };
  102. blazon02.selected = false;
  103. blazon02.active = false;
  104. blazon02.spooky = true;
  105. // Monster init: blazon03
  106. blazon03.position = (Vector2){ 800, 260 };
  107. blazon03.texture = LoadTexture("resources/textures/monster_blazon03.png");
  108. blazon03.currentFrame = 0;
  109. blazon03.framesCounter = 0;
  110. blazon03.numFrames = 4;
  111. blazon03.bounds = (Rectangle){ blazon03.position.x, blazon03.position.y + 20, 160, 230 };
  112. blazon03.frameRec = (Rectangle) { 0, 0, blazon03.texture.width/blazon03.numFrames, blazon03.texture.height };
  113. blazon03.selected = false;
  114. blazon03.active = false;
  115. blazon03.spooky = false;
  116. }
  117. // Gameplay Screen Update logic
  118. void UpdateArmoryScreen(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(&blazon01);
  162. UpdateMonster(&blazon02);
  163. UpdateMonster(&blazon03);
  164. }
  165. // Check player hover monsters to interact
  166. if (((CheckCollisionRecs(player.bounds, blazon01.bounds)) && !blazon01.active) ||
  167. ((CheckCollisionRecs(player.bounds, blazon02.bounds)) && !blazon02.active) ||
  168. ((CheckCollisionRecs(player.bounds, blazon03.bounds)) && !blazon03.active)) monsterHover = true;
  169. else monsterHover = false;
  170. // Monters logic: blazon01
  171. if ((CheckCollisionRecs(player.bounds, blazon01.bounds)) && !blazon01.active)
  172. {
  173. blazon01.selected = true;
  174. if ((IsKeyPressed(KEY_SPACE)) ||
  175. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), blazon01.bounds))))
  176. {
  177. SearchKeyPlayer();
  178. searching = true;
  179. framesCounter = 0;
  180. monsterCheck = 1;
  181. }
  182. }
  183. else blazon01.selected = false;
  184. // Monters logic: blazon02
  185. if ((CheckCollisionRecs(player.bounds, blazon02.bounds)) && !blazon02.active)
  186. {
  187. blazon02.selected = true;
  188. if ((IsKeyPressed(KEY_SPACE)) ||
  189. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), blazon02.bounds))))
  190. {
  191. SearchKeyPlayer();
  192. searching = true;
  193. framesCounter = 0;
  194. monsterCheck = 2;
  195. }
  196. }
  197. else blazon02.selected = false;
  198. // Monters logic: blazon03
  199. if ((CheckCollisionRecs(player.bounds, blazon03.bounds)) && !blazon03.active)
  200. {
  201. blazon03.selected = true;
  202. if ((IsKeyPressed(KEY_SPACE)) ||
  203. ((IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) && (CheckCollisionPointRec(GetMousePosition(), blazon03.bounds))))
  204. {
  205. SearchKeyPlayer();
  206. searching = true;
  207. framesCounter = 0;
  208. monsterCheck = 3;
  209. }
  210. }
  211. else blazon03.selected = false;
  212. if (searching)
  213. {
  214. framesCounter++;
  215. if (framesCounter > 180)
  216. {
  217. if (monsterCheck == 1)
  218. {
  219. if (blazon01.spooky)
  220. {
  221. ScarePlayer();
  222. PlaySound(sndScream);
  223. }
  224. else FindKeyPlayer();
  225. blazon01.active = true;
  226. blazon01.selected = false;
  227. }
  228. else if (monsterCheck == 2)
  229. {
  230. if (blazon02.spooky)
  231. {
  232. ScarePlayer();
  233. PlaySound(sndScream);
  234. }
  235. else FindKeyPlayer();
  236. blazon02.active = true;
  237. blazon02.selected = false;
  238. }
  239. else if (monsterCheck == 3)
  240. {
  241. if (blazon03.spooky)
  242. {
  243. ScarePlayer();
  244. PlaySound(sndScream);
  245. }
  246. else FindKeyPlayer();
  247. blazon03.active = true;
  248. blazon03.selected = false;
  249. }
  250. searching = false;
  251. framesCounter = 0;
  252. }
  253. }
  254. // Text animation
  255. framesCounter++;
  256. if ((framesCounter%2) == 0) lettersCounter++;
  257. if (msgState == 0)
  258. {
  259. if (lettersCounter <= (int)strlen(message)) strncpy(msgBuffer, message, lettersCounter);
  260. else
  261. {
  262. for (int i = 0; i < (int)strlen(msgBuffer); i++) msgBuffer[i] = '\0';
  263. lettersCounter = 0;
  264. msgState = 1;
  265. }
  266. if (IsKeyPressed(KEY_ENTER)) msgState = 1;
  267. }
  268. else if (msgState == 1)
  269. {
  270. msgCounter++;
  271. if ((IsKeyPressed(KEY_ENTER)) || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)))
  272. {
  273. msgState = 2;
  274. msgCounter = 0;
  275. }
  276. }
  277. else if (msgState == 2)
  278. {
  279. msgCounter++;
  280. if (msgCounter > 180) msgState = 3;
  281. }
  282. else msgCounter++;
  283. }
  284. // Gameplay Screen Draw logic
  285. void DrawArmoryScreen(void)
  286. {
  287. DrawTexture(background, 0, 0, WHITE);
  288. // Draw monsters
  289. DrawMonster(blazon01, 0);
  290. DrawMonster(blazon02, 0);
  291. DrawMonster(blazon03, 0);
  292. // Draw door
  293. if (doorLeft.selected) DrawTextureRec(doors, doorLeft.frameRec, doorLeft.position, GREEN);
  294. else DrawTextureRec(doors, doorLeft.frameRec, doorLeft.position, WHITE);
  295. if (doorRight.selected) DrawTextureRec(doors, doorRight.frameRec, doorRight.position, GREEN);
  296. else DrawTextureRec(doors, doorRight.frameRec, doorRight.position, WHITE);
  297. // Draw messsages
  298. if (msgState < 2) DrawRectangle(0, 40, GetScreenWidth(), 200, Fade(LIGHTGRAY, 0.5f));
  299. else if (msgState == 2) DrawRectangle(0, 80, GetScreenWidth(), 100, Fade(LIGHTGRAY, 0.5f));
  300. if (msgState == 0)
  301. {
  302. DrawTextEx(font, msgBuffer, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  303. }
  304. else if (msgState == 1)
  305. {
  306. DrawTextEx(font, message, (Vector2){ msgPosX, 80 }, font.baseSize, 2, WHITE);
  307. if ((msgCounter/30)%2) DrawText("PRESS ENTER or CLICK", GetScreenWidth() - 280, 200, 20, BLACK);
  308. }
  309. else if (msgState == 2)
  310. {
  311. if ((msgCounter/30)%2)
  312. {
  313. DrawTextEx(font, "CHOOSE WISELY!", (Vector2){ 300, 95 }, font.baseSize*2, 2, WHITE);
  314. DrawRectangleRec(blazon01.bounds, Fade(RED, 0.6f));
  315. DrawRectangleRec(blazon02.bounds, Fade(RED, 0.6f));
  316. DrawRectangleRec(blazon03.bounds, Fade(RED, 0.6f));
  317. }
  318. }
  319. else
  320. {
  321. if ((monsterHover) && ((msgCounter/30)%2))
  322. {
  323. DrawRectangle(0, 0, GetScreenWidth(), 50, Fade(LIGHTGRAY, 0.5f));
  324. DrawText("PRESS SPACE or CLICK to INTERACT", 420, 15, 20, BLACK);
  325. }
  326. }
  327. DrawPlayer(); // NOTE: Also draws mouse pointer!
  328. }
  329. // Gameplay Screen Unload logic
  330. void UnloadArmoryScreen(void)
  331. {
  332. // TODO: Unload GAMEPLAY screen variables here!
  333. UnloadTexture(background);
  334. UnloadMonster(blazon01);
  335. UnloadMonster(blazon02);
  336. UnloadMonster(blazon03);
  337. }
  338. // Gameplay Screen should finish?
  339. int FinishArmoryScreen(void)
  340. {
  341. return finishScreen;
  342. }