-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pieces.js
292 lines (268 loc) · 5.94 KB
/
Pieces.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
function makePiece(piece)
{
switch(piece)
{
case 0: return new Grass();
case 1: return new Bush();
case 2: return new Tree();
case 3: return new Hut();
case 4: return new House();
case 5: return new Mansion();
case 6: return new Castle();
case 7: return new Floating_Castle();
case 8: return new Ultimate_Castle();
case 10: return new Soldier();
case 11: return new Helicopter();
case 12: return new Tombstone();
case 13: return new Church();
case 14: return new Cathedral();
case 20: return new SmallChest;
case 21: return new BigChest;
}
}
function drawPiece(image)
{
//return '<img src="image.php?letter='+image+'" style=position:absolute;top:'+border_width+'px;left:'+border_width+'px;width:'+innerWidth+'px;height:'+innerHeight+'px;>'
//return '<img src="letters/'+image+'.png" style=position:absolute;top:'+border_width+'px;left:'+border_width+'px;width:'+innerWidth+'px;height:'+innerHeight+'px;>'
return `<s class="sprite ${image}" style=position:absolute;top:${border_width}px;left:${border_width}px;transform:scale(${innerWidth/16})></s>`;
}
function Grass()
{
this.label = 0;
this.canCombine = true;
this.image = drawPiece('G');
this.combine_count = 3;
this.getCombined = function()
{
return 1;
}
}
function Bush()
{
this.label = 1;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('B');
this.getCombined = function()
{
return 2;
}
}
function Tree()
{
this.label = 2;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('T');
this.getCombined = function()
{
return 3;
}
}
function Hut()
{
this.label = 3;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('hh');
this.getCombined = function()
{
return 4;
}
}
function House()
{
this.label = 4;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('H');
this.getCombined = function()
{
return 5;
}
}
function Mansion()
{
this.label = 5;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('M');
this.getCombined = function()
{
return 6;
}
}
function Castle()
{
this.label = 6;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('C');
this.getCombined = function()
{
return 7;
}
}
function Floating_Castle()
{
this.label = 7;
this.canCombine = true;
this.combine_count = 4;
this.image = drawPiece('F');
this.getCombined = function()
{
return 8;
}
}
function Ultimate_Castle()
{
this.label = 8;
this.canCombine = false;
this.image = drawPiece('U');
}
function Soldier()
{
this.label = 10;
this.canCombine = false;
this.image = drawPiece('soldier');
this.move = function(cell, openSpots)
{
var dirX = Math.random() > 0.5 ? 1 : Math.random() > 0.5 ? -1 : 0;
var dirY = dirX == 0 ? (Math.random() > 0.5 ? 1 : -1) : 0;
if(board[cell.row+dirX] && board[cell.row+dirX][cell.col+dirY] && board[cell.row+dirX][cell.col+dirY].canMoveInto()) {
cell.moveCell(board[cell.row+dirX][cell.col+dirY]);
return;
}
dirX *= -1;
if(board[cell.row+dirX] && board[cell.row+dirX][cell.col+dirY] && board[cell.row+dirX][cell.col+dirY].canMoveInto()) {
cell.moveCell(board[cell.row+dirX][cell.col+dirY]);
return;
}
dirY = 1;dirX=0;
if(board[cell.row+dirX] && board[cell.row+dirX][cell.col+dirY] && board[cell.row+dirX][cell.col+dirY].canMoveInto()) {
cell.moveCell(board[cell.row+dirX][cell.col+dirY]);
return;
}
dirY = -1;
if(board[cell.row+dirX] && board[cell.row+dirX][cell.col+dirY] && board[cell.row+dirX][cell.col+dirY].canMoveInto()) {
cell.moveCell(board[cell.row+dirX][cell.col+dirY]);
return;
}
this.kill(cell);
}
this.kill = function(cell) {
cell.content = new Tombstone();
checkBoard(cell, new Tombstone());
cell.makeCell(cell.content);
cell.makeInnerCell();
//hack to allow all events to finish before redrawing board
setTimeout(function(){drawBoard();}, 100);
}
this.getCombined = function() {}
}
function Helicopter()
{
this.label = 11;
this.canCombine = false;
this.image = drawPiece('helicopter');
this.move = function(cell, openSpots)
{
//no open spots, game is over;
if(openSpots.length == 0)
return;
var position = parseInt(Math.random() * openSpots.length);
cell.moveCell(openSpots[position]);
}
this.kill = function(cell) {
cell.content = new Tombstone();
matches = new Array();
checkNeighbors(cell.row, cell.col, cell.content);
//check the number of matchs and see if it is >= the match necessary count
//add one to include the cell that we have added
if(matches.length >= cell.content.combine_count)
{
//remove all the matching tile
collapseMatches();
//if the matching piece is less then run recursive check the board
//for matches against the new tile
//clear out our checks
clearChecks();
cell.content = cell.content.getCombined();
}
}
this.getCombined = function()
{
}
}
function Tombstone()
{
this.label = 12;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('TS');
this.getCombined = function()
{
return 13;
}
}
function Church()
{
this.label = 13;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('sc');
this.getCombined = function()
{
return (14)
}
}
function Cathedral()
{
this.label = 14;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('CC');
this.getCombined = function()
{
return(20)
}
}
function smallChest()
{
this.label = 20;
this.image = drawPiece('small_chest');
this.canCombine = false;
}
function bigChest()
{
this.label = 21;
this.image = drawPiece('large_chest');
this.canCombine = false;
}
function Rock()
{
this.label = 15;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('rock');
this.getCombined = function()
{
return (15);
}
}
function Boulder()
{
this.label = 16;
this.canCombine = true;
this.combine_count = 3;
this.image = drawPiece('mountain');
this.getCombined = function()
{
return (21);
}
}
function Crystal()
{
this.label = 100;
this.image = drawPiece('∆');
}