-
Notifications
You must be signed in to change notification settings - Fork 0
/
balls.js
172 lines (153 loc) · 4.34 KB
/
balls.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
var animator = new BallAnimator([]);
var clicks = -1;
var time = 0;
var interval;
var gameActive = false;
var blackBallPossibility;
var blueBallsPossibility;
var blackBallThreshold;
var gameDifficultySpeed = 2; // defualt speed is Padawan
var numBalls = localStorage.getItem('numBalls');
var blueBallsEvent = $.Event('blueBalls');
console.log('numballs',numBalls);
function updateClicks() {
if (gameActive) {
clicks++;
document.getElementById("clickCounter").innerHTML = clicks + " clicks";
}
}
function isBlackBalled() {
return blackBallPossibility > 70; //blackBallThreshold;
}
function gameIsOver() {
return (animator.getBallsList()).length === 0;
}
function isBlueBalled() {
return clicks >= blueBallsPossibility;
}
function monitorGame() {
interval = setInterval(function() {
// shoudld probably implement these as triggered event calls
if (isBlackBalled()) {
endGame("You got Black Balled. This really sucks. You'll be fine. Good bye.");
clearInterval(interval);
window.history.go(-1);
} else if (gameIsOver()) {
if (numBalls == 2) {
var vM = document.getElementById("victoryMusic");
vM.src = "./media/audio/balls.mp3";
endGame("You've constrained the balls! If it wasn't for your quick clicking, these two low-riders would be free ballin' all day long. Score = "+getScore());
vM.play();
} else {
var vM = document.getElementById("victoryMusic");
vM.src = "./media/audio/icilawb.mp3";
endGame("Wrecking Ball! Score = "+getScore());
vM.play();
}
clearInterval(interval);
} else if (isBlueBalled()) {
animator.getBallsList().forEach(function(ball) {
ball.getElement().trigger(blueBallsEvent);
});
setTimeout(function() {
if (numBalls == 2) {
endGame("Blue Balls! You've failed to capture these balls. You need both of them to win, remember, two balls of the same sack hang together.");
} else {
endGame("You got Blue Balled! Ouch, sorry :(");
}
},500);
clearInterval(interval);
} else {
time += 1;
$("#timer").text(time);
}
},0.01);
}
function add() {
var d = $('<div></div>').attr('class','circle');
d.click(function() {
d.css('background','radial-gradient(circle at 100px 100px, #FF0000, #001)');
setTimeout(function(){
animator.setBallsList(animator.getBallsList().filter(function(ball) {
return ball.getElement() !== d;
}));
d.remove();
},10);
});
var b = new Ball(d);
d.on('blueBalls', function() {
console.log("blues");
d.css('background','radial-gradient(circle at 100px 100px, #00f, #001)');
});
if (isBlackBalled()) {
d.css('background', 'radial-gradient(circle at 100px 100px, #000, #001)');
} else {
d.css('background','radial-gradient(circle at 100px 100px, #eef, #001)');
}
setBallSpeed(b, gameDifficultySpeed);
$("body").append(d);
animator.addBall(b);
return b;
}
function setGameDifficultyLevel(difficulty) {
gameDifficultySpeed = difficulty;
}
function startGame() {
document.getElementById("startButton").disabled = true;
var vM = document.getElementById("victoryMusic"); vM.play(); vM.pause();
blackBallPossibility = Math.floor(Math.random()*100);
blueBallsPossibility = Math.floor(Math.random()*6)+1;
blackBallThreshold = Math.floor(Math.random()*100);
// this is not clean code, needs to be generalized for any numBalls
if (numBalls == 2) {
add();
setTimeout(function() {
add();
gameActive = true;
monitorGame();
},300);
} else {
add();
gameActive = true;
monitorGame();
}
}
function getScore() {
var c = clicks;
console.log(clicks);
if (clicks === -1) c = 0;
return time + ((clicks) * 100);
}
function endGame(message) {
animator.stopAnimating();
clearClicks();
clearTimer();
gameActive = false;
// var el = document.getElementById('ballBoard'); // not working?
// el.removeAttribute('onclick');
alert(message);
animator.getBallsList().forEach(function(ball) {
ball.getElement().remove();
delete ball;
})
document.getElementById("startButton").disabled = false;
}
function clearClicks() {
clicks = -1;
document.getElementById("clickCounter").innerHTML = "0 clicks";
}
function clearTimer() {
time = 0;
$("#timer").text(time);
}
function setBallSpeed(ball, speed) {
ball.speed = speed;
}
function ballInDropArea(ballList) {
ballList.forEach(function(ball) {
if (ball.getCurrentPosition().x < 350 && ball.getCurrentPosition().y < 350) {
return true;
}
})
return false;
}