-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape.js
189 lines (186 loc) · 4.77 KB
/
shape.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
var shape = function(gridWidth, gridHeight, xys, grid, color, pivot){
var that = Object.create(shape.prototype);
var xyList = [];
xys.forEach(function(e){
xyList.push(e.slice(0));
});
var isFalling = true;
var shouldSettle = false;
var cantFallCount = 0;
var originalStopSpeedFactor;
that.isFalling = function(){
return isFalling;
}
that.getXyList = function(){
return xyList;
}
that.getCellName = function(x, y){
var newCellName = ""+x+"x"+y;
return "[name="+newCellName+"]";
}
that.fillCell = function(x, y){
var name = that.getCellName(x,y)
$(name).attr("class","fullCell");
}
that.emptyCell = function(x, y){
$(that.getCellName(x,y)).attr("class","emptyCell");
$('.emptyCell').css({"background-color":"skyblue"});
}
var fallCell = function(x, y){
var cell = $(that.getCellName(x,y));
cell.attr("class","fallCell");
cell.css("background-color",color);
}
that.checkCellFull = function(x, y){
return ($(that.getCellName(x,y)).attr("class") == "fullCell");
}
that.fallShape = function(speedFactor){
if (that.canFall(speedFactor)){
that.emptyShape();
xyList.forEach(function(xy){
xy[1]+=1;
fallCell(xy[0], xy[1]);
});
}
}
that.rightShape = function(){
if (canMoveRight()){
that.emptyShape();
xyList.forEach(function(xy){
xy[0]+=1;
fallCell(xy[0], xy[1]);
});
}
}
that.leftShape = function(){
if (canMoveLeft()){
that.emptyShape();
xyList.forEach(function(xy){
xy[0]-=1;
fallCell(xy[0], xy[1]);
});
}
}
that.rotateShape = function(){
if (pivot == undefined){
return;
}
var canRotateResult = canRotate();
if (canRotateResult === false){
return;
}
bumpLeft = canRotateResult[0];
bumpRight = canRotateResult[1];
that.emptyShape();
var pivotXY = xyList[pivot].slice(0);
xyList.forEach(function(xy){
xy[0] = xy[0] - pivotXY[0];
xy[1] = xy[1] - pivotXY[1];
//x,y = y,-x
var newx = xy[1];
var newy = -1*xy[0]
xy[0] = newx;
xy[1] = newy;
xy[0] = xy[0] + pivotXY[0] - bumpLeft + bumpRight;
xy[1] = xy[1] + pivotXY[1];
fallCell(xy[0], xy[1]);
});
}
that.emptyShape = function(){
xyList.forEach(function(xy){
that.emptyCell(xy[0],xy[1]);
});
}
that.fillShape = function(){
xyList.forEach(function(xy){
that.fillCell(xy[0],xy[1]);
});
}
that.canExist = function(){
var bool = true;
xyList.forEach(function(xy){
if (that.checkCellFull(xy[0]+1,xy[1])){
bool = false;
}
});
return bool;
}
var canMoveRight = function(){
var bool = true;
xyList.forEach(function(xy){
if (xy[0] + 1 >= gridWidth || that.checkCellFull(xy[0]+1,xy[1])){
bool = false;
}
});
return bool;
}
var canMoveLeft = function(){
var bool = true;
xyList.forEach(function(xy){
if (xy[0] - 1 < 0 || that.checkCellFull(xy[0]-1,xy[1])){
bool = false;
}
});
return bool;
}
that.canFall = function(speedFactor){
if (speedFactor > 1){
speedFactor = 10;
}
var bool = true;
xyList.forEach(function(xy){
if (that.checkCellFull(xy[0],xy[1]+1) || xy[1] >= gridHeight - 1){
bool = false;
}
});
cantFallCount = bool == false ? cantFallCount + 1 : 0;
if (cantFallCount == 1){
originalStopSpeedFactor = speedFactor;
}
if (cantFallCount>originalStopSpeedFactor){
isFalling = false;
} else {
isFalling = true;
}
return bool;
}
var canRotate = function(){
var bool = true;
var pivotXY = xyList[pivot].slice(0);
var maxBumpLeft = 0;
var maxBumpRight = 0;
var newXYList = [];
var preX; var preY; var newX; var newY;
xyList.forEach(function(xy){
preX = xy[0] - pivotXY[0];
preY = xy[1] - pivotXY[1];
newX = preY;
newY = -1*preX;
newX = newX + pivotXY[0];
newY = newY + pivotXY[1];
newXYList.push([newX,newY]);
//if its gonna hit other blocks or go above
if (newY >= gridHeight || that.checkCellFull(newX,newY)){
bool = false;
}
//if x is too right, needs to move left
else if (newX >= gridWidth){
maxBumpLeft = Math.max(maxBumpLeft, newX - gridWidth + 1);
}
//if x is too left, needs to move right
else if (newX < 0){
maxBumpRight = Math.max(maxBumpRight, 0 - newX);
}
});
if (maxBumpRight>0 || maxBumpLeft>0){
newXYList.forEach(function(xy){
if (that.checkCellFull(xy[0]+maxBumpRight-maxBumpLeft,xy[1]) == true){
bool = false;
}
});
}
return bool == false ? bool : [maxBumpLeft, maxBumpRight];
}
Object.freeze(that);
return that;
}