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.

336 lines
12 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - automation events
  4. *
  5. * Example originally created with raylib 5.0, last time updated with raylib 5.0
  6. *
  7. * Example based on 2d_camera_platformer example by arvyy (@arvyy)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2023 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. #define GRAVITY 400
  18. #define PLAYER_JUMP_SPD 350.0f
  19. #define PLAYER_HOR_SPD 200.0f
  20. #define MAX_ENVIRONMENT_ELEMENTS 5
  21. typedef struct Player {
  22. Vector2 position;
  23. float speed;
  24. bool canJump;
  25. } Player;
  26. typedef struct EnvElement {
  27. Rectangle rect;
  28. int blocking;
  29. Color color;
  30. } EnvElement;
  31. //------------------------------------------------------------------------------------
  32. // Program main entry point
  33. //------------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. InitWindow(screenWidth, screenHeight, "raylib [core] example - automation events");
  41. // Define player
  42. Player player = { 0 };
  43. player.position = (Vector2){ 400, 280 };
  44. player.speed = 0;
  45. player.canJump = false;
  46. // Define environment elements (platforms)
  47. EnvElement envElements[MAX_ENVIRONMENT_ELEMENTS] = {
  48. {{ 0, 0, 1000, 400 }, 0, LIGHTGRAY },
  49. {{ 0, 400, 1000, 200 }, 1, GRAY },
  50. {{ 300, 200, 400, 10 }, 1, GRAY },
  51. {{ 250, 300, 100, 10 }, 1, GRAY },
  52. {{ 650, 300, 100, 10 }, 1, GRAY }
  53. };
  54. // Define camera
  55. Camera2D camera = { 0 };
  56. camera.target = player.position;
  57. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  58. camera.rotation = 0.0f;
  59. camera.zoom = 1.0f;
  60. // Automation events
  61. AutomationEventList aelist = LoadAutomationEventList(0); // Initialize list of automation events to record new events
  62. SetAutomationEventList(&aelist);
  63. bool eventRecording = false;
  64. bool eventPlaying = false;
  65. unsigned int frameCounter = 0;
  66. unsigned int playFrameCounter = 0;
  67. unsigned int currentPlayFrame = 0;
  68. SetTargetFPS(60);
  69. //--------------------------------------------------------------------------------------
  70. // Main game loop
  71. while (!WindowShouldClose())
  72. {
  73. // Update
  74. //----------------------------------------------------------------------------------
  75. float deltaTime = 0.015f;//GetFrameTime();
  76. // Dropped files logic
  77. //----------------------------------------------------------------------------------
  78. if (IsFileDropped())
  79. {
  80. FilePathList droppedFiles = LoadDroppedFiles();
  81. // Supports loading .rgs style files (text or binary) and .png style palette images
  82. if (IsFileExtension(droppedFiles.paths[0], ".txt;.rae"))
  83. {
  84. UnloadAutomationEventList(aelist);
  85. aelist = LoadAutomationEventList(droppedFiles.paths[0]);
  86. eventRecording = false;
  87. // Reset scene state to play
  88. eventPlaying = true;
  89. playFrameCounter = 0;
  90. currentPlayFrame = 0;
  91. player.position = (Vector2){ 400, 280 };
  92. player.speed = 0;
  93. player.canJump = false;
  94. camera.target = player.position;
  95. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  96. camera.rotation = 0.0f;
  97. camera.zoom = 1.0f;
  98. }
  99. UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
  100. }
  101. //----------------------------------------------------------------------------------
  102. // Update player
  103. //----------------------------------------------------------------------------------
  104. if (IsKeyDown(KEY_LEFT)) player.position.x -= PLAYER_HOR_SPD*deltaTime;
  105. if (IsKeyDown(KEY_RIGHT)) player.position.x += PLAYER_HOR_SPD*deltaTime;
  106. if (IsKeyDown(KEY_SPACE) && player.canJump)
  107. {
  108. player.speed = -PLAYER_JUMP_SPD;
  109. player.canJump = false;
  110. }
  111. int hitObstacle = 0;
  112. for (int i = 0; i < MAX_ENVIRONMENT_ELEMENTS; i++)
  113. {
  114. EnvElement *element = &envElements[i];
  115. Vector2 *p = &(player.position);
  116. if (element->blocking &&
  117. element->rect.x <= p->x &&
  118. element->rect.x + element->rect.width >= p->x &&
  119. element->rect.y >= p->y &&
  120. element->rect.y <= p->y + player.speed*deltaTime)
  121. {
  122. hitObstacle = 1;
  123. player.speed = 0.0f;
  124. p->y = element->rect.y;
  125. }
  126. }
  127. if (!hitObstacle)
  128. {
  129. player.position.y += player.speed*deltaTime;
  130. player.speed += GRAVITY*deltaTime;
  131. player.canJump = false;
  132. }
  133. else player.canJump = true;
  134. if (IsKeyPressed(KEY_R))
  135. {
  136. // Reset game state
  137. player.position = (Vector2){ 400, 280 };
  138. player.speed = 0;
  139. player.canJump = false;
  140. camera.target = player.position;
  141. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  142. camera.rotation = 0.0f;
  143. camera.zoom = 1.0f;
  144. }
  145. //----------------------------------------------------------------------------------
  146. // Events playing
  147. // NOTE: Logic must be before Camera update because it depends on mouse-wheel value,
  148. // that can be set by the played event... but some other inputs could be affected
  149. //----------------------------------------------------------------------------------
  150. if (eventPlaying)
  151. {
  152. // NOTE: Multiple events could be executed in a single frame
  153. while (playFrameCounter == aelist.events[currentPlayFrame].frame)
  154. {
  155. PlayAutomationEvent(aelist.events[currentPlayFrame]);
  156. currentPlayFrame++;
  157. if (currentPlayFrame == aelist.count)
  158. {
  159. eventPlaying = false;
  160. currentPlayFrame = 0;
  161. playFrameCounter = 0;
  162. TraceLog(LOG_INFO, "FINISH PLAYING!");
  163. break;
  164. }
  165. }
  166. playFrameCounter++;
  167. }
  168. //----------------------------------------------------------------------------------
  169. // Update camera
  170. //----------------------------------------------------------------------------------
  171. camera.target = player.position;
  172. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  173. float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;
  174. // WARNING: On event replay, mouse-wheel internal value is set
  175. camera.zoom += ((float)GetMouseWheelMove()*0.05f);
  176. if (camera.zoom > 3.0f) camera.zoom = 3.0f;
  177. else if (camera.zoom < 0.25f) camera.zoom = 0.25f;
  178. for (int i = 0; i < MAX_ENVIRONMENT_ELEMENTS; i++)
  179. {
  180. EnvElement *element = &envElements[i];
  181. minX = fminf(element->rect.x, minX);
  182. maxX = fmaxf(element->rect.x + element->rect.width, maxX);
  183. minY = fminf(element->rect.y, minY);
  184. maxY = fmaxf(element->rect.y + element->rect.height, maxY);
  185. }
  186. Vector2 max = GetWorldToScreen2D((Vector2){ maxX, maxY }, camera);
  187. Vector2 min = GetWorldToScreen2D((Vector2){ minX, minY }, camera);
  188. if (max.x < screenWidth) camera.offset.x = screenWidth - (max.x - screenWidth/2);
  189. if (max.y < screenHeight) camera.offset.y = screenHeight - (max.y - screenHeight/2);
  190. if (min.x > 0) camera.offset.x = screenWidth/2 - min.x;
  191. if (min.y > 0) camera.offset.y = screenHeight/2 - min.y;
  192. //----------------------------------------------------------------------------------
  193. // Events management
  194. if (IsKeyPressed(KEY_S)) // Toggle events recording
  195. {
  196. if (!eventPlaying)
  197. {
  198. if (eventRecording)
  199. {
  200. StopAutomationEventRecording();
  201. eventRecording = false;
  202. ExportAutomationEventList(aelist, "automation.rae");
  203. TraceLog(LOG_INFO, "RECORDED FRAMES: %i", aelist.count);
  204. }
  205. else
  206. {
  207. SetAutomationEventBaseFrame(180);
  208. StartAutomationEventRecording();
  209. eventRecording = true;
  210. }
  211. }
  212. }
  213. else if (IsKeyPressed(KEY_A)) // Toggle events playing (WARNING: Starts next frame)
  214. {
  215. if (!eventRecording && (aelist.count > 0))
  216. {
  217. // Reset scene state to play
  218. eventPlaying = true;
  219. playFrameCounter = 0;
  220. currentPlayFrame = 0;
  221. player.position = (Vector2){ 400, 280 };
  222. player.speed = 0;
  223. player.canJump = false;
  224. camera.target = player.position;
  225. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  226. camera.rotation = 0.0f;
  227. camera.zoom = 1.0f;
  228. }
  229. }
  230. if (eventRecording || eventPlaying) frameCounter++;
  231. else frameCounter = 0;
  232. //----------------------------------------------------------------------------------
  233. // Draw
  234. //----------------------------------------------------------------------------------
  235. BeginDrawing();
  236. ClearBackground(LIGHTGRAY);
  237. BeginMode2D(camera);
  238. // Draw environment elements
  239. for (int i = 0; i < MAX_ENVIRONMENT_ELEMENTS; i++)
  240. {
  241. DrawRectangleRec(envElements[i].rect, envElements[i].color);
  242. }
  243. // Draw player rectangle
  244. DrawRectangleRec((Rectangle){ player.position.x - 20, player.position.y - 40, 40, 40 }, RED);
  245. EndMode2D();
  246. // Draw game controls
  247. DrawRectangle(10, 10, 290, 145, Fade(SKYBLUE, 0.5f));
  248. DrawRectangleLines(10, 10, 290, 145, Fade(BLUE, 0.8f));
  249. DrawText("Controls:", 20, 20, 10, BLACK);
  250. DrawText("- RIGHT | LEFT: Player movement", 30, 40, 10, DARKGRAY);
  251. DrawText("- SPACE: Player jump", 30, 60, 10, DARKGRAY);
  252. DrawText("- R: Reset game state", 30, 80, 10, DARKGRAY);
  253. DrawText("- S: START/STOP RECORDING INPUT EVENTS", 30, 110, 10, BLACK);
  254. DrawText("- A: REPLAY LAST RECORDED INPUT EVENTS", 30, 130, 10, BLACK);
  255. // Draw automation events recording indicator
  256. if (eventRecording)
  257. {
  258. DrawRectangle(10, 160, 290, 30, Fade(RED, 0.3f));
  259. DrawRectangleLines(10, 160, 290, 30, Fade(MAROON, 0.8f));
  260. DrawCircle(30, 175, 10, MAROON);
  261. if (((frameCounter/15)%2) == 1) DrawText(TextFormat("RECORDING EVENTS... [%i]", aelist.count), 50, 170, 10, MAROON);
  262. }
  263. else if (eventPlaying)
  264. {
  265. DrawRectangle(10, 160, 290, 30, Fade(LIME, 0.3f));
  266. DrawRectangleLines(10, 160, 290, 30, Fade(DARKGREEN, 0.8f));
  267. DrawTriangle((Vector2){ 20, 155 + 10 }, (Vector2){ 20, 155 + 30 }, (Vector2){ 40, 155 + 20 }, DARKGREEN);
  268. if (((frameCounter/15)%2) == 1) DrawText(TextFormat("PLAYING RECORDED EVENTS... [%i]", currentPlayFrame), 50, 170, 10, DARKGREEN);
  269. }
  270. EndDrawing();
  271. //----------------------------------------------------------------------------------
  272. }
  273. // De-Initialization
  274. //--------------------------------------------------------------------------------------
  275. CloseWindow(); // Close window and OpenGL context
  276. //--------------------------------------------------------------------------------------
  277. return 0;
  278. }