Enelvon

SES: Play SE on Walk

Apr 14th, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.30 KB | None | 0 0
  1. ###############################################################################
  2. # SES: Play SE on Walk
  3. # v1.1
  4. # 4.14.2012
  5. # Compatibility: VXAce/RGSS3
  6. # Author: Enelvon
  7. # Requested by Ae
  8. #===============================================================================
  9. # Terms of Use:
  10. #
  11. # This script may be used for free in any game, whether it's commercial or not.
  12. # The only requirement is that I be credited in some visible manner. You may not
  13. # claim this script as your own work. You are free to modify this script, but
  14. # may not redistribute it except for in a thread that I have started that relates
  15. # to it in some way.
  16. #===============================================================================
  17. # Changelog:
  18. # 4.14.2012: v1.0 - Script written.
  19. # 4.14.2012: v1.1 - Now allows offscreen events to update in a radius of your
  20. #                   choosing, but they won't be making noise unless you set them
  21. #                   to. Added support for Tileset IDs instead of Map IDs.
  22. #===============================================================================
  23. # This script was requested by my dear friend, Ae. They wanted to recreate a
  24. # function from RPG Maker 2003 that allowed a sound effect to be played when
  25. # the player was walking on certain tiles.
  26. #===============================================================================
  27. # Required Scripts: Enelvon Script Core v1.0 or higher
  28. # Known Incompatibilities: None, though there are some redefinitions so it's a
  29. #     possibility that some may occur. Refer to the Redefinitions section at the
  30. #     bottom of my comments for a complete list of what was redefined.
  31. #===============================================================================
  32. # Installation: Place below Materials and the Enelvon Script Core. Edit the
  33. # constants in SES::WalkSE to customize the script.
  34. #===============================================================================
  35. # Instructions:
  36. #
  37. # ***Constants for SES::WalkSE***
  38. #
  39. # Events_Make_Sound - setting this to true causes events to make sounds when
  40. #     walking on tiles that produce sounds, just like players do.
  41. #
  42. # Use_Tileset_ID - if this is set to true, the IDs for WalkingSoundsTerrain will
  43. #     refer to Tileset IDs for what sound to produce. If it is set to false, it
  44. #     will refer to map IDs.
  45. #
  46. # WalkingSoundsTerrain - this is a hash of SEs by Terrain Tag ID and Tileset or
  47. #     Map ID. It consists of hashes (one per map ID) and three-part arrays. The
  48. #     first part is the name of the SE to play when stepping on a tile with the
  49. #     given terrain tag. The second is the volume of the sound. The third is the
  50. #     pitch of the sound.
  51. #
  52. # WalkingSoundsRegion - this is a hash of SEs by Region ID. Values in this will
  53. #     overwrite values from WalkingSoundsTerrain, and are universal across all
  54. #     maps and tilesets. It consists of three-part arrays written in the same
  55. #     format as those for terrain tag-based sounds, minus the Map/Tileset ID
  56. #     hash.
  57. #
  58. # Event_Update_X - this is a hash consisting of Map IDs and X values. By default
  59. #     VXAce only updates events that are within twelve squares horizontally of
  60. #     the center of the screen. This is inconvenient for some game types, such as
  61. #     those that are heavily stealth-based. This setting allows you to change the
  62. #     horizontal update distance on a per-map basis. Setting it to 999 allows for
  63. #     updating of all events on the map, but may produce lag, especially on maps
  64. #     that have a heavy event load. This setting may be completely ineffective if
  65. #     you don't also edit Event_Update_Y.
  66. #
  67. # Event_Update_Y - this is a hash consisting of Map IDs and Y values. By default
  68. #     VXAce only updates events that are within eight squares vertically of the
  69. #     center of the screen. This is inconvenient for some game types, such as
  70. #     those that are heavily stealth-based. This setting allows you to change the
  71. #     vertical update distance on a per-map basis. Setting it to 999 allows for
  72. #     updating of all events on the map, but may produce lag, especially on maps
  73. #     that have a heavy event load. This setting may be completely ineffective if
  74. #     you don't also edit Event_Update_X.
  75. #
  76. # Sound_Toggle_Events - this is the RegExp that allows events to make or not make
  77. #     sounds individually, ignoring the value of Events_Make_Sound. There is more on
  78. #     this in the next section.
  79. #
  80. # Event_Unique_Sound - this is the RegExp that allows events to make their own
  81. #     sound regardless of what would normally be produced by the tile they're
  82. #     stepping on. There is more on this in the next section.
  83. #
  84. # Event_Always_Sound - this is the RegExp that allows events to make sounds even
  85. #     while offscreen. There is more on this in the next section.
  86. #
  87. # ***Additional: Event Comments***
  88. #
  89. # You can customize this script for each event page in your game by adding tags
  90. # in Comments boxes. Each box can contain only one tag, unfortunately - but you
  91. # can have as many boxes as you want. These are the tags available in this script:
  92. #
  93. # MakeWalkSound: !Bool!
  94. #    Place this in a Comments box to cause the event to play/not play a sound
  95. #    regardless of what Events_Make_Sound is set to.
  96. #    Replacements:
  97. #     !Bool! with either true (play a sound) or false (don't play a sound)
  98. #
  99. # WalkSound: !SE!, !Volume!, !Pitch!
  100. #   Place this in a Comments box to change the sound played when the event walks.
  101. #   It will always play while the event is moving, regardless of what sounds the
  102. #   tiles it's walking on would usually play.
  103. #   Replacements:
  104. #     !SE! with the name of the sound, minus its extension.
  105. #     !Volume! with the volume of the sound, 0-100.
  106. #     !Pitch! with the pitch of the sound, 0-100.
  107. #
  108. # AlwaysSound: !Volume!, !Pitch!
  109. #   Place this in a Comments box to cause an event to produce walking sounds
  110. #   even when offscreen. NOTE: Events will not make sounds outside of the map's
  111. #   update range even if they have this tag.
  112. #   Replacements:
  113. #     !Volume! with the volume of the walking sound when the event is offscreen.
  114. #     !Pitch! with the pitch of the walking sound when the event is offscreen.
  115. #
  116. #===============================================================================
  117. # Redefinitions:
  118. #
  119. # These are the methods that are redefined by this script. This information is
  120. # included for purposes of pinpointing compatibility errors between scripts.
  121. #
  122. # ***class Game_Characterbase***
  123. #
  124. #   move_straight(d, turn_ok = true)
  125. #
  126. # ***class Game_Event***
  127. #
  128. #   near_the_screen?(dx = 12, dy = 8)
  129. #
  130. ################################################################################
  131.  
  132. module SES
  133.   module WalkSE
  134.  
  135.     Events_Make_Sound = true # true - events will make walking sounds
  136.                              # false - events will not make walking sounds
  137.    
  138.     Use_Tileset_ID = true # true - WalkingSoundsTerrain will use Tileset IDs
  139.                           # false - WalkingSoundsTerrain will use Map IDs
  140.    
  141.     WalkingSoundsTerrain = {
  142.     #Map/Tileset ID => {
  143.     1 => {
  144.         #Terrain Tag => [Sound, Volume, Pitch],
  145.           1 => ["Knock", 80, 100],
  146.          
  147.          },
  148.    
  149.     }
  150.     # Default hash for Map/Tileset IDs that aren't listed in WalkingSoundsTerrain.
  151.     # You can edit it (just add Terrain Tag ID arrays, not Map/Tileset IDs) to
  152.     # create a global set of sounds for unlisted maps.
  153.     WalkingSoundsTerrain.default = {}
  154.  
  155.     WalkingSoundsRegion = {
  156.     #Region ID => [Sound, Volume, Pitch],
  157.     1 => ["Knock", 80, 100],
  158.  
  159.     }
  160.    
  161.     Event_Update_X = {
  162.     #Map ID => X, (X = Number of squares from the center of the screen that will
  163.     #          be updated horizontally)
  164.     1 => 12,
  165.    
  166.     }
  167.     # Default X value for all maps whose IDs aren't in Event_Update_X
  168.     Event_Update_X.default = 12
  169.    
  170.     Event_Update_Y = {
  171.     #Map ID => Y, (Y = Number of squares from the center of the screen that will
  172.     #          be updated vertically)
  173.     1 => 8,
  174.    
  175.     }
  176.     # Default Y value for all maps whose IDs aren't in Event_Update_Y
  177.     Event_Update_Y.default = 8
  178.  
  179.     # RegExp for ignoring Events_Make_Sound
  180.     Sound_Toggle_Events = /^MakeWalkSound: (true|false)/i
  181.     # RegExp for giving events unique walking sounds
  182.     Event_Unique_Sound = /^WalkSound: ([\w]+), ([\d]+), ([\d]+)/i
  183.     # RegExp for causing an event to make sounds while updating offscreen
  184.     Event_Always_Sound = /^AlwaysSound: ([\d]+), ([\d]+)/i
  185.    
  186.   end
  187. end
  188.  
  189. $imported = {} if $imported.nil?
  190. $imported["SES - WalkSE"] = true
  191.  
  192. class Game_CharacterBase
  193.  
  194.    def offscreen
  195.     max_x = $game_map.display_x + $game_map.screen_tile_x
  196.     x_valid = @real_x <= max_x && @real_x >= $game_map.display_x
  197.     max_y = $game_map.display_y + $game_map.screen_tile_y
  198.     y_valid = @real_y <= max_y && @real_y >= $game_map.display_y
  199.     return false if x_valid && y_valid
  200.     return true
  201.   end
  202.  
  203.   def move_straight(d, turn_ok = true)
  204.     @move_succeed = passable?(@x, @y, d)
  205.     if @move_succeed
  206.       Audio.se_stop
  207.       set_direction(d)
  208.       @x = $game_map.round_x_with_direction(@x, d)
  209.       @y = $game_map.round_y_with_direction(@y, d)
  210.       @real_x = $game_map.x_with_direction(@x, reverse_dir(d))
  211.       @real_y = $game_map.y_with_direction(@y, reverse_dir(d))
  212.       increase_steps
  213.       sound = true
  214.       if self.is_a?(Game_Event)
  215.         sound = @page.makesound
  216.         sound = false if offscreen && !@page.alwayssound[0]
  217.       end
  218.       if SES::WalkSE::Use_Tileset_ID
  219.         se = SES::WalkSE::WalkingSoundsTerrain[$game_map.tileset.id][terrain_tag]
  220.       else
  221.         se = SES::WalkSE::WalkingSoundsTerrain[$game_map.map_id][terrain_tag]
  222.       end
  223.       se = SES::WalkSE::WalkingSoundsRegion[region_id] if !SES::WalkSE::WalkingSoundsRegion[region_id].nil?
  224.       se = @page.walksound if self.is_a?(Game_Event) && @page.walksound != ""
  225.       if self.is_a?(Game_Event) && offscreen && @page.alwayssound[0]
  226.         se[1] = @page.alwayssound[1]
  227.         se[2] = @page.alwayssound[2]
  228.       end
  229.       Audio.se_play("Audio/SE/#{se[0]}", se[1], se[2]) if sound && !se.nil?
  230.     elsif turn_ok
  231.       set_direction(d)
  232.       check_event_trigger_touch_front
  233.     end
  234.   end
  235. end
  236.  
  237. class Game_Map
  238.   attr_reader :tileset_id
  239. end
  240.  
  241. class Game_Event < Game_Character
  242.  
  243.   def near_the_screen?(dx = SES::WalkSE::Event_Update_X[$game_map.map_id], dy = SES::WalkSE::Event_Update_Y[$game_map.map_id])
  244.     ax = $game_map.adjust_x(@real_x) - Graphics.width / 2 / 32
  245.     ay = $game_map.adjust_y(@real_y) - Graphics.height / 2 / 32
  246.     ax >= -dx && ax <= dx && ay >= -dy && ay <= dy
  247.   end
  248. end
  249.  
  250. class RPG::Event::Page
  251.  
  252.   alias en_walkse_sc en_scan_comments
  253.   def en_scan_comments(comments = [])
  254.     @makesound = SES::WalkSE::Events_Make_Sound
  255.     @walksound = ""
  256.     @alwayssound = [false, 0, 0]
  257.     comments.push([SES::WalkSE::Sound_Toggle_Events, "$1.to_s.downcase == 'true' ? @makesound = true : @makesound = false"])
  258.     comments.push([SES::WalkSE::Event_Unique_Sound, "@walksound = [$1.to_s, $2.to_i, $3.to_i]"])
  259.     comments.push([SES::WalkSE::Event_Always_Sound, "@alwayssound = [true, $1.to_i, $2.to_i]"])
  260.     en_walkse_sc(comments)
  261.   end
  262.  
  263.   traits = [:makesound, :walksound, :alwayssound]
  264.   traits.each_index { |i|
  265.     define_method(traits[i]) {
  266.     en_scan_comments if eval("@#{traits[i]}.nil?")
  267.     return eval("@#{traits[i]}")
  268.     }
  269.   }
  270. end
Advertisement
Add Comment
Please, Sign In to add comment