-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavePeixe.js
88 lines (63 loc) · 2.02 KB
/
NavePeixe.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
class NavePeixe extends GameComponent {
constructor(options) {
super(options);
this.velocidade = options.velocidade || 0.6;
this.controller = null;
this.ultimoTiro = new Date();
this.tempoEntreTiros = 2000 + Math.floor(Math.random() * 5000);
this.img = options.img;
this.posXinicial = this.x;
this.margemMovimentoX = 30;
this.vaiPraEsquerda = true;
}
update() {
this.resolveControles();
this.render();
this.validaColisoes();
return this.ativo;
}
resolveControles() {
if (this.posXinicial -(this.x -this.velocidade) < this.margemMovimentoX && this.vaiPraEsquerda) {
this.x -= this.velocidade;
} else if ((this.x +this.velocidade) -this.posXinicial < this.margemMovimentoX){
this.x += this.velocidade;
this.vaiPraEsquerda = false;
} else {
this.vaiPraEsquerda = true;
}
this.y += this.velocidade /10;
if (this.podeAtirar()) {
this.atirar();
}
}
podeAtirar() {
return ((new Date()).getTime() - this.ultimoTiro.getTime()) > this.tempoEntreTiros + Math.floor(Math.random() * 3000);
}
atirar() {
this.ultimoTiro = new Date();
let tiro = new Bala({
fase: this.fase,
context: this.context,
width: 5,
height: 5,
velocidade: 1,
sentido: 'sul',
tipo: 'tiroPeixe',
x: this.centroX,
y: this.y + (this.height)
});
tiro.fase = this.fase;
this.fase.addElemento(tiro);
}
render() {
this.context.drawImage(this.img, this.x, this.y, this.width, this.height);
// this.context.strokeStyle = 'white';
// this.context.strokeRect(this.x, this.y, this.width, this.height);
}
resolveColisao(outro) {
if (outro.tipo === 'tiroBorn') {
outro.destroy();
this.destroy();
}
}
}