summaryrefslogtreecommitdiff
path: root/src/game.c
blob: db9d712efbfeb4a235d20166bc3bdfab198bed9b (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
#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);
        }
    }
}


void gotofirstroom()
{
    ttplayer.room = ttmap + '0';
    magic = 0;
    ticks = SDL_GetTicks();
}

void tt_mainloop()
{
    int keyw, keys, keya, keyd, arru, arrr, arrd, arrl;
    keyw = keya = keys = keyd = arru = arrr = arrd = arrl = 0;
    ticks = SDL_GetTicks();
    int q = 0;
    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();
        }
    }
}