Douglas A 1 жил өмнө
parent
commit
26f2fdfb68
4 өөрчлөгдсөн 69 нэмэгдсэн , 44 устгасан
  1. 18 0
      bullet.h
  2. 20 0
      game.h
  3. 14 44
      main.c
  4. 17 0
      ship.h

+ 18 - 0
bullet.h

@@ -0,0 +1,18 @@
+#ifndef _BULLET_H
+#define _BULLET_H
+
+#include "raymath.h"
+#include <stdbool.h>
+typedef struct bullet_t {
+    Vector2 position;
+    Vector2 velocity;
+    Vector2 accel;
+    Vector2 direction;
+    float radius;
+    bool active;
+
+} bullet;
+
+
+
+#endif // _BULLET_H

+ 20 - 0
game.h

@@ -0,0 +1,20 @@
+#ifndef _GAME_H
+#define _GAME_H
+
+#include "bullet.h"
+#include "ship.h"
+
+#define MAX_BULLETS 1024
+
+typedef struct game_g {
+    ship s;
+    bullet bullets[MAX_BULLETS];
+    size_t bullets_fired;
+    size_t active_bullets;
+    float last_fire;
+    float dt;
+} game;
+
+
+
+#endif // _GAME_H

+ 14 - 44
main.c

@@ -5,43 +5,11 @@
 #include <stdio.h>
 #include <string.h>
 
-#define DRAG -0.001f
-#define MAX_BULLETS 1024
-
-
-typedef struct ship_t {
-    Vector2 position;
-    Vector2 velocity;
-    Vector2 accel;
-    Vector2 size;
-    Vector2 direction;
-    float angle;
-} ship;
-
-typedef struct bullet_t {
-    Vector2 position;
-    Vector2 velocity;
-    Vector2 accel;
-    Vector2 direction;
-    float radius;
-    bool active;
-
-} bullet;
-
-
-
-typedef struct game_g {
-    ship s;
-    bullet bullets[MAX_BULLETS];
-    size_t bullets_fired;
-    size_t active_bullets;
-    float last_fire;
-    float dt;
-} game;
-
-game g;
-
+#include "ship.h"
+#include "bullet.h"
+#include "game.h"
 
+#define DRAG -0.001f
 
 void ship_draw(const ship* s) { DrawRectangleV(s->position, s->size, BLACK); }
 
@@ -68,7 +36,7 @@ void ship_stop(ship* s) {
     s->accel = Vector2Clamp(s->accel, min, max);
 }
 
-void ship_update(ship* s) {
+void ship_update(game *g, ship* s) {
     Vector2 drag = { .x = DRAG * s->velocity.x, .y = DRAG * s->velocity.y };
 
     float x = cosf(s->angle);
@@ -86,12 +54,12 @@ void ship_update(ship* s) {
     s->direction.x = pos.x;
     s->direction.y = pos.y;
 
-    s->velocity.x += s->accel.x * g.dt;
-    s->velocity.y += s->accel.y * g.dt;
+    s->velocity.x += s->accel.x * g->dt;
+    s->velocity.y += s->accel.y * g->dt;
     s->velocity = Vector2Add(s->velocity, drag);
 
-    s->position.x += s->velocity.x * g.dt;
-    s->position.y += s->velocity.y * g.dt;
+    s->position.x += s->velocity.x * g->dt;
+    s->position.y += s->velocity.y * g->dt;
 
     DrawLine(s->position.x, s->position.y, pos1.x, pos1.y, RED);
 }
@@ -187,7 +155,7 @@ void text(const game *g) {
   char ind[256] = {0};
   sprintf(ind,
           "ship position %fx%f\nship speed %fx%f\nship accel %fx%f\nangle: "
-          "%f\ndirection %fx%f\nbullets fired %d\nactive bullets %zu",
+          "%f\ndirection %fx%f\nbullets fired %zu\nactive bullets %zu",
           g->s.position.x, g->s.position.y, g->s.velocity.x, g->s.velocity.y,
           g->s.accel.x, g->s.accel.y, g->s.angle, g->s.direction.x,
           g->s.direction.y, g->bullets_fired, g->active_bullets);
@@ -206,7 +174,9 @@ int main(void) {
   s1.size.y = 10.0f;
   s1.angle = 3 * PI / 2;
 
-  g.s = s1;
+
+  game g;
+
 
   memset(g.bullets, 0, MAX_BULLETS);
 
@@ -220,7 +190,7 @@ int main(void) {
     }
 
     ship_draw(&g.s);
-    ship_update(&g.s);
+    ship_update(&g, &g.s);
     text(&g);
     process_inputs(&g);
     EndDrawing();

+ 17 - 0
ship.h

@@ -0,0 +1,17 @@
+#ifndef _SHIP_H
+#define _SHIP_H
+
+#include "raymath.h"
+
+typedef struct ship_t {
+    Vector2 position;
+    Vector2 velocity;
+    Vector2 accel;
+    Vector2 size;
+    Vector2 direction;
+    float angle;
+} ship;
+
+
+
+#endif //_SHIP_H