forked from deadbeef84/tetrinet
/
playerview.js
261 lines (243 loc) · 8.01 KB
/
playerview.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
function PlayerView(player) {
this.player = player;
this.isPlayer = (this.player instanceof Player);
this.notifierSlots = [];
this.el = $(
'<div class="player">'+
'<h2>Player</h2>'+
'<div class="board"></div>'+
'<div class="nextpiece"></div>'+
'<div class="holdpiece"></div>'+
'<div class="inventory"></div>'+
'</div>').appendTo('#gamearea');
this.render();
var self = this;
this.player.on(Board.EVENT_UPDATE, function() {
self.render();
});
this.player.on(Board.EVENT_CHANGE, function() {
self.render();
});
this.player.on(Player.EVENT_INVENTORY, function() {
self.renderInventory();
});
this.player.on(Board.EVENT_REMOVE_LINE, function(y) {
self.removeLine(y);
});
this.player.on(Player.EVENT_SPECIAL, function(msg) {
switch(msg.s) {
case Special.NUKE: self.specialNuke(); break;
case Special.QUAKE: self.specialQuake(); break;
case Special.BOMB: self.specialBomb(); break;
case Special.MOSES: self.specialMoses(); break;
case Special.ZEBRA: self.specialZebra(); break;
case Special.CLEAR_SPECIALS: self.specialClearSpecials(); break;
}
});
this.player.on(Player.EVENT_NOTIFY, function(msg) {
var offset = 0;
var $msg = $(msg);
for (var i = 0; self.notifierSlots[i]; i++, offset++) ;
self.notifierSlots[offset] = true;
$msg.data('offset', offset);
$msg.css('top', offset * 50);
setTimeout(function(obj){
obj.animate({'opacity':0}, 500, function(){
self.notifierSlots[obj.data('offset')] = false;
obj.remove();
});
}, 2000, $msg);
self.el.append($msg);
});
}
PlayerView.prototype.render = function() {
var html = '';
var x, y, b;
// update ghost block
if(this.isPlayer)
this.player.updateGhostBlock();
// update board
for(y = 0; y < this.player.height; ++y) {
html += '<div class="row">';
for(x = 0; x < this.player.width; ++x) {
b = this.player.at(x,y);
html += '<div class="cell '+(b !== 0 ? (typeof b === 'string' ? 'special special-'+b : 'block block-'+b) : 'empty')+'"> </div>';
}
html += '</div>';
}
this.el.find('.board').html('<div class="board-wrapper">'+html+'</div>');
if(!this.isPlayer)
return;
// update preview (next) blocks
if(this.player.nextBlocks && this.player.nextBlocks.length) {
html = '';
for(var i = 0; i < this.player.options.nextpiece; i++) {
var bp = {};
var nextBlock = this.player.nextBlocks[i];
for(x = 0; x < nextBlock.data.length; ++x)
bp[nextBlock.data[x][0]+'_'+nextBlock.data[x][1]] = nextBlock.type + 1;
for(y = 0; y < 2; ++y) {
html += '<div class="row">';
for(x = 0; x < 4; ++x) {
b = bp[x+'_'+y];
html += '<div class="cell '+(b ? 'block block-'+(i>0?'8':b) : 'empty')+'"> </div>';
}
html += '</div>';
}
if (i < this.player.options.nextpiece - 1)
html += '<div class="row empty"/>';
}
this.el.find('.nextpiece').html('<div>'+html+'</div>');
}
// Update hold block
if (this.player.holdBlock) {
html = '';
var bp = {};
for(x = 0; x < this.player.holdBlock.data.length; ++x)
bp[this.player.holdBlock.data[x][0]+'_'+this.player.holdBlock.data[x][1]] = this.player.holdBlock.type + 1;
for(y = 0; y < 2; ++y) {
html += '<div class="row">';
for(x = 0; x < 4; ++x) {
b = bp[x+'_'+y];
html += '<div class="cell '+(b ? 'block block-'+b : 'empty')+'"> </div>';
}
html += '</div>';
}
this.el.find('.holdpiece').html('<div>'+html+'</div>');
} else {
this.el.find('.holdpiece').empty();
}
}
PlayerView.prototype.renderInventory = function() {
var inv = this.player.inventory;
var html = '<div class="row">';
for(var i = 0; inv && i < inv.length; ++i) {
var b = inv[i];
html += '<div class="cell '+(b !== 0 ? (typeof b === 'string' ? 'special special-'+b : 'block block-'+b) : 'empty')+'"> </div>';
}
html += '</div>';
if(inv.length)
html += "<p>" + Special.getSpecial(inv[0]).name + "</p>";
this.el.find('.inventory').html(html);
}
PlayerView.prototype.removeLine = function(y) {
var $original = this.el.find('.board .row').eq(y);
var $clear = $original.clone()
.appendTo(this.el)
.css({position: 'absolute'})
.offset($original.offset())
.animate({top: '+=20px', opacity: 0}, 250, function() { $clear.remove(); });
}
PlayerView.prototype.specialNuke = function() {
var $board = this.el.find('.board');
$board.addClass('nuke');
var xpos = Math.round((480 - $board.width()) * 0.5)-20;
$board.css({
'background': "black -"+xpos+"px bottom no-repeat url('../images/nuke.gif?" + Date.now() + "')"
});
setTimeout(function() {
$board.removeClass('nuke');
$board.css({
'background': 'transparent'
});
}, 2000);
}
PlayerView.prototype.specialQuake = function() {
var count = 20;
var board = this.el.find('.board');
var shakeFunction = function(){
if (count--) {
board.css({
'margin-left': Math.round(Math.random()*(count&1?-1:1)*30),
'margin-top': Math.round((Math.random()-0.5)*30)
//'-webkit-transform': 'rotate(' + Math.round((Math.random()-0.5)*40) + 'deg)'
});
setTimeout(shakeFunction, 50);
}
else {
board.css({
'margin-left': 0,
'margin-top': 0
//'-webkit-transform': 'rotate(0deg)'
});
}
};
shakeFunction();
}
PlayerView.prototype.specialBomb = function() {
// BOOOM!
var nodes = $();
this.el.find('.board .special-b').each(function() {
nodes.add(
$('<div class="explosion" />')
.offset($(this).offset())
.appendTo('#container')
.css({'background-image': "url('../images/explosion.gif?" + Date.now() + "')"}));
});
if(nodes.length)
setTimeout(function(){ nodes.remove(); }, 2000);
}
PlayerView.prototype.specialMoses = function() {
var $rainbow = $('<div class="nyancat-rainbow" />');
var $nyancat = $('<div class="nyancat" />');
var centerOffset = this.el.find('.board .row:first .cell').eq(Math.floor(this.player.width/2)).offset();
var boardHeight = this.el.find('.board').height();
centerOffset.left -= 3;
$rainbow.appendTo('#container')
.offset(centerOffset)
.css({ height: 0 })
.animate({ height: boardHeight }, 1000, function(){
$(this).animate({ opacity: 0 }, 1000, function(){ $(this).remove(); });
});
$nyancat.appendTo('#container')
.offset(centerOffset)
.css({ top: centerOffset.top - 8 })
.animate({ top: '+='+boardHeight }, 1000, function(){ $(this).remove(); });
if (!this.isPlayer) {
centerOffset.left -= 5;
$nyancat.add($rainbow).offset(centerOffset);
$nyancat.css({'-webkit-transform': 'scale(0.5)'});
$rainbow.css({'-webkit-transform': 'scaleX(0.5)'});
}
}
PlayerView.prototype.specialZebra = function() {
if (!this.isPlayer)
this.player.zebra = typeof this.player.zebra === 'undefined' ? false : !this.player.zebra;
var animationDir = this.player.zebra ? '-' : '+';
var animationLen = this.isPlayer ? 300 : 150;
var $rows = this.el.find('.board .row');
for (var x = (this.player.zebra ? 1 : 0), i = 0; x < this.player.width; x += 2, i++) {
var $column = $('<div class="column"/>');
for (var y = 0; y < this.player.height; y++) {
var b = this.player.data[y * this.player.width + x];
if (this.isPlayer && this.player.invisible) {
if (!this.player.inBlockVisinity(x, y))
b = 0;
}
var $cell = $('<div class="cell"/>');
$cell.addClass(b !== 0 ? (typeof b === 'string' ? 'special special-'+b : 'block block-'+b) : 'empty');
$column.append($cell);
}
$column.appendTo(this.el)
.css({position: 'absolute'})
.offset($rows.first().find('.cell').eq(x).offset());
setTimeout(function(obj){
obj.animate({top: animationDir+'='+animationLen+'px', opacity: 0}, 300, function(){ obj.remove(); });
}, i*100, $column);
}
}
PlayerView.prototype.specialClearSpecials = function() {
var self = this;
//var nodes = $();
this.el.find('.board .special').each(function() {
var node = $('<div class="sparkle" />')
.offset($(this).offset())
.appendTo('#container')
.css({'background-image': "url('../images/sparkle.gif?" + Date.now() + "')"});
if (!self.isPlayer)
node.css({'-webkit-transform': 'scale(0.5)', margin: '-4px -4px'});
//nodes.add(node);
node.fadeOut(2000, function(){ node.remove(); });
});
//setTimeout(function(obj){ obj.fadeOut(500, function(){ obj.remove(); }); }, 500, nodes);
}