/
mole.js
124 lines (99 loc) · 2.19 KB
/
mole.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
var MOLES = MOLES || {};
MOLES.Utils = (function() {
var randomInterval = function(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
return {
randomInterval: randomInterval,
}
})();
MOLES.Mole = function() {
this.visible = false;
this.el = null;
this.init = function() {
};
this.show = function() {
this.visible = true;
var that = this;
this.el.addClass('showing');
var interval = MOLES.Utils.randomInterval(500, 2000)
setTimeout(function() { that.hide() }, interval);
};
this.addElement = function(element) {
this.el = element;
var that = this;
this.el.click(function() {
if(that.visible) {
that.el.removeClass('showing');
that.visible = false;
MOLES.Model.addToScore();
}
})
};
this.hide = function() {
this.visible = false
this.el.removeClass('showing');
var interval = MOLES.Utils.randomInterval(1000, 16000)
var that = this;
setTimeout(function() { that.show() }, interval);
};
};
MOLES.Model = (function() {
var _numMoles = 8;
var moles = [];
var score = 0;
var init = function() {
for (var i = 0; i < _numMoles; ++i) {
var mole = new MOLES.Mole();
mole.init();
moles.push(mole);
}
}
var addToScore = function() {
score += 1;
return score;
}
var getScore = function() {
return score;
}
var getMoles = function() {
return moles;
}
return {
init: init,
getMoles: getMoles,
getScore: getScore,
addToScore: addToScore
};
})();
MOLES.View = (function() {
var $element = null;
var init = function(id) {
$element = $(id);
setup();
};
var setup = function() {
var moles = MOLES.Model.getMoles();
moles.forEach(function(mole) {
var moleObject = $('<div>');
moleObject.addClass('mole');
$element.append(moleObject);
mole.addElement(moleObject);
mole.hide();
})
}
var render = function() {
$('#score').text(MOLES.Model.getScore());
}
return {
init: init,
render: render,
}
})();
$(function() {
MOLES.Model.init();
MOLES.View.init('#moles');
setInterval(function() {
MOLES.View.render();
}, 100)
})