14_SoundEffects.as 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Sound effects example
  2. // This sample demonstrates:
  3. // - Playing sound effects and music
  4. // - Controlling sound and music master volume
  5. #include "Scripts/Utilities/Sample.as"
  6. Scene@ scene_;
  7. Array<String> soundNames = {
  8. "Fist",
  9. "Explosion",
  10. "Power-up"
  11. };
  12. Array<String> soundResourceNames = {
  13. "Sounds/PlayerFistHit.wav",
  14. "Sounds/BigExplosion.wav",
  15. "Sounds/Powerup.wav"
  16. };
  17. void Start()
  18. {
  19. // Execute the common startup for samples
  20. SampleStart();
  21. // Enable OS cursor
  22. input.mouseVisible = true;
  23. // Create the user interface
  24. CreateUI();
  25. }
  26. void CreateUI()
  27. {
  28. // Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  29. scene_ = Scene();
  30. XMLFile@ uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  31. // Set style to the UI root so that elements will inherit it
  32. ui.root.defaultStyle = uiStyle;
  33. // Create buttons for playing back sounds
  34. for (uint i = 0; i < soundNames.length; ++i)
  35. {
  36. Button@ button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]);
  37. // Store the sound effect resource name as a custom variable into the button
  38. button.vars["SoundResource"] = soundResourceNames[i];
  39. SubscribeToEvent(button, "Pressed", "HandlePlaySound");
  40. }
  41. // Create buttons for playing/stopping music
  42. Button@ button = CreateButton(20, 80, 120, 40, "Play Music");
  43. SubscribeToEvent(button, "Released", "HandlePlayMusic");
  44. button = CreateButton(160, 80, 120, 40, "Stop Music");
  45. SubscribeToEvent(button, "Released", "HandleStopMusic");
  46. // Create sliders for controlling sound and music master volume
  47. Slider@ slider = CreateSlider(20, 140, 200, 20, "Sound Volume");
  48. slider.value = audio.masterGain[SOUND_EFFECT];
  49. SubscribeToEvent(slider, "SliderChanged", "HandleSoundVolume");
  50. slider = CreateSlider(20, 200, 200, 20, "Music Volume");
  51. slider.value = audio.masterGain[SOUND_MUSIC];
  52. SubscribeToEvent(slider, "SliderChanged", "HandleMusicVolume");
  53. }
  54. Button@ CreateButton(int x, int y, int xSize, int ySize, const String&in text)
  55. {
  56. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  57. // Create the button and center the text onto it
  58. Button@ button = ui.root.CreateChild("Button");
  59. button.SetStyleAuto();
  60. button.SetPosition(x, y);
  61. button.SetSize(xSize, ySize);
  62. Text@ buttonText = button.CreateChild("Text");
  63. buttonText.SetAlignment(HA_CENTER, VA_CENTER);
  64. buttonText.SetFont(font, 12);
  65. buttonText.text = text;
  66. return button;
  67. }
  68. Slider@ CreateSlider(int x, int y, int xSize, int ySize, const String& text)
  69. {
  70. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  71. // Create text and slider below it
  72. Text@ sliderText = ui.root.CreateChild("Text");
  73. sliderText.SetPosition(x, y);
  74. sliderText.SetFont(font, 12);
  75. sliderText.text = text;
  76. Slider@ slider = ui.root.CreateChild("Slider");
  77. slider.SetStyleAuto();
  78. slider.SetPosition(x, y + 20);
  79. slider.SetSize(xSize, ySize);
  80. // Use 0-1 range for controlling sound/music master volume
  81. slider.range = 1.0f;
  82. return slider;
  83. }
  84. void HandlePlaySound(StringHash eventType, VariantMap& eventData)
  85. {
  86. Button@ button = GetEventSender();
  87. String soundResourceName = button.vars["SoundResource"].GetString();
  88. // Get the sound resource
  89. Sound@ sound = cache.GetResource("Sound", soundResourceName);
  90. if (sound !is null)
  91. {
  92. // Create a scene node with a SoundSource component for playing the sound. The SoundSource component plays
  93. // non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
  94. // SoundSource3D component would be used instead
  95. Node@ soundNode = scene_.CreateChild("Sound");
  96. SoundSource@ soundSource = soundNode.CreateComponent("SoundSource");
  97. soundSource.Play(sound);
  98. // In case we also play music, set the sound volume below maximum so that we don't clip the output
  99. soundSource.gain = 0.7f;
  100. // Set the sound component to automatically remove its scene node from the scene when the sound is done playing
  101. soundSource.autoRemove = true;
  102. }
  103. }
  104. void HandlePlayMusic(StringHash eventType, VariantMap& eventData)
  105. {
  106. // Check if the music player node/component already exist
  107. if (scene_.GetChild("Music") !is null)
  108. return;
  109. Sound@ music = cache.GetResource("Sound", "Music/Ninja Gods.ogg");
  110. // Set the song to loop
  111. music.looped = true;
  112. // Create a scene node and a sound source for the music
  113. Node@ musicNode = scene_.CreateChild("Music");
  114. SoundSource@ musicSource = musicNode.CreateComponent("SoundSource");
  115. // Set the sound type to music so that master volume control works correctly
  116. musicSource.soundType = SOUND_MUSIC;
  117. musicSource.Play(music);
  118. }
  119. void HandleStopMusic(StringHash eventType, VariantMap& eventData)
  120. {
  121. // Remove the music player node from the scene
  122. scene_.RemoveChild(scene_.GetChild("Music"));
  123. }
  124. void HandleSoundVolume(StringHash eventType, VariantMap& eventData)
  125. {
  126. float newVolume = eventData["Value"].GetFloat();
  127. audio.masterGain[SOUND_EFFECT] = newVolume;
  128. }
  129. void HandleMusicVolume(StringHash eventType, VariantMap& eventData)
  130. {
  131. float newVolume = eventData["Value"].GetFloat();
  132. audio.masterGain[SOUND_MUSIC] = newVolume;
  133. }