-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (115 loc) · 4.13 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
const { createApp } = Vue;
createApp({
data() {
return {
hero: { life: 100, name: "Herói", defending: false },
villain: { life: 100, name: "Vilão", defending: false },
gameOver: false,
message: "",
actionLog: [],
showAllLogs: false,
isMuted: false,
volume: 0.5, // Volume inicial
};
},
mounted() {
// Definir o volume inicial
this.$refs.audio.volume = this.volume;
// Tocar música automaticamente
this.playMusic();
// Listener para reproduzir música caso o autoplay seja bloqueado
document.addEventListener('click', this.playMusic);
},
methods: {
playMusic() {
const audio = this.$refs.audio;
if (audio.paused) {
audio.play().then(() => {
document.removeEventListener('click', this.playMusic); // Remover listener após a primeira interação
}).catch((error) => {
console.log('Autoplay bloqueado, aguardando interação do usuário...', error);
});
}
},
attack(isHero) {
let attacker = isHero ? this.hero : this.villain;
let defender = isHero ? this.villain : this.hero;
const damage = Math.floor(Math.random() * 20) + 5;
if (defender.defending) {
defender.life -= damage / 2;
defender.defending = false;
} else {
defender.life -= damage;
}
this.addActionToLog(isHero ? `Herói atacou e causou ${damage} de dano!` : `Vilão atacou e causou ${damage} de dano!`);
this.checkHealth();
if (isHero) this.villainAction();
},
defense(isHero) {
if (isHero) {
this.hero.defending = true;
this.addActionToLog("Herói está defendendo!");
this.villainAction();
} else {
this.villain.defending = true;
this.addActionToLog("Vilão está defendendo!");
}
},
usePotion(isHero) {
let character = isHero ? this.hero : this.villain;
const heal = Math.floor(Math.random() * 20) + 10;
character.life += heal;
if (character.life > 100) character.life = 100;
this.addActionToLog(isHero ? `Herói usou uma poção e recuperou ${heal} de vida!` : `Vilão usou uma poção e recuperou ${heal} de vida!`);
if (isHero) this.villainAction();
},
flee(isHero) {
if (isHero) {
this.addActionToLog("Herói fugiu!");
this.endGame("Você fugiu! Derrota.");
}
},
villainAction() {
const actions = ['attack', 'defense', 'usePotion'];
const randomAction = actions[Math.floor(Math.random() * actions.length)];
this[randomAction](false);
},
checkHealth() {
if (this.hero.life <= 0) {
this.endGame("Você perdeu! O vilão venceu.");
} else if (this.villain.life <= 0) {
this.endGame("Você venceu! Parabéns.");
}
},
addActionToLog(action) {
this.actionLog.unshift(action);
},
endGame(message) {
this.message = message;
this.gameOver = true;
},
restartGame() {
this.hero.life = 100;
this.villain.life = 100;
this.gameOver = false;
this.hero.defending = false;
this.villain.defending = false;
this.actionLog = [];
},
toggleLogs() {
this.showAllLogs = !this.showAllLogs;
},
toggleMute() {
this.isMuted = !this.isMuted;
this.$refs.audio.muted = this.isMuted;
},
adjustVolume() {
this.$refs.audio.volume = this.volume;
}
},
computed: {
displayedLogs() {
return this.showAllLogs ? this.actionLog : this.actionLog.slice(0, 2);
}
}
}).mount("#app");