main.c 5.0 KB

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