-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
168 lines (166 loc) · 4.89 KB
/
controller.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
var controllerFunction = function () {
var width = 10;
var height = 20;
var baseTimestep = 500;
var timestep = baseTimestep + 0;
var myGrid = grid(width, height);
var hasReleasedDownForNewShape = false;
var isHoldingDown = false;
var mySound = new Audio(['https://ia800504.us.archive.org/33/items/TetrisThemeMusic/Tetris.mp3']);
mySound.addEventListener('ended', function() {
this.currentTime = 0;
this.play();
}, false);
var initializeTable = function(){
var table = $("#gridTable");
table.empty();
var rowNum;
var colNum;
for (rowNum = 0; rowNum < height; rowNum++){
var rowEntry = $('<tr>');
for (colNum = 0 ; colNum < width; colNum++){
var entry;
var name = ""+colNum+"x"+rowNum;
entry = $('<td class="emptyCell">');
entry.attr("name",name);
rowEntry.append(entry);
}
table.append(rowEntry);
}
}
var slide = false;
var normalDropInterval;
var sideSpeedFactor = 5;
var downSpeedFactor = 20;
var fallingShapeInfo;
var shapeJustChanged;
var sideKeyDown;
var didRotate;
var fallingShape;
var intervalAction = function(){
console.log(timestep);
if (myGrid.gameOver()){
clearInterval(normalDropInterval);
}
if (myGrid.getLevelIncreased() === true){
timestep = timestep - 100 > 0 ? timestep - 100 : timestep;
changeSpeed(timestep);
myGrid.setLevelIncreased(false);
}
sideKeyDown = false;
didRotate = false;
fallingShapeInfo = myGrid.getFallingShape();
fallingShape = fallingShapeInfo[0];
shapeJustChanged = fallingShapeInfo[1];
if (shapeJustChanged == true && isHoldingDown == true){
hasReleasedDownForNewShape = false;
clearInterval(slide);
}
$(document).keydown(function(e) {
if (e.keyCode == 39 && !sideKeyDown){
sideKeyDown = true;
fallingShape.rightShape();
if (slide == false){
slide = setInterval(function(){
fallingShape.rightShape();
},baseTimestep/sideSpeedFactor);
}
}
// left
else if (e.keyCode == 37 && !sideKeyDown){
sideKeyDown = true;
fallingShape.leftShape();
if (slide == false){
slide = setInterval(function(){
fallingShape.leftShape();
},baseTimestep/sideSpeedFactor);
}
}
// slam down
else if (e.keyCode == 40 && hasReleasedDownForNewShape == true){
isHoldingDown = true;
if (slide == false){
slide = setInterval(function(){
fallingShape.fallShape(downSpeedFactor);
},timestep/downSpeedFactor);
}
// up key / rotate
} else if (e.keyCode == 38 && !didRotate){
didRotate = true;
fallingShape.rotateShape();
}
});
$(document).keyup(function(e) {
clearInterval(slide);
slide = false;
sideKeyDown = false;
didRotate = false;
hasReleasedDownForNewShape = true;
isHoldingDown = false;
});
if (sideKeyDown == false){
fallingShape.fallShape(1);
}
}
var startOver = function(){
myGrid.resetScore();
myGrid.resetLevel();
$("#level").text("Level 1");
$("#score").text("Score: 0");
clearInterval(normalDropInterval);
myGrid.resetFallingShape();
initializeTable();
console.log("base : "+baseTimestep);
timestep = baseTimestep + 0;
normalDropInterval = setInterval(intervalAction, baseTimestep);
mySound.load();
if ($('#mute').attr("state") === "on"){
mySound.play();
}
}
startOver();
$('#restart').click(function(){
startOver();
});
var keyDown = false;
var changeSpeed = function(newTimestep){
clearInterval(normalDropInterval);
normalDropInterval = setInterval(intervalAction, newTimestep);
mySound.playbackRate = 1.1;
}
$('#mute').click(function(){
console.log($('#mute').attr("state"));
//turn on
if ($('#mute').attr("state") === "off"){
console.log("turning on");
mySound.play();
mySound.muted = false;
$('#mute').attr("state","on");
$("#mute").attr("src","https://upload.wikimedia.org/wikipedia/commons/3/3f/Mute_Icon.svg");
} else {
//turn off
console.log("turning off");
mySound.muted = true;
$('#mute').attr("state","off");
$("#mute").attr("src","https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Speaker_Icon.svg/2000px-Speaker_Icon.svg.png");
}
});
$(document).keydown(function(e) {
if (e.keyCode == 27 && keyDown === false){
console.log(normalDropInterval);
if (normalDropInterval !== false){
clearInterval(normalDropInterval);
normalDropInterval = false;
} else if (normalDropInterval === false){
changeSpeed(timestep);
}
keyDown = true;
}
});
$(document).keyup(function(e) {
// console.log("hi");
if (e.keyCode == 27 && keyDown === true){
keyDown = false;
}
});
}