2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

87 linhas
2.2 KiB

  1. function AudioForm(el) {
  2. const interval = el.elements['interval']
  3. const play = el.elements['play']
  4. const output = el.querySelector('output')
  5. const audioPool = []
  6. let intervalHandle = null
  7. let audioIndex = 0
  8. const items = 50
  9. /*
  10. for (let i = 0; i < items; i += 1) {
  11. const newAudio = new Audio('./sfx/weapons/guns/smg-hkmp7/1_unload_var0.wav')
  12. newAudio.volume = 0.2;
  13. newAudio.addEventListener('canplaythrough', () => {
  14. audioPool[i].ready = true
  15. })
  16. newAudio.addEventListener('ended', () => {
  17. newAudio.pause()
  18. newAudio.currentTime = 0
  19. })
  20. audioPool[i] = {
  21. audio: newAudio,
  22. ended: true,
  23. ready: false,
  24. }
  25. newAudio.load()
  26. }
  27. */
  28. let started = false;
  29. window.addEventListener('click', () => {
  30. if (started) {
  31. return;
  32. }
  33. started = true;
  34. const audioContext = new AudioContext();
  35. const gain = audioContext.createGain();
  36. gain.gain.setValueAtTime(0, audioContext.currentTime);
  37. const oscillator = audioContext.createOscillator();
  38. oscillator.type = 'square';
  39. oscillator.frequency.setValueAtTime(1000, audioContext.currentTime);
  40. oscillator.start();
  41. oscillator.connect(gain);
  42. gain.connect(audioContext.destination);
  43. const playSound = () => {
  44. gain.gain.setValueAtTime(0.25, audioContext.currentTime);
  45. setTimeout(() => {
  46. gain.gain.setValueAtTime(0, audioContext.currentTime);
  47. }, 10);
  48. /*
  49. for (let i = 0; i < items; i += 1) {
  50. if (!audioPool[i].ready) {
  51. return;
  52. }
  53. }
  54. audioPool[audioIndex].audio.play();
  55. audioIndex = (audioIndex + 1) % items;
  56. */
  57. }
  58. el.addEventListener('submit', (e) => {
  59. e.preventDefault()
  60. const ms = 60000 / Number(interval.value);
  61. output.innerText = `${ms} ms`
  62. if (play.checked) {
  63. intervalHandle = window.setInterval(() => {
  64. playSound()
  65. }, ms)
  66. } else {
  67. gain.gain.setValueAtTime(0, audioContext.currentTime);
  68. window.clearInterval(intervalHandle);
  69. }
  70. play.checked = !play.checked
  71. })
  72. })
  73. }
  74. const audioForm = new AudioForm(window.document.getElementById('formAudio'))