-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
312 lines (262 loc) · 7.96 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
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
/**
* dice-or-die!
* https://github.com/bkis/dice-or-die
* (MIT-license)
*/
//// GENERAL CONSTANTS
const imgDirPath = "img/";
const imgNamePrefix = "d";
const imgNameSuffix = ".png";
//// TEMPLATE ELEMENTS
const templateDie = $("#templates > .die").first();
//// ROLL ANIMATION (thanks to https://stackoverflow.com/a/15191130)
$.fn.animateRotate = function(angle, duration, easing, complete) {
var args = $.speed(duration, easing, complete);
var step = args.step;
return this.each(function(i, e) {
args.complete = $.proxy(args.complete, e);
args.step = function(now) {
$.style(e, 'transform', 'rotate(' + now + 'deg)');
if (step) return step.apply(e, arguments);
};
$({deg: 0}).animate({deg: angle}, args);
});
};
//// IMAGES AND IMAGE-PRELOADING
var images = {
files: {
d4: "d4.png",
d6: "d6.png",
d8: "d8.png",
d10: "d10.png",
d12: "d12.png",
d20: "d20.png",
d100: "d100.png",
iconSoundOn: "icon-sound-on.png",
iconSoundOff: "icon-sound-off.png",
iconGitHub: "icon-gh.png",
iconRoll: "icon-roll.png",
iconRemove: "icon-remove.png",
iconAdd: "icon-add.png"
},
path: function(id){ return imgDirPath + this.files[id]; },
allPaths: function(){ return Object.keys(this.files).map(k => this.path(k)); }
}
// define and call (!) preloading of images
var preLoadImages = function(imagesArray) {
$('<div id="loaded-images">').css('display','none').appendTo('body');
$(imagesArray).each(function () {
$('<img />').attr('src', this).appendTo('#loaded-images');
});
}(images.allPaths());
//// preps
var total = 0;
var currentRollId = "";
var sound = false;
var rollSound = document.createElement("audio");
rollSound.src = "sounds/roll.ogg";
rollSound.load();
// fnc to toggle sound on/off
function toggleSound(){
sound = !sound;
$("#sound-toggle").css(
"background-image",
sound
? "url(" + images.path("iconSoundOn") + ")"
: "url(" + images.path("iconSoundOff") + ")"
);
}
// fnc to play dice rolling sound
function playRollSound(){
if (sound){
rollSound.pause();
rollSound.currentTime = 0;
rollSound.play();
}
}
// fnc to reset dice's values to their maximum
// so the types are visually obvious...
function resetDieValues(){
$(".die").each(function(){
$(this).find(".rotatable > .die-value").first().text($(this).attr("data-type"));
});
}
// fnc to call when rolling all selected dice
function rollSelectedDice(die) {
// set last Roll ID
currentRollId = "roll_" + Date.now();
let thisRollId = currentRollId;
//play sound
playRollSound();
//reset total
total = 0;
$("#total-value").text("?").animateRotate(720, 1000, "swing");
//reset all dice to their natural max value
resetDieValues();
// roll this and all active dice
let toRoll = die ? (die.hasClass("active") ? $("#table .active").add(die) : die) : $("#table .active");
toRoll.each(function(){
let currentDie = $(this);
currentDie.find(".rotatable > .die-value").first().text("?");
currentDie.children(".rotatable").first().animateRotate(
720,
1000,
"swing",
function() {
if (thisRollId !== currentRollId) return;
let result = Math.floor(Math.random() * parseInt(currentDie.attr("data-type"))) + 1;
total += parseInt(result);
currentDie.find(".rotatable > .die-value").first().text(result);
$("#total-value").text(total);
}
);
});
}
// fnc to remove a die from table
function removeDie(die) {
// fade out and remove
die.hide(200, function(){
die.remove();
// if no dice left: hide "total"
if ($("#table .die").length == 0){
$("#total").fadeOut(200);
}
});
}
//// ADD DIE FUNCTION
function addDie(type) {
let die = templateDie.clone();
// set data attribute
die.attr("data-type", type);
// set image
die.find(".rotatable > .die-image").first().css(
"background-image",
"url(" + imgDirPath + imgNamePrefix + type + imgNameSuffix + ")"
);
// set initial value text
die.find(".rotatable > .die-value").first().text(type);
// button functionality: SELECT
die.click(function(e){
e.stopPropagation();
die.toggleClass("active");
});
// button functionality: ROLL
die.find(".die-buttons > .btn-roll").first().click(function(e){
e.stopPropagation();
rollSelectedDice(die);
});
// button functionality: REMOVE
die.find(".die-buttons > .btn-remove").first().click(function(e) {
e.stopPropagation();
removeDie(die);
});
// hide die buttons initially
die.children(".die-buttons").first().hide(0);
// show die buttons on mouse enter
die.mouseenter(function() {
die.children(".die-buttons").first().show(100);
});
// hide die buttons on mouse leave
die.mouseleave(function() {
die.children(".die-buttons").first().hide(100);
});
// add die to table
$("#table").append(die);
//show "total"
$("#total").show(0);
};
// fnc to check if all dice are selected
function allDiceSelected(){
let allDiceSelected = true;
$("#table .die").each(function(){
if (!$(this).hasClass("active")){
allDiceSelected = false;
return;
}
});
return allDiceSelected;
}
//// INITIALIZE UI
$(function() {
//// INIT UI
// init stock dice
$("#stock > .die-stock").each(function(i) {
// index
$(this).attr("data-index", i);
// title
$(this).attr(
"title",
"Add d" + $(this).attr("data-type") + " to the table"
);
// bg image
$(this).css(
"background-image",
"url(" +
imgDirPath +
imgNamePrefix +
$(this).attr("data-type") +
imgNameSuffix +
")"
);
// label
$(this).append($(this).attr("data-type"));
// click handler
$(this).click(function(e) {
e.stopPropagation();
addDie(parseInt($(this).attr("data-type")));
});
});
//hide "total" itself (as there are no dice on the table, yet)
$("#total").hide(0);
// EVENT: deselect all dice and hide help
$("html").click(function(e) {
e.stopPropagation();
$(".die").removeClass("active");
resetDieValues();
});
// EVENT: toggle sound
$("#sound-toggle").click(function(e){
e.stopPropagation();
toggleSound();
});
// EVENT: show help
$("#help-button").click(function(e){
e.stopPropagation();
$("#help").css("display", "flex");
});
// EVENT: hide help
$("#help").click(function(e){
e.stopPropagation();
$("#help").css("display", "none");
});
// EVENT: click total
$("#total").click(function(e){
e.stopPropagation();
// check if all dice are selected
let allSelected = allDiceSelected();
if (!allSelected){
$(".die").addClass("active");
} else {
$(".die").removeClass("active");
}
resetDieValues();
});
// EVENT: click total: roll selected
$("#total .btn-roll-selected").click(function(e){
e.stopPropagation();
if (!$("#table .die").hasClass("active")){
rollSelectedDice($("#table .die"));
} else {
rollSelectedDice($("#table .die.active"));
}
});
// EVENT: click total: remove selected
$("#total .btn-remove-selected").click(function(e){
e.stopPropagation();
if (!$("#table .die").hasClass("active")){
removeDie($("#table .die"));
} else {
removeDie($("#table .die.active"));
}
});
});