forked from Andre1999Lopes/TP1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaming.cpp
340 lines (312 loc) · 10.9 KB
/
gaming.cpp
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
#include "utility.h"
/*
Este arquivo implementa as funções de jogo.
*/
//Função que reinicia o jogador, os inimigos e os recoloca em suas posições iniciais
void resetar(){
inimigos.clear();
bullets.clear();
iniciarJogador();
iniciarInimigos();
if(movimentoInimigos<0)
movimentoInimigos*=-1;
}
//Função que verifica se os inimigos chegaram na borda da parte jogável da tela
void verificaPosicao(){
for(int i=0;i<inimigos.size();i++){
if(inimigos[i].posicaoX>=1720){
movimentoInimigos*=-1;
for(int j=0;j<inimigos.size();j++){
inimigos[j].posicaoY-=20;
}
break;
}
else if(inimigos[i].posicaoX<=200){
movimentoInimigos*=-1;
for(int j=0;j<inimigos.size();j++){
inimigos[j].posicaoY-=20;
}
break;
}
}
}
//Função que troca o valor da tela pelo valor recebido
void trocaTela(int valor){
telaAtual=valor;
}
//Função que muda a posição das naves inimigas
void moverNaves(){
for(int i=0;i<inimigos.size();i++){
inimigos[i].posicaoX+=movimentoInimigos;
}
}
//Em teoria, esta é a função que desenha um fundo animado. Não faço ideia do pq não tá funcionando, mas acontece
void desenhaFundo(){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fundo1);
glTexParameterf(fundo1, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(fundo1, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(fundo1, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPushMatrix();
glTranslatef(960,540/*deslocaFundo1*/,0);
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0,0); glVertex2f(-1920/2, -1080/2);
glTexCoord2f(1,0); glVertex2f( 1920/2, -1080/2);
glTexCoord2f(1,1); glVertex2f( 1920/2, 1080/2);
glTexCoord2f(0,1); glVertex2f(-1920/2, 1080/2);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glFlush();
}
//Função que desenha um retângulo dados a sua posição no eixo X e no eixo Y, sua largura, sua altura e a textura a ser utilizada. Textura estática
void desenhaTexturaEstatica(float x, float y, float larg, float alt, GLuint textura){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textura);
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0,0); glVertex2f(-larg/2, -alt/2);
glTexCoord2f(1,0); glVertex2f( larg/2, -alt/2);
glTexCoord2f(1,1); glVertex2f( larg/2, alt/2);
glTexCoord2f(0,1); glVertex2f(-larg/2, alt/2);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glFlush();
}
//Função que inicializa o vector de inimigos e dá uma posição para cada um
void iniciarInimigos(){
incrementoX=0;
decrementoY=0;
int cont=0;
for(int i=0;i<vetorInimigos;i++){
Enemies inimigo;
inimigo.posicaoX=500+incrementoX;
inimigo.posicaoY=980-decrementoY;
inimigo.textura = carregaTexturas("imgs/nave1.png");
incrementoX+=60;
cont++;
inimigos.push_back(inimigo);
if(cont==15){
decrementoY+=60;
incrementoX=0;
cont=0;
}
}
}
//Função que checa colisão entre as balas atiradas pelo jogador e as naves inimigas. Método AABB
GLboolean checarColisao(Enemies enemy, Bullet bala){
bool colisaoX = (enemy.posicaoX + (enemy.larg)/2 >= (bala.x - 2)) && (bala.x + 2 >= enemy.posicaoX - (enemy.larg)/2);
bool colisaoY = (enemy.posicaoY + (enemy.alt)/2 >= (bala.y - 15)) && (bala.y + 15 >= enemy.posicaoY - (enemy.alt)/2);
return colisaoX && colisaoY;
}
//Função que checa colisão entre as balas atiradas pelos inimigos e o jogador. Método AABB
GLboolean checarColisaoPlayerBala(Player player, Bullet bala){
bool colisaoX = (player.posicaoX + (player.larg)/2 >= bala.x - 2) && (bala.x + 2 >= player.posicaoX - (player.larg/2));
bool colisaoY = (player.posicaoY + (player.alt)/2 >= bala.y - 15) && (bala.y + 15 >= player.posicaoY - (player.alt/2));
return colisaoX && colisaoY;
}
//Função que checa colisão entre as naves inimigas e o jogador. Método AABB
GLboolean checarColisaoPlayerNaves(Player player, Enemies enemy){
bool colisaoX = (player.posicaoX + (player.larg)/2 >= enemy.posicaoX - (enemy.larg)/2) && (enemy.posicaoX + (enemy.larg)/2 >= player.posicaoX - (enemy.larg)/2);
bool colisaoY = (player.posicaoY + (player.alt)/2 >= enemy.posicaoY - (enemy.alt)/2) && (enemy.posicaoY + (enemy.larg)/2 >= player.posicaoY - (enemy.alt)/2);
return colisaoX && colisaoY;
}
//Função que muda a posição do jogador baseada em uma constante de movimento
void mover(){
if(direita==true && jogador.posicaoX<1700)
jogador.posicaoX+=movimentoJogador;
if(esquerda==true && jogador.posicaoX>200)
jogador.posicaoX-=movimentoJogador;
}
//Função de checagem de vitória
GLboolean checarVitoria(){
if(inimigos.size()==0)
return true;
return false;
}
GLuint carregaTexturas(const char *arquivo){
GLuint idTextura = SOIL_load_OGL_texture(arquivo, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
if(idTextura==0)
printf("Erro ao carregar a textura: %s\n", SOIL_last_result());
return idTextura;
}
void iniciarJogador(){
jogador.posicaoX=960;
jogador.posicaoY=200;
jogador.larg=80;
jogador.alt=100;
jogador.vida=2;
jogador.estado=1.0;
jogador.tamanho=3.0;
trocaTextura();
}
void desenhaTexturaAnimada(float x, float y, float larg, float alt, GLuint textura, float tamanho, float estado){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textura);
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(estado/tamanho,0); glVertex2f(-larg/2, -alt/2);
glTexCoord2f((estado+1)/tamanho,0); glVertex2f( larg/2, -alt/2);
glTexCoord2f((estado+1)/tamanho,1); glVertex2f( larg/2, alt/2);
glTexCoord2f(estado/tamanho,1); glVertex2f(-larg/2, alt/2);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glFlush();
}
//Função que muda a música de acordo com a tela na qual o jogador se encontra
void tocaMusica(){
if(telaAtual == MENU && !Mix_PlayingMusic()){
Mix_PlayMusic(musicaMenu,-1);
Mix_VolumeMusic(32);
}
else if(telaAtual==JOJINHO && !Mix_PlayingMusic()){
Mix_PlayMusic(musicaBatalha,-1);
Mix_VolumeMusic(96);
}
}
//Função que inicializa as texturas do menu
void iniciarTexturas(){
mfalcon = carregaTexturas("imgs/mfalconAnimado.png");
xwing = carregaTexturas("imgs/x-wingAnimado.png");
longTime = carregaTexturas("imgs/a-long-time.png");
imagemLogo = carregaTexturas("imgs/star-wars-logo.png");
imagemInstrucao = carregaTexturas("imgs/instrucoes.png");
imagemOpcoes = carregaTexturas("imgs/opcoes.png");
imagemIniciar = carregaTexturas("imgs/iniciar.png");
imagemSair = carregaTexturas("imgs/sair.png");
imagemPause = carregaTexturas("imgs/pause.png");
imagemReset = carregaTexturas("imgs/reset.png");
imagemSairMenu = carregaTexturas("imgs/sairMenu.png");
imagemCredito = carregaTexturas("imgs/creditos.png");
imagemDificuldade = carregaTexturas("imgs/dificuldade.png");
imagemVoltar = carregaTexturas("imgs/voltar.png");
imagemInstrucoes = carregaTexturas("imgs/asInstrucoes.png");
imagemOsCreditos = carregaTexturas("imgs/osCreditos.png");
facil = carregaTexturas("imgs/facil.png");
facilSel = carregaTexturas("imgs/facilSel.png");
medio = carregaTexturas("imgs/medio.png");
medioSel = carregaTexturas("imgs/medioSel.png");
dificil = carregaTexturas("imgs/dificil.png");
dificilSel = carregaTexturas("imgs/dificilSel.png");
vitoria = carregaTexturas("imgs/telaVitoria.png");
derrota = carregaTexturas("imgs/telaDerrota.png");
imagemIniciarSel = carregaTexturas("imgs/iniciarSel.png");
imagemOpcoesSel = carregaTexturas("imgs/opcoesSel.png");
imagemDificuldadeSel = carregaTexturas("imgs/dificuldadeSel.png");
imagemCreditoSel = carregaTexturas("imgs/creditosSel.png");
imagemSairMenuSel = carregaTexturas("imgs/sairMenuSel.png");
imagemInstrucaoSel = carregaTexturas("imgs/instrucoesSel.png");
imagemVoltarSel = carregaTexturas("imgs/voltarSel.png");
fundo1 = carregaTexturas("imgs/fundo1-transparente.png");
fundo2 = carregaTexturas("imgs/fundo2-transparente.png");
fundo3 = carregaTexturas("imgs/fundo3-transparente.png");
n0 = carregaTexturas("imgs/0.png");
n1 = carregaTexturas("imgs/1.png");
n2 = carregaTexturas("imgs/2.png");
n3 = carregaTexturas("imgs/3.png");
n4 = carregaTexturas("imgs/4.png");
n5 = carregaTexturas("imgs/5.png");
n6 = carregaTexturas("imgs/6.png");
n7 = carregaTexturas("imgs/7.png");
n8 = carregaTexturas("imgs/8.png");
n9 = carregaTexturas("imgs/9.png");
vidas = carregaTexturas("imgs/simbolo-resistencia.png");
}
//Função que inicializa o jogo no geral
void setup(){
glClearColor(0,0,0,0);
biri.push_back(2500);
biri.push_back(1800);
biri.push_back(400);
Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,4096);
musicaBatalha=Mix_LoadMUS("songs/BattleOfTheHeroes.mp3");
musicaMenu=Mix_LoadMUS("songs/Abertura8bit.mp3");
tiro=Mix_LoadWAV("songs/tiroMF.wav");
tirotf=Mix_LoadWAV("songs/tirotf.wav");
iniciarJogador();
iniciarInimigos();
iniciarTexturas();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
//Função que verifica se o player pode ou não atirar. O tempo muda de acordo com a dificuldade
void trocaValorAtira(int x){
if(podeAtirar)
podeAtirar = false;
else
podeAtirar = true;
}
//Cria uma bala do jogador
void atira(int x, int y){
Bullet bullet;
bullet.x = x;
bullet.y = y;
bullets.push_back(bullet);
}
//Cria uma bala das naves inimigas
void navesAtirar(int valor){
if(telaAtual==JOJINHO && !venceu && !perdeu){
if(!pausa && !reset && !sair){
srand(time(0));
Bullet enemyBullet;
valor = rand()%inimigos.size();
enemyBullet.x = inimigos[valor].posicaoX;
enemyBullet.y = inimigos[valor].posicaoY;
enemyBullets.push_back(enemyBullet);
Mix_PlayChannel(-1,tirotf,0);
glutTimerFunc(5000,navesAtirar,0);
}
}
}
//Desenha um tiro. Se a posição do tiro ultrapassar a altura máxima da tela, o tiro é apagado da memória
void desenhaTiro(){
for(int i = 0; i < bullets.size();i++){
if(bullets[i].y>= 1080)
bullets.erase(bullets.begin()+i);
}
for(int i = 0; i < bullets.size(); i++){
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glTranslatef (bullets[i].x, bullets[i].y, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-2, 15, 0);
glVertex3f(2, 15, 0);
glVertex3f(2, -15, 0);
glVertex3f(-2, -15, 0);
glEnd();
glPopMatrix();
bullets[i].y+=25;
}
}
void trocaTextura(){
if(texturaAtual==3)
texturaAtual=1;
if(texturaAtual==1)
jogador.textura=mfalcon;
else if(texturaAtual==2)
jogador.textura=xwing;
}
//Mesma coisa da função de cima, porém para as naves inimigas
void desenhaTiroInimigo(){
for(int i = 0; i < enemyBullets.size();i++){
if(enemyBullets[i].y<=0)
enemyBullets.erase(enemyBullets.begin()+i);
}
for(int i = 0; i < enemyBullets.size(); i++){
glColor3f(0.0, 1.0, 0.0);
glPushMatrix();
glTranslatef (enemyBullets[i].x, enemyBullets[i].y, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-2, 15, 0);
glVertex3f(2, 15, 0);
glVertex3f(2, -15, 0);
glVertex3f(-2, -15, 0);
glEnd();
glPopMatrix();
enemyBullets[i].y-=25;
}
}