main.c 4.6 KB

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