1
0

tools.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. function heriter(destination, source) {
  2. function initClassIfNecessary(obj) {
  3. if( typeof obj["_super"] == "undefined" ) {
  4. obj["_super"] = function() {
  5. var methodName = arguments[0];
  6. var parameters = arguments[1];
  7. return this["__parent_methods"][methodName].apply(this, parameters);
  8. }
  9. }
  10. if( typeof obj["__parent_methods"] == "undefined" ) {
  11. obj["__parent_methods"] = {};
  12. }
  13. }
  14. for (var element in source) {
  15. if( typeof destination[element] != "undefined" ) {
  16. initClassIfNecessary(destination);
  17. destination["__parent_methods"][element] = source[element];
  18. } else {
  19. destination[element] = source[element];
  20. }
  21. }
  22. }
  23. function distance(point1, point2) {
  24. return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
  25. }
  26. function normalize(vector) {
  27. var norm = Math.sqrt(vector.x * vector.x + vector.y * vector.y);
  28. if (norm == 0)
  29. return {
  30. x : 0,
  31. y : 0
  32. };
  33. var x = vector.x / norm;
  34. var y = vector.y / norm;
  35. return {
  36. x : x,
  37. y : y
  38. }
  39. }
  40. function angle(vector1, vector2) {
  41. if ((vector1.x == 0 && vector1.y == 0)
  42. && (vector1.x == 0 && vector1.y == 0)) {
  43. return 0;
  44. }
  45. if (vector1.x == 0 && vector1.y == 0) {
  46. return Math.acos(vector2.x
  47. / Math.sqrt(vector2.x * vector2.x + vector2.y * vector2.y));
  48. }
  49. if (vector2.x == 0 && vector2.y == 0) {
  50. return Math.acos(vector1.x
  51. / Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y));
  52. }
  53. var product = vector1.x * vector2.x + vector1.y * vector2.y,
  54. alpha = Math.acos(product
  55. / (Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y) * Math
  56. .sqrt(vector2.x * vector2.x + vector2.y * vector2.y))),
  57. sign = normalize(vector1).y > normalize(vector2).y ? -1 : 1;
  58. return sign * alpha;
  59. }
  60. function getAliensMidHeight() {
  61. var higherAlien = Math.max.apply( null,
  62. $(".alien").map(function() {
  63. return $(this).y();
  64. }).get() ),
  65. lowerAlien = Math.min.apply( null,
  66. $(".alien").map(function() {
  67. return $(this).y();
  68. }).get() );
  69. return (higherAlien + lowerAlien) / 2;
  70. }