You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

73 lines
1.1 KiB

  1. define([
  2. 'howler'
  3. ], function (
  4. howler
  5. ) {
  6. return {
  7. sounds: [],
  8. init: function (zone) {
  9. this.unload();
  10. if (zone != 'fjolarok')
  11. return;
  12. this.addSound('fire.ogg', 123, 123);
  13. this.addSound('stream.ogg', 107, 69);
  14. this.addSound('wind.ogg', 176, 104);
  15. },
  16. unload: function () {
  17. this.sounds.forEach(function (s) {
  18. if (s.sound)
  19. s.sound.unload();
  20. });
  21. this.sounds = [];
  22. },
  23. update: function (x, y) {
  24. this.sounds.forEach(function (s) {
  25. let dx = Math.abs(s.x - x);
  26. if (dx > 10) {
  27. if (s.sound)
  28. s.sound.volume(0);
  29. return;
  30. }
  31. let dy = Math.abs(s.y - y);
  32. if (dy > 10) {
  33. if (s.sound)
  34. s.sound.volume(0);
  35. return;
  36. }
  37. let dist = 10 - Math.max(dx, dy);
  38. dist = (dist * dist) / 100;
  39. let volume = 0.3 * dist;
  40. if (!s.sound) {
  41. s.sound = new Howl({
  42. src: ['audio/' + s.file],
  43. autoplay: true,
  44. loop: true,
  45. volume: 0
  46. });
  47. }
  48. s.sound.volume(volume);
  49. });
  50. },
  51. addSound: function (file, x, y) {
  52. let sound = {
  53. file: file,
  54. x: x,
  55. y: y,
  56. sound: null
  57. };
  58. this.sounds.push(sound);
  59. }
  60. };
  61. });