-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.js
367 lines (338 loc) · 11.1 KB
/
Game.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
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
const ATTACK_VALUE = 10;
const MONSTER_ATTACK_VALUE = 12;
const STRONG_ATTACK_VALUE = 15;
const HEAL_VALUE = 20;
const MODE_ATTACK = 'ATTACK';
const MODE_STRONG_ATTACK = 'STRONG_ATTACK';
const LOG_EVENT_PLAYER_ATTACK = 'PLAYER_ATTACK';
const LOG_EVENT_PLAYER_STRONG_ATTACK = 'PLAYER_STRONG_ATTACK';
const LOG_EVENT_MONSTER_ATTACK = 'MONSTER_ATTACK';
const LOG_EVENT_PLAYER_HEAL = 'PLAYER_HEAL';
const LOG_EVENT_GAME_OVER = 'GAME_OVER';
//all hero name
const heroname =
["Swordman Health","Warrioress Health","Warrior Health","Ninja Health","Hero Health"];
var countH = 3;
var countSA = 10;
var counts = 0;
var count = 0;
let chosenMaxLife = 300;
let battleLog = [];
let currentMonsterHealth = chosenMaxLife;
let currentPlayerHealth = chosenMaxLife;
adjustHealthBars(chosenMaxLife);
function writeToLog(ev, val, monsterHealth, playerHealth) {
let logEvent = {
event: ev,
value: val,
finalMonsterHealth: monsterHealth,
finalPlayerHealth: playerHealth };
if (ev === LOG_EVENT_PLAYER_ATTACK) {
logEvent.target = 'MONSTER';
} else if (ev === LOG_EVENT_PLAYER_STRONG_ATTACK) {
logEvent.target = 'MONSTER';
} else if (ev === LOG_EVENT_MONSTER_ATTACK) {
logEvent.target = 'PLAYER';
} else if (ev === LOG_EVENT_PLAYER_HEAL) {logEvent.target = 'PLAYER'; }
battleLog.push(logEvent);}
//before start game loading page
setTimeout(function move() {
var elem = document.querySelector("#progress-bar");
var width = 1;
var id = setInterval(frame, 8);
function frame() {
if (width >= 100) {
clearInterval(id);}
else {
width++;
elem.style.width = width + '%';
elem.innerHTML = width * 1 + '%';}}}, 10)
// hide game loading page
var loading = document.querySelector('.loading');
var gA =document.querySelector('.game-name');
var lA = document.querySelector('.loader');
setTimeout(function hideLoader(){
setTimeout(function(){
gA.classList.add('game-name-hide');
lA.classList.add('loader-hide');
}, 1200);
setTimeout(function () {
loading.children[0].classList.add('hideLoad');
loading.children[1].classList.add('hideLoad');
}, 1800);
setTimeout(function () {
loading.parentNode.removeChild(loading);
}, 3400);}, 800)
/****End game loading page******/
//- all reset on by click button
function re(){
currentMonsterHealth = chosenMaxLife;
currentPlayerHealth = chosenMaxLife;
resetGame(chosenMaxLife);}
//default hero name
function dheroname(){
var pic = "Media/hero.png";
document.getElementById('myhero').src = pic.replace();
document.getElementById("hero-name").innerHTML = heroname[4];}
//heal btn on
function resetheal(){
count = 0;
var tbtn=document.getElementById('heal-btn');
if(count >= 0){
tbtn.disabled = false;}}
//super attack btn on
function resetsa(){
counts = 0;
var sabtn=document.getElementById('strong-attack-btn');
if(counts == 0){
sabtn.disabled = false;}}
//super attack reset 0
function countsa(){
countSA = 10;
if(countSA == 10){
document.getElementById('count-sa').innerHTML = countSA;}}
//heal count reset 0
function counth(){
countH = 3;
if(countH == 3){
document.getElementById('count-h').innerHTML = '0' + countH;}}
//off music if reset click
function offmusical(){
var music=document.getElementById("myAudio");
music.pause();
music.currentTime = 0;}
//game history remove
function resethistory(){
var childNodes = document.getElementById("box").getElementsByTagName('*');
for (var node of childNodes) {
node.style.display = 'none';}}
//all reset btn end
//*****************************************//
//play again game after results
function reset() {
currentMonsterHealth = chosenMaxLife;
currentPlayerHealth = chosenMaxLife;
resetGame(chosenMaxLife);}
function endRound() {
const initialPlayerHealth = currentPlayerHealth;
const playerDamge = dealPlayerDamage(MONSTER_ATTACK_VALUE);
currentPlayerHealth -= playerDamge;
writeToLog(LOG_EVENT_MONSTER_ATTACK, playerDamge, currentMonsterHealth, currentPlayerHealth);
//if hero win battle
if (currentMonsterHealth <= 0 && currentPlayerHealth > 0){
Swal.fire('You won the battle! 🎉');
navigator.vibrate(10);
//super attack & heal reset 0
count = 0;
var tbtn=document.getElementById('heal-btn');
if(count == 0){
tbtn.disabled = false;}
counts = 0;
var sabtn=document.getElementById('strong-attack-btn');
if(counts == 0){
sabtn.disabled = false;}
//super attack & heal count reset 0
countSA = 10;
if(countSA == 10){
document.getElementById('count-sa').innerHTML = countSA;}
countH = 3;
if(countH == 3){
document.getElementById('count-h').innerHTML = countH;}
//create history
var box = document.createElement('div');
box.classList.add('myDiv');
box.innerHTML = 'You Won! 🎉';
document.getElementById('box').appendChild(box);
writeToLog(LOG_EVENT_GAME_OVER, 'PLAYER WON', currentMonsterHealth, currentPlayerHealth);}
//if hero lose battle
else if (currentPlayerHealth <= 0 && currentMonsterHealth > 0){
Swal.fire('You lose! ☠️');
navigator.vibrate(10);
//super attack & heal reset 0
count = 0;
var tbtn=document.getElementById('heal-btn');
if(count == 0){
tbtn.disabled = false;}
counts = 0;
var sabtn=document.getElementById('strong-attack-btn');
if(counts == 0){
sabtn.disabled = false;}
//super attack & heal count reset 0
countSA = 10;
if(countSA == 10){
document.getElementById('count-sa').innerHTML = countSA;}
countH = 3;
if(countH == 3){
document.getElementById('count-h').innerHTML = countH;}
//create history
var box = document.createElement('div');
box.classList.add('myDiv');
box.innerHTML = 'You Lose! ☠️';
document.getElementById('box').appendChild(box);
writeToLog(LOG_EVENT_GAME_OVER, 'MONSTER WON', currentMonsterHealth, currentPlayerHealth);}
//if battle draw
else if (currentMonsterHealth <= 0 && currentPlayerHealth <= 0) {
Swal.fire('Battle draw! 🤕');
navigator.vibrate(10);
//super attack & heal reset 0
count = 0;
var tbtn=document.getElementById('heal-btn');
if(count == 0){
tbtn.disabled = false;}
counts = 0;
var sabtn=document.getElementById('strong-attack-btn');
if(counts == 0){
sabtn.disabled = false;}
//super attack & heal count reset 0
countSA = 10;
if(countSA == 10){
document.getElementById('count-sa').innerHTML = countSA;}
countH = 3;
if(countH == 3){
document.getElementById('count-h').innerHTML = countH;}
//create history
var box = document.createElement('div');
box.classList.add('myDiv');
box.innerHTML = 'Battle Draw! 🤕';
document.getElementById('box').appendChild(box);
writeToLog(LOG_EVENT_GAME_OVER, 'A DRAW', currentMonsterHealth, currentPlayerHealth);}
if (currentMonsterHealth <= 0 || currentPlayerHealth <= 0) {
reset();}}
function attackMonster(mode) {
let maxDamage;
let logEvent;
if (mode === MODE_ATTACK) {
maxDamage = ATTACK_VALUE;
logEvent = LOG_EVENT_PLAYER_ATTACK;}
else if (mode === MODE_STRONG_ATTACK){
maxDamage = STRONG_ATTACK_VALUE;
logEvent = LOG_EVENT_PLAYER_STRONG_ATTACK;}
const damage = dealMonsterDamage(maxDamage);
currentMonsterHealth -= damage;
writeToLog(logEvent, damage, currentMonsterHealth, currentPlayerHealth);
endRound();}
//attack and super attack all functions
function attackHandler() {
attackMonster(MODE_ATTACK);
navigator.vibrate(6);}
function strongAttackHandler() {
attackMonster(MODE_STRONG_ATTACK);
navigator.vibrate(12);
//count one by one super attack
counts += 1;
var sabtn=document.getElementById('strong-attack-btn');
if (counts >= 10){
sabtn.disabled = true;
navigator.vibrate(12);
Swal.fire({
position: 'top-end',
title: 'Strong Attack is over',
showConfirmButton: false,
timer: 1600});}}
// super attck & heal count and show inside btn
function saclick(){
countSA -= 1;
if(countSA >= 0){
document.getElementById('count-sa').innerHTML = '0' + countSA;}}
function hclick(){
countH -= 1;
if(countH >= 0){
document.getElementById('count-h').innerHTML = '0' + countH;}}
// heal button all function
function healPlayerHandler() {
navigator.vibrate(4);
let healValue;
if (currentPlayerHealth >= chosenMaxLife - HEAL_VALUE) {
Swal.fire({
title: "<h5>You can't heal to more than your max initial health.</h5>",});
healValue = chosenMaxLife - currentPlayerHealth;}
else { healValue = HEAL_VALUE;}
increasePlayerHealth(healValue);
currentPlayerHealth += healValue;
writeToLog(LOG_EVENT_PLAYER_HEAL, healValue, currentMonsterHealth, currentPlayerHealth);
//count one by one heal
count += 1;
var hbtn=document.getElementById('heal-btn');
if (count >= 3){
hbtn.disabled = true;
navigator.vibrate(12);
Swal.fire({
position: 'top-end',
title: 'Heal is over',
showConfirmButton: false,
timer: 1500})}
endRound();}
attackBtn.addEventListener('click', attackHandler);
strongAttackBtn.addEventListener('click', strongAttackHandler);
healBtn.addEventListener('click', healPlayerHandler);
//choose hero avatar
function hpic1(){
var pic = "Media/swordman.png";
document.getElementById('myhero').src = pic.replace()
document.getElementById("hero-name").innerHTML = heroname[0];}
function hpic2(){
var pic = "Media/female warrior.png";
document.getElementById('myhero').src = pic.replace()
document.getElementById("hero-name").innerHTML = heroname[1];}
function hpic3(){
var pic = "Media/warrior.png";
document.getElementById('myhero').src = pic.replace()
document.getElementById("hero-name").innerHTML = heroname[2];}
function hpic4(){
var pic = "Media/ninja.png";
document.getElementById('myhero').src = pic.replace()
document.getElementById("hero-name").innerHTML = heroname[3];}
function fade(){
var ani = document.getElementById("container");
ani.classList.add("fading");
setTimeout(function(){ ani.style.display = "none"; }, 600);
//play music
var music=document.getElementById("myAudio").play();}
//button inside settings to on-off music
function s_on_music(){
var music=document.getElementById("myAudio");
music.play();}
function s_off_music(){
var music=document.getElementById("myAudio");
music.pause();
music.currentTime = 0;}
//setting theme colour for website
const themes = {
blue: {
'--primary': '#0099ff',
'--secondary' : '#33adff',
'--player_bg' : '#80dfff', },
tomato: {
'--primary': 'tomato',
'--secondary': '#FF7F50',
'--player_bg': '#FFA07A',},
pink: {
'--primary': '#ff4da6',
'--secondary': '#ff69b4',
'--player_bg': '#ff99cc', },
green: {
'--primary': '#339961',
'--secondary': '#40bf79',
'--player_bg': ' #33ff99',
},
Purple: {
'--primary': '#BA55D3',
'--secondary': '#DA70D6',
'--player_bg': '#ff99ff', },
dblu: {
'--primary': '#0099ff',
'--secondary' : '#33adff',
'--player_bg' : '#80dfff',
'--white': 'white',
'--gray': 'gray',
'--monster_bar': '#ff4d4d',
'--control_btn': '#ff0062',
'--setting_menu': '#474747',},
};
[...document.querySelectorAll('.color-button')].forEach(el => {
el.addEventListener('click', () => {
const theme = themes[el.dataset.theme];
for (var variable in theme) {
document.documentElement.style.setProperty(variable, theme[variable]);
};} );});
// end theme colour