-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
142 lines (88 loc) · 3.14 KB
/
timer.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
;(function() {
'use strict';
var t;
function Timer(grid) {
t=this;
t.grid=grid;
t.currentInterval=t.grid.levels[1]['interval'];
t.eventCounterSpan=document.getElementById('js-event-counter');
}
Timer.prototype = {
// the current number of milliseconds between timer event
currentInterval:0,
// a counter for the number of events since game started
eventCount:0,
// a dom element for outputting event count (dev)
eventCounterSpan:{},
// stores the interval id for pausing (useful for dev)
intervalId:null,
// state, if running === true then the timer is running, else it's paused
running:true,
/**
* startTimer() - sets the timer running in response to a game start
*/
startTimer:function()
{
t.intervalId = setInterval(t.intervalTrigger,t.currentInterval);
t.running=true;
},
/**
* pauseTimer() - pause the timer
*/
pauseTimer:function()
{
window.clearInterval(t.intervalId);
t.running=false;
},
/**
* intervalTrigger() - function called each time an interval elapsed
*/
intervalTrigger:function()
{
var pieces=t.grid.pieces;
t.eventCount++;
t.eventCounterSpan.innerHTML=t.eventCount;
pieces[1].movePiece(t.grid.cells,'down',t.currentInterval);
if (true === pieces[1].stopped)
{
pieces[1].displayPiece(t.grid.cells);
t.grid.findCompletedRows(pieces[1].currentPosition);
pieces.unshift(new Piece());
pieces[0].displayPreviewPiece(t.grid.previewCells);
// stop game if new piece won't fit
var gameOver=false;
for (var index in pieces[1].currentPosition)
{
var coordinates=pieces[1].currentPosition[index];
if (1 === t.grid.cells[coordinates.y][coordinates.x].state)
{
gameOver=true;
break;
}
}
if (true === gameOver)
{
t.pauseTimer();
document.getElementById('js-form-score').value=t.grid.score;
document.getElementById('js-form-level').value=t.grid.level;
document.getElementById('js-form-background').style.zIndex=1;
}
}
// do levels
var levelData=t.grid.levels[t.grid.level];
if (t.eventCount>=levelData.threshold)
{
t.grid.level++;
// Neo-Nazis, f*k off
if (t.grid.level === 14) {
t.grid.level++;
}
t.grid.outputLevel();
t.pauseTimer();
t.currentInterval=levelData.interval;
t.startTimer()
}
}
};
window.Timer = Timer;
}());