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.

378 lines
14 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - sample game: asteroids survival
  4. *
  5. * Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria
  6. *
  7. * This game has been created using raylib v1.3 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include <math.h>
  15. #if defined(PLATFORM_WEB)
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. //----------------------------------------------------------------------------------
  19. // Some Defines
  20. //----------------------------------------------------------------------------------
  21. #define PLAYER_BASE_SIZE 20.0f
  22. #define PLAYER_SPEED 6.0f
  23. #define PLAYER_MAX_SHOOTS 10
  24. #define METEORS_SPEED 2
  25. #define MAX_MEDIUM_METEORS 8
  26. #define MAX_SMALL_METEORS 16
  27. //----------------------------------------------------------------------------------
  28. // Types and Structures Definition
  29. //----------------------------------------------------------------------------------
  30. typedef struct Player {
  31. Vector2 position;
  32. Vector2 speed;
  33. float acceleration;
  34. float rotation;
  35. Vector3 collider;
  36. Color color;
  37. } Player;
  38. typedef struct Meteor {
  39. Vector2 position;
  40. Vector2 speed;
  41. float radius;
  42. bool active;
  43. Color color;
  44. } Meteor;
  45. //------------------------------------------------------------------------------------
  46. // Global Variables Declaration
  47. //------------------------------------------------------------------------------------
  48. static int screenWidth = 800;
  49. static int screenHeight = 450;
  50. static int framesCounter;
  51. static bool gameOver;
  52. static bool pause;
  53. // NOTE: Defined triangle is isosceles with common angles of 70 degrees.
  54. static float shipHeight;
  55. static Player player;
  56. static Meteor mediumMeteor[MAX_MEDIUM_METEORS];
  57. static Meteor smallMeteor[MAX_SMALL_METEORS];
  58. //------------------------------------------------------------------------------------
  59. // Module Functions Declaration (local)
  60. //------------------------------------------------------------------------------------
  61. static void InitGame(void); // Initialize game
  62. static void UpdateGame(void); // Update game (one frame)
  63. static void DrawGame(void); // Draw game (one frame)
  64. static void UnloadGame(void); // Unload game
  65. static void UpdateDrawFrame(void); // Update and Draw (one frame)
  66. //------------------------------------------------------------------------------------
  67. // Program main entry point
  68. //------------------------------------------------------------------------------------
  69. int main(void)
  70. {
  71. // Initialization (Note windowTitle is unused on Android)
  72. //---------------------------------------------------------
  73. InitWindow(screenWidth, screenHeight, "sample game: asteroids survival");
  74. InitGame();
  75. #if defined(PLATFORM_WEB)
  76. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  77. #else
  78. SetTargetFPS(60);
  79. //--------------------------------------------------------------------------------------
  80. // Main game loop
  81. while (!WindowShouldClose()) // Detect window close button or ESC key
  82. {
  83. // Update and Draw
  84. //----------------------------------------------------------------------------------
  85. UpdateDrawFrame();
  86. //----------------------------------------------------------------------------------
  87. }
  88. #endif
  89. // De-Initialization
  90. //--------------------------------------------------------------------------------------
  91. UnloadGame(); // Unload loaded data (textures, sounds, models...)
  92. CloseWindow(); // Close window and OpenGL context
  93. //--------------------------------------------------------------------------------------
  94. return 0;
  95. }
  96. //------------------------------------------------------------------------------------
  97. // Module Functions Definitions (local)
  98. //------------------------------------------------------------------------------------
  99. // Initialize game variables
  100. void InitGame(void)
  101. {
  102. int posx, posy;
  103. int velx, vely;
  104. bool correctRange = false;
  105. pause = false;
  106. framesCounter = 0;
  107. shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
  108. // Initialization player
  109. player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
  110. player.speed = (Vector2){0, 0};
  111. player.acceleration = 0;
  112. player.rotation = 0;
  113. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  114. player.color = LIGHTGRAY;
  115. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  116. {
  117. posx = GetRandomValue(0, screenWidth);
  118. while(!correctRange)
  119. {
  120. if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
  121. else correctRange = true;
  122. }
  123. correctRange = false;
  124. posy = GetRandomValue(0, screenHeight);
  125. while(!correctRange)
  126. {
  127. if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
  128. else correctRange = true;
  129. }
  130. correctRange = false;
  131. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  132. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  133. while(!correctRange)
  134. {
  135. if (velx == 0 && vely == 0)
  136. {
  137. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  138. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  139. }
  140. else correctRange = true;
  141. }
  142. mediumMeteor[i].position = (Vector2){posx, posy};
  143. mediumMeteor[i].speed = (Vector2){velx, vely};
  144. mediumMeteor[i].radius = 20;
  145. mediumMeteor[i].active = true;
  146. mediumMeteor[i].color = GREEN;
  147. }
  148. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  149. {
  150. posx = GetRandomValue(0, screenWidth);
  151. while(!correctRange)
  152. {
  153. if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
  154. else correctRange = true;
  155. }
  156. correctRange = false;
  157. posy = GetRandomValue(0, screenHeight);
  158. while(!correctRange)
  159. {
  160. if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
  161. else correctRange = true;
  162. }
  163. correctRange = false;
  164. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  165. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  166. while(!correctRange)
  167. {
  168. if (velx == 0 && vely == 0)
  169. {
  170. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  171. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  172. }
  173. else correctRange = true;
  174. }
  175. smallMeteor[i].position = (Vector2){posx, posy};
  176. smallMeteor[i].speed = (Vector2){velx, vely};
  177. smallMeteor[i].radius = 10;
  178. smallMeteor[i].active = true;
  179. smallMeteor[i].color = YELLOW;
  180. }
  181. }
  182. // Update game (one frame)
  183. void UpdateGame(void)
  184. {
  185. if (!gameOver)
  186. {
  187. if (IsKeyPressed('P')) pause = !pause;
  188. if (!pause)
  189. {
  190. framesCounter++;
  191. // Player logic
  192. // Rotation
  193. if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
  194. if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
  195. // Speed
  196. player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
  197. player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
  198. // Controller
  199. if (IsKeyDown(KEY_UP))
  200. {
  201. if (player.acceleration < 1) player.acceleration += 0.04f;
  202. }
  203. else
  204. {
  205. if (player.acceleration > 0) player.acceleration -= 0.02f;
  206. else if (player.acceleration < 0) player.acceleration = 0;
  207. }
  208. if (IsKeyDown(KEY_DOWN))
  209. {
  210. if (player.acceleration > 0) player.acceleration -= 0.04f;
  211. else if (player.acceleration < 0) player.acceleration = 0;
  212. }
  213. // Movement
  214. player.position.x += (player.speed.x*player.acceleration);
  215. player.position.y -= (player.speed.y*player.acceleration);
  216. // Wall behaviour for player
  217. if (player.position.x > screenWidth + shipHeight) player.position.x = -(shipHeight);
  218. else if (player.position.x < -(shipHeight)) player.position.x = screenWidth + shipHeight;
  219. if (player.position.y > (screenHeight + shipHeight)) player.position.y = -(shipHeight);
  220. else if (player.position.y < -(shipHeight)) player.position.y = screenHeight + shipHeight;
  221. // Collision Player to meteors
  222. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  223. for (int a = 0; a < MAX_MEDIUM_METEORS; a++)
  224. {
  225. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
  226. }
  227. for (int a = 0; a < MAX_SMALL_METEORS; a++)
  228. {
  229. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, smallMeteor[a].position, smallMeteor[a].radius) && smallMeteor[a].active) gameOver = true;
  230. }
  231. // Meteor logic
  232. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  233. {
  234. if (mediumMeteor[i].active)
  235. {
  236. // movement
  237. mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
  238. mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
  239. // wall behaviour
  240. if (mediumMeteor[i].position.x > screenWidth + mediumMeteor[i].radius) mediumMeteor[i].position.x = -(mediumMeteor[i].radius);
  241. else if (mediumMeteor[i].position.x < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.x = screenWidth + mediumMeteor[i].radius;
  242. if (mediumMeteor[i].position.y > screenHeight + mediumMeteor[i].radius) mediumMeteor[i].position.y = -(mediumMeteor[i].radius);
  243. else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
  244. }
  245. }
  246. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  247. {
  248. if (smallMeteor[i].active)
  249. {
  250. // movement
  251. smallMeteor[i].position.x += smallMeteor[i].speed.x;
  252. smallMeteor[i].position.y += smallMeteor[i].speed.y;
  253. // wall behaviour
  254. if (smallMeteor[i].position.x > screenWidth + smallMeteor[i].radius) smallMeteor[i].position.x = -(smallMeteor[i].radius);
  255. else if (smallMeteor[i].position.x < 0 - smallMeteor[i].radius) smallMeteor[i].position.x = screenWidth + smallMeteor[i].radius;
  256. if (smallMeteor[i].position.y > screenHeight + smallMeteor[i].radius) smallMeteor[i].position.y = -(smallMeteor[i].radius);
  257. else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
  258. }
  259. }
  260. }
  261. }
  262. else
  263. {
  264. if (IsKeyPressed(KEY_ENTER))
  265. {
  266. InitGame();
  267. gameOver = false;
  268. }
  269. }
  270. }
  271. // Draw game (one frame)
  272. void DrawGame(void)
  273. {
  274. BeginDrawing();
  275. ClearBackground(RAYWHITE);
  276. if (!gameOver)
  277. {
  278. // Draw spaceship
  279. Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
  280. Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  281. Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  282. DrawTriangle(v1, v2, v3, MAROON);
  283. // Draw meteor
  284. for (int i = 0;i < MAX_MEDIUM_METEORS; i++)
  285. {
  286. if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
  287. else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  288. }
  289. for (int i = 0;i < MAX_SMALL_METEORS; i++)
  290. {
  291. if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, DARKGRAY);
  292. else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  293. }
  294. DrawText(FormatText("TIME: %.02f", (float)framesCounter/60), 10, 10, 20, BLACK);
  295. if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
  296. }
  297. else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
  298. EndDrawing();
  299. //----------------------------------------------------------------------------------
  300. }
  301. // Unload game variables
  302. void UnloadGame(void)
  303. {
  304. // TODO: Unload all dynamic loaded data (textures, sounds, models...)
  305. }
  306. // Update and Draw (one frame)
  307. void UpdateDrawFrame(void)
  308. {
  309. UpdateGame();
  310. DrawGame();
  311. }