-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
304 lines (278 loc) · 9.33 KB
/
app.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
var row = 2
var col = 2
var pieceHeight = 200
var pieceWidth = 200
var width = 400
var height = 400
var dropPices = []
topDr = 10
leftDr = 10
var zIndex = 1
var posValues = []
var filledValues = []
for (let i = 0; i < col * row; i++) {
posValues.push(false)
filledValues.push(false)
dropPices[i] = document.createElement('div')
dropPices[i].classList.add('dopable')
document.body.appendChild(dropPices[i])
dropPices[i].style.top = topDr + 'px'
dropPices[i].style.left = leftDr + 'px'
dropPices[i].style.width = pieceWidth + 'px'
dropPices[i].style.height = pieceHeight + 'px'
topDr += pieceHeight
if (topDr === height + 10) {
topDr = 10
leftDr += pieceWidth
}
}
var x = 0
var y = 0
var pices = []
for (let i = 0; i < col * row; i++) {
pices[i] = document.createElement('div')
pices[i].classList.add('draggable')
document.body.appendChild(pices[i])
pices[i].style.zIndex = zIndex
pices[i].style.backgroundPosition = x + 'px ' + y + 'px'
pices[i].style.width = pieceWidth + 'px'
pices[i].style.height = pieceHeight + 'px'
y -= pieceHeight
if (y === -height) {
y = 0
x -= pieceWidth
}
}
x = width + 20
y = 10
for (let i = 0; i < col * row; i++) {
var ran = Math.floor(Math.random() * col * row)
while (posValues[ran]) {
ran++
if (ran === col * row) {
ran = 0
}
}
posValues[ran] = true
pices[ran].style.top = y + 'px'
pices[ran].style.left = x + 'px'
y += pieceHeight + 10
if (y === 10 + row * (pieceHeight + 10)) {
y = 10
x += pieceWidth + 10
}
}
var selectedPice
var mousePosX
var offSetX
var mousePosY
var offSetY
pices.forEach(pice =>
pice.addEventListener('mousedown', function (e) {
selectedPice = e.target
mousePosX = e.offsetX
mousePosY = e.offsetY
offSetX = e.target.offsetLeft
offSetY = e.target.offsetTop
zIndex = zIndex + 1
e.target.style.zIndex = zIndex
if (
e.pageX > 10 &&
e.pageY > 10 &&
e.pageX < width + 10 &&
e.pageY < height + 10
) {
// find the right place for element in which cell its gonna be by calc the top/left within the droppable area
var left = Math.floor((e.pageX - 10) / pieceWidth)
var top = Math.floor((e.pageY - 10) / pieceHeight)
filledValues[left * row + top] = false
}
})
)
document.body.addEventListener('mousemove', function moveing (e) {
if (typeof selectedPice !== 'undefined') {
selectedPice.style.left = e.pageX - mousePosX + 'px'
selectedPice.style.top = e.pageY - mousePosY + 'px'
}
})
document.body.addEventListener('mouseup', function stopMoving (e) {
if (typeof selectedPice !== 'undefined') {
// checkimg the dragged element if it in the scoupe of the dropalble area
if (
e.pageX > 10 &&
e.pageY > 10 &&
e.pageX < width + 10 &&
e.pageY < height + 10
) {
// find the right place for element in which cell its gonna be by calc the top/left within the droppable area
var left = Math.floor((e.pageX - 10) / pieceWidth)
var top = Math.floor((e.pageY - 10) / pieceHeight)
selectedPice.style.left = left * pieceWidth + 10 + 'px'
selectedPice.style.top = top * pieceHeight + 10 + 'px'
if (filledValues[left * row + top]) {
selectedPice.style.left = offSetX + 'px'
selectedPice.style.top = offSetY + 'px'
} else {
filledValues[left * row + top] = true
if (filledValues.every(value => value)) {
var rightTop = 10 - pieceHeight
var rightLeft = 10
var win = pices.every(pice => {
rightTop += pieceHeight
if (rightTop === height + 10) {
rightTop = 10
rightLeft += pieceWidth
}
return pice.offsetLeft === rightLeft && pice.offsetTop === rightTop
})
if (win) {
/// ---------------------------------- winner ending -----------------------------
var resolver = {
resolve: function resolve (options, callback) {
// The string to resolve
var resolveString =
options.resolveString ||
options.element.getAttribute('data-target-resolver')
var combinedOptions = Object.assign({}, options, {
resolveString: resolveString
})
function getRandomInteger (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function randomCharacter (characters) {
return characters[getRandomInteger(0, characters.length - 1)]
}
function doRandomiserEffect (options, callback) {
var characters = options.characters
var timeout = options.timeout
var element = options.element
var partialString = options.partialString
var iterations = options.iterations
setTimeout(function () {
if (iterations >= 0) {
var nextOptions = Object.assign({}, options, {
iterations: iterations - 1
})
// Ensures partialString without the random character as the final state.
if (iterations === 0) {
element.textContent = partialString
} else {
// Replaces the last character of partialString with a random character
element.textContent =
partialString.substring(0, partialString.length - 1) +
randomCharacter(characters)
}
doRandomiserEffect(nextOptions, callback)
} else if (typeof callback === 'function') {
callback()
}
}, options.timeout)
}
function doResolverEffect (options, callback) {
var resolveString = options.resolveString
var characters = options.characters
var offset = options.offset
var partialString = resolveString.substring(0, offset)
var combinedOptions = Object.assign({}, options, {
partialString: partialString
})
doRandomiserEffect(combinedOptions, function () {
var nextOptions = Object.assign({}, options, {
offset: offset + 1
})
if (offset <= resolveString.length) {
doResolverEffect(nextOptions, callback)
} else if (typeof callback === 'function') {
callback()
}
})
}
doResolverEffect(combinedOptions, callback)
}
/* Some GLaDOS quotes from Portal 2 chapter 9: The Part Where He Kills You
* Source: http://theportalwiki.com/wiki/GLaDOS_voice_lines#Chapter_9:_The_Part_Where_He_Kills_You
*/
}
var strings = [
'Oh thank god, you Win It Man.',
'wtf really man',
'ok wait my next game Ha3....',
'......'
]
var counter = 0
var options = {
// Initial position
offset: 0,
// Timeout between each random character
timeout: 5,
// Number of random characters to show
iterations: 10,
// Random characters to pick from
characters: [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'x',
'y',
'x',
'#',
'%',
'&',
'-',
'+',
'_',
'?',
'/',
'\\',
'='
],
// String to resolve
resolveString: strings[counter],
// The element
element: document.querySelector('[data-target-resolver]')
// Callback function when resolve completes
}
function callback () {
setTimeout(function () {
counter++
if (counter >= strings.length) {
counter = 0
}
var nextOptions = Object.assign({}, options, {
resolveString: strings[counter]
})
resolver.resolve(nextOptions, callback)
}, 1000)
}
resolver.resolve(options, callback)
/// //////////////////////////////////////////////////////-------------------------------------
}
}
}
}
selectedPice = undefined
}
})
var newGame = document.getElementById('new')
newGame.onclick = function () {
document.location.reload()
}