TheoAllen

Theo - Anim Portrait ENG

May 17th, 2014
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.95 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Animated Message Portrait
  3. # Version : 1.3
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (English Documentation)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_AnimPortrait] = true
  8. # =============================================================================
  9. # Change Logs:
  10. # -----------------------------------------------------------------------------
  11. # 2014.05.18 - Bugfix, Talk refresh rate wont affect rate
  12. # 2014.01.31 - Added screen y reposition
  13. #            - Show portrait in bottom when placing message window in top
  14. # 2014.01.08 - Added show picture in middle
  15. # 2013.11.14 - Dimmed inactive portrait
  16. #            - Bugfix, when remove portrait
  17. # 2013.10.18 - Finished Script
  18. # =============================================================================
  19. =begin
  20.  
  21.   -----------------------------------------------------------------------------
  22.   *) Introduction :
  23.   -----------------------------------------------------------------------------
  24.   This script is simply adds animated portrait in dialog scene
  25.  
  26.   -----------------------------------------------------------------------------
  27.   *) How to use :
  28.   -----------------------------------------------------------------------------
  29.   Put this script below material but above main
  30.  
  31.   Prepare two images in any size. One will be used for idle animation. The
  32.   other will be used for talking animation. The image itself must consist three
  33.   grids to the side. Named those image like these
  34.  
  35.   imagename_idle.png  << Image name for idle
  36.   imagename_talk.png  << Image name for talks
  37.  
  38.   -----------------------------------------------------------------------------
  39.   *) Script call:
  40.   -----------------------------------------------------------------------------
  41.   To display animated portrait, use this script call
  42.  
  43.   - set_portrait(filename, pos, flip, active)
  44.     It's to set the portrait. Here are the explanations
  45.    
  46.     *) Filename >> is a image filename. Must be inside quotes ("").
  47.                    You don't need to add _talk / _idle.
  48.     *) Pos      >> For position. Choose between A (left), B (right),
  49.                    and C (middle)
  50.     *) Flip     >> Is the image flipped? Just put it true/false. Can be ommited.
  51.                    The default is false
  52.     *) Active   >> Is the image active? If true, when the message text is being
  53.                    typed, the image will talks. (Default : true)
  54.        
  55.   - remove_potrait(pos)
  56.     To remove one of the portraits. Replace pos with A, B, or C
  57.  
  58.   - active_portrait(pos)
  59.     To switch active portrait. Replace pos with A, B, or C
  60.    
  61.   - clear_portrait
  62.     Must be called when the conversation ends
  63.    
  64.   - $game_system.portrait_ypos[pos] = value
  65.     To give reposition of the place where the portrait is being displayed.
  66.     Replace pos with (:A, :B, :C). Replace value with number. e.g,
  67.    
  68.     $game_system.portrait_ypos[:B] = 60
  69.  
  70.   -----------------------------------------------------------------------------
  71.   *) Terms of use :
  72.   -----------------------------------------------------------------------------
  73.   Credit me, TheoAllen. You are free to edit this script by your own. As long
  74.   as you don't claim it yours. For commercial purpose, don't forget to give me
  75.   a free copy of the game.
  76.  
  77. =end
  78. # =============================================================================
  79. # Configuration
  80. # =============================================================================
  81. module Theo
  82.   module AnimePortrait
  83.    
  84.   # --------------------------------------------------------------------------
  85.     Talk_Refresh_Rate = 4
  86.   # Refresh rate for the currently active image. If you set 4, then the image
  87.   # will be refreshed once per 4 characters is typed in message box. Default
  88.   # value is 4. Just in case if you want to reset to the original setting
  89.   # --------------------------------------------------------------------------
  90.  
  91.   # --------------------------------------------------------------------------
  92.     Idle_Interval = [20,120]
  93.   # Wait interval for idle animation. The format [absolute wait, random wait]
  94.   #
  95.   # *) absolute wait  >> Absolute wait in frame
  96.   # *) random wait    >> Random wait in frame
  97.   #
  98.   # If you set it to [20,120] it means that the gap between idle animation
  99.   # is 20 frame plus the random value between 0 - 120
  100.   # (default : [20,120])
  101.   #
  102.   # Note : 60 frame is equal to 1 second
  103.   # --------------------------------------------------------------------------
  104.    
  105.   # --------------------------------------------------------------------------
  106.     Use_Dim   = true    # Set true if you want to use dimness
  107.     Dim_Power = 90      # Dimness power (default : 90)
  108.   # --------------------------------------------------------------------------
  109.   # It's an option to make inactive image will be dimmed
  110.   # --------------------------------------------------------------------------
  111.   end
  112. end
  113. # =============================================================================
  114. # End of config. Don't touch anything below this line
  115. # =============================================================================
  116. class Game_System
  117.   attr_reader :portrait_ypos
  118.  
  119.   alias theo_animportrait_init initialize
  120.   def initialize
  121.     theo_animportrait_init
  122.     @portrait_ypos = {
  123.     :A => 0,
  124.     :B => 0,
  125.     :C => 0,
  126.     }
  127.   end
  128.  
  129. end
  130.  
  131. class Game_Interpreter
  132.   A = :A  # Kiri
  133.   B = :B  # Kanan
  134.   C = :C  # Tengah
  135.  
  136.   def set_portrait(filename, pos, flip = false, active = true)
  137.     $game_message.animportrait_name[pos] = [filename, flip]
  138.     $game_message.active_portrait = pos if active
  139.   end
  140.  
  141.   def clear_portrait
  142.     $game_message.reset_animportrait
  143.   end
  144.  
  145.   def remove_portrait(pos)
  146.     $game_message.animportrait_name[pos][0] = ""
  147.   end
  148.  
  149.   def active_portrait(pos)
  150.     $game_message.active_portrait = pos
  151.   end
  152.  
  153. end
  154.  
  155. class Game_Message
  156.   attr_accessor :active_portrait
  157.   attr_reader :animportrait_name
  158.  
  159.   alias theo_animportrait_init initialize
  160.   def initialize
  161.     theo_animportrait_init
  162.     reset_animportrait
  163.     @active_portrait = :A
  164.   end
  165.  
  166.   def reset_animportrait
  167.     @animportrait_name = {
  168.       :A => ["", false],
  169.       :B => ["", false],
  170.       :C => ["", false],
  171.     }
  172.   end
  173.  
  174. end
  175.  
  176. class AnimPortrait < Sprite
  177.   include Theo::AnimePortrait
  178.   Dimmed_Color = Color.new(0,0,0,Dim_Power)
  179.   Normal_Color = Color.new(0,0,0,0)
  180.   attr_accessor :self_switch  # Self Activation ~
  181.  
  182.   def initialize(pos, msg_window, viewport = nil)
  183.     super(viewport)
  184.     self.z = 150
  185.     @msg_window = msg_window
  186.     @pos = pos
  187.     @self_switch = false
  188.     @idle_pattern = 0
  189.     @fiber = nil
  190.     init_bitmap
  191.     generate_idle_count
  192.   end
  193.  
  194.   def init_bitmap
  195.     @filename = img_name
  196.     self.bitmap = Cache.system(@filename)
  197.     self.mirror = $game_message.animportrait_name[@pos][1]
  198.     case @pos
  199.     when :A
  200.       self.x = 0
  201.     when :B
  202.       self.x = Graphics.width - width
  203.     when :C
  204.       self.x = (Graphics.width - width) / 2
  205.     end
  206.   end
  207.  
  208.   def img_name
  209.     str = $game_message.animportrait_name[@pos][0]
  210.     return "" if str.empty?
  211.     return str + (active? ? "_talk" : "_idle")
  212.   end
  213.  
  214.   def bitmap=(bmp)
  215.     super
  216.     src_rect.width = bitmap.width / 3
  217.   end
  218.  
  219.   def generate_idle_count
  220.     @idle_count = random_idle_number
  221.   end
  222.  
  223.   def random_idle_number
  224.     return Idle_Interval[0] + rand(Idle_Interval[1])
  225.   end
  226.  
  227.   def update
  228.     super
  229.     self.y = ypos_case
  230.     self.opacity = @msg_window.openness
  231.     update_bitmap
  232.     update_color if Use_Dim
  233.     update_idle unless active?
  234.   end
  235.  
  236.   def ypos_case
  237.     return Graphics.height - height + $game_system.portrait_ypos[@pos] if
  238.       $game_message.position == 0
  239.     return @msg_window.y - height + $game_system.portrait_ypos[@pos]
  240.   end
  241.  
  242.   def update_bitmap
  243.     if @filename != img_name
  244.       init_bitmap
  245.     end
  246.   end
  247.  
  248.   def update_color
  249.     if pos_active?
  250.       self.color = Normal_Color
  251.     else
  252.       self.color = Dimmed_Color
  253.     end
  254.   end
  255.  
  256.   def update_active
  257.     @fiber = nil if @fiber
  258.     set_src_rect(rand(3))
  259.   end
  260.  
  261.   def active?
  262.     return pos_active? && self_switch
  263.   end
  264.  
  265.   def pos_active?
  266.     @pos == $game_message.active_portrait
  267.   end
  268.  
  269.   def update_idle
  270.     if @fiber
  271.       @fiber.resume
  272.     elsif @idle_count == 0
  273.       @fiber = Fiber.new { fiber_idle }
  274.     else
  275.       @idle_count -= 1
  276.     end
  277.   end
  278.  
  279.   def fiber_idle
  280.     setup_idle_anim
  281.     update_idle_src_rect
  282.     terminate_idle_anim
  283.   end
  284.  
  285.   def setup_idle_anim
  286.     set_src_rect(0)
  287.   end
  288.  
  289.   def update_idle_src_rect
  290.     random_wait
  291.     set_src_rect(1)
  292.     random_wait
  293.     set_src_rect(2)
  294.     random_wait
  295.     set_src_rect(1)
  296.     random_wait
  297.     set_src_rect(0)
  298.     random_wait
  299.   end
  300.  
  301.   def set_src_rect(amount)
  302.     @pattern = amount
  303.     src_rect.x = @pattern * bitmap.width/3
  304.   end
  305.  
  306.   def random_wait
  307.     (2 + rand(3)).times { Fiber.yield }
  308.   end
  309.  
  310.   def terminate_idle_anim
  311.     @fiber = nil
  312.     generate_idle_count
  313.   end
  314.  
  315.   def dispose
  316.     bitmap.dispose
  317.     super
  318.   end
  319.  
  320. end
  321.  
  322. class Window_Message < Window_Base
  323.  
  324.   alias theo_animportrait_init initialize
  325.   def initialize
  326.     theo_animportrait_init
  327.     create_animportrait
  328.   end
  329.  
  330.   def create_animportrait
  331.     @animportrait = {
  332.       :A => AnimPortrait.new(:A, self, viewport),
  333.       :B => AnimPortrait.new(:B, self, viewport),
  334.       :C => AnimPortrait.new(:C, self, viewport),
  335.     }
  336.     @talk_anim = 0
  337.   end
  338.  
  339.   def active_portrait
  340.     @animportrait[$game_message.active_portrait]
  341.   end
  342.  
  343.   alias theo_animportrait_process_all_text process_all_text
  344.   def process_all_text
  345.     active_portrait.self_switch = true
  346.     theo_animportrait_process_all_text
  347.   end
  348.  
  349.   alias theo_animportrait_updt_show_fast update_show_fast
  350.   def update_show_fast
  351.     theo_animportrait_updt_show_fast
  352.     active_portrait.set_src_rect(0) if Input.trigger?(:C)
  353.   end
  354.  
  355.   alias theo_animportrait_wait_for_one_char wait_for_one_character
  356.   def wait_for_one_character
  357.     theo_animportrait_wait_for_one_char
  358.     @talk_anim += 1
  359.     refrate = Theo::AnimePortrait::Talk_Refresh_Rate
  360.     active_portrait.update_active if @talk_anim % refrate == 0
  361.   end
  362.  
  363.   alias theo_animportrait_process_input process_input
  364.   def process_input
  365.     active_portrait.set_src_rect(0)
  366.     active_portrait.self_switch = false
  367.     theo_animportrait_process_input
  368.   end
  369.  
  370.   alias theo_animportrait_update update
  371.   def update
  372.     theo_animportrait_update
  373.     @animportrait.values.each {|img| img.update }
  374.   end
  375.  
  376.   alias theo_animportrait_dispose dispose
  377.   def dispose
  378.     theo_animportrait_dispose
  379.     @animportrait.values.each {|img| img.dispose }
  380.   end
  381.  
  382.   alias theo_animportrait_wait wait
  383.   def wait(duration)
  384.     active_portrait.set_src_rect(0)
  385.     theo_animportrait_wait(duration)
  386.   end
  387.  
  388. end
Advertisement
Add Comment
Please, Sign In to add comment