No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

94 líneas
1.6 KiB

  1. define([
  2. 'howler',
  3. 'js/misc/physics'
  4. ], function (
  5. howler,
  6. physics
  7. ) {
  8. return {
  9. sounds: [],
  10. init: function (zoneId) {
  11. this.unload(zoneId);
  12. },
  13. unload: function (zoneId) {
  14. this.sounds.forEach(function (s) {
  15. if ((s.sound) && (s.zoneId != zoneId))
  16. s.sound.unload();
  17. });
  18. this.sounds.spliceWhere(function (s) {
  19. return (s.zoneId != zoneId);
  20. });
  21. },
  22. update: function (x, y) {
  23. this.sounds.forEach(function (s) {
  24. var volume = 1;
  25. if (!s.w) {
  26. var dx = Math.abs(s.x - x);
  27. if (dx > 10) {
  28. if (s.sound)
  29. s.sound.volume(0);
  30. return;
  31. }
  32. var dy = Math.abs(s.y - y);
  33. if (dy > 10) {
  34. if (s.sound)
  35. s.sound.volume(0);
  36. return;
  37. }
  38. var dist = 10 - Math.max(dx, dy);
  39. dist = (dist * dist) / 100;
  40. volume = 0.3 * dist;
  41. } else {
  42. if (!s.area) {
  43. var inside = (!((x < s.x) || (y < s.y) || (x >= s.x + s.w) || (y >= s.y + s.h)));
  44. if (!inside) {
  45. if (s.sound)
  46. s.sound.volume(0);
  47. return;
  48. }
  49. } else {
  50. var inside = physics.isInPolygon(x, y, s.area);
  51. if (!inside) {
  52. if (s.sound)
  53. s.sound.volume(0);
  54. return;
  55. }
  56. }
  57. }
  58. if (!s.sound) {
  59. s.sound = new Howl({
  60. src: ['audio/' + s.file],
  61. autoplay: true,
  62. loop: true,
  63. volume: 0
  64. });
  65. }
  66. s.sound.volume(volume * s.volume);
  67. });
  68. },
  69. addSound: function (zoneId, file, volume, x, y, w, h, area) {
  70. var sound = {
  71. file: file,
  72. x: x,
  73. y: y,
  74. w: w,
  75. h: h,
  76. volume: volume,
  77. area: area,
  78. sound: null,
  79. zoneId: zoneId
  80. };
  81. this.sounds.push(sound);
  82. }
  83. };
  84. });