-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtgcounter.js
329 lines (287 loc) · 10.4 KB
/
mtgcounter.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
//arrays for image URLs and bg color values
var symbolArray = ["img/w.png", "img/u.png", "img/b.png", "img/r.png", "img/g.png", "img/x.png"]; // w u b r g colorless
var symbolTransparentArray = ["img/tr/w.png", "img/tr/u.png", "img/tr/b.png", "img/tr/r.png", "img/tr/g.png", "img/tr/x.png"]; // w u b r g colorless
var bgColorArray = ["rgb(254, 251, 216)", "rgb(169, 224, 250)", "rgb(208, 208, 208)", "rgb(250, 169, 144)", "rgb(155, 210, 176)", "rgb(203, 161, 53)"]; //w u b r g gold
//inits for global vars
var numberOfPlayers;
var playersSoFar;
var players = [];
var customPlayer = {};
//DOM variables for screenspaces
var menuContainer = document.querySelector("#menuContainer");
var choiceContainer = document.querySelector("#choiceContainer");
var playContainer = document.querySelector("#playContainer");
//DOM variables for play screen
var playerNameDisplays = document.querySelectorAll(".pName");
var lifeTotalDisplays = document.querySelectorAll(".pLife");
var playerCountButtons = document.querySelectorAll(".playerCountBtn");
var customBtn = document.querySelector("#customBtn");
var playNowBtn = document.querySelector("#playNowBtn");
//DOM variables for customization screen
var nameLabel = document.querySelector("input[type='text']");
var lifeLabel = document.querySelector("input[type='number']");
var changeSymbol = document.querySelector("#symbolImg");
var colorPicker = document.querySelector("#choiceColorPicker");
var playerDone = document.querySelector("#playerDone");
//DOM variables for play screen
var p1Box = document.querySelector("#boxP1");
var p2Box = document.querySelector("#boxP2");
var p3Box = document.querySelector("#boxP3");
var p4Box = document.querySelector("#boxP4");
var playerBoxes = document.querySelectorAll(".playerBox");
var zeroLifeOptions = document.querySelectorAll(".zeroLife");
var resurrectButtons = document.querySelectorAll(".resBtn");
var endGameBtns = document.querySelectorAll(".endGameBtn");
function init(e) {
numberOfPlayers = e;
playersSoFar = 0;
players = [
{
name: "Player 1",
lifeTotal: 20,
isAlive: true,
symbol: "img/tr/w.png",
background: "#fefbd8"
},
{
name: "Player 2",
lifeTotal: 20,
isAlive: true,
symbol: "img/tr/u.png",
background: "#a9e0fa"
},
{
name: "Player 3",
lifeTotal: 20,
isAlive: true,
symbol: "img/tr/b.png",
background: "#d0d0d0"
},
{
name: "Player 4",
lifeTotal: 20,
isAlive: true,
symbol: "img/tr/r.png",
background: "#faa990"
}
];
customPlayer = {
name: "Player 1",
lifeTotal: 20,
isAlive: true,
symbol: "img/tr/w.png",
background: "#fefbd8"
}
menuContainer.classList.remove("hidden");
playContainer.classList.add("hidden");
choiceContainer.classList.add("hidden");
playerCountButtons[0].classList.add("selected");
playerCountButtons[1].classList.remove("selected");
playerCountButtons[2].classList.remove("selected");
playContainer.classList.remove("player3Container");
for (var i = 0; i < 4; i ++) {
playerBoxes[i].classList.remove("playerBox2p","playerBox3p","playerBox4p","flipdiv","hidden");
zeroLifeOptions[i].classList.add("hidden");
playerNameDisplays[i].style.color = "#333";
lifeTotalDisplays[i].classList.remove("pLife2P","pLife3P","pLife4P")
}
}
init(2);
//Menu screen functionality
playNowBtn.addEventListener("click", function(){
init(numberOfPlayers);
playScreen();
});
playerCountButtons.forEach(function(player, i) {
playerCountButtons[i].addEventListener("click", function(){
numberOfPlayers = i + 2; //button 0 sets to 2 players, button 1 to 3, etc
for (var b = 0; b < playerCountButtons.length; b++) {
playerCountButtons[b].classList.remove("selected");
}
playerCountButtons[i].classList.add("selected");
});
});
//Customize screen functionality
playerDone.addEventListener("click", function(){
copyCustomToPlayer(playersSoFar);
playersSoFar++;
if (playersSoFar < numberOfPlayers) {
resetCustomToDefaults(playersSoFar);
customizeScreenInit(playersSoFar);
} else {
document.body.classList.remove("altBodyBG");
playScreen();
}
});
customBtn.addEventListener("click", function(){
menuContainer.classList.add("hidden");
playContainer.classList.add("hidden");
choiceContainer.classList.remove("hidden");
customizeScreenInit(playersSoFar);
});
function customizeScreenInit (whichPlayer) {
nameLabel.value = players[whichPlayer].name;
lifeLabel.value = players[whichPlayer].lifeTotal;
changeSymbol.src = symbolArray[whichPlayer];
colorPicker.style.backgroundColor = players[whichPlayer].background;
playerDone.style.backgroundColor = players[whichPlayer].background;
document.body.classList.toggle("altBodyBG");
}
function copyCustomToPlayer(whichPlayer) {
players[whichPlayer].name = customPlayer.name;
players[whichPlayer].lifeTotal = customPlayer.lifeTotal;
players[whichPlayer].symbol = customPlayer.symbol;
players[whichPlayer].background = customPlayer.background;
}
function resetCustomToDefaults(whichPlayer) {
customPlayer.name = players[whichPlayer].name;
customPlayer.lifeTotal = players[whichPlayer].lifeTotal;
customPlayer.symbol = players[whichPlayer].symbol;
customPlayer.background = players[whichPlayer].background;
}
//customizeScreenEvents
nameLabel.addEventListener("change", function() {
customPlayer.name = nameLabel.value;
});
lifeLabel.addEventListener("change", function() {
customPlayer.lifeTotal = lifeLabel.value;
});
changeSymbol.addEventListener("click", function() {
var currentSymbolSrc = changeSymbol.src.slice(changeSymbol.src.indexOf("img"),changeSymbol.length);
if (symbolArray.indexOf(currentSymbolSrc) === symbolArray.length - 1) {
changeSymbol.src = symbolArray[0];
customPlayer.symbol = symbolTransparentArray[0];
} else {
changeSymbol.src = symbolArray[symbolArray.indexOf(currentSymbolSrc) + 1];
customPlayer.symbol = symbolTransparentArray[symbolArray.indexOf(currentSymbolSrc) + 1];
}
});
colorPicker.addEventListener("click", function(){
var currColor = colorPicker.style.backgroundColor;
if (bgColorArray.indexOf(currColor) === bgColorArray.length - 1) {
colorPicker.style.backgroundColor = bgColorArray[0];
playerDone.style.backgroundColor = bgColorArray[0];
customPlayer.background = bgColorArray[0];
} else {
colorPicker.style.backgroundColor = bgColorArray[bgColorArray.indexOf(currColor) + 1];
playerDone.style.backgroundColor = bgColorArray[bgColorArray.indexOf(currColor) + 1];
customPlayer.background = bgColorArray[bgColorArray.indexOf(currColor) + 1];
}
});
//GAMEPLAY FUNCTIONS
function playScreen() {
initPlayScreen();
menuContainer.classList.add("hidden");
choiceContainer.classList.add("hidden");
playContainer.classList.remove("hidden");
for (i = 0; i < playerBoxes.length; i++) {
if (i > numberOfPlayers - 1) {
playerBoxes[i].classList.add("hidden");
}
}
}
function initPlayScreen() {
for (var i = 0; i < numberOfPlayers; i++) {
playerNameDisplays[i].textContent = players[i].name;
lifeTotalDisplays[i].textContent = players[i].lifeTotal;
playerBoxes[i].style.backgroundImage = "url(" + players[i].symbol + ")";
playerBoxes[i].style.backgroundSize = "contain";
playerBoxes[i].style.backgroundRepeat = "no-repeat";
playerBoxes[i].style.backgroundPosition = "center";
playerBoxes[i].style.backgroundColor = players[i].background;
}
if (numberOfPlayers === 2) {
playerBoxes[0].classList.add("playerBox2p","flipdiv");
playerBoxes[1].classList.add("playerBox2p");
lifeTotalDisplays[0].classList.add("pLife2P")
lifeTotalDisplays[1].classList.add("pLife2P")
} else if (numberOfPlayers === 3) {
playerBoxes[0].classList.add("playerBox3p");
playerBoxes[1].classList.add("playerBox3p","flipdiv");
playerBoxes[2].classList.add("playerBox3p");
playContainer.classList.add("player3Container");
lifeTotalDisplays[0].classList.add("pLife3P")
lifeTotalDisplays[1].classList.add("pLife3P")
lifeTotalDisplays[2].classList.add("pLife3P")
} else if (numberOfPlayers === 4) {
playerBoxes[0].classList.add("playerBox4p","flipdiv");
playerBoxes[1].classList.add("playerBox4p","flipdiv");
playerBoxes[2].classList.add("playerBox4p");
playerBoxes[3].classList.add("playerBox4p");
lifeTotalDisplays[0].classList.add("pLife4P")
lifeTotalDisplays[1].classList.add("pLife4P")
lifeTotalDisplays[2].classList.add("pLife4P")
lifeTotalDisplays[3].classList.add("pLife4P")
}
}
function changeLife(event, id) {
var x = event.clientX;
var ele = document.getElementById("boxP" + (id+1));
var rect = ele.getBoundingClientRect();
var half = ((rect.left + rect.right) / 2);
if (players[id].isAlive){
if (playerBoxes[id].classList.contains("flipdiv")) {
if(x < half) {
addLife(id);
}
else {
loseLife(id);
}
} else {
if(x > half) {
addLife(id);
}
else {
loseLife(id);
}
}
}
}
function addLife (i) {
flashColor(i, "green");
players[i].lifeTotal++;
lifeTotalDisplays[i].textContent = players[i].lifeTotal;
}
function loseLife (i) {
flashColor(i, "#800000");
players[i].lifeTotal--;
lifeTotalDisplays[i].textContent = players[i].lifeTotal;
checkIfDead(i);
}
function checkIfDead(i) {
if (players[i].lifeTotal <= 0) {
players[i].isAlive = false;
killPlayer(i);
}
}
function killPlayer(i) {
playerNameDisplays[i].textContent = players[i].name + " has died.";
zeroLifeOptions[i].classList.remove("hidden");
playerNameDisplays[i].style.color = "#fff";
playerBoxes[i].style.backgroundColor = "#222";
}
endGameBtns.forEach(function(player, i) {
endGameBtns[i].addEventListener("click", function(){
init();
});
});
resurrectButtons.forEach(function(player, i) {
resurrectButtons[i].addEventListener("click", function(){
revivePlayer(i);
});
});
function revivePlayer(i) {
addLife(i);
addLife(i); //have to do it twice because "resurrect" exists on left side, meaning clicking it ALSO triggers a loseLife()
players[i].isAlive = true;
playerNameDisplays[i].textContent = players[i].name;
zeroLifeOptions[i].classList.add("hidden");
playerNameDisplays[i].style.color = "#333";
playerBoxes[i].style.backgroundColor = players[i].background;
}
function flashColor (i, color) {
lifeTotalDisplays[i].style.color = color;
setTimeout(function(){
lifeTotalDisplays[i].style.color = "#333";
},40);
}