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.

578 lines
23 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - sample game: asteroids
  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_BIG_METEORS 4
  26. #define MAX_MEDIUM_METEORS 8
  27. #define MAX_SMALL_METEORS 16
  28. //----------------------------------------------------------------------------------
  29. // Types and Structures Definition
  30. //----------------------------------------------------------------------------------
  31. typedef struct Player {
  32. Vector2 position;
  33. Vector2 speed;
  34. float acceleration;
  35. float rotation;
  36. Vector3 collider;
  37. Color color;
  38. } Player;
  39. typedef struct Shoot {
  40. Vector2 position;
  41. Vector2 speed;
  42. float radius;
  43. float rotation;
  44. int lifeSpawn;
  45. bool active;
  46. Color color;
  47. } Shoot;
  48. typedef struct Meteor {
  49. Vector2 position;
  50. Vector2 speed;
  51. float radius;
  52. bool active;
  53. Color color;
  54. } Meteor;
  55. //------------------------------------------------------------------------------------
  56. // Global Variables Declaration
  57. //------------------------------------------------------------------------------------
  58. static int screenWidth = 800;
  59. static int screenHeight = 450;
  60. static int framesCounter;
  61. static bool gameOver;
  62. static bool pause;
  63. static bool victory;
  64. // NOTE: Defined triangle is isosceles with common angles of 70 degrees.
  65. static float shipHeight;
  66. static Player player;
  67. static Shoot shoot[PLAYER_MAX_SHOOTS];
  68. static Meteor bigMeteor[MAX_BIG_METEORS];
  69. static Meteor mediumMeteor[MAX_MEDIUM_METEORS];
  70. static Meteor smallMeteor[MAX_SMALL_METEORS];
  71. static int countMediumMeteors;
  72. static int countSmallMeteors;
  73. static int meteorsDestroyed;
  74. //------------------------------------------------------------------------------------
  75. // Module Functions Declaration (local)
  76. //------------------------------------------------------------------------------------
  77. static void InitGame(void); // Initialize game
  78. static void UpdateGame(void); // Update game (one frame)
  79. static void DrawGame(void); // Draw game (one frame)
  80. static void UnloadGame(void); // Unload game
  81. static void UpdateDrawFrame(void); // Update and Draw (one frame)
  82. static void InitShoot(Shoot shoot);
  83. static void DrawSpaceship(Vector2 position, float rotation, Color color);
  84. //------------------------------------------------------------------------------------
  85. // Program main entry point
  86. //------------------------------------------------------------------------------------
  87. int main()
  88. {
  89. // Initialization
  90. //--------------------------------------------------------------------------------------
  91. InitWindow(screenWidth, screenHeight, "sample game: asteroids");
  92. InitGame();
  93. #if defined(PLATFORM_WEB)
  94. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  95. #else
  96. SetTargetFPS(60);
  97. //--------------------------------------------------------------------------------------
  98. // Main game loop
  99. while (!WindowShouldClose()) // Detect window close button or ESC key
  100. {
  101. // Update
  102. //----------------------------------------------------------------------------------
  103. UpdateGame();
  104. //----------------------------------------------------------------------------------
  105. // Draw
  106. //----------------------------------------------------------------------------------
  107. DrawGame();
  108. //----------------------------------------------------------------------------------
  109. }
  110. #endif
  111. // De-Initialization
  112. //--------------------------------------------------------------------------------------
  113. UnloadGame(); // Unload loaded data (textures, sounds, models...)
  114. CloseWindow(); // Close window and OpenGL context
  115. //--------------------------------------------------------------------------------------
  116. return 0;
  117. }
  118. //------------------------------------------------------------------------------------
  119. // Module Functions Definitions (local)
  120. //------------------------------------------------------------------------------------
  121. // Initialize game variables
  122. void InitGame(void)
  123. {
  124. int posx, posy;
  125. int velx, vely;
  126. bool correctRange = false;
  127. victory = false;
  128. pause = false;
  129. shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
  130. // Initialization player
  131. player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
  132. player.speed = (Vector2){0, 0};
  133. player.acceleration = 0;
  134. player.rotation = 0;
  135. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  136. player.color = LIGHTGRAY;
  137. meteorsDestroyed = 0;
  138. // Initialization shoot
  139. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  140. {
  141. shoot[i].position = (Vector2){0, 0};
  142. shoot[i].speed = (Vector2){0, 0};
  143. shoot[i].radius = 2;
  144. shoot[i].active = false;
  145. shoot[i].lifeSpawn = 0;
  146. shoot[i].color = WHITE;
  147. }
  148. for (int i = 0; i < MAX_BIG_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. bigMeteor[i].position = (Vector2){posx, posy};
  164. correctRange = false;
  165. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  166. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  167. while(!correctRange)
  168. {
  169. if (velx == 0 && vely == 0)
  170. {
  171. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  172. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  173. }
  174. else correctRange = true;
  175. }
  176. bigMeteor[i].speed = (Vector2){velx, vely};
  177. bigMeteor[i].radius = 40;
  178. bigMeteor[i].active = true;
  179. bigMeteor[i].color = BLUE;
  180. }
  181. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  182. {
  183. mediumMeteor[i].position = (Vector2){-100, -100};
  184. mediumMeteor[i].speed = (Vector2){0,0};
  185. mediumMeteor[i].radius = 20;
  186. mediumMeteor[i].active = false;
  187. mediumMeteor[i].color = BLUE;
  188. }
  189. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  190. {
  191. smallMeteor[i].position = (Vector2){-100, -100};
  192. smallMeteor[i].speed = (Vector2){0,0};
  193. smallMeteor[i].radius = 10;
  194. smallMeteor[i].active = false;
  195. smallMeteor[i].color = BLUE;
  196. }
  197. countMediumMeteors = 0;
  198. countSmallMeteors = 0;
  199. }
  200. // Update game (one frame)
  201. void UpdateGame(void)
  202. {
  203. if (!gameOver)
  204. {
  205. if (IsKeyPressed('P')) pause = !pause;
  206. if (!pause)
  207. {
  208. // Player logic
  209. // Rotation
  210. if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
  211. if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
  212. // Speed
  213. player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
  214. player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
  215. // Controller
  216. if (IsKeyDown(KEY_UP))
  217. {
  218. if (player.acceleration < 1) player.acceleration += 0.04f;
  219. }
  220. else
  221. {
  222. if (player.acceleration > 0) player.acceleration -= 0.02f;
  223. else if (player.acceleration < 0) player.acceleration = 0;
  224. }
  225. if (IsKeyDown(KEY_DOWN))
  226. {
  227. if (player.acceleration > 0) player.acceleration -= 0.04f;
  228. else if (player.acceleration < 0) player.acceleration = 0;
  229. }
  230. // Movement
  231. player.position.x += (player.speed.x*player.acceleration);
  232. player.position.y -= (player.speed.y*player.acceleration);
  233. // Wall behaviour for player
  234. if (player.position.x > screenWidth + shipHeight) player.position.x = -(shipHeight);
  235. else if (player.position.x < -(shipHeight)) player.position.x = screenWidth + shipHeight;
  236. if (player.position.y > (screenHeight + shipHeight)) player.position.y = -(shipHeight);
  237. else if (player.position.y < -(shipHeight)) player.position.y = screenHeight + shipHeight;
  238. // Activation of shoot
  239. if (IsKeyPressed(KEY_SPACE))
  240. {
  241. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  242. {
  243. if (!shoot[i].active)
  244. {
  245. shoot[i].position = (Vector2){ player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight) };
  246. shoot[i].active = true;
  247. shoot[i].speed.x = 1.5*sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
  248. shoot[i].speed.y = 1.5*cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
  249. shoot[i].rotation = player.rotation;
  250. break;
  251. }
  252. }
  253. }
  254. // Shoot life timer
  255. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  256. {
  257. if (shoot[i].active) shoot[i].lifeSpawn++;
  258. }
  259. // Shot logic
  260. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  261. {
  262. if (shoot[i].active)
  263. {
  264. // Movement
  265. shoot[i].position.x += shoot[i].speed.x;
  266. shoot[i].position.y -= shoot[i].speed.y;
  267. // Wall behaviour for shoot
  268. if (shoot[i].position.x > screenWidth + shoot[i].radius)
  269. {
  270. shoot[i].active = false;
  271. shoot[i].lifeSpawn = 0;
  272. }
  273. else if (shoot[i].position.x < 0 - shoot[i].radius)
  274. {
  275. shoot[i].active = false;
  276. shoot[i].lifeSpawn = 0;
  277. }
  278. if (shoot[i].position.y > screenHeight + shoot[i].radius)
  279. {
  280. shoot[i].active = false;
  281. shoot[i].lifeSpawn = 0;
  282. }
  283. else if (shoot[i].position.y < 0 - shoot[i].radius)
  284. {
  285. shoot[i].active = false;
  286. shoot[i].lifeSpawn = 0;
  287. }
  288. // Life of shoot
  289. if (shoot[i].lifeSpawn >= 60)
  290. {
  291. shoot[i].position = (Vector2){0, 0};
  292. shoot[i].speed = (Vector2){0, 0};
  293. shoot[i].lifeSpawn = 0;
  294. shoot[i].active = false;
  295. }
  296. }
  297. }
  298. // Collision Player to meteors
  299. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  300. for (int a = 0; a < MAX_BIG_METEORS; a++)
  301. {
  302. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, bigMeteor[a].position, bigMeteor[a].radius) && bigMeteor[a].active) gameOver = true;
  303. }
  304. for (int a = 0; a < MAX_MEDIUM_METEORS; a++)
  305. {
  306. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
  307. }
  308. for (int a = 0; a < MAX_SMALL_METEORS; a++)
  309. {
  310. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, smallMeteor[a].position, smallMeteor[a].radius) && smallMeteor[a].active) gameOver = true;
  311. }
  312. // Meteor logic
  313. for (int i = 0; i < MAX_BIG_METEORS; i++)
  314. {
  315. if (bigMeteor[i].active)
  316. {
  317. // movement
  318. bigMeteor[i].position.x += bigMeteor[i].speed.x;
  319. bigMeteor[i].position.y += bigMeteor[i].speed.y;
  320. // wall behaviour
  321. if (bigMeteor[i].position.x > screenWidth + bigMeteor[i].radius) bigMeteor[i].position.x = -(bigMeteor[i].radius);
  322. else if (bigMeteor[i].position.x < 0 - bigMeteor[i].radius) bigMeteor[i].position.x = screenWidth + bigMeteor[i].radius;
  323. if (bigMeteor[i].position.y > screenHeight + bigMeteor[i].radius) bigMeteor[i].position.y = -(bigMeteor[i].radius);
  324. else if (bigMeteor[i].position.y < 0 - bigMeteor[i].radius) bigMeteor[i].position.y = screenHeight + bigMeteor[i].radius;
  325. }
  326. }
  327. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  328. {
  329. if (mediumMeteor[i].active)
  330. {
  331. // movement
  332. mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
  333. mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
  334. // wall behaviour
  335. if (mediumMeteor[i].position.x > screenWidth + mediumMeteor[i].radius) mediumMeteor[i].position.x = -(mediumMeteor[i].radius);
  336. else if (mediumMeteor[i].position.x < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.x = screenWidth + mediumMeteor[i].radius;
  337. if (mediumMeteor[i].position.y > screenHeight + mediumMeteor[i].radius) mediumMeteor[i].position.y = -(mediumMeteor[i].radius);
  338. else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
  339. }
  340. }
  341. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  342. {
  343. if (smallMeteor[i].active)
  344. {
  345. // movement
  346. smallMeteor[i].position.x += smallMeteor[i].speed.x;
  347. smallMeteor[i].position.y += smallMeteor[i].speed.y;
  348. // wall behaviour
  349. if (smallMeteor[i].position.x > screenWidth + smallMeteor[i].radius) smallMeteor[i].position.x = -(smallMeteor[i].radius);
  350. else if (smallMeteor[i].position.x < 0 - smallMeteor[i].radius) smallMeteor[i].position.x = screenWidth + smallMeteor[i].radius;
  351. if (smallMeteor[i].position.y > screenHeight + smallMeteor[i].radius) smallMeteor[i].position.y = -(smallMeteor[i].radius);
  352. else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
  353. }
  354. }
  355. // Collision behaviour
  356. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  357. {
  358. if ((shoot[i].active))
  359. {
  360. for (int a = 0; a < MAX_BIG_METEORS; a++)
  361. {
  362. if (bigMeteor[a].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, bigMeteor[a].position, bigMeteor[a].radius))
  363. {
  364. shoot[i].active = false;
  365. shoot[i].lifeSpawn = 0;
  366. bigMeteor[a].active = false;
  367. meteorsDestroyed++;
  368. for (int j = 0; j < 2; j ++)
  369. {
  370. if (countMediumMeteors%2 == 0)
  371. {
  372. mediumMeteor[countMediumMeteors].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y};
  373. mediumMeteor[countMediumMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1};
  374. }
  375. else
  376. {
  377. mediumMeteor[countMediumMeteors].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y};
  378. mediumMeteor[countMediumMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED};
  379. }
  380. mediumMeteor[countMediumMeteors].active = true;
  381. countMediumMeteors ++;
  382. }
  383. //bigMeteor[a].position = (Vector2){-100, -100};
  384. bigMeteor[a].color = RED;
  385. a = MAX_BIG_METEORS;
  386. }
  387. }
  388. }
  389. if ((shoot[i].active))
  390. {
  391. for (int b = 0; b < MAX_MEDIUM_METEORS; b++)
  392. {
  393. if (mediumMeteor[b].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, mediumMeteor[b].position, mediumMeteor[b].radius))
  394. {
  395. shoot[i].active = false;
  396. shoot[i].lifeSpawn = 0;
  397. mediumMeteor[b].active = false;
  398. meteorsDestroyed++;
  399. for (int j = 0; j < 2; j ++)
  400. {
  401. if (countSmallMeteors%2 == 0)
  402. {
  403. smallMeteor[countSmallMeteors].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y};
  404. smallMeteor[countSmallMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1};
  405. }
  406. else
  407. {
  408. smallMeteor[countSmallMeteors].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y};
  409. smallMeteor[countSmallMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED};
  410. }
  411. smallMeteor[countSmallMeteors].active = true;
  412. countSmallMeteors ++;
  413. }
  414. //mediumMeteor[b].position = (Vector2){-100, -100};
  415. mediumMeteor[b].color = GREEN;
  416. b = MAX_MEDIUM_METEORS;
  417. }
  418. }
  419. }
  420. if ((shoot[i].active))
  421. {
  422. for (int c = 0; c < MAX_SMALL_METEORS; c++)
  423. {
  424. if (smallMeteor[c].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, smallMeteor[c].position, smallMeteor[c].radius))
  425. {
  426. shoot[i].active = false;
  427. shoot[i].lifeSpawn = 0;
  428. smallMeteor[c].active = false;
  429. meteorsDestroyed++;
  430. smallMeteor[c].color = YELLOW;
  431. // smallMeteor[c].position = (Vector2){-100, -100};
  432. c = MAX_SMALL_METEORS;
  433. }
  434. }
  435. }
  436. }
  437. }
  438. if (meteorsDestroyed == MAX_BIG_METEORS + MAX_MEDIUM_METEORS + MAX_SMALL_METEORS) victory = true;
  439. }
  440. else
  441. {
  442. if (IsKeyPressed(KEY_ENTER))
  443. {
  444. InitGame();
  445. gameOver = false;
  446. }
  447. }
  448. }
  449. // Draw game (one frame)
  450. void DrawGame(void)
  451. {
  452. BeginDrawing();
  453. ClearBackground(RAYWHITE);
  454. if (!gameOver)
  455. {
  456. // Draw spaceship
  457. Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
  458. Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  459. Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  460. DrawTriangle(v1, v2, v3, MAROON);
  461. // Draw meteors
  462. for (int i = 0; i < MAX_BIG_METEORS; i++)
  463. {
  464. if (bigMeteor[i].active) DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, DARKGRAY);
  465. else DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  466. }
  467. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  468. {
  469. if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
  470. else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  471. }
  472. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  473. {
  474. if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, GRAY);
  475. else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  476. }
  477. // Draw shoot
  478. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  479. {
  480. if (shoot[i].active) DrawCircleV(shoot[i].position, shoot[i].radius, BLACK);
  481. }
  482. if (victory) DrawText("VICTORY", screenWidth/2 - MeasureText("VICTORY", 20)/2, screenHeight/2, 20, LIGHTGRAY);
  483. if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
  484. }
  485. else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
  486. EndDrawing();
  487. }
  488. // Unload game variables
  489. void UnloadGame(void)
  490. {
  491. // TODO: Unload all dynamic loaded data (textures, sounds, models...)
  492. }
  493. // Update and Draw (one frame)
  494. void UpdateDrawFrame(void)
  495. {
  496. UpdateGame();
  497. DrawGame();
  498. }