-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamefuncs.js
executable file
·1311 lines (1164 loc) · 41.6 KB
/
gamefuncs.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* transform: A jQuery cssHooks adding cross-browser 2d transform capabilities to $.fn.css() and $.fn.animate()
*
* limitations:
* - requires jQuery 1.4.3+
* - Should you use the *translate* property, then your elements need to be absolutely positionned in a relatively positionned wrapper **or it will fail in IE678**.
* - transformOrigin is not accessible
*
* latest version and complete README available on Github:
* https://github.com/louisremi/jquery.transform.js
*
* Copyright 2011 @louis_remi
* Licensed under the MIT license.
*
* This saved you an hour of work?
* Send me music http://www.amazon.co.uk/wishlist/HNTU0468LQON
*
*/
(function ($, window, document, Math, undefined) {
/*
* Feature tests and global variables
*/
var div = document.createElement("div"),
divStyle = div.style,
suffix = "Transform",
testProperties = [
"O" + suffix,
"ms" + suffix,
"Webkit" + suffix,
"Moz" + suffix
],
i = testProperties.length,
supportProperty,
supportMatrixFilter,
supportFloat32Array = "Float32Array" in window,
propertyHook,
propertyGet,
rMatrix = /Matrix([^)]*)/,
rAffine = /^\s*matrix\(\s*1\s*,\s*0\s*,\s*0\s*,\s*1\s*(?:,\s*0(?:px)?\s*){2}\)\s*$/,
_transform = "transform",
_transformOrigin = "transformOrigin",
_translate = "translate",
_rotate = "rotate",
_scale = "scale",
_skew = "skew",
_matrix = "matrix";
// test different vendor prefixes of these properties
while (i--) {
if (testProperties[i] in divStyle) {
$.support[_transform] = supportProperty = testProperties[i];
$.support[_transformOrigin] = supportProperty + "Origin";
continue;
}
}
// IE678 alternative
if (!supportProperty) {
$.support.matrixFilter = supportMatrixFilter = divStyle.filter === "";
}
// px isn't the default unit of these properties
$.cssNumber[_transform] = $.cssNumber[_transformOrigin] = true;
/*
* fn.css() hooks
*/
if (supportProperty && supportProperty != _transform) {
// Modern browsers can use jQuery.cssProps as a basic hook
$.cssProps[_transform] = supportProperty;
$.cssProps[_transformOrigin] = supportProperty + "Origin";
// Firefox needs a complete hook because it stuffs matrix with "px"
if (supportProperty == "Moz" + suffix) {
propertyHook = {
get: function (elem, computed) {
return (computed ?
// remove "px" from the computed matrix
$.css(elem, supportProperty).split("px").join("") :
elem.style[supportProperty]
);
},
set: function (elem, value) {
// add "px" to matrices
elem.style[supportProperty] = /matrix\([^)p]*\)/.test(value) ?
value.replace(/matrix((?:[^,]*,){4})([^,]*),([^)]*)/, _matrix + "$1$2px,$3px") :
value;
}
};
/* Fix two jQuery bugs still present in 1.5.1
* - rupper is incompatible with IE9, see http://jqbug.com/8346
* - jQuery.css is not really jQuery.cssProps aware, see http://jqbug.com/8402
*/
} else if (/^1\.[0-5](?:\.|$)/.test($.fn.jquery)) {
propertyHook = {
get: function (elem, computed) {
return (computed ?
$.css(elem, supportProperty.replace(/^ms/, "Ms")) :
elem.style[supportProperty]
);
}
};
}
/* TODO: leverage hardware acceleration of 3d transform in Webkit only
else if ( supportProperty == "Webkit" + suffix && support3dTransform ) {
propertyHook = {
set: function( elem, value ) {
elem.style[supportProperty] =
value.replace();
}
}
}*/
} else if (supportMatrixFilter) {
propertyHook = {
get: function (elem, computed, asArray) {
var elemStyle = (computed && elem.currentStyle ? elem.currentStyle : elem.style),
matrix, data;
if (elemStyle && rMatrix.test(elemStyle.filter)) {
matrix = RegExp.$1.split(",");
matrix = [
matrix[0].split("=")[1],
matrix[2].split("=")[1],
matrix[1].split("=")[1],
matrix[3].split("=")[1]
];
} else {
matrix = [1, 0, 0, 1];
}
if (!$.cssHooks[_transformOrigin]) {
matrix[4] = elemStyle ? parseInt(elemStyle.left, 10) || 0 : 0;
matrix[5] = elemStyle ? parseInt(elemStyle.top, 10) || 0 : 0;
} else {
data = $._data(elem, "transformTranslate", undefined);
matrix[4] = data ? data[0] : 0;
matrix[5] = data ? data[1] : 0;
}
return asArray ? matrix : _matrix + "(" + matrix + ")";
},
set: function (elem, value, animate) {
var elemStyle = elem.style,
currentStyle,
Matrix,
filter,
centerOrigin;
if (!animate) {
elemStyle.zoom = 1;
}
value = matrix(value);
// rotate, scale and skew
Matrix = [
"Matrix(" +
"M11=" + value[0],
"M12=" + value[2],
"M21=" + value[1],
"M22=" + value[3],
"SizingMethod='auto expand'"
].join();
filter = (currentStyle = elem.currentStyle) && currentStyle.filter || elemStyle.filter || "";
elemStyle.filter = rMatrix.test(filter) ?
filter.replace(rMatrix, Matrix) :
filter + " progid:DXImageTransform.Microsoft." + Matrix + ")";
if (!$.cssHooks[_transformOrigin]) {
// center the transform origin, from pbakaus's Transformie http://github.com/pbakaus/transformie
if ((centerOrigin = $.transform.centerOrigin)) {
elemStyle[centerOrigin == "margin" ? "marginLeft" : "left"] = -(elem.offsetWidth / 2) + (elem.clientWidth / 2) + "px";
elemStyle[centerOrigin == "margin" ? "marginTop" : "top"] = -(elem.offsetHeight / 2) + (elem.clientHeight / 2) + "px";
}
// translate
// We assume that the elements are absolute positionned inside a relative positionned wrapper
elemStyle.left = value[4] + "px";
elemStyle.top = value[5] + "px";
} else {
$.cssHooks[_transformOrigin].set(elem, value);
}
}
};
}
// populate jQuery.cssHooks with the appropriate hook if necessary
if (propertyHook) {
$.cssHooks[_transform] = propertyHook;
}
// we need a unique setter for the animation logic
propertyGet = propertyHook && propertyHook.get || $.css;
/*
* fn.animate() hooks
*/
$.fx.step.transform = function (fx) {
var elem = fx.elem,
start = fx.start,
end = fx.end,
pos = fx.pos,
transform = "",
precision = 1E5,
i, startVal, endVal, unit;
// fx.end and fx.start need to be converted to interpolation lists
if (!start || typeof start === "string") {
// the following block can be commented out with jQuery 1.5.1+, see #7912
if (!start) {
start = propertyGet(elem, supportProperty);
}
// force layout only once per animation
if (supportMatrixFilter) {
elem.style.zoom = 1;
}
// replace "+=" in relative animations (-= is meaningless with transforms)
end = end.split("+=").join(start);
// parse both transform to generate interpolation list of same length
$.extend(fx, interpolationList(start, end));
start = fx.start;
end = fx.end;
}
i = start.length;
// interpolate functions of the list one by one
while (i--) {
startVal = start[i];
endVal = end[i];
unit = +false;
switch (startVal[0]) {
case _translate:
unit = "px";
case _scale:
unit || (unit = "");
transform = startVal[0] + "(" +
Math.round((startVal[1][0] + (endVal[1][0] - startVal[1][0]) * pos) * precision) / precision + unit + "," +
Math.round((startVal[1][1] + (endVal[1][1] - startVal[1][1]) * pos) * precision) / precision + unit + ")" +
transform;
break;
case _skew + "X":
case _skew + "Y":
case _rotate:
transform = startVal[0] + "(" +
Math.round((startVal[1] + (endVal[1] - startVal[1]) * pos) * precision) / precision + "rad)" +
transform;
break;
}
}
fx.origin && (transform = fx.origin + transform);
propertyHook && propertyHook.set ?
propertyHook.set(elem, transform, +true) :
elem.style[supportProperty] = transform;
};
/*
* Utility functions
*/
// turns a transform string into its "matrix(A,B,C,D,X,Y)" form (as an array, though)
function matrix(transform) {
transform = transform.split(")");
var
trim = $.trim
, i = -1
// last element of the array is an empty string, get rid of it
, l = transform.length - 1
, split, prop, val
, prev = supportFloat32Array ? new Float32Array(6) : []
, curr = supportFloat32Array ? new Float32Array(6) : []
, rslt = supportFloat32Array ? new Float32Array(6) : [1, 0, 0, 1, 0, 0]
;
prev[0] = prev[3] = rslt[0] = rslt[3] = 1;
prev[1] = prev[2] = prev[4] = prev[5] = 0;
// Loop through the transform properties, parse and multiply them
while (++i < l) {
split = transform[i].split("(");
prop = trim(split[0]);
val = split[1];
curr[0] = curr[3] = 1;
curr[1] = curr[2] = curr[4] = curr[5] = 0;
switch (prop) {
case _translate + "X":
curr[4] = parseInt(val, 10);
break;
case _translate + "Y":
curr[5] = parseInt(val, 10);
break;
case _translate:
val = val.split(",");
curr[4] = parseInt(val[0], 10);
curr[5] = parseInt(val[1] || 0, 10);
break;
case _rotate:
val = toRadian(val);
curr[0] = Math.cos(val);
curr[1] = Math.sin(val);
curr[2] = -Math.sin(val);
curr[3] = Math.cos(val);
break;
case _scale + "X":
curr[0] = +val;
break;
case _scale + "Y":
curr[3] = val;
break;
case _scale:
val = val.split(",");
curr[0] = val[0];
curr[3] = val.length > 1 ? val[1] : val[0];
break;
case _skew + "X":
curr[2] = Math.tan(toRadian(val));
break;
case _skew + "Y":
curr[1] = Math.tan(toRadian(val));
break;
case _matrix:
val = val.split(",");
curr[0] = val[0];
curr[1] = val[1];
curr[2] = val[2];
curr[3] = val[3];
curr[4] = parseInt(val[4], 10);
curr[5] = parseInt(val[5], 10);
break;
}
// Matrix product (array in column-major order)
rslt[0] = prev[0] * curr[0] + prev[2] * curr[1];
rslt[1] = prev[1] * curr[0] + prev[3] * curr[1];
rslt[2] = prev[0] * curr[2] + prev[2] * curr[3];
rslt[3] = prev[1] * curr[2] + prev[3] * curr[3];
rslt[4] = prev[0] * curr[4] + prev[2] * curr[5] + prev[4];
rslt[5] = prev[1] * curr[4] + prev[3] * curr[5] + prev[5];
prev = [rslt[0], rslt[1], rslt[2], rslt[3], rslt[4], rslt[5]];
}
return rslt;
}
// turns a matrix into its rotate, scale and skew components
// algorithm from http://hg.mozilla.org/mozilla-central/file/7cb3e9795d04/layout/style/nsStyleAnimation.cpp
function unmatrix(matrix) {
var
scaleX
, scaleY
, skew
, A = matrix[0]
, B = matrix[1]
, C = matrix[2]
, D = matrix[3]
;
// Make sure matrix is not singular
if (A * D - B * C) {
// step (3)
scaleX = Math.sqrt(A * A + B * B);
A /= scaleX;
B /= scaleX;
// step (4)
skew = A * C + B * D;
C -= A * skew;
D -= B * skew;
// step (5)
scaleY = Math.sqrt(C * C + D * D);
C /= scaleY;
D /= scaleY;
skew /= scaleY;
// step (6)
if (A * D < B * C) {
A = -A;
B = -B;
skew = -skew;
scaleX = -scaleX;
}
// matrix is singular and cannot be interpolated
} else {
// In this case the elem shouldn't be rendered, hence scale == 0
scaleX = scaleY = skew = 0;
}
// The recomposition order is very important
// see http://hg.mozilla.org/mozilla-central/file/7cb3e9795d04/layout/style/nsStyleAnimation.cpp#l971
return [
[_translate, [+matrix[4], +matrix[5]]],
[_rotate, Math.atan2(B, A)],
[_skew + "X", Math.atan(skew)],
[_scale, [scaleX, scaleY]]
];
}
// build the list of transform functions to interpolate
// use the algorithm described at http://dev.w3.org/csswg/css3-2d-transforms/#animation
function interpolationList(start, end) {
var list = {
start: [],
end: []
},
i = -1, l,
currStart, currEnd, currType;
// get rid of affine transform matrix
(start == "none" || isAffine(start)) && (start = "");
(end == "none" || isAffine(end)) && (end = "");
// if end starts with the current computed style, this is a relative animation
// store computed style as the origin, remove it from start and end
if (start && end && !end.indexOf("matrix") && toArray(start).join() == toArray(end.split(")")[0]).join()) {
list.origin = start;
start = "";
end = end.slice(end.indexOf(")") + 1);
}
if (!start && !end) { return; }
// start or end are affine, or list of transform functions are identical
// => functions will be interpolated individually
if (!start || !end || functionList(start) == functionList(end)) {
start && (start = start.split(")")) && (l = start.length);
end && (end = end.split(")")) && (l = end.length);
while (++i < l - 1) {
start[i] && (currStart = start[i].split("("));
end[i] && (currEnd = end[i].split("("));
currType = $.trim((currStart || currEnd)[0]);
append(list.start, parseFunction(currType, currStart ? currStart[1] : 0));
append(list.end, parseFunction(currType, currEnd ? currEnd[1] : 0));
}
// otherwise, functions will be composed to a single matrix
} else {
list.start = unmatrix(matrix(start));
list.end = unmatrix(matrix(end))
}
return list;
}
function parseFunction(type, value) {
var
// default value is 1 for scale, 0 otherwise
defaultValue = +(!type.indexOf(_scale)),
scaleX,
// remove X/Y from scaleX/Y & translateX/Y, not from skew
cat = type.replace(/e[XY]/, "e");
switch (type) {
case _translate + "Y":
case _scale + "Y":
value = [
defaultValue,
value ?
parseFloat(value) :
defaultValue
];
break;
case _translate + "X":
case _translate:
case _scale + "X":
scaleX = 1;
case _scale:
value = value ?
(value = value.split(",")) && [
parseFloat(value[0]),
parseFloat(value.length > 1 ? value[1] : type == _scale ? scaleX || value[0] : defaultValue + "")
] :
[defaultValue, defaultValue];
break;
case _skew + "X":
case _skew + "Y":
case _rotate:
value = value ? toRadian(value) : 0;
break;
case _matrix:
return unmatrix(value ? toArray(value) : [1, 0, 0, 1, 0, 0]);
break;
}
return [[cat, value]];
}
function isAffine(matrix) {
return rAffine.test(matrix);
}
function functionList(transform) {
return transform.replace(/(?:\([^)]*\))|\s/g, "");
}
function append(arr1, arr2, value) {
while (value = arr2.shift()) {
arr1.push(value);
}
}
// converts an angle string in any unit to a radian Float
function toRadian(value) {
return ~value.indexOf("deg") ?
parseInt(value, 10) * (Math.PI * 2 / 360) :
~value.indexOf("grad") ?
parseInt(value, 10) * (Math.PI / 200) :
parseFloat(value);
}
// Converts "matrix(A,B,C,D,X,Y)" to [A,B,C,D,X,Y]
function toArray(matrix) {
// remove the unit of X and Y for Firefox
matrix = /([^,]*),([^,]*),([^,]*),([^,]*),([^,p]*)(?:px)?,([^)p]*)(?:px)?/.exec(matrix);
return [matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6]];
}
$.transform = {
centerOrigin: "margin"
};
})(jQuery, window, document, Math);
/* val() function of jquery to return number instead of string in an input box */
(function ($) {
var originalVal = $.fn.val;
$.fn.val = function (value) {
if (typeof value == 'undefined') {
var text = this[0].value;
var textInNum = parseInt(text);
var type = this.attr('type');
if (text == textInNum) {
return textInNum;
} else {
if (type == 'number') {
return parseFloat(text);
} else {
return this[0].value;
}
}
} else {
return originalVal.call(this, value);
}
};
})(jQuery);
var Canvas;
var pause_timeTime = 0;
var start = function () {
Canvas = $('#canvas');
};
/**
* If there is no heading, adds a heading to project with id heading otherwise, it updates the existing heading.
* @example
* print_heading("Heading text", 64)
* @param {string[]} heading text of heading
* @param {number} size size of heading
*/
var print_heading = function (heading, size) {
if ($('#heading').length) {
$('#heading').text(heading);
} else {
Canvas.append('<h1 class="heading" id="heading">' + heading + '</h1>');
}
$('#heading').css({
'font-size': size + 'px'
});
};
/**
* Returns a random number within given range
* @example
* random_num(1,10);
* @param {number} start Minimum number of range
* @param {number} end Maximum number of range
*/
var random_num = function (start, end) {
return Math.floor(start + (Math.random() * (end - start)));
}
/**
* Adds background image
* @example
* add_background("background.jpg")
* @param {string[]} url path to image
*/
var add_background = function (url) {
$('#canvas').css("background-image", "url('" + url + "')")
}
/**
* Adds background color
* @example
* background_color("#000000")
* @param {string} color code
*/
var background_color = function (color) {
$('body').css("background-color", color)
console.log(color)
}
/**
* Adds a button with label
* @example
* add_button("Submit")
* @param {string[]} label Text to be shown on button
*/
var add_button = function (label) {
var uid = 'button' + $('button').length;
Canvas.append('<button id="' + uid + '">' + label + '</button>');
return $('#' + uid);
}
/**
* Adds input type text to the project.
* @example
* input("Enter your name")
* @param {string[]} text placeholder of the input
*/
var input = function (text) {
var uid = 'input' + $('input').length;
Canvas.append("<input type='text' id=" + uid + " placeholder='" + text + "'>");
return $("#" + uid);
}
/**
* Adds input type number to the project.
* @example
* input_num("Enter your age")
* @param {string[]} text placeholder of the input
*/
var input_num = function (text) {
var uid = 'input' + $('input').length;
Canvas.append("<input type='number' id=" + uid + " placeholder='" + text + "'>");
return $("#" + uid);
}
/**
* Adds audio with preload and without loop, autoplay.
* @example
* sound = add_sound_effect("bazinga.mp3")
* play_audio(sound)
* @param {string[]} src Path to the audio file
*/
var add_sound_effect = function (src) {
var audioHTML = "<audio src='" + src + "' preload></audio>";
$('body').append(audioHTML);
return $(audioHTML)[0];
}
/**
* Adds audio with loop and without preload, autoplay.
* @example
* song = add_audio("uptown-funk.mp3")
* play_audio(song)
* @param {string[]} src Path to the audio file
*/
var add_audio = function (src) {
var audioHTML = "<audio src='" + src + "' loop></audio>";
$('body').append(audioHTML);
return $(audioHTML)[0];
}
/**
* Plays the audio object passed as parameter
* @example
* boom = add_audio("boom.mp3");
* play_audio(boom);
* @param {object} audio audio object which is to be played.
*/
var play_audio = function (audio) {
audio.play();
}
/**
* Change the position of element to position specified
* @example
* demo = add_image("demo.png", 50);
* place_element(demo, 20, 100);
* @param {object} element el object whose position is required.
* @param {number} left required left position of element
* @param {number} top required top position of element
*/
var place_element = function (el, left, top) {
el.css({
'position': 'absolute',
'top': top + 'px',
'left': left + 'px'
});
};
/**
* Adds a image to project
* @example
* add_image("demo.png", 100)
* @param {string[]} src path of image to be added
* @param {number} size width of the image
*/
var add_image = function (src, size) {
console.log(src);
var filename = src.replace(/^.*[\\\/]/, ''); // gets the filename from file path
var name = filename.split('.')[0];
var imgClass = name;
var id = name + $('.' + name).length;
Canvas.append('<img class="' + imgClass + '" src ="' + src + '" id = "' + id + '" width="' + size + 'px"/>');
$('#' + id).css({
'position': 'absolute'
});
return $('#' + id);
};
/**
* Resizes an image
*/
var resize_image = function (el, size) {
$(el).css('width', size + 'px');
}
/**
* Adds text using a p tag with a unique id.
* @example
* print_text("Hello world", 24)
* @param {string[]} text content of text
* @param {number} size size of text
*/
var print_text = function (text, size) {
uid = 'text' + $('p').length;
Canvas.append('<p id="' + uid + '" style="font-size:' + size + 'px">' + text + '</p>');
return $('#' + uid);
};
/**
* Updates the text element added using print_text
* @example
* text = print_text("Hello world", 25)
* update_text(text, "Hi there")
* @param {object} el text element whose text is to be updated
* @param {string[]} text text message
*/
var update_text = function (el, text) {
el.text(text);
};
/**
* Show error message using alert
* @example
* alert("Bug on line 24")
* @param {string[]} error Error message to be shown
*/
var showError = function (error) {
alert(error);
};
/*
Incomplete attempt to create weapon firing capability
Issues left to solve:
-moving weapons with player element
-Directional aim (partially complete, see v2)
-Implementing collision detection
var arm_player = function (el, img, num) {
var weapon;
var top = $(el).css('top');
var left = $(el).css('left');
left = parseInt(left);
top = parseInt(top);
console.log(typeof left);
for (let i = 0; i < num; i++) {
weapon = add_image(img, 80);
place_element(weapon, left, top);
}
fire_weapon(num, weapon);
//return weapon;
}
var fire_weapon = function (num, weapon) {
counter = num;
shoot = {
'top': '-100px'
}
imgName = $(weapon).attr('class');
console.log(imgName);
document.addEventListener('click', function () {
counter = counter - 1
$('#' + imgName + counter).animate(shoot, 250);
})
}
*/
/**
* Defines the bouncing effect used by the bounce function
*
* @param {object} el element on which animation is to be applied
* @param {number} scale magnitude of bounce effect
* @param {number} pause delay before animation start
*/
var bounce_animate = function (el, scale, pause) {
randomDuration = Math.floor(Math.random() * 500);
pause = typeof pause !== 'undefined' ? pause : pause_timeTime;
var aniObj = {
duration: 1000 + randomDuration,
queue: 'bounce' + el[0].id
};
el.delay(pause, 'bounce' + el[0].id).animate({
"top": "+=" + scale + "px"
}, aniObj).animate({
"top": "-=" + scale + "px"
}, {
duration: aniObj.duration,
queue: aniObj.queue,
complete: function () {
bounce_animate(el, scale, pause);
}
});
};
/**
* Adds/increments delay time to animation
* @example
* pause_time(2)
* @param time delay time in seconds
*/
var pause_time = function (time) {
pause_timeTime += time * 1000;
};
/**
* Adds bounce effect to the element. Uses bounce_animate for effect
* @example
* demo = add_image("ball.png", 30)
* bounce(demo, 20)
* @param {object} el element on which animation is to be applied
* @param {number} scale magnitude of bounce effect
*/
var bounce = function (el, scale) {
bounce_animate(el, scale);
el.dequeue('bounce' + el[0].id);
};
/**
* Remove an element with fade effect
* @example
* demo = add_image("demo.png", 20)
* remove_el(demo)
* @param {object} el element to be removed
*/
var remove_el = function (el) {
var blastCSS = {
'width': '80',
'height': 'auto',
'opacity': 0,
}
$(el).animate(blastCSS, function () {
$(el).remove();
});
}
/**
* Makes an element jump the specified Y value on click
*/
var jump = function (el, how_high, speed) {
document.addEventListener("click", function () {
var currentYPos = $(el).css("top");
//console.log(currentYPos)
var currentYPosNum = parseInt(currentYPos);
newHeight = currentYPosNum - how_high;
//console.log(newHeight);
height = {
"top": newHeight + 'px'
}
current = {
"top": currentYPos
}
$(el).animate(height, speed);
$(el).animate(current, speed);
});
}
/**
* Makes an element jump the specified Y value on click of space bar
*/
var jump_spacebar = function (el, how_high, speed) {
$(document).keydown(function (event) {
var key = event.originalEvent.code;
console.log(key)
if (key == "Space") {
var currentYPos = $(el).css("top");
//console.log(currentYPos)
var currentYPosNum = parseInt(currentYPos);
newHeight = currentYPosNum - how_high;
//console.log(newHeight);
height = {
"top": newHeight + 'px'
}
current = {
"top": currentYPos
}
$(el).animate(height, speed);
$(el).animate(current, speed);
}
});
}
/**
* Moves the element horizontally. Use moveHAnimation for animation effect.
* @example
* var demo = add_image("ball.png", 30);
* animate_x(demo, 20, 80, 2, false, 100);
* @param {object} el element on which animation is to be applied
* @param {number} initialX initial position of element
* @param {number} finalX final position of element after moving
* @param {number} turns turns of how many time animation should take effect, infinite for never ending animation
* @param {boolean} alternate if true animates the element to final and then back to initial, if false doesn't animate the element from final to initial position.
* @param {number} speed speed of moving
*/
var animate_x = function (el, initialX, finalX, turns, alternate, speed) {
var randomSpeed = Math.floor(Math.random() * 100) + 100;
speed = typeof speed !== 'undefined' ? speed : randomSpeed;
var distance = Math.abs(finalX - initialX);
var duration = (distance / speed) * 1000;
moveHAnimation(el, initialX, finalX, turns, alternate, duration, 'linear');
el.dequeue('animate_x' + el[0].id);
}
/**
* Moves the element vertically. Use moveVAnimation for animation effect.
* @example
* var demo = add_image("ball.png", 30);
* animate_y(demo, 20, 80, 2, false, 100);
* @param {object} el element on which animation is to be applied
* @param {number} initialY initial position of element
* @param {number} finalY final position of element after moving
* @param {number} turns turns of how many time animation should take effect, infinite for never ending animation
* @param {boolean} alternate if true animates the element to final and then back to initial, if false doesn't animate the element from final to initial position.
* @param {number} speed speed of moving
*/
var animate_y = function (el, initialY, finalY, turns, alternate, speed, pause) {
var randomSpeed = Math.floor(Math.random() * 100) + 100;
speed = typeof speed !== 'undefined' ? speed : randomSpeed;
pause = typeof pause !== 'undefined' ? pause : pause_timeTime;
var distance = Math.abs(finalY - initialY);
var duration = (distance / speed) * 1000;
moveVAnimation(el, initialY, finalY, turns, alternate, duration, 'linear', pause);
el.dequeue('animate_y' + el[0].id);
}
/**
* Defines the vertical animation being used by animate_y
* @param {object} el element on which animation is to be applied
* @param {number} initialY initial position of element
* @param {number} finalY final position of element after moving
* @param {number} turns turns of how many time animation should take effect, infinite for never ending animation
* @param {boolean} alternate if true animates the element to final and then back to initial, if false doesn't animate the element from final to initial position.
* @param {number} duration duration in milliseconds
* @param {string[]} easing animation effect
* @param {number} pause animation delay
*/