-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaper.js
117 lines (116 loc) · 2.79 KB
/
shaper.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
var shaper = function(gridWidth, gridHeight, xys, grid){
var that = Object.create(shaper.prototype);
var xyList = [];
xys.forEach(function(e){
xyList.push(e.slice(0));
});
var isFalling = true;
var shouldSettle = false;
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){
$(that.getCellName(x,y)).attr("class","fullCell");
}
that.emptyCell = function(x, y){
$(that.getCellName(x,y)).attr("class","emptyCell");
}
that.fillFallCell = function(x, y){
$(that.getCellName(x,y)).attr("class","fallCell");
}
that.checkCellFull = function(x, y){
return ($(that.getCellName(x,y)).attr("class") == "fullCell");
}
that.fallShape = function(){
if (canFall()){
that.emptyShape();
xyList.forEach(function(xy){
xy[1]+=1;
that.fillFallCell(xy[0], xy[1]);
});
} else {
xyList.forEach(function(xy){
that.fillFallCell(xy[0], xy[1]);
if (shouldSettle == false){
shouldSettle = true;
} else {
isFalling = false;
shouldSettle = false;
}
});
}
}
that.rightShape = function(){
if (canMoveRight()){
that.emptyShape();
xyList.forEach(function(xy){
xy[0]+=1;
that.fillFallCell(xy[0], xy[1]);
});
}
}
that.leftShape = function(){
if (canMoveLeft()){
that.emptyShape();
xyList.forEach(function(xy){
xy[0]-=1;
that.fillFallCell(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.fillFallShape = function(){
xyList.forEach(function(xy){
that.fillFallCell(xy[0],xy[1]);
});
}
var bool = true;
var canMoveRight = function(){
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(){
bool = true;
xyList.forEach(function(xy){
if (xy[0] - 1 < 0 || that.checkCellFull(xy[0]-1,xy[1])){
bool = false;
}
});
return bool;
}
var canFall = function(){
bool = true;
var floorHeight = grid.getFloorHeight();
xyList.forEach(function(xy){
console.log(that.checkCellFull(xy[0],xy[1]+1));
if (xy[1] >= gridHeight - 1 || that.checkCellFull(xy[0],xy[1]+1)){
console.log("STOP FALLING");
bool = false;
}
});
return bool;
}
Object.freeze(that);
return that;
}