14_SoundEffects.as 6.0 KB

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