summaryrefslogtreecommitdiff
path: root/src/game.c
blob: b1ef3416c03fc0910ded6b520ea2cd7e4a76ecdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "game.h"

#include <SDL2/SDL.h>
#include "globals.h"


int magic = 0;

static int ticks;

static void step(int d)
{
    int xw = ttplayer.xwalk * 200;
    int yw = ttplayer.ywalk * 200;
    if (xw || yw) {
        ttplayer.rem += d;
    }
    static int first_gulag = 1;
    if (ttplayer.tobein_gulag && ttplayer.until_gulag > 0) {
        if (first_gulag) {
            first_gulag = 0;
            Mix_PlayMusic(stalin, -1);
        }
        ttplayer.until_gulag -= d;
        if (ttplayer.until_gulag <= 0) {
            ttplayer.room = ttmap + 'G';
            Mix_PlayMusic(ussr, -1);
        }
    }
    if (xw && yw) {
        ttplayer.xrem += (double)(xw * d) / sqrt(2);
        ttplayer.yrem += (double)(yw * d) / sqrt(2);
    } else if (xw || yw) {
        ttplayer.xrem += xw * d;
        ttplayer.yrem += yw * d;
    }
    int x = ttplayer.x;
    int y = ttplayer.y;
    ttplayer.x += ttplayer.xrem / 1000;
    ttplayer.y += ttplayer.yrem / 1000;
    ttplayer.xrem = ttplayer.xrem % 1000;
    ttplayer.yrem = ttplayer.yrem % 1000;
    {
        SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 };
        int out = tt_room_out(ttplayer.room, &box);
        if (out) ttplayer.room = ttplayer.room->neighbours[out - 1];
        if (out == 1)      ttplayer.y = TT_ROOM_H * 32 - 32;
        else if (out == 2) ttplayer.x = 0;
        else if (out == 3) ttplayer.y = 0;
        else if (out == 4) ttplayer.x = TT_ROOM_W * 32 - 32;
    }
    {
        SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 };
        if (tt_room_collide(ttplayer.room, &box)) {
            ttplayer.x = x;
            ttplayer.y = y;
        }
    }
    tt_body_move(d);
    {
        int i;
        SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 };
        for (i = 0; i != ttplayer.room->bodies_count; ++i) {
            tt_body *b = ttplayer.room->bodies + i;
            SDL_Rect body = { 4 + b->x, 4 + b->y, 24, 24 };
            if (SDL_HasIntersection(&body, &box)) b->collision_act(b);
        }
    }
}


static int q = 0;
static int keyw, keys, keya, keyd, arru, arrr, arrd, arrl;

static int min(int a, int b)  { return a < b ? a : b; }

static void gotofirstroom()
{
    SDL_BlendMode mode;
    SDL_GetRenderDrawBlendMode(ttrdr, &mode);
    int newticks = SDL_GetTicks();
    SDL_SetRenderDrawBlendMode(ttrdr, SDL_BLENDMODE_BLEND);
    while (!q && newticks < ticks + 800) {
        int delta = newticks - ticks;
        SDL_Event e;
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT) q = 1;
            else if (e.type == SDL_KEYDOWN) {
                int code = e.key.keysym.scancode;
                if (code == SDL_SCANCODE_W)          keyw = 1;
                else if (code == SDL_SCANCODE_S)     keys = 1;
                else if (code == SDL_SCANCODE_A)     keya = 1;
                else if (code == SDL_SCANCODE_D)     keyd = 1;
                else if (code == SDL_SCANCODE_UP)    arru = 1;
                else if (code == SDL_SCANCODE_RIGHT) arrr = 1;
                else if (code == SDL_SCANCODE_DOWN)  arrd = 1;
                else if (code == SDL_SCANCODE_LEFT)  arrl = 1;
            } else if (e.type == SDL_KEYUP) {
                int code = e.key.keysym.scancode;
                if (code == SDL_SCANCODE_W)          keyw = 0;
                else if (code == SDL_SCANCODE_S)     keys = 0;
                else if (code == SDL_SCANCODE_A)     keya = 0;
                else if (code == SDL_SCANCODE_D)     keyd = 0;
                else if (code == SDL_SCANCODE_UP)    arru = 0;
                else if (code == SDL_SCANCODE_RIGHT) arrr = 0;
                else if (code == SDL_SCANCODE_DOWN)  arrd = 0;
                else if (code == SDL_SCANCODE_LEFT)  arrl = 0;
            }
        }
        SDL_RenderClear(ttrdr);
        SDL_SetRenderDrawColor(ttrdr, 0, 0, 0, min(255, delta * 256 / 600));
        tt_player_draw();
        SDL_Rect d = { 14, 14, 32 * 20, 32 * 16 };
        SDL_RenderFillRect(ttrdr, &d);
        {
            SDL_Rect src = { 0, 16 * 5, 16, 16 };
            SDL_Rect dst = { 14 + ttplayer.x - delta +
                             rand() % delta,
                             14 + ttplayer.y - delta +
                             rand() % delta,
                             32 + delta / 2 + (rand() % delta),
                             32 + delta / 2 + (rand() % delta) };
            SDL_RenderCopy(ttrdr, tttxr, &src, &dst);
        }
        SDL_RenderPresent(ttrdr);
        newticks = SDL_GetTicks();
        SDL_SetRenderDrawColor(ttrdr, 0, 0, 0, 255);
    }
    SDL_SetRenderDrawBlendMode(ttrdr, mode);
    ttplayer.room = ttmap + '0';
    magic = 0;
    ticks = SDL_GetTicks();
}

void tt_mainloop()
{
    keyw = keya = keys = keyd = arru = arrr = arrd = arrl = 0;
    ticks = SDL_GetTicks();
    while (!q) {
        SDL_Event e;
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT) q = 1;
            else if (e.type == SDL_KEYDOWN) {
                int code = e.key.keysym.scancode;
                if (code == SDL_SCANCODE_W)          keyw = 1;
                else if (code == SDL_SCANCODE_S)     keys = 1;
                else if (code == SDL_SCANCODE_A)     keya = 1;
                else if (code == SDL_SCANCODE_D)     keyd = 1;
                else if (code == SDL_SCANCODE_UP)    arru = 1;
                else if (code == SDL_SCANCODE_RIGHT) arrr = 1;
                else if (code == SDL_SCANCODE_DOWN)  arrd = 1;
                else if (code == SDL_SCANCODE_LEFT)  arrl = 1;
            } else if (e.type == SDL_KEYUP) {
                int code = e.key.keysym.scancode;
                if (code == SDL_SCANCODE_W)          keyw = 0;
                else if (code == SDL_SCANCODE_S)     keys = 0;
                else if (code == SDL_SCANCODE_A)     keya = 0;
                else if (code == SDL_SCANCODE_D)     keyd = 0;
                else if (code == SDL_SCANCODE_UP)    arru = 0;
                else if (code == SDL_SCANCODE_RIGHT) arrr = 0;
                else if (code == SDL_SCANCODE_DOWN)  arrd = 0;
                else if (code == SDL_SCANCODE_LEFT)  arrl = 0;
            }
        }
        ttplayer.ywalk = (keys | arrd) - (keyw | arru);
        ttplayer.xwalk = (keyd | arrr) - (keya | arrl);
        int newticks = SDL_GetTicks();
        step(newticks - ticks);
        ticks = newticks;
        SDL_RenderClear(ttrdr);
        tt_player_draw();
        SDL_RenderPresent(ttrdr);
        if (magic == tt_gotofirstroom) {
            gotofirstroom();
        }
    }
}