DEADSOFTWARE

Добавлена звезда и гравитация
[netwar.git] / game.c
diff --git a/game.c b/game.c
index 36dccb41669cf628070979921560f8fdf93ec3d2..3acaa36a82a9a558f5501e349e5bc29c7e31171b 100644 (file)
--- a/game.c
+++ b/game.c
 #define MAX_SPEED  (0.3 / TICK)
 #define MAX_ROTATE (0.3 / TICK)
 
+// Цифры взяты с потолка, но вроде неплохо подобраны
+#define G      3.673e-4
+#define M_STAR 10.0
+#define M_SHIP 0.1
+
 Player g_player[MAX_PLAYERS];
 Bullet g_bullet[MAX_BULLETS];
 
@@ -95,9 +100,17 @@ static void checkspacebound(float * a) {
        *a = x;
 }
 
+static float length(float x1, float y1, float x2, float y2) {
+       return sqrtf(powf(x1 - x2, 2) + powf(y1 - y2, 2));
+}
+
 static bool collide(float x1, float y1, float r1, float x2, float y2, float r2) {
-       float l = sqrtf(powf(x1 - x2, 2) + powf(y1 - y2, 2));
-       return ((l - r1 - r2) <= 0);
+       return ((length(x1, y1, x2, y2) - r1 - r2) <= 0);
+}
+
+static void gravity(float * vx, float * vy, float x, float y) {
+       *vx += copysign(G * M_STAR * M_SHIP / length(0, 0, x, y), x * -1);
+       *vy += copysign(G * M_STAR * M_SHIP / length(0, 0, x, y), y * -1);
 }
 
 void g_player_does(unsigned int id, DoesBits code) {
@@ -122,12 +135,18 @@ void g_update() {
                        g_player[i].x += g_player[i].vx;
                        g_player[i].y += g_player[i].vy;
                        g_player[i].r += g_player[i].vr;
+
+                       gravity(&g_player[i].vx, &g_player[i].vy, g_player[i].x, g_player[i].y);
+
                        checkspacebound(&g_player[i].x);
                        checkspacebound(&g_player[i].y);
 
                        ++g_player[i].shoot;
                        if(g_player[i].shoot > PLAYER_SHOOT)
                                g_player[i].shoot = PLAYER_SHOOT;
+
+                       if(collide(g_player[i].x, g_player[i].y, PLAYER_SIZE, 0, 0, STAR_SIZE))
+                               g_player[i].live = false;
                }
        }