-
Notifications
You must be signed in to change notification settings - Fork 1
/
snap-puzzle-game.ts
530 lines (518 loc) · 16.8 KB
/
snap-puzzle-game.ts
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**
* @module jqSnapPuzzle
*//** */
import {SnapPuzzleOptions} from "./snap-puzzle-options";
import {SnapPuzzlePiece} from "./snap-puzzle-piece";
import {
SnapPuzzleEvents,
SnapPuzzlePieceDropEvent
} from "./snap-puzzle-events";
import {SnapPuzzleSolveOptions} from "./snap-puzzle-solve-options";
/**
* Puzzle game
*/
export class SnapPuzzleGame{
/**
* Root element of the plugin
*/
protected element: JQuery;
/**
* Current options
*/
protected options: SnapPuzzleOptions;
/**
* Disabled state
*/
protected disabled: boolean;
/**
* Width of the image
*/
protected imageWidth:number;
/**
* Height of the image
*/
protected imageHeight:number;
/**
* Width for each piece
*/
protected pieceWidth:number;
/**
* Height for each piece
*/
protected pieceHeight:number;
/**
* Source of the image
*/
protected src:string;
/**
* Wrapper element for the widget.
*/
protected wrapperEl:JQuery;
/**
* All the SnapPuzzlePieces that conforms the puzzle
*/
protected pieces:SnapPuzzlePiece[];
/**
* All the SnapPuzzlePieces as matrix
*/
protected piecesMatrix:SnapPuzzlePiece[][];
/**
* Jquery object with all the slots
*/
protected slotsEls:JQuery;
/**
* Jquery object with all the pieces
*/
protected piecesEls:JQuery;
/**
* Jquery element that will contain all the slots
*/
protected slotsContainerEl:JQuery;
/**
* Jquery element that will contain all the pieces
*/
protected piecesContainerEl:JQuery;
/**
* Control of the resize event
*/
protected resizeTimeout:any;
/**
* Pending pieces
*/
protected pendingPieces:SnapPuzzlePiece[];
/**
* Destroy the component
*/
destroy(){
this._destroy();
//@ts-ignore
this._super();
}
/**
* Disable the widget
*/
disable() {
//@ts-ignore
this._super();
this.element.addClass(this.options.classes.disabled);
this.wrapperEl.addClass(this.options.classes.disabled);
for(let piece of this.pieces){
//@ts-ignore
piece.disable();
}
}
/**
* Enable the widget
*/
enable() {
//@ts-ignore
this._super();
this.element.removeClass(this.options.classes.disabled);
this.wrapperEl.removeClass(this.options.classes.disabled);
for(let piece of this.pieces){
//@ts-ignore
piece.enable();
}
}
/**
* Refresh the dimensions and positions of the pieces and slots
*/
refresh(){
this._resolveDimensions();
const pieces = this.pieces;
for(let piece of pieces){
piece.refresh();
}
}
/**
* Solve the puzzle. Multiple options are available
* @param {boolean | number} [resolveAllOrIndex] If is not provided, a random of the pending pieces will be resolved.
* @param {SnapPuzzleSolveOptions} [options] Options
* If true is provided, all the pieces will be resolved. If a number is provided, the piece with that index will be resolved
*/
solve(resolveAllOrIndex?:boolean|number,options?:SnapPuzzleSolveOptions){
if(resolveAllOrIndex === true){
for(let piece of this.pieces){
piece.solve();
}
}else if (typeof resolveAllOrIndex == "number"){
let itemToSolve = this.pieces[<number>resolveAllOrIndex];
if(itemToSolve){
itemToSolve.solve(options);
}
}else{
const random = Math.floor(Math.random()*this.pendingPieces.length),
itemToSolve = this.pendingPieces[random];
itemToSolve.solve(options);
}
}
/**
* Reset the puzzle reverting the pieces and resetting the progress.
* @emits [[SnapPuzzleEvents.reset]]
* @param [trigger=true] If true, the reset event will be triggered
*/
reset(trigger:boolean = true){
this.pendingPieces = this.pieces.concat([]);
this.wrapperEl.removeClass(this.options.classes.completed);
this.element.removeClass(this.options.classes.completed);
let pieces = this.pieces;
for(let piece of pieces){
piece.reset();
}
if(trigger) {
this.element.trigger(SnapPuzzleEvents.reset);
}
}
/**
* JQuery ui function to get the default options
* @protected
*/
protected _getCreateOptions() {
let options: SnapPuzzleOptions = {
columns:0,
rows:0,
namespace: "jq-snap-puzzle",
classes: {//css classes for elements
root: "c-snap-puzzle__image",
wrapper:"c-snap-puzzle",
completed:"c-snap-puzzle--completed",
disabled: "c-snap-puzzle--disabled",
piecesContainer:"c-snap-puzzle__pieces-container",
slotsContainer:"c-snap-puzzle__slots-container",
piece:"c-snap-puzzle__piece",
pieceIncorrect:"c-snap-puzzle__piece--incorrect",
pieceCorrect:"c-snap-puzzle__piece--correct",
pieceDisabled:"c-snap-puzzle__piece--disabled",
slot:"c-snap-puzzle__slot",
slotHasPiece:"c-snap-puzzle__slot--has-piece",
slotIncorrect:"c-snap-puzzle__slot--incorrect",
slotCorrect:"c-snap-puzzle__slot--correct",
slotDisabled:"c-snap-puzzle__slot--disabled",
feedbackOnHover:"c-snap-puzzle--feedback-on-hover",
backgroundInSlots:"c-snap-puzzle--slots-background"
},
onlyDropOnValid:false,
feedbackOnHover:true,
backgroundInSlots:true,
randomPieceStartPosition:true
};
return options;
}
/**
* Internal destroy.
* Only destroys the markup and events, the instance of the widget still remains.
* Used to recreate the widget
* @protected
*/
protected _destroy(){
$(window).off("."+this.options.namespace);
this.element.off("."+this.options.namespace);
this.element.removeClass([this.options.classes.disabled,this.options.classes.root,this.options.classes.completed]);
this.element.insertAfter(this.wrapperEl);
this.wrapperEl.remove();
}
/**
* Invoked by jquery widget
* @param options
* @protected
* @see [JQuery ui widget _setOptions](http://api.jqueryui.com/jQuery.widget/#method-_setOptions)
*/
protected _setOptions( options ) {
//Refresh the widget
let refresh = false,
//recreate the pieces
recreatePieces = false,
//complete recreate the puzzle
recreate = false;
for(let option in options){
switch (option) {
case "namespace":
case "classes":
recreate = true;
break;
case "rows":
case "columns":
recreatePieces = true;
break;
case "onlyDropOnValid":
case "feedbackOnHover":
case "backgroundInSlots":
case "randomPieceStartPosition":
refresh = true;
break;
}
}
if(recreate){
this._destroy();
//@ts-ignore
this._super(options);
this._create();
}else if(recreatePieces){
//@ts-ignore
this._super(options);
this.pieces = [];
this.reset(false);
this._construct();
}else if(refresh){
//@ts-ignore
this._super(options);
this._applyClassModifiers();
this.refresh();
}
}
/**
* Creates the container for pieces
* @returns {JQuery}
* @protected
*/
protected _createPiecesContainer():JQuery{
let result: JQuery;
if ((typeof this.options.createPiecesContainer).toLowerCase() == "function") {
result = this.options.createPiecesContainer.apply(this, arguments);
} else {
result = $(`<div class="${this.options.classes.piecesContainer}"></div>`);
}
return result;
}
/**
* Creates the container for slots
* @returns {JQuery}
* @protected
*/
protected _createSlotsContainer():JQuery{
let result: JQuery;
if ((typeof this.options.createSlotsContainer).toLowerCase() == "function") {
result = this.options.createSlotsContainer.apply(this, arguments);
} else {
result = $(`<div class="${this.options.classes.slotsContainer}"></div>`);
}
return result;
}
/**
* Creates the wrapper for the widget
* @returns {JQuery}
* @protected
*/
protected _createWrapper():JQuery{
let result: JQuery;
if ((typeof this.options.createWrapper).toLowerCase() == "function") {
result = this.options.createWrapper.apply(this, arguments);
} else {
result = $(`<div></div>`);
}
return result;
}
/**
* Creates each piece
* @param x
* @param y
* @returns {JQuery}
* @protected
*/
protected _createPiece(x,y):JQuery{
let result: JQuery;
if ((typeof this.options.createPiece).toLowerCase() == "function") {
result = this.options.createPiece.apply(this, arguments);
} else {
result = $(`<div class="${this.options.classes.piece}"></div>`);
}
return result;
}
/**
* Creates each slot
* @param x
* @param y
* @returns {JQuery}
* @protected
*/
protected _createSlot(x,y):JQuery{
let result: JQuery;
if ((typeof this.options.createSlot).toLowerCase() == "function") {
result = this.options.createSlot.apply(this, arguments);
} else {
result = $(`<div class="${this.options.classes.slot}"></div>`);
}
return result;
}
/**
* Initialize t he SnapPuzzlePiece for a piece and slot
* @param x
* @param y
* @returns {SnapPuzzlePiece}
* @protected
*/
protected _constructPiece(x,y):SnapPuzzlePiece{
let pieceEl = this._createPiece(x,y),
slotEl = this._createSlot(x,y).attr("data-x",x).attr("data-y",y),
piece = new SnapPuzzlePiece({
puzzle:this,
pieceEl:pieceEl,
slotEl:slotEl,
x:x,
y:y
});
return piece;
}
/**
* Apply the css classes for options
* @protected
*/
protected _applyClassModifiers(){
if(this.options.feedbackOnHover){
this.wrapperEl.addClass(this.options.classes.feedbackOnHover);
}else{
this.wrapperEl.removeClass(this.options.classes.feedbackOnHover);
}
if(this.options.backgroundInSlots){
this.wrapperEl.addClass(this.options.classes.backgroundInSlots);
}else{
this.wrapperEl.removeClass(this.options.classes.backgroundInSlots);
}
}
/**
* Creation of the widget
* @protected
*/
protected _create(){
if(this.element.is("img")){
let src = this.element.attr("src");
this.src = src;
this.element.addClass(this.options.classes.root);
this.wrapperEl = this._createWrapper();
this.wrapperEl.addClass(this.options.classes.wrapper);
this.piecesContainerEl = this.options.appendPiecesTo != undefined ? $(this.options.appendPiecesTo) : this._createPiecesContainer();
this.slotsContainerEl = this._createSlotsContainer();
if(this.options.appendPiecesTo == undefined){
this.wrapperEl.append(this.piecesContainerEl);
}
this.wrapperEl.append(this.slotsContainerEl);
this.wrapperEl.insertAfter(this.element);
this.slotsContainerEl.append(this.element);
this.pieces = [];
this.piecesMatrix = [];
this._resolveDimensions();
if((this.imageWidth != Infinity && this.imageWidth != 0) && (this.imageHeight != Infinity && this.imageHeight != 0)){
this._construct();
}else{
this.element.one("load",this._construct.bind(this));
}
this.element.off(this.options.namespace).on(SnapPuzzleEvents.pieceDrop+"."+this.options.namespace,this._onPieceDrop.bind(this));
}else{
throw "[SnapPuzzleGame] The widget must be initialized for <img> elements";
}
}
/**
* Invoked when the wrapper dimensions changes. Updates the pieces and slots
*/
protected _onResize(){
this.refresh();
}
/**
* Invoked when a piece is dropped inside of the pieces container. Resets the classes
* @param e
* @param ui
*/
protected _onDrop(e,ui){
let snapPiece:SnapPuzzlePiece = ui.draggable.data(SnapPuzzlePiece.DATA_KEY);
if(snapPiece && snapPiece.pieceDroppedInto){
snapPiece.pieceDroppedInto.pieceDropped = null;
snapPiece.pieceDroppedInto = null;
}
ui.draggable.removeClass([this.options.classes.pieceIncorrect,this.options.classes.pieceCorrect]);
}
protected _resolveDimensions(){
this.imageWidth = (this.element.width() || this.options.width || 0);
this.imageHeight = (this.element.height() || this.options.height || 0);
this.pieceWidth = this.imageWidth/this.options.columns;
this.pieceHeight = this.imageHeight/this.options.rows;
}
/**
* Construct the puzzle
* @private
*/
protected _construct(){
//wait for image to load
let rows = this.options.rows,
columns = this.options.columns,
piecesEls = [],
slotsEls = [],
numPieces = columns*rows;
this._resolveDimensions();
this._applyClassModifiers();
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
let piecesRow = [];
for(let columIndex = 0; columIndex < columns; columIndex++){
let piece = this._constructPiece(columIndex,rowIndex);
slotsEls.push(piece.slotEl.get(0));
piecesEls.push(piece.pieceEl.get(0));
piecesRow.push(piece);
this.pieces.push(piece);
piece.slotEl.appendTo(this.slotsContainerEl);
}
this.piecesMatrix[rowIndex]=piecesRow;
}
if(this.slotsEls){
this.slotsEls.remove();
this.piecesEls.remove();
}
this.slotsEls = $(slotsEls);
this.piecesEls = $(piecesEls);
this.pendingPieces = this.pieces.concat([]);
//shuffle
const randomPieces = this.pieces.concat([]);
randomPieces.sort(()=>Math.floor(Math.random() * numPieces));
//append
for(let piece of randomPieces){
piece.pieceEl.appendTo(this.piecesContainerEl);
}
this.piecesContainerEl.droppable({
//@ts-ignore
accept: "."+this.options.classes.piece,
drop:this._onDrop.bind(this)
});
if(this.options.hints && this.options.hints > 0){
for(let i = 0; i< this.options.hints; i++) {
this.solve(null,{emitChange:false,animate:false})
}
}
this._throttleResize();
}
/**
* Invoked when a piece is placed.
* If the piece is correct, increment the completed counter.
* When all the pieces are placed correctly, triggers the [[SnapPuzzleEvents.end]] event
* @param e
* @param {SnapPuzzlePieceDropEvent} data
* @protected
*/
protected _onPieceDrop(e,data:SnapPuzzlePieceDropEvent){
if(data.isCorrect){
const index = this.pendingPieces.indexOf(data.piece);
if(index != -1){
this.pendingPieces.splice(index,1);
}
}
if(this.pendingPieces.length == 0){
this._complete();
}
}
/**
* Mark the widget as completed
* Triggers the [[SnapPuzzleEvents.end]] event
* @private
*/
protected _complete(){
this.wrapperEl.addClass(this.options.classes.completed);
this.element.addClass(this.options.classes.completed);
this.element.trigger(SnapPuzzleEvents.complete,this);
}
protected _onNativeResize(){
if(this.resizeTimeout){
clearTimeout(this.resizeTimeout);
}
this.resizeTimeout = setTimeout(this._onResize.bind(this),200);
}
protected _throttleResize () {
$(window).off("."+this.options.namespace).on(`resize.${this.options.namespace}`, this._onNativeResize.bind(this));
};
}