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.

280 lines
7.3 KiB

  1. /***********************************************************************************
  2. *
  3. * KING GAME JAM - GRAY TEAM
  4. *
  5. * <Game title>
  6. * <Game description>
  7. *
  8. * This game has been created using raylib (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  12. *
  13. ************************************************************************************/
  14. #include "raylib.h"
  15. #include "player.h"
  16. #define PLAYER_ANIM_FRAMES 4
  17. #define PLAYER_ANIM_SEQ 6
  18. //----------------------------------------------------------------------------------
  19. // Module Variables Definition
  20. //----------------------------------------------------------------------------------
  21. // Player mouse moving variables
  22. static bool movingAnim;
  23. static int moveDirection;
  24. static int nextMovePoint;
  25. // Mouse pointer variables
  26. static Vector2 pointerPosition;
  27. static bool pointerAnim;
  28. static float pointerAlpha;
  29. static int framesCounter;
  30. static bool outControl = false;
  31. static int animTimer = 0;
  32. static Texture2D texLife;
  33. static void DrawLifes(void);
  34. // player initialitaction definition
  35. void InitPlayer(void)
  36. {
  37. // NOTE: Some player variables are only initialized once
  38. player.texture = LoadTexture("resources/textures/skully.png");
  39. player.position = (Vector2){ 350, 400 };
  40. player.numLifes = 4;
  41. ResetPlayer();
  42. framesCounter = 0;
  43. texLife = LoadTexture("resources/textures/skully_icon.png");
  44. }
  45. // player update definition
  46. void UpdatePlayer(void)
  47. {
  48. if (!outControl)
  49. {
  50. if ((IsKeyDown(KEY_LEFT)) || (IsKeyDown(KEY_RIGHT)))
  51. {
  52. moveDirection = -1;
  53. movingAnim = false;
  54. }
  55. if ((IsKeyDown(KEY_RIGHT)) || (moveDirection == 0))
  56. {
  57. player.currentSeq = WALK_RIGHT;
  58. framesCounter++;
  59. if (framesCounter > 15)
  60. {
  61. player.currentFrame++;
  62. framesCounter = 0;
  63. if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0;
  64. }
  65. player.position.x += 4;
  66. }
  67. else if ((IsKeyDown(KEY_LEFT)) || (moveDirection == 1))
  68. {
  69. player.currentSeq = WALK_LEFT;
  70. framesCounter++;
  71. if (framesCounter > 15)
  72. {
  73. player.currentFrame++;
  74. framesCounter = 0;
  75. if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0;
  76. }
  77. player.position.x -= 4;
  78. }
  79. else player.currentFrame = 0;
  80. }
  81. else
  82. {
  83. framesCounter++;
  84. animTimer++;
  85. if (framesCounter > 10)
  86. {
  87. player.currentFrame++;
  88. framesCounter = 0;
  89. if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0;
  90. // We can adjust animation playing time depending on sequence
  91. switch (player.currentSeq)
  92. {
  93. case SCARE_RIGHT:
  94. {
  95. if (animTimer > 180)
  96. {
  97. animTimer = 0;
  98. outControl = false;
  99. player.currentSeq = WALK_LEFT;
  100. }
  101. } break;
  102. case SCARE_LEFT:
  103. {
  104. if (animTimer > 240)
  105. {
  106. animTimer = 0;
  107. outControl = false;
  108. player.currentSeq = WALK_RIGHT;
  109. }
  110. } break;
  111. case SEARCH:
  112. case FIND_KEY:
  113. {
  114. if (animTimer > 240)
  115. {
  116. animTimer = 0;
  117. outControl = false;
  118. player.currentSeq = WALK_RIGHT;
  119. }
  120. } break;
  121. }
  122. }
  123. }
  124. if (player.position.x < 30) player.position.x = 30;
  125. else if (player.position.x > (GetScreenWidth() - 200)) player.position.x = GetScreenWidth() - 200;
  126. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  127. {
  128. pointerPosition = GetMousePosition();
  129. pointerAnim = true;
  130. pointerAlpha = 1.0f;
  131. nextMovePoint = (int)pointerPosition.x;
  132. movingAnim = true;
  133. }
  134. if (movingAnim)
  135. {
  136. if (nextMovePoint > (player.position.x + (player.frameRec.width/2) + 5)) moveDirection = 0; // Move Left
  137. else if (nextMovePoint < (player.position.x + (player.frameRec.width/2) - 5)) moveDirection = 1; // Move Right
  138. else
  139. {
  140. moveDirection = -1;
  141. movingAnim = 0;
  142. }
  143. }
  144. player.frameRec.x = player.currentFrame*player.texture.width/PLAYER_ANIM_FRAMES;
  145. player.frameRec.y = (player.currentSeq - 1)*player.texture.height/PLAYER_ANIM_SEQ;
  146. // Update player bounds
  147. player.bounds = (Rectangle){ player.position.x + 50, player.position.y - 60, 100, 300 };
  148. // Mouse pointer alpha animation
  149. if (pointerAnim)
  150. {
  151. pointerAlpha -= 0.1f;
  152. if (pointerAlpha <= 0.0f)
  153. {
  154. pointerAlpha = 0.0f;
  155. pointerAnim = false;
  156. }
  157. }
  158. }
  159. //
  160. void DrawPlayer(void)
  161. {
  162. DrawTextureRec(player.texture, player.frameRec, player.position, WHITE);
  163. // Draw mouse pointer on click
  164. if (pointerAnim) DrawCircleV(pointerPosition, 20, Fade(RED, pointerAlpha));
  165. DrawLifes();
  166. }
  167. void UnloadPlayer(void)
  168. {
  169. UnloadTexture(player.texture);
  170. UnloadTexture(texLife);
  171. }
  172. void ResetPlayer(void)
  173. {
  174. // Reset player variables
  175. player.frameRec = (Rectangle){ 0, 0, player.texture.width/PLAYER_ANIM_FRAMES, player.texture.height/PLAYER_ANIM_SEQ };
  176. player.currentFrame = 0;
  177. player.currentSeq = WALK_RIGHT;
  178. player.key = false;
  179. player.dead = false;
  180. // Reset player position
  181. if (player.position.x < 400) player.position.x = GetScreenWidth() - 350;
  182. if (player.position.x > (GetScreenWidth() - 400)) player.position.x = 350;
  183. // Reset moving variables
  184. movingAnim = false;
  185. moveDirection = -1;
  186. nextMovePoint = 0;
  187. framesCounter = 0;
  188. outControl = false;
  189. animTimer = 0;
  190. // Reset pointer
  191. pointerAlpha = 0.0f;
  192. pointerAnim = false;
  193. }
  194. void ScarePlayer(void)
  195. {
  196. player.currentFrame = 0;
  197. if (moveDirection == 0) player.currentSeq = SCARE_RIGHT;
  198. else if (moveDirection == 1) player.currentSeq = SCARE_LEFT;
  199. else player.currentSeq = SCARE_RIGHT;
  200. player.numLifes--;
  201. if (player.numLifes <= 0) player.dead = true;
  202. outControl = true;
  203. }
  204. void SearchKeyPlayer(void)
  205. {
  206. moveDirection = -1;
  207. movingAnim = 0;
  208. player.currentFrame = 0;
  209. player.currentSeq = SEARCH;
  210. outControl = true;
  211. }
  212. void FindKeyPlayer(void)
  213. {
  214. player.currentFrame = 0;
  215. player.currentSeq = FIND_KEY;
  216. player.key = true;
  217. outControl = true;
  218. }
  219. static void DrawLifes(void)
  220. {
  221. if (player.numLifes != 0)
  222. {
  223. Vector2 position = { 20, GetScreenHeight() - texLife.height - 20 };
  224. for(int i = 0; i < player.numLifes; i++)
  225. {
  226. DrawTexture(texLife, position.x + i*texLife.width, position.y, Fade(RAYWHITE, 0.7f));
  227. }
  228. }
  229. }