DEADSOFTWARE

Первая версия протестированная на сервере
[netwar.git] / game.c
1 #include <SDL2/SDL.h>
3 #include <assert.h>
4 #include <stdbool.h>
5 #include <math.h>
7 #include "game.h"
9 #define SPEED 0.00006
10 #define ROTATE 0.00004
12 Player g_player[MAX_PLAYERS];
14 static void moveplayer(int id, float speed) {
15 g_player[id].vx += speed * cos(g_player[id].r * 2 * M_PI);
16 g_player[id].vy += speed * sin(g_player[id].r * 2 * M_PI);
17 }
19 static void checkspacebound(float * a) {
20 float x = *a;
21 if(x < -1)
22 x = abs(x);
23 else if(x > 1)
24 x = -x;
25 *a = x;
26 }
28 void g_player_set(unsigned int id, bool live, float x, float y, float r, float vx, float vy, float vr) {
29 assert(id < MAX_PLAYERS);
30 g_player[id] = (Player) {
31 .updated = true,
32 .live = live,
33 .x = x,
34 .y = y,
35 .r = r,
36 .vx = vx,
37 .vy = vy,
38 .vr = vr,
39 };
40 }
42 void g_player_does(unsigned int id, DoesCode code) {
43 assert(id < MAX_PLAYERS);
44 g_player[id].updated = true;
45 switch(code) {
46 case DOES_UP: moveplayer(id, SPEED); break;
47 case DOES_DOWN: moveplayer(id, -SPEED); break;
48 case DOES_LEFT: g_player[id].vr -= ROTATE; break;
49 case DOES_RIGHT: g_player[id].vr += ROTATE; break;
50 case DOES_FIRE: break;
51 }
52 }
54 void g_update() {
55 for(int i = 0; i < MAX_PLAYERS; i++) {
56 if(g_player[i].live) {
57 g_player[i].updated = true;
58 g_player[i].x += g_player[i].vx;
59 g_player[i].y += g_player[i].vy;
60 g_player[i].r += g_player[i].vr;
61 checkspacebound(&g_player[i].x);
62 checkspacebound(&g_player[i].y);
63 }
64 }
65 }