-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.c
218 lines (181 loc) · 4.86 KB
/
setup.c
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "setup.h"
#include "model.h"
#include "movement.h"
#include "grid.h"
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
int reshapeWidth, reshapeHeight;
float orr, ol, ob, ot;
int readyFlag = 0, animation_ongoing = 0, spawn_time = 50, spawn_position = 0;
float thet = 0 + EPS, phi = EPS, targetThet = PI/4, targetPhi = PI/4;
float posix, posiy, mvmnt = -5;
extern minion m[10];
extern int minion_count, minions_left;
/* Lights function */
void lights(void){
/* Light position */
GLfloat light_position[] = { 1, 1, 1, 0};
/* Object light */
GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 1 };
GLfloat ambient_coeffs[] = { 0, 0, 0, 1 };
/* From-point light */
GLfloat light_diffuse[] = { 0.6, 0.6, 0.6, 1 };
GLfloat diffuse_coeffs[] = { 0.4, 0.4, 0.4, 1 };
/* From-point light */
GLfloat light_specular[] = { 0.9, 0.9, 0.9, 1 };
GLfloat specular_coeffs[] = { 1, 1, 1, 1 };
GLfloat shininess = 40;
glEnable(GL_LIGHT0);
/* Setup */
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_coeffs);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular_coeffs);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
glShadeModel(GL_SMOOTH);
glutPostRedisplay();
}
/* Reshape function */
void reshape(int width, int height){
glViewport(0, 0, width, height);
reshapeWidth = width;
reshapeHeight = height;
/* Ortogonal borders */
ol = -9*reshapeWidth/reshapeHeight;
orr = 9*reshapeWidth/reshapeHeight;
ot = 6*reshapeWidth/reshapeHeight;
ob = -6*reshapeWidth/reshapeHeight;
}
/* Keyboard function */
void keyboard(unsigned char key, int x, int y){
(void)x;
(void)y;
switch(key){
/* Escape as an exit key */
case 27:
exit(0);
break;
case 'r':
glutMouseFunc(NULL);
/* Representing movement graph for minions */
map_graph();
dijkstra(graph_representation, begin, finish, parents);
/* Constructing a path */
construct_path();
if(!animation_ongoing){
readyFlag = 1;
glutTimerFunc(20, cam_move, 0);
animation_ongoing = 1;
}
glutPostRedisplay();
break;
}
}
/* Start - moving camera to in-game perspective */
void cam_move(int value){
if(value != 0){
return;
}
if(thet < targetThet + EPS){
thet += PI/150;
}
if(phi < targetPhi + EPS){
phi += PI/150;
}
if(thet < targetThet + EPS || phi < targetPhi + EPS){
glutPostRedisplay();
glutTimerFunc(20, cam_move, 0);
} else {
glutPostRedisplay();
glutTimerFunc(20, game_mode, 1);
}
}
/* Starting the game - movement of minions */
void game_mode(int value){
if(value != 1){
return;
}
/* Spawning additional minnions - depending on the time interval */
if(spawn_position < minion_count){
spawn_time--;
if(spawn_time < 0){
m[spawn_position].alive = 1;
spawn_time = 50;
spawn_position++;
}
}
/* Movement of a minion */
for(int i = 0; i < minion_count; i++){
if(m[i].alive){
/* Target is index of field in path */
if(path[m[i].target-1] - path[m[i].target] == -10){
m[i].y += 0.05;
if(fabs(m[i].y - field_y(path[m[i].target])) < EPS){
m[i].target++;
}
} else if(path[m[i].target-1] - path[m[i].target] == 10){
m[i].y -= 0.05;
if(fabs(m[i].y - field_y(path[m[i].target])) < EPS){
m[i].target++;
}
} else if(path[m[i].target-1] - path[m[i].target] < 0){
m[i].x += 0.05;
if(fabs(m[i].x - field_x(path[m[i].target])) < EPS){
m[i].target++;
}
} else if(path[m[i].target-1] - path[m[i].target] > 0){
m[i].x -= 0.05;
if(fabs(m[i].x - field_x(path[m[i].target])) < EPS){
m[i].target++;
}
}
/* End condition */
if(path[m[i].target-1] == finish){
m[i].alive = 0;
minions_left = 0;
printf("G A M E O V E R \n");
}
}
}
if(minions_left != 0){
glutPostRedisplay();
glutTimerFunc(20, game_mode, 1);
}
}
/* Mouse function */
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
/* Scaling map coordinates */
posix = (float)(orr-ol)/reshapeWidth*x+ol;
posiy = (float)(ob-ot)/reshapeHeight*y+ot;
/* Disabling making towers out of boundaries */
if(posix < -5 || posix > 5 || posiy < -5 || posiy > 5){
return;
}
/* Loading tower into grid matrix */
add_to_matrix(posix+5, posiy-5);
/* Help */
//printf("%f %f\n", posix, posiy);
/*
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
printf("%d ", grid_representation[i][j]);
}
printf("\n");
}
*/
/*
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
printf("%d", graph_representation[i][j]);
}
printf("\n");
}
*/
}
}