/
main.js
487 lines (438 loc) · 14.5 KB
/
main.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
if (typeof webkitAudioContext !== 'undefined') {
window.ctx = new webkitAudioContext();
window.dst = window.ctx.createDynamicsCompressor();
window.gain = window.ctx.createGain();
window.gain.gain.value = 1.0;
window.dst.connect(window.gain);
window.gain.connect(window.ctx.destination);
}
else if (typeof AudioContext !== 'undefined') {
window.ctx = new AudioContext();
window.dst = window.ctx.createDynamicsCompressor();
window.gain = window.ctx.createGain();
window.gain.gain.value = 1.0;
window.dst.connect(window.gain);
window.gain.connect(window.ctx.destination);
}
window.KYO = [
'観自在菩薩', '行深般若波羅蜜多時', '照見五蘊皆空',
'度一切苦厄', '舎利子', '色不異空', '空不異色', '色即是空',
'空即是色', '受想行識亦復如是', '舎利子', '是諸法空相',
'不生不滅', '不垢不浄', '不増不減', '是故空中',
'無色', '無受想行識', '無眼耳鼻舌身意', '無色声香味触法',
'無眼界', '乃至無意識界', '無無明亦', '無無明尽',
'乃至無老死', '亦無老死尽', '無苦集滅道', '無智亦無得',
'以無所得故', '菩提薩埵', '依般若波羅蜜多故',
'心無罣礙', '無罣礙故', '無有恐怖', '遠離一切顛倒夢想',
'究竟涅槃', '三世諸仏', '依般若波羅蜜多故',
'得阿耨多羅三藐三菩提', '故知般若波羅蜜多',
'是大神呪', '是大明呪', '是無上呪', '是無等等呪',
'能除一切苦', '真実不虚', '故説般若波羅蜜多呪',
'即説呪日', '羯諦', '羯諦', '波羅羯諦', '波羅僧羯諦',
'菩提薩婆訶', '般若心経'
];
var MP3 = function(url){
this.setSample(url);
this.sources = [];
};
MP3.prototype = {
setSample: function(url){
var self = this;
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.responseType = "arraybuffer";
req.onload = function(){
window.ctx.decodeAudioData(
req.response,
function(buffer){ self.buffer = buffer; },
function(err){ console.log('ajax error'); console.log(err); }
);
};
req.send();
},
play: function(rate){
if (typeof this.buffer === 'undefined') return;
var source = window.ctx.createBufferSource();
source.buffer = this.buffer;
source.connect(window.dst);
if (typeof rate !== 'undefined') { source.playbackRate = rate; }
source.start(0);
this.sources.push(source);
if (this.sources.length > 20) {
var s = this.sources.shift();
s.stop();
}
},
stop: function(){
for (var i=10; i < this.sources.length; i++) {
this.sources[i].stop(0);
}
this.sources.splice(10);
}
};
var ASSET_KPS = [0.5, 1, 3, 10, 100, 1000, 10000];
var ASSET_PRICE = [15, 100, 300, 1000, 4000, 10000, 100000];
//var ASSET_PRICE = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var Game = function(){
this.time = 0;
this.count = 0;
this.kps = 0; // kudoku per second
this.kpc = 1; // kudoku per click
this.evaluator = new Evaluator();
this.assets = [0,0,0,0,0,0,0];
this.ofuse = {};
this.storage = window.localStorage;
this.view = new GameView(this);
};
Game.prototype = {
start: function(){
this.load();
this.view.updateCount(this.count, this.kps);
var self = this;
setInterval(function(){
self.loop();
}, 1000);
},
loop: function(){
this.count += this.kps;
this.evaluate();
this.view.updateCount(this.count, this.kps);
console.log(window.KYO[this.time % window.KYO.length]);
this.time++;
if (this.time % 60 == 0) {
this.save();
}
},
evaluate: function(){
var achievement = this.evaluator.eval(this.count);
if (typeof achievement !== 'undefined') {
this.view.showAchievement(achievement);
}
var ofuse = this.evaluator.evalOfuse(this.count, this.assets);
if (typeof ofuse !== 'undefined') {
this.view.showOfuse(ofuse);
}
this.view.evaluate(this.count);
},
click: function(){
this.count += this.kpc;
this.view.updateCount(this.count, this.kps);
this.evaluate();
},
showOfuse: function(name){
this.ofuse[name] = 0;
},
addOfuse: function(name){
this.ofuse[name] = 1;
},
addAsset: function(i){
this.assets[i] += 1;
this.count -= ASSET_PRICE[i];
this.kps += ASSET_KPS[i];
this.view.updateCount(this.count, this.kps);
this.evaluate();
if (i == 1) {
if (this.assets[i] == 1) {
this.view.showAchievement('Okyo Debut', i, this.assets[i]);
}
else {
this.view.playOkyo(i);
}
}
else if (i == 3) {
this.view.playBuddha();
}
else if (i > 3) {
if (this.assets[i] == 1) {
this.view.playOkyo(i);
}
else {
this.view.playOkyoSub();
}
}
return this.assets[i];
},
save: function(){
var g = {
count: this.count,
kps: this.kps,
kpc: this.kpc,
assets:this.assets,
ofuse: this.ofuse
};
this.storage.game = JSON.stringify(g);
this.view.showMessage('Game saved');
},
load: function(){
var s = this.storage.game;
if (typeof s === 'undefined') return;
s = s.replace(/(\r\n|\n|\r)/gm, '');
var g = JSON.parse(s);
if (g === null) return;
this.count = g.count;
this.kps = g.kps;
this.kpc = g.kpc;
this.view.load(this.assets);
for (var i = 0; i < g.assets.length; i++) {
for (var j = 0; j < g.assets[i]; j++) {
this.view.addAsset(i);
}
}
for (var o in g.ofuse) {
if (g.ofuse[o] === 0) {
this.view.showOfuse(o);
}
else {
this.view.addOfuse(o);
}
}
},
reset: function(){
this.count = 0;
this.kps = 0;
this.kpc = 1;
this.assets = [0,0,0,0,0,0,0];
this.ofuse = {};
this.storage.clear('game');
}
};
var GameView = function(model){
this.model = model;
this.mokugyo = $('#mokugyo');
this.mokugyo_mp3 = new MP3('./sound/mokugyo.mp3');
this.gong_mp3 = new MP3('./sound/gong.mp3');
this.okyo_mp3 = new MP3('./sound/okyo.mp3');
this.okyo_sub1_mp3 = new MP3('./sound/okyo_sub1.mp3');
this.okyo_sub2_mp3 = new MP3('./sound/okyo_sub2.mp3');
this.bm1_mp3 = new MP3('./sound/BM101.mp3');
this.bm2_mp3 = new MP3('./sound/BM202.mp3');
this.okyo_mp3.loop = true;
this.okyo_sub1_mp3.loop = true;
this.okyo_sub2_mp3.loop = true;
this.bm1_mp3.loop = true;
this.bm2_mp3.loop = true;
this.ofuse_img = {
'Mute': './img/mute.bmp'
};
var self = this;
this.ofuse = {
'Mute': function(){
var is_listening = false;
self.btn_mute.show();
self.btn_mute.on('click', function(){
if (window.gain.gain.value == 1.0) {
window.gain.gain.value = 0;
is_listening = false;
$('.jakucho-face').each(function(i){
var f = $(this);
setTimeout(function(){f.addClass('wiggle');}, 10*i);
});
var listenToMe = function(){
if (is_listening) {return;}
self.showMessage('listen to me!!');
setTimeout(function(){listenToMe();}, 4000);
};
setTimeout(function(){listenToMe();}, 4000);
$(this).css('opacity', '0.6');
}
else {
window.gain.gain.value = 1.0;
is_listening = true;
$('.jakucho-face').removeClass('wiggle');
clearTimeout();
$(this).css('opacity', '1.0');
}
});
}
};
this.count_total = $('#count-total');
this.count_kps = $('#count-kps');
this.body = $('body');
this.left = $('#left');
this.center = $('#center');
this.right = $('#right');
this.achievement = $('#achievement');
this.asset = $('.asset');
this.btn_save = $('#save');
this.btn_reset = $('#reset');
this.btn_mute = $('#mute');
this.ofuse_list = $('#ofuse-list');
this.bonji_num = 0;
this.init();
};
GameView.prototype = {
init: function(){
var self = this;
this.mokugyo.on('click', function(e){self.click(e);});
$(window).on("load resize", function(){ self.resize(); });
this.btn_save.on('click', function(){self.save();});
this.btn_reset.on('click', function(){self.reset();});
},
resize: function(){
this.center.css({
left: this.left.width(),
width: this.body.width() - (this.left.width() + this.right.width()) - 10 + 'px'
});
},
click: function(e){
var pos = [e.clientX, e.clientY];
var bonji = $('<div class="bonji"></div>');
var plus = $('<div class="plus">+' + this.model.kpc + '</div>');
bonji.css({
'background-position': (this.bonji_num++ % 25) * 50 + 'px 0',
'left': (pos[0] - 40 + Math.random() * 30) + 'px',
'top': (pos[1] - 40 + Math.random() * 30) + 'px'
});
plus.css({
'left': (pos[0] + 30) + 'px',
'top': (pos[1] - 30) + 'px'
});
this.left.append(bonji).append(plus);
window.setTimeout(function(){bonji.remove(); plus.remove();}, 3000);
this.mokugyo_mp3.play();
this.model.click();
},
updateCount: function(count, kps){
this.count_total.text(Math.floor(count) + ' kudoku');
this.count_kps.text('per second: ' + kps);
},
showMessage: function(msg){
var a = $('<div class="message">' + msg + '</div>');
this.center.append(a);
window.setTimeout(function(){a.remove();}, 5000);
},
showAchievement: function(msg, okyo_num){
this.showMessage('Achievement :<br>' + msg);
if (typeof okyo_num === 'undefined') {
this.gong_mp3.play();
}
else if (okyo_num == 1) {
this.okyo_mp3.play();
}
},
showOfuse: function(name){
this.model.showOfuse(name);
var e = $('<img class="ofuse" name="' + name + '"src="' + this.ofuse_img[name] + '" />');
this.ofuse_list.append(e);
var self = this;
e.on('click', function(){
self.addOfuse($(this).attr('name'));
$(this).remove();
});
},
addOfuse: function(name){
this.model.addOfuse(name);
this.ofuse[name]();
$('.ofuse[name=' + name + ']').remove();
},
addAsset: function(i){
var num = this.model.addAsset(i);
if (i == 1) {
var face = $('<div class="jakucho-face"></div>');
face.css({
top: Math.random() * (this.center.width() - 100) + 'px',
left: Math.random() * (this.center.height() - 120) + 'px'
});
this.center.append(face);
if (num == 58) {
this.showAchievement('Goji Hakkyo');
var youtube = $('<iframe id="youtube" width="200" height="120" src="//www.youtube.com/embed/videoseries?list=PLF3r3_0zc-GKSURk5LjEfd3y4fw2NTHYz&autoplay=1" frameborder="0" allowfullscreen></iframe>');
this.center.append(youtube);
}
}
else if (i == 3) {
var buddha = $('<div class="buddha-machine"></div>');
buddha.css({
bottom: '0px',
left: Math.random() * (this.center.height() - 120) + 'px'
});
this.center.append(buddha);
}
},
evaluate: function(count){
var self = this;
this.asset.each(function(i){
$(this).children().filter(".asset-kudoku").html(window.ASSET_PRICE[i]); // Auto Setup
if (count >= window.ASSET_PRICE[i]) {
if ($(this).hasClass('available')) return;
$(this).addClass('available').on('click', function(){
self.addAsset(self.asset.index(this));
});
}
else {
$(this).removeClass('available').off();
}
});
},
playOkyo: function(i){
this.okyo_mp3.stop();
this.okyo_mp3.play(ASSET_KPS[i]);
},
playOkyoSub: function(){
var r = Math.random();
if (r < 0.45) {
this.okyo_sub1_mp3.play();
}
else if (r < 0.93) {
this.okyo_sub2_mp3.play();
}
else {
this.okyo_mp3.play();
}
},
playBuddha: function(i){
var r = Math.random();
if (r < 0.7) {
this.bm1_mp3.play();
}
else {
this.bm2_mp3.play();
}
},
load: function(assets){
// if (assets === undefined) return;
},
reset: function(){
this.model.reset();
$('.jakucho-face').remove();
$('.buddha-machine').remove();
$('#youtube').remove();
this.btn_mute.hide();
// TODO: stop MP3
},
save: function(){
this.model.save();
}
};
var Evaluator = function(){
this.pos = 0;
this.counts = [10, 108, 110359, 1000000000, 5670000000, 1000000000000000000000000];
this.achievements = ['Ten Bonnoh', 'Bonnoh Master', 'Human Sacrifice', 'Trisahasramahasahasralokadhatu', 'Salvation of Maitreya', 'Nirvana'];
this.pos_ofuse = 0;
this.ofuse_flag = [
function(count, assets){return (assets[1] > 0) && (count > 10000);},
function(){return false;}
];
this.ofuse = ['Mute'];
};
Evaluator.prototype = {
eval: function(count){
if (count >= this.counts[this.pos]) {
return this.achievements[this.pos++];
}
else {
return undefined;
}
},
evalOfuse: function(count, assets){
var cond = this.ofuse_flag[this.pos_ofuse](count, assets);
if (cond) {
return this.ofuse[this.pos_ofuse++];
}
return undefined;
}
};
$(function(){
window.game = new Game();
game.start();
});