/
loop.c
377 lines (339 loc) · 11.1 KB
/
loop.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Snake 3d -- loop.c
*
* Gestione del loop del programma
*
*/
#include "loop.h"
/*
* Funzione che controlla se il verme sta uscendo dai confini di gioco
*/
int checkWall(float xy, float z, int piano){
switch(piano){
case 1: /* controllo il piano X */
if((xy >= WORLDIM)) return 0;
if((xy <= -WORLDIM)) return 0;
break;
case 2: /* controllo il piano Y */
if((xy >= WORLDIM)) return 0;
if((xy <= -WORLDIM)) return 0;
break;
default:
break;
}
/* controllo il piano Z */
if( z <= -WORLDIM) return 0;
if( z >= WORLDIM) return 0;
return 1;
}
int sCollisionDetection(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat dim1, GLfloat dim2){
GLdouble d;
GLdouble dist;
dim1 = dim1/2;
dim2 = dim2/2;
dist = (dim1+dim2)*(dim1+dim2)-0.01;
d = dist2Point(x1, y1, z1, x2, y2, z2);
if(d<dist){
/* printf("DEBUG[cd]: distanza %f %f %f %f\n",d, dist, dim1, dim2); */
return 1;
}else
return 0;
}
/*
* qui bisogna gestire tutto quello che riguarda i vari "update" del gioco, quindi
* movimento del verme(velocita', direzione...) , mappa, collision detection
*/
void loop(){
GLuint moveFrame = 0;
GLuint delay = 0;
/* calcolo del framerate */
programData.frame++;
programData.time = glutGet(GLUT_ELAPSED_TIME);
if(programData.timerender == 0) programData.timerender = programData.time;
if(programData.time - programData.timebase > 1000){
sprintf(programData.fps, "FPS: %.0f", programData.frame*1000.0/(programData.time-programData.timebase));
programData.timebase = programData.time;
programData.frame = 0;
}
/* controllo lo stato della partita */
switch(programData.gameStatus){
case 0: /* ho perso */
return;
break;
case 1: /* sto giocando */
/* continuo e stampo i punti */
sprintf(programData.pointsOSD, "PUNTI: %d",programData.points );
break;
case 2: /* ho vinto */
return;
break;
default:
break;
}
/* se sono nel menu' non devo muovere nulla */
if(programData.menu == 1){
return;
}
/* movimento della telecamera a scatti */
if(programData.time - programData.timerender > programData.timeFrame){
moveFrame = 1;
delay = programData.time - (programData.timerender + programData.timeFrame);
programData.timerender = programData.time + delay ;
}
/* controllo l'input dell'utente */
/* movimento verticale --> freccia up e down */
if((worldData.kup)||(worldData.kdown)){
if(worldData.yStatus == worldData.nextYstatus){ /* sono fermo */
switch(worldData.yStatus){
case 0: /*sono orizzontale*/
if(worldData.kup){
worldData.nextYstatus = 1;
worldData.nextAngleX = worldData.angleX - 90.0f;
worldData.nextAngleMX = worldData.angleMX -90.0f;
worldData.angleX = worldData.angleMX;
}else{
worldData.nextYstatus = 2;
worldData.nextAngleX = worldData.angleX + 90.0f;
worldData.nextAngleMX = worldData.angleMX + 90.0f;
worldData.angleX = worldData.angleMX;
}
break;
case 1: /*sono in alto*/
if(worldData.kdown){
worldData.nextYstatus = 0;
worldData.nextAngleX = worldData.angleX + 90.0f;
worldData.nextAngleMX = worldData.angleMX + 90.0f;
worldData.angleX = worldData.angleMX;
}
break;
case 2: /*sono in basso*/\
if(worldData.kup){
worldData.nextYstatus = 0;
worldData.nextAngleX = worldData.angleX - 90.0f;
worldData.nextAngleMX = worldData.angleMX -90.0f;
worldData.angleX = worldData.angleMX;
}
break;
default:
break;
}
}else{ /* caso in cui sono in movimento*/
if(worldData.kup){
if(worldData.nextYstatus == 2){ /*sto andando verso il basso*/
worldData.nextYstatus = 0;
worldData.yStatus = 2;
worldData.angleX = worldData.nextAngleX;
/* qui bisogna muovere il verme */
worldData.nextAngleX = worldData.angleX - 90.0f;
worldData.nextAngleMX = worldData.angleX - 90.0f;
}
}else{
if(worldData.nextYstatus == 1){ /*sto andando verso l'alto*/
worldData.nextYstatus = 0;
worldData.yStatus = 1;
worldData.angleX = worldData.nextAngleX;
/* qui bisogna muovere il verme */
worldData.nextAngleX = worldData.angleX + 90.0f;
worldData.nextAngleMX = worldData.angleX + 90.0f;
}
}
}
worldData.kup = 0;
worldData.kdown = 0;
}
/* movimento laterale --> freccia sx e dx */
if((worldData.kleft)||(worldData.kright)){
if(worldData.xStatus == 0){ /* sto andando dritto */
if(worldData.kleft){
worldData.nextXstatus = 1; /* vado a sinistra */
worldData.nextAngleY = worldData.angleY - 90.0f ;
worldData.nextAngleMY = worldData.angleMY - 90.0f ;
worldData.angleY = worldData.angleMY;
}
else{
worldData.nextXstatus = 2; /* vado a destra */
worldData.nextAngleY = worldData.angleY + 90.0f ;
worldData.nextAngleMY = worldData.angleMY + 90.0f ;
worldData.angleY = worldData.angleMY;
}
worldData.xStatus = 1;
}else{
switch(worldData.nextXstatus){
case 1: /* sto andando verso sinistra */
if(worldData.kright){
worldData.nextAngleY = worldData.nextAngleY + 90.0f;
worldData.nextAngleMY = worldData.nextAngleMY + 90.0f;
worldData.nextXstatus = 2;
worldData.angleY = worldData.angleMY;
}
break;
case 2: /* sto andando verso destra */
if(worldData.kleft){
worldData.nextAngleY = worldData.nextAngleY - 90.0f;
worldData.nextAngleMY = worldData.nextAngleMY - 90.0f;
worldData.nextXstatus = 1;
worldData.angleY = worldData.angleMY;
}
break;
default:
break;
}
}
worldData.kleft = 0;
worldData.kright = 0;
}
/* modifico l'angolo X */
if(worldData.yStatus != worldData.nextYstatus){
switch(worldData.nextYstatus){
case 0: /* devo arrivare sul piano */
switch(worldData.yStatus){
case 1:
worldData.angleMX += programData.velAngolare;
moveFrame = 0;
if(worldData.angleMX > worldData.nextAngleMX){
worldData.angleMX = worldData.nextAngleMX;
worldData.angleX = worldData.nextAngleX;
worldData.yStatus = 0;
}
break;
case 2:
worldData.angleMX -= programData.velAngolare;
moveFrame = 0;
if(worldData.angleMX < worldData.nextAngleMX){
worldData.angleMX = worldData.nextAngleMX;
worldData.angleX = worldData.nextAngleX;
worldData.yStatus = 0;
}
break;
default:
break;
}
break;
case 1:
worldData.angleMX -= programData.velAngolare;
moveFrame = 0;
if(worldData.angleMX < worldData.nextAngleMX){
worldData.angleMX = worldData.nextAngleMX;
worldData.angleX = worldData.nextAngleX;
worldData.yStatus = 1;
}
break;
case 2:
worldData.angleMX += programData.velAngolare;
moveFrame = 0;
if(worldData.angleMX > worldData.nextAngleMX){
worldData.angleMX = worldData.nextAngleMX;
worldData.angleX = worldData.nextAngleX;
worldData.yStatus = 2;
}
break;
default:
break;
}
}
/* modifico l'angolo Y */
if(worldData.xStatus){
switch(worldData.nextXstatus){
case 1: /* devo muovermi a sinistra */
worldData.angleMY -= programData.velAngolare;
moveFrame = 0;
if(worldData.angleMY < worldData.nextAngleMY){
worldData.angleMY = worldData.nextAngleMY;
worldData.angleY = worldData.nextAngleY;
worldData.xStatus = 0;
worldData.nextXstatus = 0;
}
break;
case 2: /* devo muovermi a destra */
worldData.angleMY += programData.velAngolare;
moveFrame = 0;
if(worldData.angleMY > worldData.nextAngleMY){
worldData.angleMY = worldData.nextAngleMY;
worldData.angleY = worldData.nextAngleY;
worldData.xStatus = 0;
worldData.nextXstatus = 0;
}
break;
default:
break;
}
}
/* controllo se sono in posizione tale da muovermi */
if(moveFrame){
float offSetX = 0.0f, offSetY = 0.0f, offSetZ = 0.0f;
// collision detection
double dist;
int collision = 0;
if(worldData.yStatus == 0){
/* movimento sul piano X */
offSetX = (float)sin(worldData.angleY * PIOVER180) * 3.5f;
offSetZ = (float)cos(worldData.angleY * PIOVER180) * 3.5f;
if(!moveWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0)){
programData.gameStatus = 0;
}
dist = dist2Point(-((myWorm.head)->x), -((myWorm.head)->y), (myWorm.head)->z, ball.x, ball.y, ball.z);
if(dist<= ((WORMDIA+DIA)*(WORMDIA+DIA)) )
collision = 1;
else
collision = 0;
if(checkWall((myWorm.head)->x+myWorm.dia, (myWorm.head)->z +myWorm.dia, 1)){
/*collision testa del verme*/
if(collision){
if(worldData.yStatus == 0){
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
}
else{
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
}
generateBall();
programData.points+=100;
increaseVel(5);
}
}else{
/* e' uscito dai muri */
programData.gameStatus = 0;
}
}else{
/* movimento sul piano Y */
offSetY = (float)sin(worldData.angleX * PIOVER180) * 3.5f;
offSetZ = (float)cos(worldData.angleX * PIOVER180) * 3.5f;
if(!moveWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1)){
programData.gameStatus = 0;
}
dist = dist2Point(-((myWorm.head)->x), -((myWorm.head)->y), (myWorm.head)->z, ball.x, ball.y, ball.z);
if(dist<= ((WORMDIA+DIA)*(WORMDIA+DIA)) )
collision = 1;
else
collision = 0;
if(checkWall((myWorm.head)->y+myWorm.dia, (myWorm.head)->z +myWorm.dia, 2)){
/*collision testa del verme*/
if(collision){
if(worldData.yStatus == 0){
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
increaseWorm((float)sin(worldData.angleY * PIOVER180), (float)cos(worldData.angleY * PIOVER180), 0);
}
else{
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
increaseWorm((float)sin(worldData.angleX * PIOVER180), (float)cos(worldData.angleX * PIOVER180), 1);
}
generateBall();
programData.points+=100;
increaseVel(5);
}
}else{
/* e' uscito dai muri */
programData.gameStatus = 0;
}
}
}
}