-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminigame.js
125 lines (112 loc) · 2.82 KB
/
minigame.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
// == interface for the game ==
function run_mini_game (size, prize, shuffle) {
var size = size || 10,
shuffle = shuffle || size,
game = {
is_win : false,
gold : prize || 30
},
field = [];
var turn = function(x, y) {
var row = field[y];
row[x] = !row[x];
$('#m' + x + '_' + y)['toggleClass']('r');
}
var is_win_position = function() {
for(var j = 0; j < size; ++j)
for(var i = 0; i < size; ++i)
if (!field[j][i])
return false;
return true;
}
var win = function () {
game.is_win = true;
$('#mg')['hide']()['empty']();
++map.fixed;
map.hero.add_cash(game.gold);
}
// do the move
var move = function(x, y, shuffle_phase) {
for(var i=0; i<size; i++) {
turn(x, i);
turn(i, y);
}
turn(x,y);
if (is_win_position() && !shuffle_phase) {
win();
}
}
var generate = function() {
// generate field
for(var j=0, s=''; j<size; ++j) {
field[j] = [];
for(var i=0; i<size; ++i) {
field[j][i] = true;
s += '<b class="f" id="m' + i + '_' + j + '"></b>'
}
}
// game field
$('#mf') // hide on start
['html'](s) // draw
['css']({ // set dimensions and position
width: size * 32,
height: size * 32
});
// shuffle field
for (var i=0; i < shuffle; ++i) {
move(rand(0, size), rand(0, size), true);
if (is_win_position())
i=0;
};
}
// create html
$('#mg')['html']('<p id="mt">$' + prize + '</p>'+
'<p id="mr">' +
"<br>Goal of this minigame is turn <br>all the pipes to its right position (+).<br>" +
"Click to turn pipe. <br>" +
'<br><button id="ms">Start!</button>' +
'</p>' +
'<p id="mf"/><button id="g">Give up!</button>');
generate();
// start button runs the game
// $('#ms')['bind']('click.mg', function() {
// });
// set onclick for
$('#mg')['bind']('click.mg', function(e) {
var el = e.target;
if (el.tagName == 'B') {
move( el.id.replace(/^m(\d+)_\d+$/, '$1'), el.id.replace(/^m\d+_(\d+)$/, '$1'));
}
switch(el.id) {
case 'g':
game.gold = 1;
win();
break;
case 'ms':
$('#mr')['hide']();
$('#mf, #g')['show']();
// start timer!
$('#mg')['bind']('tick.mg', {
's': game,
'id': setInterval("$('#mg').trigger('tick.mg')", 1000)
}, function(e) {
var s = e.data['s'];
if (s.gold > 100)
s.gold = floor(s.gold / 1.5);
else
s.gold -= rand(1, 4);
if (s.gold < 1)
s.gold = 1;
if (s.is_win) {
clearInterval(e.data['id']);
$('#mg')['unbind']('.mg');
}
else {
$('#mt')['html']('$' + s.gold);
}
}
);
}
})['show']();
$('#ms')['focus']();
}