-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMissao.js
110 lines (78 loc) · 2.97 KB
/
Missao.js
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
class Missao extends Fase {
load(nivel) {
this.nivel = nivel;
this.player = new NaveBorn({
fase: this,
context: this.game.context,
width: 100,
height: 100,
tipo: 'naveBorn',
img: document.getElementById('bornImg'),
x: this.game.canvas.width / 2,
y: this.game.canvas.height / 1.4
});
this.player.setController(new KeyboardController(window));
this.player.controller.bind();
this.addElemento(this.player);
this.inimigos = [];
this.inimigos.push( document.getElementById('doryImg'));
this.criaOnda(nivel);
}
criaOnda(nivel) {
let qtdInimigos = Math.ceil(nivel + (nivel * 1.5));
let altura = 100;
let largura = 100;
for (let i=0; i < qtdInimigos; i++) {
let pos = this.getMelhorPosicao(qtdInimigos, i, largura, altura);
let inimigo = new NavePeixe({
fase: this,
context: this.game.context,
width: largura,
height: altura,
tipo: 'navePeixe',
img: this.inimigos[0],
x: pos.x,
y: pos.y
});
this.addElemento(inimigo);
}
}
getMelhorPosicao(qtdInimigos, i, largura, altura) {
let margem = 10;
let largMax = this.game.canvas.width;
// let altMax = this.game.canvas.height - this.player.height - margem;
let qtdMaxColuna = Math.floor(largMax / (largura + margem));
// let qtdMaxLinha = Math.floor(altMax / (altura + margem));
let minhaLinha = Math.floor(i / qtdMaxColuna);
let minhaColuna = Math.abs((minhaLinha * qtdMaxColuna) - i);
let ultimaLinha = Math.floor(qtdInimigos/ qtdMaxColuna);
let offsetX = 0;
if(minhaLinha >= ultimaLinha) {
offsetX = Math.abs(Math.abs(((ultimaLinha * qtdMaxColuna) - qtdInimigos) * (largura + margem)) - largMax) / 2;
} else {
offsetX = Math.abs((qtdMaxColuna * (largura + margem)) - largMax) / 2;
}
return {
x: ((largura + margem) * minhaColuna) + offsetX,
y: (altura + margem) * -minhaLinha
}
}
verificaInimigosVivos() {
let inimigosVivos = this.elementos.filter((elemento) => {
return elemento.tipo === 'navePeixe' && elemento.ativo;
});
if (!inimigosVivos.length) {
this.criaOnda(this.nivel++);
}
}
update() {
super.update();
this.verificaInimigosVivos();
this.game.context.fillStyle = `white`;
this.game.context.font = "normal 14pt ubuntu";
this.game.context.fillText(`Utilize espaco para atirar e as setas para mover a Nave Born`,
10, this.game.canvas.height - 10);
this.game.context.font = "normal 16pt ubuntu";
this.game.context.fillText(`Round ${this.nivel}`, 10, 30);
}
}