设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 103|回复: 0

[转载发布] [CloudTheWolf] Event Sounds

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    9071

    主题

    864

    回帖

    6万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    58883
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    68848

    灌水之王

    发表于 2026-8-3 07:49:23 | 显示全部楼层 |阅读模式
    Something I recently wanted to add to my project was Event sounds that get louder the closer you were to them.

    I found ways to do this in Event functions using regions, however this felt clunky and a pain to implement, so I turned to scripts. I couldn't seem to find any that would work so ended up having to make my own.

    After a lot of trial and error I managed to get it working with as much functionality as I could and thought other might want to have a similar system in their games.

    All it needs is your event to have the following comment block:

    [sound "SOUND_NAME" max_volume min_volume range]

    Eg

    [sound "Fire" 80 30 8]

    Alternative Links:
    Official Website: https://cloudthewolf.com/view-file/ctw-event-sounds-rb
    GitHub: https://github.com/CloudTheWolf/rpg-maker-vx-ace-scripts/blob/master/audio/Clouds Event Sounds.rb
                    Ruby:       
    1. #===============================================================================
    2. #
    3. # CloudTheWolf Event Sounds
    4. # Author: CloudTheWolf
    5. # Date (18/06/2023)
    6. # Version: (1.0.1) (VXA)
    7. # Level: (Easy)
    8. #
    9. #
    10. #===============================================================================
    11. # Change Log:
    12. #   - 1.0.0
    13. #     Initial Release
    14. #
    15. #   - 1.0.1
    16. #     Fix tranitioning from a map with an even sound to a map with a BGS set
    17. #===============================================================================
    18. #
    19. # NOTES: 1) This script will only work with ace.
    20. #        2) This will only work with the closest event.
    21. #        3) This will not work if the map has a BGS Set
    22. #
    23. #===============================================================================
    24. #
    25. # Description: Adds a BGS Sound Source to an event
    26. #
    27. # Credits: Me (CloudTheWolf)
    28. #
    29. #===============================================================================
    30. #
    31. # Instructions
    32. # Paste above ▼ Below and above ▼ Main Process.
    33. #
    34. # Add the following comment to your event:
    35. # [sound "SOUND_NAME" max_volume min_volume range]
    36. #
    37. #  Eg.
    38. #  [sound "Fire" 50 2 7]
    39. #
    40. #  This will play to firew BGS when the player is within the range.
    41. #  Outside of the range it will play at the min_volume so set to 0 if you don't
    42. #  want it on the while map
    43. #
    44. #  FAQ:
    45. #
    46. #  Q: Can I have more than 1 sound source on a map?
    47. #  A: Yes, however, please not only 1 can play at a time.
    48. #     This is based on which ever is closest to the player.
    49. #  Q: The BGS Still plays even when leaving the map
    50. #  A: In your Map Settings just enable "Change BGS" and set it to none.
    51. #
    52. #  Q: Does this work with X, Y or Z?
    53. #  A: This has not been testing with other scripts so this may or may not work
    54. #     
    55. #===============================================================================
    56. #
    57. # Free for any use as long as I'm credited.
    58. #
    59. #===============================================================================
    60. #==============================================================================
    61. # ** Game_Map
    62. #------------------------------------------------------------------------------
    63. #  This class handles maps. It includes scrolling and passage determination
    64. # functions. The instance of this class is referenced by $game_map.
    65. #==============================================================================
    66. class Game_Map
    67.   attr_accessor :custom_bgs_playing
    68.   # Calculate the straight-line distance between two points
    69.   def distance(x1, y1, x2, y2)
    70.     Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    71.   end
    72. # Update BGS settings for events on the map
    73.   def update_event_sounds
    74.     closest_event = nil
    75.     closest_distance = Float::INFINITY
    76.     closest_range = nil
    77.     custom_bgs_name = nil
    78.     events.each_value do |event|
    79.       comment = event.list.select { |command| command.code == 108 || command.code == 408 }
    80.       next unless comment
    81.       sound_matches = comment.map { |command| command.parameters[0].match(/\[sound\s+"(.+)"\s+(\d+)\s+(\d+)\s+(\d+)\]/) }.compact
    82.       sound_matches.each do |sound_match|
    83.         bgs_name = sound_match[1]
    84.         max_volume = sound_match[2].to_i
    85.         min_volume = sound_match[3].to_i
    86.         range = sound_match[4].to_i
    87.         player = $game_player
    88.         distance = distance(event.x, event.y, player.x, player.y)           
    89.         
    90.         if closest_event == nil || (distance <= range && distance < closest_distance)
    91.           closest_event = event
    92.           closest_distance = distance
    93.           closest_range = range
    94.           custom_bgs_name = bgs_name
    95.         end
    96.       end
    97.     end
    98.     if closest_event && custom_bgs_name
    99.         comment = closest_event.list.select { |command| command.code == 108 || command.code == 408 }
    100.         sound_match = comment.map { |command| command.parameters[0].match(/\[sound\s+"(.+)"\s+(\d+)\s+(\d+)\s+(\d+)\]/) }.compact.first
    101.         bgs_name = sound_match[1]
    102.         max_volume = sound_match[2].to_i
    103.         min_volume = sound_match[3].to_i     
    104.         if closest_distance <= closest_range
    105.           current_volume = $temp_bgs_volume || 0
    106.           target_volume = max_volume - ((max_volume - min_volume) * closest_distance / closest_range).to_i
    107.           if current_volume != target_volume || $temp_bgs_name != bgs_name
    108.             Audio.bgs_stop if current_volume == 0 && $temp_bgs_name != bgs_name
    109.             Audio.bgs_play("Audio/BGS/#{bgs_name}", target_volume, 100, 0)
    110.             $temp_bgs_name = bgs_name
    111.           end
    112.           $temp_bgs_volume = target_volume
    113.         else
    114.           Audio.bgs_play("Audio/BGS/#{bgs_name}", min_volume, 100, 0) unless min_volume == 0
    115.           self.custom_bgs_playing = bgs_name
    116.         end
    117.       else
    118.         self.custom_bgs_playing = nil
    119.     end
    120.   end
    121.   alias ces_setup setup
    122.   def setup(map_id)
    123.     # Stop only the custom BGS before changing maps
    124.     if self.custom_bgs_playing
    125.       Audio.bgs_stop
    126.       self.custom_bgs_playing = nil
    127.     end
    128.     ces_setup(map_id)
    129.   end
    130. end
    131. #==============================================================================
    132. # ** Scene_Map
    133. #------------------------------------------------------------------------------
    134. #  This class performs the map screen processing.
    135. #==============================================================================
    136. class Scene_Map < Scene_Base
    137.   # ...
    138.   # Update scene (called every frame)
    139.   alias ces_update update
    140.   def update
    141.     ces_update
    142.     update_dynamic_sounds
    143.     # ... Other update logic for the scene
    144.   end
    145.   # Update dynamic sound effects
    146.   def update_dynamic_sounds
    147.     $game_map.update_event_sounds
    148.   end
    149.   # ...
    150. end
    复制代码




    本贴来自国际rpgmaker官方论坛作者:CloudTheWolf处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/cloudthewolf-event-sounds.158451/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-8-17 08:29 , Processed in 0.136554 second(s), 55 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表