-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforest.js
284 lines (247 loc) · 7.29 KB
/
forest.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
** Define some global vars
*/
var ip = window.location.href;
var ipurl = ip.slice(0, (ip.lastIndexOf('/')));
// var ipurl = "http://grmmph.com";
var socket = io.connect(ipurl);
var login, note, ani = {}, assets = [], scaleAnimal;
/*
** Here we go
*/
var Forest = {
init: function () {
Forest.isMobile();
},
/*
** Mobile detection
*/
isMobile: function () {
if($('#ismobile').css('opacity') != 0) {
window.location.replace("mobile.html");
}
// Preload stuff
for(var i=1; i<10; i++) {
assets.push('sounds/keyboard/'+i+'.mp3');
assets.push('sounds/keyboard-d/'+i+'.mp3');
assets.push('sounds/drums/'+i+'.mp3');
assets.push('sounds/drums-d/'+i+'.mp3');
assets.push('sounds/guitar/'+i+'.mp3');
}
assets.push('img/sprites/keyboard.png');
assets.push('img/sprites/drums.png');
assets.push('img/sprites/guitar.png');
assets.push('img/sprites/bass.png');
for(var j=0; j<assets.length; j++) {
load(assets[j])
if(j==assets.length-1) {
Forest.login();
};
};
function load(s) {
$.ajax({
url: s
});
};
},
login: function () {
socket.on('s-msg', function (a) {
});
login = {};
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
if(typeof localStorage.sid === 'undefined') {
login.sid = makeid();
localStorage.sid = login.sid;
} else {
login.sid = localStorage.sid;
};
login.isMobile = false;
socket.emit('login', login);
Forest.makeqr();
},
/*
** Generate QR Code
*/
makeqr: function () {
var jamAddress = ipurl + "/forestjam/mobile.html?" + login.sid;
var qrcode = new QRCode(document.getElementById("qrcode"), {
text: jamAddress,
width: 128,
height: 128,
colorDark : "#C06060",
colorLight : "white",
});
var qrcodebig = new QRCode(document.getElementById("qrcode-big"), {
text: jamAddress,
width: 256,
height: 256,
colorDark : "#C06060",
colorLight : "white",
});
$('#ip-address-big').html("Scan the code above with your phone's QR code scanner. <br>Or instead, go to <b class='code'>"+ipurl+"/forestjam</b><br> on your mobile browser and type: <b class='code'>" + login.sid+'</b> at the code box.')
function getAddress() {
if(ipurl.search("http://") >= 0) {
return ipurl.slice(7, ipurl.length)
} else {
return ipurl
}
}
$('#qr-holder').mouseenter(function(){
$('#ip-address').animate({
'height' : 70
},300)
.html("Start Forest Jam on your mobile browser and type: <b class='code'>" + login.sid+'</b>')
}).mouseleave(function(){
setTimeout(function() {
$('#ip-address').animate({
'height' : 17
},300)
.html("Or use <a id='show-address'>URL address</a>")
}, 1500)
})
},
/*
** Calls when user logs in, setting the animal
*/
setPlayer: function(p) {
$('#splash').fadeOut('fast');
$('#stage').append("<div class='animal' uid='"+p.uid+"' instrument='"+p.instrument+"'><a class='btn small hide kick'>kick out</a><div class='player-name' uid='"+p.uid+"'></div></div>");
$(".animal[uid='"+p.uid+"']").append("<div class='floor' style='border-bottom: 20px solid "+p.color+"'></div>")
Forest.scale('set');
$(".animal[uid='"+p.uid+"']").css({
'margin-top' : '-100px',
"background": "transparent url(../img/sprites/"+p.instrument+".png) no-repeat",
'background-position-x' : -scaleAnimal*11,
})
.animate({'margin-top' : '0', 'opacity' : 1},500, function(){
$(this).css( 'background-position' , 0)
$(".animal[uid='"+p.uid+"']").children('.floor').animate({'opacity': 0.8}, 300)
})
$('.kick').click(function() {
var kickUid = $(this).parent('.animal').attr('uid');
Forest.killPlayer(kickUid)
socket.emit('kick', kickUid)
});
$('.animal').mouseenter(function(){
$(this).find('.kick').fadeIn('fast');
}).mouseleave(function() {
$(this).find('.kick').fadeOut('fast');
});
$(window).resize(function(){Forest.scale()});
},
/*
** Layout scaling
*/
scale: function (event) {
var $s = $('#stage').width(),
windwi = $(window).width(),
a = $('.animal').length;
$('#stage').width(a*280)
scaleAnimal = 250;
var scaleStage = (windwi / (a*250))*0.6;
if(scaleStage>1){
scaleStage = 1;
}
if($s >= windwi*0.7) {
var marg = (a*250)*0.6 / windwi;
$('#stage').css('margin-left', - (marg*300))
} else {
$('#stage').css('margin-left', 'auto')
};
$('#stage').css("-webkit-transform", "scale("+scaleStage+")");
},
killPlayer: function (uid) {
$(".animal[uid='"+uid+"']").fadeOut(500, function (){
$(this).remove();
Forest.scale('kill');
});
setTimeout(function (){
if($('.animal').length == 0) {
$('#splash').fadeIn('fast')
}
},7000);
},
/*
** Define the note options
*/
note: function (key) {
var inst = key.instrument;
var n = key.note;
note = new Howl({
urls: ["sounds/"+inst+"/"+n+".mp3"],
sprite: {
start: [0,1000],
all: [0,3000],
ending: [1500, 2500]
}
});
},
playNote: function (key) {
Forest.note(key);
$(".animal[uid="+key.uid+"]").css('background-position-x' , (-scaleAnimal*(parseInt(key.note))));
note.play('all');
},
endNote: function(key) {
var inst = key.instrument;
if(inst != 'drums' && inst != 'keyboard-d' && inst != 'drums-d' && inst != 'bass' && inst != 'bass-d')
note.fadeOut(0.0, 500);
$(".animal[uid="+key.uid+"]").css('background-position' , 0)
},
changeInstrument: function(d) {
var uid = d.uid,
instrument = d.instrument;
$(".animal[uid="+uid+"]").css('background-position-x' , -scaleAnimal*10)
.animate({
'background-position-y' : 250,
// 'opacity' : 0
}, 400, function(){
$(this).css({
"background-image": "url(../img/sprites/"+instrument+".png)",
'background-position-x' : -scaleAnimal*11,
})
.animate({
'background-position-y' : 0,
},400, function(){
$(this).css( 'background-position' , 0)
});
})
}
}; // Forest End
// ------------------------- //
socket.on('key', function(key){
var cmd = key.cmd;
n = note;
if(cmd=='play') Forest.playNote(key);
if(cmd=='stop') Forest.endNote(key);
});
socket.on('new-player',function (player) {
setTimeout(function() {
Forest.setPlayer(player)
},500)
});
/*
** For debugging purposes only at the moment.
*/
// socket.on('cur-players',function (clients) {
// console.log("clients:");
// console.log(clients);
// })
socket.on('dis-player',function (player) {
Forest.killPlayer(player.uid)
});
socket.on('name',function (n) {
$(".player-name[uid='"+n.uid+"']").html(n.name)
});
socket.on('change',function (d) {
if(d.instrument != 'undefined')
Forest.changeInstrument(d)
});
$(window).load(function () {
Forest.init();
});