TheoAllen

Theo - Animated Message Portrait

Oct 18th, 2013
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.88 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. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_AnimPortrait] = true
  8. # =============================================================================
  9. # Change Logs:
  10. # -----------------------------------------------------------------------------
  11. # 2014.01.31 - Added screen y reposition
  12. #            - Show portrait in bottom when placing message window in top
  13. # 2014.01.08 - Added show picture in middle
  14. # 2013.11.14 - Dimmed inactive portrait
  15. #            - Bugfix, when remove portrait
  16. # 2013.10.18 - Finished Script
  17. # =============================================================================
  18. =begin
  19.  
  20.   -----------------------------------------------------------------------------
  21.   *) Perkenalan :
  22.   -----------------------------------------------------------------------------
  23.   Script ini ngebikin kamu bisa nampilin animated portrait waktu dalam dialog.
  24.  
  25.   -----------------------------------------------------------------------------
  26.   *) Cara penggunaan :
  27.   -----------------------------------------------------------------------------
  28.   Pasang script ini dibawah material namun diatas main
  29.  
  30.   Siapkan dua buah gambar ukuran terserah. Satu gambar untuk animasi idle.
  31.   Satu lainnya untuk animasi bicara. Gambar terdiri dari 3 grid kesamping. Beri
  32.   nama dua gambar tersebut seperti ini
  33.  
  34.   namagambar_idle.png  << nama gambar untuk idle
  35.   namagambar_talk.png  << nama gambar untuk bicara
  36.  
  37.   -----------------------------------------------------------------------------
  38.   *) Script call:
  39.   -----------------------------------------------------------------------------
  40.   Untuk nampilin gambar dan semacemnya, gunakan script call sebagai berikut
  41.  
  42.   - set_portrait(filename, pos, flip, active)
  43.     Script call ini untuk mengeset portrait. Keterangannya kek gini
  44.    
  45.     *) Filename >> adalah nama file gambar yang ditulis dalam kutip ("").
  46.        Kamu tidak perlu menambahkan _talk / _idle dalam pemanggilannya.
  47.     *) Pos >> untuk posisi. Pilih antara A (kiri), B (kanan), dan C (tengah)
  48.     *) Flip >> apakah gambarnya dibalik atau tidak. Cukup tuliskan true
  49.        atau false (bisa diabaikan. Default : false)
  50.     *) Active >> Apakah gambar tersebut aktif? Jika true, maka saat text
  51.        berjalan, gambar tersebut akan berbicara. (Default : true)
  52.        
  53.   - remove_portrait(pos)
  54.     Untuk menghilangkan salah satu portrait. Pos isi dengan A atau B
  55.  
  56.   - active_portrait(pos)
  57.     Untuk mengganti portrait mana yang aktif
  58.    
  59.   - clear_portrait
  60.     Untuk menghilangkan portrait yang ada
  61.    
  62.   - $game_system.portrait_ypos[pos] = value
  63.     Untuk mereposisi tempat portrait di tampilkan. Ganti pos dengan
  64.     (:A, :B, :C). Ganti value dengan angka. Contoh :
  65.    
  66.     $game_system.portrait_ypos[:B] = 60
  67.  
  68.   -----------------------------------------------------------------------------
  69.   *) Terms of use :
  70.   -----------------------------------------------------------------------------
  71.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  72.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Kalo semisal mau
  73.   dipake buat komersil, jangan lupa, gw dibagi gratisannya.
  74.  
  75. =end
  76. # =============================================================================
  77. # Konfigurasi
  78. # =============================================================================
  79. module Theo
  80.   module AnimePortrait
  81.    
  82.   # --------------------------------------------------------------------------
  83.     Talk_Refresh_Rate = 4
  84.   # Refresh rate gambar yang sedang aktif. Jika kamu memasukkan angka 4, maka
  85.   # gambar akan direfresh / diganti setiap 4 huruf telah berjalan di message
  86.   # system. (default : 4)
  87.   # --------------------------------------------------------------------------
  88.  
  89.   # --------------------------------------------------------------------------
  90.     Idle_Interval = [20,120]
  91.   # Interval atau selang animasi idle. Format [absolute wait, random wait]
  92.   #
  93.   # *) absolute wait  >> Selang tunggu absolut dalam hitungan frame
  94.   # *) random wait    >> Selang tunggu acak
  95.   #
  96.   # Jika kamu mengisinya [20, 120] maka itu artinya, jeda antara animasi idle
  97.   # satu dengan yang adalah 20 ditambah angka acak dari 0 - 120.
  98.   # (default : [20,120])
  99.   #
  100.   # Note : 60 frame adalah 1 detik
  101.   # --------------------------------------------------------------------------
  102.    
  103.   # --------------------------------------------------------------------------
  104.     Use_Dim   = true    # Set true kalo mau gunain. false kalo kaga
  105.     Dim_Power = 90      # Kekuatan dimness (default : 90)
  106.   # --------------------------------------------------------------------------
  107.   # Opsi untuk membuat gambar yang tidak aktif menjadi redup.
  108.   # --------------------------------------------------------------------------
  109.   end
  110. end
  111. # =============================================================================
  112. # Akhir dari konfigurasi
  113. # =============================================================================
  114. class Game_System
  115.   attr_reader :portrait_ypos
  116.  
  117.   alias theo_animportrait_init initialize
  118.   def initialize
  119.     theo_animportrait_init
  120.     @portrait_ypos = {
  121.     :A => 0,
  122.     :B => 0,
  123.     :C => 0,
  124.     }
  125.   end
  126.  
  127. end
  128.  
  129. class Game_Interpreter
  130.   A = :A  # Kiri
  131.   B = :B  # Kanan
  132.   C = :C  # Tengah
  133.  
  134.   def set_portrait(filename, pos, flip = false, active = true)
  135.     $game_message.animportrait_name[pos] = [filename, flip]
  136.     $game_message.active_portrait = pos if active
  137.   end
  138.  
  139.   def clear_portrait
  140.     $game_message.reset_animportrait
  141.   end
  142.  
  143.   def remove_portrait(pos)
  144.     $game_message.animportrait_name[pos][0] = ""
  145.   end
  146.  
  147.   def active_portrait(pos)
  148.     $game_message.active_portrait = pos
  149.   end
  150.  
  151. end
  152.  
  153. class Game_Message
  154.   attr_accessor :active_portrait
  155.   attr_reader :animportrait_name
  156.  
  157.   alias theo_animportrait_init initialize
  158.   def initialize
  159.     theo_animportrait_init
  160.     reset_animportrait
  161.     @active_portrait = :A
  162.   end
  163.  
  164.   def reset_animportrait
  165.     @animportrait_name = {
  166.       :A => ["", false],
  167.       :B => ["", false],
  168.       :C => ["", false],
  169.     }
  170.   end
  171.  
  172. end
  173.  
  174. class AnimPortrait < Sprite
  175.   include Theo::AnimePortrait
  176.   Dimmed_Color = Color.new(0,0,0,Dim_Power)
  177.   Normal_Color = Color.new(0,0,0,0)
  178.   attr_accessor :self_switch  # Self Activation ~
  179.  
  180.   def initialize(pos, msg_window, viewport = nil)
  181.     super(viewport)
  182.     self.z = 150
  183.     @msg_window = msg_window
  184.     @pos = pos
  185.     @self_switch = false
  186.     @idle_pattern = 0
  187.     @fiber = nil
  188.     init_bitmap
  189.     generate_idle_count
  190.   end
  191.  
  192.   def init_bitmap
  193.     @filename = img_name
  194.     self.bitmap = Cache.system(@filename)
  195.     self.mirror = $game_message.animportrait_name[@pos][1]
  196.     case @pos
  197.     when :A
  198.       self.x = 0
  199.     when :B
  200.       self.x = Graphics.width - width
  201.     when :C
  202.       self.x = (Graphics.width - width) / 2
  203.     end
  204.   end
  205.  
  206.   def img_name
  207.     str = $game_message.animportrait_name[@pos][0]
  208.     return "" if str.empty?
  209.     return str + (active? ? "_talk" : "_idle")
  210.   end
  211.  
  212.   def bitmap=(bmp)
  213.     super
  214.     src_rect.width = bitmap.width / 3
  215.   end
  216.  
  217.   def generate_idle_count
  218.     @idle_count = random_idle_number
  219.   end
  220.  
  221.   def random_idle_number
  222.     return Idle_Interval[0] + rand(Idle_Interval[1])
  223.   end
  224.  
  225.   def update
  226.     super
  227.     self.y = ypos_case
  228.     self.opacity = @msg_window.openness
  229.     update_bitmap
  230.     update_color if Use_Dim
  231.     update_idle unless active?
  232.   end
  233.  
  234.   def ypos_case
  235.     return Graphics.height - height + $game_system.portrait_ypos[@pos] if
  236.       $game_message.position == 0
  237.     return @msg_window.y - height + $game_system.portrait_ypos[@pos]
  238.   end
  239.  
  240.   def update_bitmap
  241.     if @filename != img_name
  242.       init_bitmap
  243.     end
  244.   end
  245.  
  246.   def update_color
  247.     if pos_active?
  248.       self.color = Normal_Color
  249.     else
  250.       self.color = Dimmed_Color
  251.     end
  252.   end
  253.  
  254.   def update_active
  255.     @fiber = nil if @fiber
  256.     set_src_rect(rand(3))
  257.   end
  258.  
  259.   def active?
  260.     return pos_active? && self_switch
  261.   end
  262.  
  263.   def pos_active?
  264.     @pos == $game_message.active_portrait
  265.   end
  266.  
  267.   def update_idle
  268.     if @fiber
  269.       @fiber.resume
  270.     elsif @idle_count == 0
  271.       @fiber = Fiber.new { fiber_idle }
  272.     else
  273.       @idle_count -= 1
  274.     end
  275.   end
  276.  
  277.   def fiber_idle
  278.     setup_idle_anim
  279.     update_idle_src_rect
  280.     terminate_idle_anim
  281.   end
  282.  
  283.   def setup_idle_anim
  284.     set_src_rect(0)
  285.   end
  286.  
  287.   def update_idle_src_rect
  288.     random_wait
  289.     set_src_rect(1)
  290.     random_wait
  291.     set_src_rect(2)
  292.     random_wait
  293.     set_src_rect(1)
  294.     random_wait
  295.     set_src_rect(0)
  296.     random_wait
  297.   end
  298.  
  299.   def set_src_rect(amount)
  300.     @pattern = amount
  301.     src_rect.x = @pattern * bitmap.width/3
  302.   end
  303.  
  304.   def random_wait
  305.     (2 + rand(3)).times { Fiber.yield }
  306.   end
  307.  
  308.   def terminate_idle_anim
  309.     @fiber = nil
  310.     generate_idle_count
  311.   end
  312.  
  313.   def dispose
  314.     bitmap.dispose
  315.     super
  316.   end
  317.  
  318. end
  319.  
  320. class Window_Message < Window_Base
  321.  
  322.   alias theo_animportrait_init initialize
  323.   def initialize
  324.     theo_animportrait_init
  325.     create_animportrait
  326.   end
  327.  
  328.   def create_animportrait
  329.     @animportrait = {
  330.       :A => AnimPortrait.new(:A, self, viewport),
  331.       :B => AnimPortrait.new(:B, self, viewport),
  332.       :C => AnimPortrait.new(:C, self, viewport),
  333.     }
  334.     @talk_anim = 0
  335.   end
  336.  
  337.   def active_portrait
  338.     @animportrait[$game_message.active_portrait]
  339.   end
  340.  
  341.   alias theo_animportrait_process_all_text process_all_text
  342.   def process_all_text
  343.     active_portrait.self_switch = true
  344.     theo_animportrait_process_all_text
  345.   end
  346.  
  347.   alias theo_animportrait_updt_show_fast update_show_fast
  348.   def update_show_fast
  349.     theo_animportrait_updt_show_fast
  350.     active_portrait.set_src_rect(0) if Input.trigger?(:C)
  351.   end
  352.  
  353.   alias theo_animportrait_wait_for_one_char wait_for_one_character
  354.   def wait_for_one_character
  355.     theo_animportrait_wait_for_one_char
  356.     @talk_anim += 1
  357.     active_portrait.update_active if @talk_anim % 3 == 0
  358.   end
  359.  
  360.   alias theo_animportrait_process_input process_input
  361.   def process_input
  362.     active_portrait.set_src_rect(0)
  363.     active_portrait.self_switch = false
  364.     theo_animportrait_process_input
  365.   end
  366.  
  367.   alias theo_animportrait_update update
  368.   def update
  369.     theo_animportrait_update
  370.     @animportrait.values.each {|img| img.update }
  371.   end
  372.  
  373.   alias theo_animportrait_dispose dispose
  374.   def dispose
  375.     theo_animportrait_dispose
  376.     @animportrait.values.each {|img| img.dispose }
  377.   end
  378.  
  379.   alias theo_animportrait_wait wait
  380.   def wait(duration)
  381.     active_portrait.set_src_rect(0)
  382.     theo_animportrait_wait(duration)
  383.   end
  384.  
  385. end
Advertisement
Add Comment
Please, Sign In to add comment