Advertisement
neonblack

Region/Terrain Effects v1.0a

Aug 14th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.21 KB | None | 0 0
  1. ##----------------------------------------------------------------------------##
  2. ## Terrain and Region Effects v1.0a
  3. ## Created by Neon Black
  4. ##
  5. ## For both commercial and non-commercial use as long as credit is given to
  6. ## Neon Black and any additional authors.  Licensed under Creative Commons
  7. ## CC BY 3.0 - http://creativecommons.org/licenses/by/3.0/.
  8. ##----------------------------------------------------------------------------##
  9.                                                                               ##
  10. ##----------------------------------------------------------------------------##
  11. ##    Revision Info:
  12. ## v1.0a - 8.13.2013
  13. ##  Fixed an issue with screen position
  14. ## v1.0 - 8.7.2013
  15. ##  Finished main script
  16. ##----------------------------------------------------------------------------##
  17.                                                                               ##
  18. $imported ||= {}                                                              ##
  19. $imported["CP_TR_EFFECTS"] = 1.0                                              ##
  20.                                                                               ##
  21. ##----------------------------------------------------------------------------##
  22. ##    Instructions:
  23. ## Place this script in the script editor below "Materials" and above "Main".
  24. ## This script allows a character's step to cause a sound effect or play an
  25. ## effect.  These effects can be based on terrains or regions and are defined
  26. ## in the notes of tilesets in the database.  Both the player and events can
  27. ## cause these effects while followers and vehichles cannot.  All step sounds
  28. ## and effects are defined by the following tags.
  29. ##
  30. ##------
  31. ##    Tileset Tags:
  32. ## <terrain 1 se>  -or-  <region 1 se>
  33. ##  The first tag used when defining effects for a terrain or region.  ALL
  34. ##  lines that follow one of these will affect how that terrain or region
  35. ##  acts.
  36. ##
  37. ## Sound Name
  38. ##  After using a starting name, typing out anything in the line that is not
  39. ##  one of the tags below will cause the tag to be read as a sound effect
  40. ##  name.  You can have several sound effect names and one will be selected
  41. ##  at random when a sound is to be played.
  42. ##
  43. ## vol: 80  -or-  vol: 60-100
  44. ##  The volume of sound effects played when a step effect is activated.  A
  45. ##  set number may be used to have a set volume or a range may be used to
  46. ##  have a random volume within the range.
  47. ##
  48. ## pit: 90  -or-  pit: 80-120
  49. ##  The pitch of sound effects played when a step effect is activated.  This
  50. ##  works the same way as volume and may be variable.
  51. ##
  52. ## eff: Effect Name
  53. ##  Plays a visual effect from the "VisEffects" hash in the config section.
  54. ##
  55. ## </terrain se>  -or-  </region se>
  56. ##  Stops checking for tags for a specific region or terrain.  This allows
  57. ##  other tags to continue working below these tags without interfering.
  58. ##
  59. ##------
  60. ##    Event Tags:
  61. ## <allow step effects>
  62. ##  This tag can be placed in a comment on an event page to allow that page
  63. ##  of the event to activate stepping effects.  This will only affect the
  64. ##  page the event is currently on while the event is stepping.
  65. ##----------------------------------------------------------------------------##
  66.                                                                               ##
  67. module CPStepEffects  # Do not touch this line.                               ##
  68.                                                                               ##
  69. ##----------------------------------------------------------------------------##
  70. ##    Config:
  71. ## The config options are below.  You can set these depending on the flavour of
  72. ## your game.  Each option is explained in a bit more detail above it.
  73. ##
  74. ##------
  75. # The following 6 values affect offset and lead of footstep visual effects.
  76. # Each setting with "Offset" in it's name is how far from the base of the
  77. # character it will appear depending on which foot is currently out in front.
  78. # This first value alternates with each foot.  Each setting with "Lead" in it's
  79. # name is how far in front of the event an effect appears.  This second value
  80. # ignored by downward movement.
  81.  
  82. # These numbers affect the offsets of a visual effect when its footprint flag
  83. # is enabled and the character is moving up/down.
  84. HorzOffset = 4
  85. VertLead = 4
  86.  
  87. # These numbers affect the offsets of a visual effect when its footprint flag
  88. # is enabled and the character is moving left/right.
  89. VertOffset = 5
  90. HorzLead = 5
  91.  
  92. # These numbers affect the offsets of a visual effect when its footprint flag
  93. # is enabled and the character is moving diagonally.
  94. DiagHorz = 4
  95. DiagVert = 4
  96.  
  97. # This is the angle of rotation to display when an effect is rotated and the
  98. # character is moving diagonally.  This value is taken from an event's
  99. # left/right rotation, so a value of 0 will ALWAYS be facing either left or
  100. # right.
  101. DiagDirAngles = 45
  102.  
  103. # A hash containing the names of all the visual effect files and their
  104. # properties.  Visuals effects go in the "Graphics/System" folder.
  105. #  Name      - The id name for the effect.  Used with the "eff: Name" tag.
  106. #  FileName  - The file name for the effect's graphic.
  107. #  cells     - The number of cells in the graphic left to right.
  108. #  delay     - The number of frames to display a single cell.
  109. #  footprint - Determines if the effect offsets based on which foot is out.
  110. #  rotate    - Determines if the effect is rotated to match character direction.
  111. #  NOTE: When both footprint and rotate are set to true, the effect will mirror
  112. #        when the left foot activates the effect.
  113.  
  114. VisEffects ={
  115. # "Name"   => ["FileName",  cells, delay, footprint, rotate],
  116.   "Grass"  => ["LeafFall",  6,     4,     false,     false],
  117.   "Splash" => ["Splash",    6,     4,     true,      false],
  118.   "Snow"   => ["Footprint", 6,     12,    true,      true],
  119.  
  120. ##----------------------------------------------------------------------------##
  121.                                                                               ##
  122.                                                                               ##
  123. ##----------------------------------------------------------------------------##
  124. ## The following lines are the actual core code of the script.  While you are
  125. ## certainly invited to look, modifying it may result in undesirable results.
  126. ## Modify at your own risk!
  127. ###----------------------------------------------------------------------------
  128.  
  129.  
  130. }
  131.  
  132. end
  133.  
  134. class Game_Map
  135.   attr_reader :stepping_effect
  136.  
  137.   ## Adds a hash containing the steps related to terrain and region.
  138.   alias :cp_080113_setup :setup
  139.   def setup(*args)
  140.     cp_080113_setup(*args)
  141.     make_terrain_step_sounds
  142.   end
  143.  
  144.   alias :cp_080113_change_tileset :change_tileset
  145.   def change_tileset(*args)
  146.     cp_080113_change_tileset(*args)
  147.     make_terrain_step_sounds
  148.   end
  149.  
  150.   alias :cp_080113_update_events :update_events
  151.   def update_events(*args)
  152.     cp_080113_update_events(*args)
  153.     @stepping_effect.each { |eff| eff.update }
  154.     @stepping_effect.delete_if { |eff| eff.delete_me? }
  155.   end
  156.  
  157.   ## Creates the hash by checking tags to REGEXP.
  158.   def make_terrain_step_sounds
  159.     @stepping_sounds = {}
  160.     @stepping_effect = []
  161.     @slse = nil
  162.     tileset.note.split(/[\r\n]+/).each do |line| ## Begins, ends, or changes
  163.       if line =~ /<(terrain|region) (\d+) se>/i  ## the terrain or region to
  164.         if $1.to_s.downcase == "terrain"         ## check.
  165.           @slse = $2.to_i
  166.         else
  167.           @slse = $2.to_s
  168.         end
  169.         @stepping_sounds[@slse] = [[], 80, 100, nil]
  170.         next
  171.       elsif line =~ /<\/(terrain|region) se>/i
  172.         @slse = nil
  173.         next
  174.       elsif @slse.nil?
  175.         next
  176.       end
  177.       case line
  178.       when /(vol|pit): (\d+)-(\d+)/i
  179.         n = 1 if $1.to_s.downcase == "vol"
  180.         n = 2 if $1.to_s.downcase == "pit"
  181.         @stepping_sounds[@slse][n] = [$2.to_i, $3.to_i]
  182.       when /(vol|pit): (\d+)/i
  183.         n = 1 if $1.to_s.downcase == "vol"
  184.         n = 2 if $1.to_s.downcase == "pit"
  185.         @stepping_sounds[@slse][n] = $2.to_i
  186.       when /eff: (.+)/i
  187.         @stepping_sounds[@slse][3] = $1.to_s
  188.       else
  189.         @stepping_sounds[@slse][0].push(line.to_s)
  190.       end
  191.     end
  192.   end
  193.  
  194.   ## Plays a sound effect.  Auto checks random effect with array.
  195.   def play_terrain_step_sound(terrain, region)
  196.     tag = @stepping_sounds[terrain].nil? ? region.to_s : terrain.to_i
  197.     return if @stepping_sounds[tag].nil? || @stepping_sounds[tag][0].empty?
  198.     name = @stepping_sounds[tag][0].shuffle[0]
  199.     vol = @stepping_sounds[tag][1]
  200.     pit = @stepping_sounds[tag][2]
  201.     if vol.is_a?(Array)
  202.       vol = vol[0] + rand(vol[1] - vol[0] + 1)
  203.     end
  204.     if pit.is_a?(Array)
  205.       pit = pit[0] + rand(pit[1] - pit[0] + 1)
  206.     end
  207.     RPG::SE.new(name, vol, pit).play
  208.   end
  209.  
  210.   ## Adds a visual effect to the list.
  211.   def play_terrain_step_effect(terrain, region, x, y, dir, offset)
  212.     tag = @stepping_sounds[terrain].nil? ? region.to_s : terrain.to_i
  213.     return if @stepping_sounds[tag].nil? || @stepping_sounds[tag][3].nil?
  214.     name = @stepping_sounds[tag][3]
  215.     return unless CPStepEffects::VisEffects.include?(name)
  216.     @stepping_effect.push(Game_StepEffect.new(name, x, y, dir, offset))
  217.   end
  218. end
  219.  
  220. class Game_CharacterBase
  221.   include CPStepEffects
  222.  
  223.   alias :cp_080113_update_anime_pattern :update_anime_pattern
  224.   def update_anime_pattern(*args)
  225.     cp_080113_update_anime_pattern(*args)
  226.     do_step_effects_set if pattern_step_sets.include?(@pattern)
  227.   end
  228.  
  229.   def allowable_effects?  ## Determine what does not activate effects.
  230.     return false if self.is_a?(Game_Follower)
  231.     return false if self.is_a?(Game_Vehicle)
  232.     return false if self.is_a?(Game_Player) && @vehicle_type != :walk
  233.     return false if self.is_a?(Game_Event) && !@step_event_cont
  234.     return false if self.is_a?(Game_Event) && !near_the_screen?
  235.     return true
  236.   end
  237.  
  238.   def do_step_effects_set  ## Does sounds and/or visuals.
  239.     return unless allowable_effects?
  240.     do_step_sound_effects
  241.     do_step_action_effects
  242.   end
  243.  
  244.   def do_step_sound_effects
  245.     $game_map.play_terrain_step_sound(screen_terrain, screen_region)
  246.   end
  247.  
  248.   def do_step_action_effects
  249.     xs, ys = step_effects_display_array
  250.     $game_map.play_terrain_step_effect(screen_terrain, screen_region, xs, ys,
  251.                                        facing_direction_sub, pos_step_offset)
  252.   end
  253.  
  254.   ## Gets the region or terrain for the exact position on screen.
  255.   def screen_terrain
  256.     xs, ys = step_effects_display_array
  257.     $game_map.terrain_tag(xs/32, ys/32)
  258.   end
  259.  
  260.   def screen_region
  261.     xs, ys = step_effects_display_array
  262.     $game_map.region_id(xs/32, ys/32)
  263.   end
  264.  
  265.   def step_effects_display_array
  266.     [self.screen_x + $game_map.display_x * 32,
  267.      self.screen_y + $game_map.display_y * 32 - 16]
  268.   end
  269.  
  270.   ## Offsets the footsteps for visual effects and sends it to the effect.
  271.   def pos_step_offset
  272.     frontstep = @pattern == low_pattern_num
  273.     case facing_direction_sub
  274.     when 2
  275.       frontstep ? [-HorzOffset, 0, false] : [HorzOffset, 0, true]
  276.     when 8
  277.       frontstep ? [HorzOffset, -VertLead, false] : [-HorzOffset, -VertLead, true]
  278.     when 4
  279.       frontstep ? [-HorzLead, -VertOffset, false] : [-HorzLead, 0, true]
  280.     when 6
  281.       frontstep ? [HorzLead, -VertOffset, true] : [HorzLead, 0, false]
  282.     when 1
  283.       frontstep ? [0, -DiagVert, true] : [-DiagHorz, 0, false]
  284.     when 3
  285.       frontstep ? [DiagHorz, 0, true] : [0, -DiagVert, false]
  286.     when 7
  287.       frontstep ? [-DiagHorz, -DiagVert, true] : [0, -DiagVert, false]
  288.     when 9
  289.       frontstep ? [0, -DiagVert, true] : [DiagHorz, -DiagVert, false]
  290.     end
  291.   end
  292.  
  293.   ## Other methods related to stepping.  Set for easy compatibility patching.
  294.   def facing_direction_sub
  295.     @direction
  296.   end
  297.  
  298.   def pattern_step_sets
  299.     [low_pattern_num, top_pattern_num]
  300.   end
  301.  
  302.   def low_pattern_num
  303.     0
  304.   end
  305.  
  306.   def top_pattern_num
  307.     @original_pattern * 2
  308.   end
  309. end
  310.  
  311. ## Checks events to see if they can activate effects.
  312. class Game_Event < Game_Character
  313.   alias :cp_080313_setup_page_settings :setup_page_settings
  314.   def setup_page_settings
  315.     cp_080313_setup_page_settings
  316.     get_step_effects_condition
  317.   end
  318.  
  319.   def get_step_effects_condition
  320.     @step_event_cont = false
  321.     return if @list.nil? || @list.empty?
  322.     @list.each do |line|
  323.       next unless line.code == 108 || line.code == 408
  324.       case line.parameters[0]
  325.       when /<allow step effects>/i
  326.         @step_event_cont = true
  327.       end
  328.     end
  329.   end
  330. end
  331.  
  332. ## Displays and alters the visual effects on screen.
  333. class Spriteset_Map
  334.   alias :cp_080313_update_characters :update_characters
  335.   def update_characters(*args)
  336.     cp_080313_update_characters(*args)
  337.     @step_effect_sprites = [] if @step_effect_sprites.nil?
  338.     [@step_effect_sprites.size, $game_map.stepping_effect.size].max.times do |i|
  339.       if $game_map.stepping_effect[i].nil?
  340.         if @step_effect_sprites[i]
  341.           @step_effect_sprites[i].dispose
  342.           @step_effect_sprites.delete_at(i)
  343.         end
  344.         next
  345.       elsif @step_effect_sprites[i].nil?
  346.         sprite = Sprite.new(@viewport1)
  347.         sprite.z = 25
  348.         @step_effect_sprites[i] = sprite
  349.       end
  350.       sprite, effect = @step_effect_sprites[i], $game_map.stepping_effect[i]
  351.       sprite.bitmap = Cache.system(effect.file)
  352.       wd = sprite.bitmap.width / effect.frames
  353.       sprite.src_rect.set(wd * effect.get_frame, 0, wd, sprite.bitmap.height)
  354.       sprite.ox = sprite.width / 2
  355.       sprite.oy = sprite.height / 2
  356.       sprite.mirror = effect.flip?
  357.       sprite.angle = effect.rotation
  358.       sprite.x = effect.screen_x
  359.       sprite.y = effect.screen_y
  360.     end
  361.   end
  362.  
  363.   alias :cp_080313_dispose_characters :dispose_characters
  364.   def dispose_characters
  365.     cp_080313_dispose_characters
  366.     @step_effect_sprites.each { |eff| eff.dispose }
  367.   end
  368. end
  369.  
  370. ## The effect class to store an effect's info.
  371. class Game_StepEffect
  372.   def initialize(name, x, y, dir, offset)
  373.     @name, @x, @y, @dir = name, x, y, dir
  374.     @array = CPStepEffects::VisEffects[name]
  375.     @ticks = 0
  376.     @flip = offset[2]
  377.     if @array[3]
  378.       @x += offset[0]
  379.       @y += offset[1]
  380.     end
  381.   end
  382.  
  383.   def update
  384.     @ticks += 1
  385.   end
  386.  
  387.   def get_frame
  388.     @ticks / delay
  389.   end
  390.  
  391.   def delete_me?
  392.     @ticks >= frames * delay
  393.   end
  394.  
  395.   def screen_x
  396.     @x - $game_map.display_x * 32
  397.   end
  398.  
  399.   def screen_y
  400.     @y - $game_map.display_y * 32 + y_offset
  401.   end
  402.  
  403.   def y_offset
  404.     16
  405.   end
  406.  
  407.   def frames
  408.     @array[1]
  409.   end
  410.  
  411.   def delay
  412.     @array[2]
  413.   end
  414.  
  415.   def file
  416.     @array[0]
  417.   end
  418.  
  419.   def direction
  420.     case @dir
  421.     when 2; return 180
  422.     when 4; return 90
  423.     when 6; return 270
  424.     when 8; return 0
  425.     when 1; return 90  + CPStepEffects::DiagDirAngles
  426.     when 3; return 270 - CPStepEffects::DiagDirAngles
  427.     when 7; return 90  - CPStepEffects::DiagDirAngles
  428.     when 9; return 270 + CPStepEffects::DiagDirAngles
  429.     else;   return 0
  430.     end
  431.   end
  432.  
  433.   def rotation
  434.     @array[4] ? direction : 0
  435.   end
  436.  
  437.   def flip?
  438.     @array[3] && @array[4] && @flip
  439.   end
  440. end
  441.  
  442.  
  443. ##-----------------------------------------------------------------------------
  444. ## End of script.
  445. ##-----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement