summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 0afebb1)
raw | patch | inline | side by side (parent: 0afebb1)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Tue, 4 Apr 2017 17:49:30 +0000 (20:49 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Tue, 4 Apr 2017 17:49:30 +0000 (20:49 +0300) |
game.c | patch | blob | history | |
game.h | patch | blob | history | |
netwar.c | patch | blob | history |
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];
*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) {
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;
}
}
index ea505cf3692fa27fa172a184e2b5c2145b4852c6..e41bb05b40a5a75a2ff54bc39db0c044601a4a41 100644 (file)
--- a/game.h
+++ b/game.h
#define TICK 24 // Количество обновлений мира в секунду
#define TICK_DELAY (1000 / TICK) // Задержка между тиками
#define BULLET_TIME (TICK * 6) // Время жизни игрока
-#define PLAYER_SIZE 0.01785 // Радиус игрока
#define PLAYER_SHOOT ((int)(TICK * 0.8)) // Задержка между выстрелами
+#define PLAYER_SIZE 0.01785 // Радиус игрока
+#define STAR_SIZE 0.1 // Радиус звезды
+
typedef struct Player {
/* public */
bool live;
diff --git a/netwar.c b/netwar.c
index a3236f122f95dd8ff4a6d58c3dcee5668fd856ac..44e8725cc6ff9c117784d59b64472fd207d491a2 100644 (file)
--- a/netwar.c
+++ b/netwar.c
#define HEIGHT 480
static const float ship[] = {
- +1.0, +0.0, -1.0, -1.0,
- -1.0, -1.0, +0.0, +0.0,
- +0.0, +0.0, -1.0, +1.0,
- -1.0, +1.0, +1.0, +0.0,
+ +1.0, +0.0,
+ -1.0, -1.0,
+ +0.0, +0.0,
+ -1.0, +1.0,
+ +1.0, +0.0,
+};
+
+static const float star[] = {
+ +1.0, +0.0,
+ +0.0, -1.0,
+ -1.0, +0.0,
+ +0.0, +1.0,
+ +1.0, +0.0,
};
static char * host = "localhost";
SDL_DestroyWindow(window);
}
+static void paintmodel(const float * model, int modelsz, float mx, float my, float r, float size, int cx, int cy) {
+ int scale = (WIDTH + HEIGHT) / 2 * size;
+
+ int count = modelsz / sizeof(model[0]) / 2;
+ int x = mx * WIDTH / 2;
+ int y = my * HEIGHT / 2;
+
+ SDL_Point pix[count];
+ for(int j = 0; j < count * 2; j += 2) {
+ float c = cos(r * 2 * M_PI);
+ float s = sin(r * 2 * M_PI);
+ float rx = model[j + 0] * scale;
+ float ry = model[j + 1] * scale;
+ pix[j / 2].x = cx + x + (c * rx - s * ry);
+ pix[j / 2].y = cy + y + (s * rx + c * ry);
+ }
+
+ SDL_RenderDrawLines(renderer, pix, count);
+}
+
static void paintwindow() {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
int cx = WIDTH / 2;
int cy = HEIGHT / 2;
- int shipsz = (WIDTH + HEIGHT) / 2 * PLAYER_SIZE;
+
+ SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0xFF, 0x00);
+ paintmodel(star, sizeof(star), 0, 0, 0, STAR_SIZE, cx, cy);
for(int i = 0; i < MAX_PLAYERS; i++) {
if(i == cl_playerid)
else
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
- int count = sizeof(ship) / sizeof(ship[0]) / 2;
- int x = g_player[i].x * WIDTH / 2;
- int y = g_player[i].y * HEIGHT / 2;
-
- SDL_Point pixship[count];
- for(int j = 0; j < count * 2; j += 2) {
- float c = cos(g_player[i].r * 2 * M_PI);
- float s = sin(g_player[i].r * 2 * M_PI);
- float rx = ship[j + 0] * shipsz;
- float ry = ship[j + 1] * shipsz;
- pixship[j / 2].x = cx + x + (c * rx - s * ry);
- pixship[j / 2].y = cy + y + (s * rx + c * ry);
- }
-
if(g_player[i].live)
- SDL_RenderDrawLines(renderer, pixship, count);
+ paintmodel(ship, sizeof(ship), g_player[i].x, g_player[i].y, g_player[i].r, PLAYER_SIZE, cx, cy);
}
for(int i = 0; i < MAX_BULLETS; i++) {