main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "raylib.h"
  2. #include "raymath.h"
  3. #include <stdbool.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #define DRAG -0.001f
  8. #define MAX_BULLETS 1024
  9. typedef struct ship_t {
  10. Vector2 position;
  11. Vector2 velocity;
  12. Vector2 accel;
  13. Vector2 size;
  14. Vector2 direction;
  15. float angle;
  16. } ship;
  17. typedef struct bullet_t {
  18. Vector2 position;
  19. Vector2 velocity;
  20. Vector2 accel;
  21. Vector2 direction;
  22. float radius;
  23. bool active;
  24. } bullet;
  25. typedef struct game_g {
  26. ship s;
  27. bullet bullets[MAX_BULLETS];
  28. size_t bullets_fired;
  29. size_t active_bullets;
  30. float last_fire;
  31. float dt;
  32. } game;
  33. game g;
  34. void ship_draw(const ship* s) { DrawRectangleV(s->position, s->size, BLACK); }
  35. void ship_up(ship* s) {
  36. Vector2 min = { .x = -1000.0f, .y = -1000.0f };
  37. Vector2 max = { .x = 1000.0f, .y = 1000.0f };
  38. Vector2 diff = Vector2Subtract(s->direction, s->position);
  39. Vector2 accellv = CLITERAL(Vector2) { .x = diff.x, .y = diff.y };
  40. Vector2Normalize(accellv);
  41. s->accel = Vector2Add(accellv, s->accel);
  42. s->accel = Vector2Clamp(s->accel, min, max);
  43. }
  44. void ship_stop(ship* s) {
  45. Vector2 min = { .x = 1.0, .y = 1.0 };
  46. Vector2 max = { .x = 0.0, .y = 0.0 };
  47. s->accel.y -= 0.0001f;
  48. s->accel.x -= 0.0001f;
  49. s->accel = Vector2Clamp(s->accel, min, max);
  50. }
  51. void ship_update(ship* s) {
  52. Vector2 drag = { .x = DRAG * s->velocity.x, .y = DRAG * s->velocity.y };
  53. float x = cosf(s->angle);
  54. float y = sinf(s->angle);
  55. Vector2 polar = CLITERAL(Vector2) { .x = x, .y = y };
  56. Vector2 pos = Vector2Add(s->position, polar);
  57. float x1 = 100 * cosf(s->angle);
  58. float y1 = 100 * sinf(s->angle);
  59. Vector2 polar1 = CLITERAL(Vector2) { .x = x1, .y = y1 };
  60. Vector2 pos1 = Vector2Add(s->position, polar1);
  61. s->direction.x = pos.x;
  62. s->direction.y = pos.y;
  63. s->velocity.x += s->accel.x * g.dt;
  64. s->velocity.y += s->accel.y * g.dt;
  65. s->velocity = Vector2Add(s->velocity, drag);
  66. s->position.x += s->velocity.x * g.dt;
  67. s->position.y += s->velocity.y * g.dt;
  68. DrawLine(s->position.x, s->position.y, pos1.x, pos1.y, RED);
  69. }
  70. void ship_fire(game* g, ship* s)
  71. {
  72. //Vector2 min = { .x = -300.0f, .y = -300.0f };
  73. //Vector2 max = { .x = 300.0f, .y = 300.0f };
  74. if (g->bullets_fired >= MAX_BULLETS) {
  75. g->bullets_fired = 0;
  76. //g->active_bullets -= 1;
  77. }
  78. float current_fire = GetFrameTime();
  79. float allowed_fire = 0.0002;
  80. if (current_fire > allowed_fire) {
  81. Vector2 diff = Vector2Subtract(s->direction, s->position);
  82. Vector2 accellv = CLITERAL(Vector2) { .x = diff.x, .y = diff.y };
  83. g->bullets[g->bullets_fired].accel = Vector2Zero();//Vector2Scale(diff,3000);//Vector2Add(accellv, s->accel);
  84. g->bullets[g->bullets_fired].position = s->position;
  85. g->bullets[g->bullets_fired].radius = 5.0f;
  86. g->bullets[g->bullets_fired].velocity = Vector2Scale(diff, 3000);
  87. g->bullets[g->bullets_fired].active = true;
  88. g->bullets_fired++;
  89. g->active_bullets += 1;
  90. g->last_fire = GetFrameTime();
  91. }
  92. }
  93. void bullets_draw(game* g, bullet* b) {
  94. if (b->active) {
  95. DrawCircle(b->position.x, b->position.y, b->radius, RED);
  96. }
  97. }
  98. void bullets_update(game* g, bullet* b) {
  99. if (b->active) {
  100. b->velocity.x += b->accel.x * g->dt;
  101. b->velocity.y += b->accel.y * g->dt;
  102. b->position.x += b->velocity.x * g->dt;
  103. b->position.y += b->velocity.y * g->dt;
  104. b->velocity = Vector2ClampValue(b->velocity, -3000, 3000);
  105. if (b->position.x > (GetScreenWidth() - b->radius) || b->position.y > (GetScreenHeight() - b->radius)) {
  106. b->active = false;
  107. g->active_bullets -= 1;
  108. }
  109. if (b->position.x < b->radius || b->position.y < b->radius) {
  110. b->active = false;
  111. g->active_bullets -= 1;
  112. }
  113. //DrawLine(g->s.position.x, g->s.position.y, b->direction.x, b->direction.y, GREEN);
  114. }
  115. }
  116. void process_inputs(game *g) {
  117. if (IsKeyDown(KEY_W)) {
  118. ship_up(&g->s);
  119. } else {
  120. ship_stop(&g->s);
  121. }
  122. if (IsKeyDown(KEY_D)) {
  123. g->s.angle += 1.0f * g->dt;
  124. }
  125. if (IsKeyDown(KEY_A)) {
  126. g->s.angle -= 1.0f * g->dt;
  127. }
  128. if (IsKeyDown(KEY_SPACE)) {
  129. ship_fire(g, &g->s);
  130. }
  131. }
  132. void text(const game *g) {
  133. char ind[256] = {0};
  134. sprintf(ind,
  135. "ship position %fx%f\nship speed %fx%f\nship accel %fx%f\nangle: "
  136. "%f\ndirection %fx%f\nbullets fired %d\nactive bullets %zu",
  137. g->s.position.x, g->s.position.y, g->s.velocity.x, g->s.velocity.y,
  138. g->s.accel.x, g->s.accel.y, g->s.angle, g->s.direction.x,
  139. g->s.direction.y, g->bullets_fired, g->active_bullets);
  140. DrawText(ind, 10, 10, 20, BLACK);
  141. }
  142. int main(void) {
  143. InitWindow(1820, 900, "Hello World");
  144. ship s1 = {0};
  145. s1.position.x = 910.0f;
  146. s1.position.y = 450.0f;
  147. s1.size.x = 10.0f;
  148. s1.size.y = 10.0f;
  149. s1.angle = 3 * PI / 2;
  150. g.s = s1;
  151. memset(g.bullets, 0, MAX_BULLETS);
  152. while (!WindowShouldClose()) {
  153. BeginDrawing();
  154. ClearBackground(WHITE);
  155. for (size_t i=0; i < MAX_BULLETS; i++) {
  156. bullets_draw(&g, &g.bullets[i]);
  157. bullets_update(&g, &g.bullets[i]);
  158. }
  159. ship_draw(&g.s);
  160. ship_update(&g.s);
  161. text(&g);
  162. process_inputs(&g);
  163. EndDrawing();
  164. g.dt = GetFrameTime();
  165. }
  166. return (0);
  167. }