neonblack

Variable Pictures v2.0

Sep 19th, 2013
134
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##----------------------------------------------------------------------------##
  2. ## Picture Variables v2.0
  3. ## Created by Neon Black at request of Celianna
  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. ## v2.0 - 8.14.2013
  13. ##  Complete overhaul of how pictures are displayed
  14. ##  Numerous bugfixes
  15. ## v1.0 - 12.24.2012
  16. ##  Finished main script
  17. ##----------------------------------------------------------------------------##
  18.                                                                               ##
  19. $imported ||= {}                                                              ##
  20. $imported["CP_PIC_VARS"] = 2.0                                                ##
  21.                                                                               ##
  22. ##----------------------------------------------------------------------------##
  23. ##    Instructions:
  24. ## Place this script in the script editor below "Materials" and above "Main".
  25. ## This script allows you to set pictures from the "Graphics/Pictures" folder
  26. ## to show up when a switch is turned on and a variable is set to a certain
  27. ## value.  This pictures can appear on any of the map's viewports and at any Z
  28. ## position.  This allows the script to be useful for parallax mapping as well
  29. ## as any form of HUD.
  30. ##----------------------------------------------------------------------------##
  31.                                                                               ##
  32. module CPVPics  # Do not touch this line.                                     ##
  33.                                                                               ##
  34. ##----------------------------------------------------------------------------##
  35. ##    Config:
  36. ## The following is a hash containing the images assigned to a variable.  Each
  37. ## variable has several options that apply to all image in it as follows.
  38. ##
  39. ## :comment
  40. ##   A comment used to help identify the use of the variable.  Not read by the
  41. ##   script for anything.
  42. ## :files
  43. ##   A hash of image names for certain variable settings.  Each contains a
  44. ##   value on the left and a file name on the right.
  45. ## :z
  46. ##   The z position of the image.  Higher Z values appear above lower values.
  47. ## :sw
  48. ##   The switch that must be turned on for the picture to appear.  If the
  49. ##   switch is turned off the picture will dissappear.
  50. ## :map_mode
  51. ##   Determines how the picture will follow the map.
  52. ##   0 - The picture will snap it's top left corner to the top left position
  53. ##       of the screen and stay there.
  54. ##   1 - The picture will snap it's top left corner to the top left position
  55. ##       of the screen, but will scroll as the map scrolls.
  56. ##   2 - The picture will snap it's top left corner to the top left position
  57. ##       of the MAP and will scroll with the map.  Most useful for parallax
  58. ##       mapping.
  59. ## :viewport
  60. ##   The Spriteset_Map viewport for the picture to appear in.
  61. ##   1 - Used by all map objects/tilesets/events.  Any picture in this
  62. ##       viewport will tint with the screen.  As a general rule of thumb for
  63. ##       z values in this viewport, 0 = below events, 100 = same as events,
  64. ##       200 = over events.
  65. ##   2 - Viewport used by the default game pictures.
  66. ##   3 - Viewport used by weather.
  67. ##   Any other value assigned to this will cause the picture to not use a
  68. ##   viewport.
  69. ## :loop
  70. ##   Another feature useful for parallax mapping.  If this value is set to
  71. ##   true the picture will tile across the screen.  If it is set to false only
  72. ##   a single picture will be displayed based on the other parameters.  Set
  73. ##   this value to true when using a picture for a parallax map and having a
  74. ##   map that loops.
  75. ##------
  76. #
  77.  
  78. Pictures ={
  79.  
  80. 1 =>{ ## <- This number is the variable the picture is assigned to.
  81.   :comment => "This is the background for a parallax map",
  82.   :files =>{
  83.     1 => "Map_1",
  84.     2 => "Map_2",
  85.     }, #end of files
  86.   :z  => -50,
  87.   :sw => 21,
  88.   :map_mode => 2,
  89.   :viewport => 1,
  90.   :loop => true,
  91.   },
  92.  
  93. 2 =>{
  94.   :comment => "The upper part of each map.  Holds stuff over the player.",
  95.   :files =>{
  96.     1 => "Map_1_Up",
  97.     2 => "Map_2_Up",
  98.   }, #end of files
  99.   :z  => 199,
  100.   :sw => 21,
  101.   :map_mode => 2,
  102.   :viewport => 0,
  103.   :loop => true,
  104.   },
  105.  
  106. } #Pictures
  107.  
  108.  
  109. ##------------------------------------------------------------------------------
  110. ## End of configuration settings.
  111. ##------------------------------------------------------------------------------
  112.  
  113. end
  114.  
  115. ## Sets up variables for the system.  This allows them to be saved when the game
  116. ## is saved.
  117. class Game_System
  118.   attr_accessor :vpics
  119. end
  120.  
  121. ## New class that holds the variable style picture.  This stores and allows
  122. ## access to some additional information.
  123. class Game_VariablePic < Game_Picture
  124.   attr_accessor :num
  125.   attr_reader :xo
  126.   attr_reader :yo
  127.  
  128.   def initialize(num)
  129.     super(-1)  ## Changes the default blend type, sets the ID, sets the map set.
  130.     @blend_type = 0
  131.     @num = num
  132.     @xo = $game_map.display_x * 32
  133.     @yo = $game_map.display_y * 32
  134.   end
  135.  
  136.   def name
  137.     return "" unless @num
  138.     return info[:files][$game_variables[@num]] || ""
  139.   end
  140.  
  141.   def info
  142.     CPVPics::Pictures[@num]
  143.   end
  144. end
  145.  
  146. ## New class that is used to actually display the picture.  Changes a bit
  147. ## about how pictures work.
  148. class Sprite_VariablePic < Sprite_Picture
  149.   def initialize(num, viewport, picture)
  150.     @num = num  ## Stores the picture's number.
  151.     super(viewport, picture)
  152.   end
  153.  
  154.   ## Changes how the picture is updated to allow map scrolling.
  155.   def update_position
  156.     case @picture.info[:map_mode]
  157.     when 1
  158.       self.x = -$game_map.display_x * 32 + @picture.xo
  159.       self.y = -$game_map.display_y * 32 + @picture.yo
  160.     when 2
  161.       self.x = -$game_map.display_x * 32
  162.       self.y = -$game_map.display_y * 32
  163.     else
  164.       self.x = self.y = 0
  165.     end
  166.     self.z = @picture.info[:z] ? @picture.info[:z] : 100
  167.   end
  168. end
  169.  
  170. ## Replicates the Sprite_Picture class as a plane.
  171. class Sprite_VariablePln < Plane
  172.   def initialize(num, viewport, picture)
  173.     @num = num
  174.     @picture = picture
  175.     super(viewport)
  176.     update
  177.   end
  178.  
  179.   def dispose
  180.     bitmap.dispose if bitmap
  181.     super
  182.   end
  183.  
  184.   def update
  185.     update_bitmap
  186.     update_position
  187.     update_zoom
  188.     update_other
  189.   end
  190.  
  191.   def update_bitmap
  192.     if @picture.name.empty?
  193.       self.bitmap = nil
  194.     else
  195.       self.bitmap = Cache.picture(@picture.name)
  196.     end
  197.   end
  198.  
  199.   def update_position
  200.     case @picture.info[:map_mode]
  201.     when 1
  202.       self.ox = $game_map.display_x * 32 + @picture.xo
  203.       self.oy = $game_map.display_y * 32 + @picture.yo
  204.     when 2
  205.       self.ox = $game_map.display_x * 32
  206.       self.oy = $game_map.display_y * 32
  207.     else
  208.       self.ox = self.oy = 0
  209.     end
  210.     self.z = @picture.info[:z] ? @picture.info[:z] : 100
  211.   end
  212.  
  213.   def update_zoom
  214.     self.zoom_x = @picture.zoom_x / 100.0
  215.     self.zoom_y = @picture.zoom_y / 100.0
  216.   end
  217.  
  218.   def update_other
  219.     self.opacity = @picture.opacity
  220.     self.blend_type = @picture.blend_type
  221.     self.tone.set(@picture.tone)
  222.   end
  223. end
  224.  
  225. ## Modifies some methods on the map just for funnies.
  226. class Spriteset_Map
  227.   alias :cp_vpic_create_pictures :create_pictures
  228.   def create_pictures
  229.     cp_vpic_create_pictures
  230.     create_vpics
  231.   end
  232.  
  233.   def create_vpics
  234.     $game_system.vpics = []
  235.     @var_pictures = []
  236.   end
  237.  
  238.   alias :cp_vpic_update :update
  239.   def update
  240.     cp_vpic_update
  241.     update_vpic
  242.   end
  243.  
  244.   ## This sucker hangs on to pictures.
  245.   def update_vpic
  246.     CPVPics::Pictures.each do |var, info|
  247.       if $game_switches[info[:sw]]
  248.         bitmap = info[:files][$game_variables[var]]
  249.         next unless bitmap
  250.         unless $game_system.vpics[var]
  251.           $game_system.vpics[var] = Game_VariablePic.new(var)
  252.           case info[:viewport]
  253.           when 1; vp = @viewport1
  254.           when 2; vp = @viewport2
  255.           when 3; vp = @viewport3
  256.           else; vp = nil
  257.           end
  258.           values = [var, vp, $game_system.vpics[var]]
  259.           if info[:loop]
  260.             @var_pictures[var] = Sprite_VariablePln.new(*values)
  261.           else
  262.             @var_pictures[var] = Sprite_VariablePic.new(*values)
  263.           end
  264.         end
  265.       else
  266.         if $game_system.vpics[var]
  267.           @var_pictures[var].dispose
  268.           @var_pictures.delete_at(var)
  269.           $game_system.vpics.delete_at(var)
  270.         end
  271.       end
  272.     end
  273.     @var_pictures.each do |pic|
  274.       next unless pic
  275.       pic.update
  276.     end
  277.   end
  278.  
  279.   ## Disposes the pictures.
  280.   alias :cp_vpic_dispose :dispose
  281.   def dispose
  282.     cp_vpic_dispose
  283.     dispose_vpic
  284.   end
  285.  
  286.   ## Does what I said up there.
  287.   def dispose_vpic
  288.     return unless @var_pictures
  289.     @var_pictures.each do |pic|
  290.       next if pic.nil?
  291.       pic.dispose
  292.     end
  293.   end
  294. end
  295.  
  296.  
  297. ##-----------------------------------------------------------------------------
  298. ## End of script.
  299. ##-----------------------------------------------------------------------------
RAW Paste Data