aboutsummaryrefslogtreecommitdiff
path: root/game/view.cpp
blob: 7fddcd556a12e49f2c87236e77a1828becce76b0 (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
178
179
#include "view.h"

void View::process (Gst &gst, vec2 cam, vec2 mouse, int *mheld) {
    Ground &gr = gst.ground;
    std::vector<Entity> &entities = gst.entities;
    
    vec2 absmouse { mouse }; 
    absmouse -= cam;

    cursor_ground = -1;
    cursor_entity = -1;
    back = -1;
    opt = -1;
    hover_ground = -1;
    
    bool hfound = false;
    for (int y=0; y<gr.sizey && !hfound; y++) {
        for (int x=0; x<gr.sizex && !hfound; x++) {
            vec2 pos { (float)x*32, (float)y*32 };
            if (pos.x < absmouse.x && absmouse.x <= pos.x+32 
            && pos.y < absmouse.y && absmouse.y <= pos.y+32) 
            {
                hover_ground = x+y*gr.sizex;
                hfound = true;
            }
        }
    }
    
    if (menu_train.active) {
        menu_train.over = menu_train.mouse_option(mouse);
    }
    if (menu_build.active) {
        menu_build.over = menu_build.mouse_option(mouse);
    }
    if (menu_unit.active) {
        menu_unit.over = menu_unit.mouse_option(mouse);
    }
    if (menu_day.active) {
        menu_day.over = menu_day.mouse_option(mouse);
    }
    if (menu_tech.active) {
        menu_tech.over = menu_tech.mouse_option(mouse);
    }
    if (menu_trade.active) {
        menu_trade.over = menu_trade.mouse_option(mouse);
    }
    if (menu_age_up.active) {
        menu_age_up.over = menu_age_up.mouse_option(mouse);
    }
    
    if (mheld[0] == 1) {
        bool found = false;  
        
        if (moves.size() > 0 && !found) {
            for (int i=0; i<moves.size() && !found; i++) {
                int x = moves[i] % gr.sizex;
                int y = moves[i] / gr.sizex;
                int valid = 1;
                for (Entity &e : gst.entities) {
                    if (e.x == x && e.y == y && e.info->unit == 1) {
                        valid = 0;
                    }
                }
                if (!valid) continue;
                vec2 pos { (float)x*32, (float)y*32 };
                if (pos.x < absmouse.x && absmouse.x <= pos.x+32 
                && pos.y < absmouse.y && absmouse.y <= pos.y+32) 
                {
                    cursor_ground = moves[i];
                }
            }
            found = true;
        }
        
        if (attacks.size() > 0 && !found) {
            for (int i=0; i<attacks.size() && !found; i++) {
                int x = attacks[i] % gr.sizex;
                int y = attacks[i] / gr.sizex;
                vec2 pos { (float)x*32, (float)y*32 };
                if (pos.x < absmouse.x && absmouse.x <= pos.x+32 
                && pos.y < absmouse.y && absmouse.y <= pos.y+32) 
                {
                    cursor_ground = attacks[i];
                }
            }
            found = true;
        }

        if (menu_train.active && !found) {
            int selected = menu_train.mouse_option(mouse);
            if (selected != -1) {
                opt = selected; found = true;
            } else {
                back = 1; found = 1;
            }
        }

        if (menu_build.active && !found) {
            int selected = menu_build.mouse_option(mouse);
            if (selected != -1) {
                opt = selected; found = true;
            } else {
                back = 1; found = 1;
            }
        }

        if (menu_unit.active && !found) {
            int selected = menu_unit.mouse_option(mouse);
            if (selected != -1) {
                opt = selected; found = true;
            } else {
                back = 1; found = 1;
            }
        }

        if (menu_day.active && !found) {
            int selected = menu_day.mouse_option(mouse);
            if (selected != -1) {
                opt = selected; found = true;
            } else {
                back = 1; found = 1;
            }
        }

        if (menu_tech.active && !found) {
            int selected = menu_tech.mouse_option(mouse);
            if (selected != -1) {
                opt = selected; found = true;
            } else {
                back = 1; found = 1;
            }
        }

        if (menu_trade.active && !found) {
            int selected = menu_trade.mouse_option(mouse);
            if (selected != -1) {
                opt = selected; found = true;
            } else {
                back = 1; found = 1;
            }
        }

        if (menu_age_up.active && !found) {
            int selected = menu_age_up.mouse_option(mouse);
            if (selected != -1) {
                opt = selected; found = true;
            } else {
                back = 1; found = 1;
            }
        }
        
        for (int i=0; i<entities.size() && !found; i++) {
            if (entities[i].done) continue;
            if (entities[i].owner != gst.turn) continue;
            vec2 pos { (float)entities[i].x*32, (float)entities[i].y*32 };
            if (pos.x < absmouse.x && absmouse.x <= pos.x+32 
            && pos.y < absmouse.y && absmouse.y <= pos.y+32) 
            {
                cursor_entity = i;
                if (entities[i].info->unit == 1) {
                    found = true;
                }
            }
        }
        if (cursor_entity != -1) found = true;
        
        for (int y=0; y<gr.sizey && !found; y++) {
            for (int x=0; x<gr.sizex && !found; x++) {
                vec2 pos { (float)x*32, (float)y*32 };
                if (pos.x < absmouse.x && absmouse.x <= pos.x+32 
                && pos.y < absmouse.y && absmouse.y <= pos.y+32) 
                {
                    cursor_ground = x+y*gr.sizex;
                    found = true;
                }
            }
        }
    }
}