This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.js
More file actions
128 lines (86 loc) · 4.19 KB
/
Copy pathMain.js
File metadata and controls
128 lines (86 loc) · 4.19 KB
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
Game.Main = function (game) {
};
Game.Main.prototype = {
create: function () {
this.piecesleft = 9;
//this.game.physics.startSystem(Phaser.Physics.ARCADE);
// Add graphics
this.bg = this.add.sprite(0, 0, 'bg');
//this.interiorTitle = this.add.sprite(40, 8, 'interiorTitle');
this.backButton = this.add.button(568, 19, 'backButton', this.backToMenu, this, 1, 0, 1);
this.backButton.input.useHandCursor = true;
this.bg_dark = this.add.sprite(0, 72, 'bg_dark');
this.base = this.game.add.sprite(368, 121, 'base');
// Add Draggable Pieces
// Initial position is hard coded,
// it would be nice to disposed them randomly :)
this.piece1 = this.game.add.sprite(24, 269, 'piece1');
this.piece2 = this.game.add.sprite(244, 149, 'piece2');
this.piece3 = this.game.add.sprite(161, 115, 'piece3');
this.piece4 = this.game.add.sprite(27, 104, 'piece4');
this.piece5 = this.game.add.sprite(70, 161, 'piece5');
this.piece6 = this.game.add.sprite(203, 291, 'piece6');
this.piece7 = this.game.add.sprite(203, 201, 'piece7');
this.piece8 = this.game.add.sprite(154, 249, 'piece8');
this.piece9 = this.game.add.sprite(58, 331, 'piece9');
// Pieces final position (Hard coded)
this.piece1.finalPositionX = 368;
this.piece1.finalPositionY = 121;
this.piece2.finalPositionX = 465;
this.piece2.finalPositionY = 121;
this.piece3.finalPositionX = 538;
this.piece3.finalPositionY = 121;
this.piece4.finalPositionX = 368;
this.piece4.finalPositionY = 218;
this.piece5.finalPositionX = 440;
this.piece5.finalPositionY = 218;
this.piece6.finalPositionX = 561;
this.piece6.finalPositionY = 193;
this.piece7.finalPositionX = 368;
this.piece7.finalPositionY = 314;
this.piece8.finalPositionX = 465;
this.piece8.finalPositionY = 293;
this.piece9.finalPositionX = 535;
this.piece9.finalPositionY = 314;
// Remember the original position
for (var i = 1; i <= 9; i++) {
this['piece' + i].originX = this['piece' + i].x;
this['piece' + i].inputEnabled = true;
this['piece' + i].input.enableDrag(false, true);
this['piece' + i].events.onDragStop.add(this.pieceDragStop, this);
};
},
// When we release a piece do this...
pieceDragStop: function(item) {
// Calculate the distance between the piece and its final spot
this.totalDistance = this.game.physics.arcade.distanceToXY( item , item.finalPositionX, item.finalPositionY);
// If the distance is minor than 50 pixels the piece is placed in its final spot,
// otherwise the piece return to its original position
if (this.totalDistance < 50) {
this.game.add.tween(item).to({x: item.finalPositionX, y: item.finalPositionY }, 500, Phaser.Easing.Back.Out, true);
// This piece is not draggable anymore
item.inputEnabled = false;
// Pieces to finish minus one
this.piecesleft--;
// If all the pieces are assembled the puzzle is finished :)
if (this.piecesleft === 0) {
// Show a greeting message
this.basecolor = this.game.add.sprite(368, 121, 'basecolor');
this.welldone = this.game.add.sprite(179.5, 251.5, 'welldone');
this.welldone.anchor.setTo(0.5, 0.5);
this.game.add.tween(this.welldone.scale).from({x: 0, y: 0 }, 500, Phaser.Easing.Back.Out, true);
// Play the final animation
this.animationWin = this.game.add.sprite(366, 119, 'jigsawWin_anim');
this.animationWin.animations.add('win');
this.animationWin.animations.play('win', 24, true);
};
} else {
this.game.add.tween(item).to({x: item.originX, y: item.originY }, 500, Phaser.Easing.Back.Out, true);
};
},
update: function () {
},
backToMenu: function (pointer) {
this.state.start('MainMenu');
}
};