GameAudioMgr.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { _decorator, assetManager, AudioClip, Component, Node, Vec3 } from 'cc';
  2. import { ModuleDef } from '../../scripts/ModuleDef';
  3. const BundleName = ModuleDef.GAME;
  4. export class GameAudioMgr {
  5. /**
  6. * @en
  7. * play short audio, such as strikes,explosions
  8. * @zh
  9. * 播放短音频,比如 打击音效,爆炸音效等
  10. * @param sound clip or url for the audio
  11. * @param volume
  12. */
  13. public static playOneShot(sound: AudioClip | string, volume: number = 1.0) {
  14. tgx.AudioMgr.inst.playOneShot(sound, volume, BundleName);
  15. }
  16. /**
  17. * @en
  18. * play long audio, such as the bg music
  19. * @zh
  20. * 播放长音频,比如 背景音乐
  21. * @param sound clip or url for the sound
  22. * @param volume
  23. */
  24. public static play(sound: AudioClip | string, volume: number = 1.0,) {
  25. tgx.AudioMgr.inst.play(sound, volume, BundleName);
  26. }
  27. /**
  28. * stop the audio play
  29. */
  30. public static stop() {
  31. tgx.AudioMgr.inst.stop();
  32. }
  33. /**
  34. * pause the audio play
  35. */
  36. public static pause() {
  37. tgx.AudioMgr.inst.pause();
  38. }
  39. /**
  40. * resume the audio play
  41. */
  42. public static resume() {
  43. tgx.AudioMgr.inst.audioSource.play();
  44. }
  45. public static getVolumeByDist(soundPos: { x: number, y: number, z: number }, earPos: { x: number, y: number, z: number }, maxDist: number = 50.0) {
  46. let dx = soundPos.x - earPos.x;
  47. let dy = soundPos.y - earPos.y;
  48. let dz = 0;
  49. let dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
  50. if(dist < 1.0){
  51. return 1.0;
  52. }
  53. let factor = 1.0 / (dist / maxDist);
  54. if (factor > 1.0) {
  55. factor = 1.0;
  56. }
  57. return factor
  58. }
  59. }