-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.js
310 lines (284 loc) · 10.1 KB
/
temp.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
var double = 0;
var curTop;
var curTopLen;
var curLeft;
var curLeftLen;
var maxLeft = 0;
var maxHeight = 0;
var movingLeft = 0;
var movingRight = 0;
var hStop = false;
var vStop = false;
var leftKeyDown = false;
var rightKeyDown = false;
var makeNewRainbow = true;
var rainbowStartingTop = 0;
var sceneSpeed = 100;
var rainbows = [];
var colourCode;
function horizontalStop(fx, prop) {
//stop height animation by external "event" -> if button was clicked
if (hStop && prop.prop === "left") {
//stop animation. start == end means nothing to be done anymore
prop.start = prop.end = prop.now;
//if you just want to stop height and then don't anything else with the step callback
//you can do the following to save a few cpu cycles
//prop.options.step = null;
hStop = false;
return;
}
}
function verticalStop(fx, prop) {
//stop height animation by external "event" -> if button was clicked
if (hStop && prop.prop === "top") {
//stop animation. start == end means nothing to be done anymore
prop.start = prop.end = prop.now;
//if you just want to stop height and then don't anything else with the step callback
//you can do the following to save a few cpu cycles
//prop.options.step = null;
vStop = false;
return;
}
}
$(document).ready(function(){
maxHeight = $('#mainFrame').height() - $('#obj').height();
maxLeft = $('#mainFrame').width() - $('#obj').width();
rainbowStartingTop = Math.floor((Math.random() * (maxHeight - 70)) + 1);
$('#obj').css('top', maxHeight);
$('#obj').css('left', 0);
var maxTop = 100;
var space = false;
var jumpNum = 0;
var halt = 0;
animateRainbow(Math.floor((Math.random() * 2) + 1), Math.floor((Math.random() * 20) + 1));
$(document).keyup(function(event) {
if (event.keyCode == 87) {
vStop = true;
//console.log("JUMP up");
}
if (event.keyCode == 65) {
hStop = true;
leftKeyDown = false;
//console.log("LEFT up");
}
if (event.keyCode == 68) {
hStop = true;
rightKeyDown = false;
//console.log("RIGHT up");
}
}).keydown(function(event) {
curTop = $("#obj").css("top");
curTopLen = curTop.length;
curTop = curTop.substring(0, curTopLen-2);
if (event.keyCode == 87) {
space = true;
//console.log("JUMP down");
if ($("#obj").css("top") == maxHeight) {
//console.log("not animated");
//console.log($("#obj").css("top"));
if(double < 1){
$("#obj").stop();
ballJump();
}
}else if(double == 0){
//console.log("animated");
//console.log($("#obj").css("top"));
double = 1;
//$("#obj").stop();
ballJump();
}
}
if (event.keyCode == 65) {
if(rightKeyDown == false){
leftKeyDown = true;
//console.log("LEFT down");
curLeft = $("#obj").css("left");
if(curLeft == "auto") curLeft = "0px";
curLeftLen = curLeft.length;
curLeft = curLeft.substring(0, curLeftLen-2);
if(curLeft > 0){
//console.log("moving to the left");
$("#obj").animate({ left: 0 }, {duration: curLeft * 2, queue: false, step: horizontalStop, easing: "linear"});
}
}
}
if (event.keyCode == 68) {
if(leftKeyDown == false){
rightKeyDown = true;
//console.log("RIGHT down");
curLeft = $("#obj").css("left");
if(curLeft == "auto") curLeft = "0px";
curLeftLen = curLeft.length;
curLeft = curLeft.substring(0, curLeftLen-2);
if(curLeft < maxLeft){
//if(movingLeft == 1)
//console.log("moving to the right");
$("#obj").animate({ left: maxLeft }, {duration: (maxLeft - curLeft) * 2, queue: false, step: horizontalStop, easing: "linear"});
}
}
}
});
$(document).click(function(event) {
if(event.which == 1){
colourCode = Math.floor((Math.random() * 3) + 1);
curTop = $("#obj").css("top");
curTopLen = curTop.length;
curTop = curTop.substring(0, curTopLen-2);
setbullet(curTop);
}
});
});
function ballJump(){
$("#spriteRearLeg").css("top", "32px")
.css("left", "-4px")
.css("width", "20px")
.css("height", "6px");
$("#spriteFrontLeg").css("top", "32px")
.css("left", "34px")
.css("width", "20px")
.css("height", "6px");
$("#obj").animate({ top: "-=200px" }, {duration: maxHeight, queue: false, step: verticalStop, easing: "easeOutQuad", complete: function(){
if(double == 0){
console.log("normal down");
$("#obj").animate({ top: maxHeight }, {duration: maxHeight, queue: false, easing: "easeInQuad", complete: function(){
$("#spriteRearLeg").css("top", "30px")
.css("left", "10px")
.css("width", "6px")
.css("height", "20px");
$("#spriteFrontLeg").css("top", "30px")
.css("left", "34px")
.css("width", "6px")
.css("height", "20px");
}});
}else if(double == 1){
console.log("double down");
var animateTime = (maxHeight - curTop + 200) / 200 * maxHeight;
if(animateTime > maxHeight){
animateTime = maxHeight + Math.pow((animateTime - maxHeight), 0.8);
}
$("#obj").animate({ top: maxHeight }, {duration: animateTime, queue: false, easing: "easeInQuad", complete: function(){
double = 0;
$("#spriteRearLeg").css("top", "30px")
.css("left", "10px")
.css("width", "6px")
.css("height", "20px");
$("#spriteFrontLeg").css("top", "30px")
.css("left", "34px")
.css("width", "6px")
.css("height", "20px");
}});
}
}});
}
function setbullet(height){
if(height <= maxHeight){
var bulletColour;
if(colourCode == 1){
bulletColour = "#F7819F";
}else if(colourCode == 2){
bulletColour = "#F5DA81";
}else if(colourCode == 3){
bulletColour = "#81DAF5";
}
var left = $('#obj').css("left");
if(left == "auto") left = "0px";
var leftLen = left.length;
left = parseInt(left.substring(0, leftLen-2)) + 60;
var res = $("<div class='bullet' style='background-color:" + bulletColour + "; top: " + (height - 4 + 25) + "px; left: " + left + "px;'></div>").appendTo($('body'));
animateBullet(res, left);
}
//console.log(res);
}
function animateBullet(obj, left){
if(left < maxLeft){
obj.css("left", left);
setTimeout(function(){
animateBullet(obj, left + 15);
}, 50);
}else{
bulletExplode(obj, left);
}
}
function bulletExplode(bullet, left){
var spark1;
var spark2;
var spark3;
var spark4;
setTimeout(function(){
var origTop = bullet.css("top");
origTop = parseInt(origTop.substring(0, origTop.length - 2));
setTimeout(function(){
bullet.remove();
spark1 = $("<div class='spark1' style='top: " + (origTop - 8) + "px; left: " + (left + 12 - 4) + "px;'></div>").appendTo($('body'));
spark2 = $("<div class='spark1' style='top: " + (origTop + 8) + "px; left: " + (left + 12 - 4) + "px;'></div>").appendTo($('body'));
spark3 = $("<div class='spark1' style='top: " + (origTop + 0) + "px; left: " + (left + 12 - 12) + "px;'></div>").appendTo($('body'));
spark4 = $("<div class='spark1' style='top: " + (origTop + 0) + "px; left: " + (left + 12 + 4) + "px;'></div>").appendTo($('body'));
setTimeout(function(){
spark1.addClass('spark2').removeClass('spark1').css("top", "+=2px").css("left", "+=2px");
spark2.addClass('spark2').removeClass('spark1').css("top", "+=2px").css("left", "+=2px");
spark3.addClass('spark2').removeClass('spark1').css("top", "+=2px").css("left", "+=2px");
spark4.addClass('spark2').removeClass('spark1').css("top", "+=2px").css("left", "+=2px");
setTimeout(function(){
spark1.remove();
spark2.remove();
spark3.remove();
spark4.remove();
}, 60);
}, 60);
}, 60);
}, 10);
}
function animateRainbow(direction, duration){
var dir = direction;
var dur;
if(makeNewRainbow == true){
var rainbow = $('<div class="rainbowPack" style="top:' + rainbowStartingTop + 'px; left:' + $('#mainFrame').width() + 'px" >' +
'<div class="rainbowSingle" style="background-color:#FFCDCD;"></div>' +
'<div class="rainbowSingle" style="background-color:#FFE8CD;"></div>' +
'<div class="rainbowSingle" style="background-color:#FFFFCD;"></div>' +
'<div class="rainbowSingle" style="background-color:#CDFFCF;"></div>' +
'<div class="rainbowSingle" style="background-color:#CDFFFF;"></div>' +
'<div class="rainbowSingle" style="background-color:#CDCDFF;"></div>' +
'<div class="rainbowSingle" style="background-color:#F9CDFF;"></div>' +
'</div>');
rainbow.appendTo($('#backgroundFrame'));
rainbows[rainbows.length] = rainbow;
makeNewRainbow = false;
}
for(var i = 0; i < rainbows.length; i++){
var rainbowObj = rainbows[i];
var rainbowLeft = parseInt(rainbowObj.css('left').substring(0, rainbowObj.css('left').length -2));
if(rainbowLeft + 10 < 0){
rainbows.splice(i, 1);
}
rainbowObj.css('left', rainbowLeft - 10);
if(i == rainbows.length - 1 && rainbowLeft == $('#backgroundFrame').width()){
var rainbowTop = parseInt(rainbowObj.css('top').substring(0, rainbowObj.css('top').length -2));
if(duration >= 0){
if(direction == 1){
if(rainbowTop >= 1){
rainbowStartingTop -= 1;
}else{
dir = 2;
rainbowStartingTop += 1;
}
}else{
if(rainbowTop <= $('#backgroundFrame').height() - 71){
rainbowStartingTop += 1;
}else{
rainbowStartingTop -= 1;
dir = 1;
}
}
makeNewRainbow = true;
}
}
//setTimeout(function(){animateRainbow(dir, dur)}, 100);
}
if(duration == 0){
makeNewRainbow = true;
setTimeout(function(){animateRainbow(Math.floor((Math.random() * 2) + 1), Math.floor((Math.random() * 20) + 1))}, 200);
}else{
setTimeout(function(){animateRainbow(dir, duration - 1)}, 200);
}
}