TheoAllen

Theo - Light Effect

Jul 25th, 2013
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.31 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Light Effect
  3. # Version : 1.1
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_LightFX] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.08.03 - Bugfix at event page condition
  12. #            - Added fading funtion
  13. # 2013.07.25 - Finished script
  14. # =============================================================================
  15. =begin
  16.  
  17.   Pembukaan :
  18.   Berlatar belakang dari keluhan beberapa orang tentang script light effect
  19.   untuk RGSS3 yang terkadang kelewat susah konfignya. Seperti punyanya Victor
  20.   (Walo gw belom coba seh). Dan kelewat ngelag, (punya e Khas).
  21.  
  22.   Beberapa orang pengen script light effect yang simple aja, kayak script
  23.   Thomas Edison VX buatan BulletXT & Kylock. Akhirnya gw putusin, gw bikin
  24.   script yang serupa.
  25.  
  26.   Oh iya, disini gw bukan port script T.Edison VX ke Ace. Ane cuman ngambil
  27.   idenya doank ama cara nyetingnya. Sisanya gw bikin dari 0 tanpa nyontek
  28.   sedikitpun.
  29.  
  30.   Cara penggunaan :
  31.   Pasang script ini dibawah material namun diatas main
  32.   Sisanya bisa diliat di konfigurasi script
  33.  
  34.   Terms of Use :
  35.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  36.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Kalo semisal mau
  37.   dipake buat komersil, jangan lupa, gw dibagi gratisannya.
  38.  
  39.   Note :
  40.   Karena ini inspirasi dari BulletXT, alangkah baiknya kalo kamu juga credit
  41.   dia.
  42.  
  43. =end
  44. # =============================================================================
  45. # Konfigurasi :
  46. # =============================================================================
  47. module THEO
  48.   module Lights
  49.     # ------------------------------------------------------------------------
  50.     # General Setting (Settingan umum)
  51.     # ------------------------------------------------------------------------
  52.     DefaultName = "le" # Nama file yang harus ada di folder Graphics/Picture
  53.     DefaultRate = 3    # Refresh rate. Makin kecil, makin sering refresh
  54.     # ------------------------------------------------------------------------
  55.     # Switch ID untuk tipe2 light effect. Kamu juga bisa nambahin tipe switch
  56.     # kamu sendiri. Dengan syarat, huruf awal harus besar.
  57.     # ------------------------------------------------------------------------
  58.     Fire  = 1
  59.     Torch = 2
  60.     Light = 3
  61.     # ------------------------------------------------------------------------
  62.     # Konfigurasi mode Simple :
  63.     # ------------------------------------------------------------------------
  64.     # Paduan Konfig :
  65.     # - Keyword >> Nama light effect yang nanti digunakan dalam comment
  66.     # - RGB     >> Nilai Red,Green,Blue. Antara -255 s.d 255
  67.     # - Zoom    >> Nilai zoom light effectnya. 1.0 = normal
  68.     # - Opacity >> Opacity light effectnya
  69.     # - Var     >> Makin gede, makin bervariasi nilai opacitynya
  70.     # - Offset  >> Jarak perpindahan dari posisi original
  71.     # - Blend   >> Tipe blend. Pilih antara 0 - 2
  72.     # - Switch  >> Switch ID untuk disable.
  73.     #
  74.     # Penggunaan dalam comment :
  75.     # Gunakan comment pada event <light: keyword>
  76.     # Contohnya <light: Fire>
  77.     # Inget, besar kecil hurufnya harus sama :v
  78.     # ------------------------------------------------------------------------
  79.     Database = {
  80.     # Keyword => [  R,   G,   B, Zoom, Opacity, Var, Offset, Blend, Switch]
  81.       "Fire"  => [128, -50,  -50, 2.0,     200,  50,      2,     0,   Fire],
  82.       "Light" => [  0,   0,    0, 1.5,     200,   0,      0,     0,  Light],
  83.    
  84.     # Tambahin sendiri kalo perlu
  85.     }
  86.     # ------------------------------------------------------------------------
  87.     # Konfigurasi mode Lanjut / Advanced
  88.     # ------------------------------------------------------------------------
  89.     # Bagian sini adalah konfigurasi yang lebih lanjut jika kamu masi tidak
  90.     # puas dengan konfigurasi simple diatas. Tidak disarankan ngedit bagian
  91.     # sini kalo ngga ngerti
  92.     #
  93.     # Paduan konfig :
  94.     # Gunakan awalan seperti ini
  95.     # light = $data_lights["keyword"]
  96.     #
  97.     # Lalu tambahkan variable yang akan diubah. Misalnya gini
  98.     # light.name = "le"
  99.     # light.red = 100
  100.     # light.offset = 1
  101.     #
  102.     # Variable2 yang dapat diubah :
  103.     # - light.name          = ngubah nama default gambar cahaynya
  104.     # - light.red           = unsur merah (default : 0)
  105.     # - light.green         = unsur hijau (default : 0)
  106.     # - light.blue          = unsur biru (default : 0)
  107.     # - light.gray          = unsur gray (default : 0)
  108.     # - light.blink         = jika true, maka cahaya akan ngeblink
  109.     # - light.blink_rate    = blink refresh rate (default : DefaultRate)
  110.     # - light.opacity       = nilai opacity (default : 255)
  111.     # - light.opacity_var   = opacity variance (default : 0)
  112.     # - light.opacity_rate  = opacity refresh rate (default : DefaultRate)
  113.     # - light.offset        = nilai offset (default : 0)
  114.     # - light.offset_rate   = offset refresh rate (default : DefaultRate)
  115.     # - light.zoom          = nilai zoom (default : 1.0)
  116.     # - light.switch        = switch id (default : 0)
  117.     # - light.buffer_x      = Jarak X dari event (default : 0)
  118.     # - light.buffer_y      = Jarak Y dari event (default : -15)
  119.     # - light.fading_speed  = Kecepatan ilang muncul (default : 0)
  120.     # - light.blend_type    = Tipe blending. Pilih antara 0 - 2 (default : 0)
  121.     # ------------------------------------------------------------------------
  122.     def self.load_custom_database
  123.      
  124.       light = $data_lights["Orb"]
  125.       light.red = -120
  126.       light.green = -20
  127.       light.blue = 150
  128.       light.zoom = 2.0
  129.       light.buffer_y = 5
  130.       light.opacity = 150
  131.       light.fading_speed = 2
  132.      
  133.       light = $data_lights["Lamp"]
  134.       light.buffer_y = 5
  135.       light.zoom = 2.0
  136.       light.switch = 2
  137.      
  138.       light = $data_lights["Lamp2"]
  139.       light.name = "le2"
  140.       light.buffer_y = 5
  141.       light.opacity = 150
  142.       light.opacity_var = 50
  143.       light.opacity_rate = 1
  144.       light.switch = 2
  145.      
  146.       light = $data_lights["Fire"]
  147.       light.buffer_x = 2
  148.      
  149.       light = $data_lights["Orb2"]
  150.       light.red = -50
  151.       light.green = 150
  152.       light.blue = 20
  153.       light.blink = true
  154.       light.blink_rate = 30
  155.      
  156.     end
  157.   end
  158. end
  159. # =============================================================================
  160. # Akhir dari konfigurasi
  161. # =============================================================================
  162. module THEO
  163.   module Lights
  164.     def self.load_database
  165.       Database.each do |key,data|
  166.         light = $data_lights[key]
  167.         light.red = data[0]
  168.         light.green = data[1]
  169.         light.blue = data[2]
  170.         light.zoom = data[3]
  171.         light.opacity = data[4]
  172.         light.opacity_var = data[5]
  173.         light.offset = data[6]
  174.         light.blend_type = data[7]
  175.         light.switch = data[8]
  176.         light.offset_rate = light.opacity_rate = light.blink_rate = DefaultRate
  177.         light.name = DefaultName
  178.       end
  179.     end
  180.   end
  181. end
  182.  
  183. module Math
  184.   def self.radian(degree)
  185.     return (degree.to_f/180) * Math::PI
  186.   end
  187. end
  188.  
  189. class Data_Light
  190.   attr_reader :key
  191.   attr_accessor :name
  192.   attr_accessor :red
  193.   attr_accessor :green
  194.   attr_accessor :gray
  195.   attr_accessor :blue
  196.   attr_accessor :blink
  197.   attr_accessor :blink_rate
  198.   attr_accessor :opacity
  199.   attr_accessor :opacity_rate
  200.   attr_accessor :opacity_var
  201.   attr_accessor :offset
  202.   attr_accessor :offset_rate
  203.   attr_accessor :blend_type
  204.   attr_accessor :zoom
  205.   attr_accessor :switch
  206.   attr_accessor :buffer_x
  207.   attr_accessor :buffer_y
  208.   attr_accessor :fading_speed
  209.  
  210.   def initialize(key)
  211.     @key = key
  212.     @name = THEO::Lights::DefaultName
  213.     @red = @green = @blue = @gray = 0
  214.     @blink = false
  215.     @blink_rate = 0
  216.     @opacity = 255
  217.     @opacity_rate = 0
  218.     @opacity_var = 0
  219.     @offset = 0
  220.     @offset_rate = 0
  221.     @blend_type = 0
  222.     @zoom = 1.0
  223.     @switch = 0
  224.     @buffer_x = 0
  225.     @buffer_y = -15
  226.     @fading_speed = 0
  227.   end
  228.  
  229.   def tone
  230.     Tone.new(red,green,blue,gray)
  231.   end
  232.  
  233. end
  234.  
  235. class << DataManager
  236.  
  237.   alias theo_le_load_db load_database
  238.   def load_database
  239.     theo_le_load_db
  240.     load_le_database
  241.   end
  242.  
  243.   def load_le_database
  244.     $data_lights = Data_Lights.new
  245.     THEO::Lights.load_database
  246.     THEO::Lights.load_custom_database
  247.   end
  248.  
  249. end
  250.  
  251. class Data_Lights
  252.  
  253.   def initialize
  254.     @data = {}
  255.   end
  256.  
  257.   def [](key)
  258.     @data[key] ||= Data_Light.new(key)
  259.   end
  260.  
  261. end
  262.  
  263. class Game_Event < Game_Character
  264.   attr_accessor :light_key
  265.   attr_writer :light_start_count
  266.  
  267.   alias theo_sle_page_settings setup_page_settings
  268.   def setup_page_settings
  269.     theo_sle_page_settings
  270.     setup_light_key
  271.   end
  272.  
  273.   def setup_light_key
  274.     @light_key = ""
  275.     @light_start_count = 0
  276.     return unless list
  277.     list.each do |command|
  278.       next unless command.code == 108 || command.code == 408
  279.       case command.parameters[0]
  280.       when /<(?:LIGHT|light): (.*)>/i
  281.         @light_key = $1.to_s
  282.       when /<(?:LIGHT_START_COUNT|light start count): [ ]*(\d+)>/i
  283.         @light_start_count = $1.to_i
  284.       when /<(?:LIGHT_RAND_COUNT|light rand count): [ ]*(\d+)>/i
  285.         @light_start_count = rand($1.to_i)
  286.       end
  287.     end
  288.   end
  289.  
  290.   def light_start_count
  291.     @light_start_count ||= 0
  292.   end
  293.  
  294. end
  295. # ----------------------------------------------------------------------------
  296. # Unique Names for compatibility purposes :P
  297. # ----------------------------------------------------------------------------
  298. class Theolized_Light_Effect < Sprite
  299.  
  300.   def initialize(char, viewport = nil)
  301.     super(viewport)
  302.     @char = char
  303.     setup(@char.light_key)
  304.   end
  305.  
  306.   def setup(key)
  307.     @key = key.nil? ? "" : key
  308.     init_members
  309.     return if @key.empty?
  310.     setup_database(key)
  311.     self.ox = self.width/2
  312.     self.oy = self.height/2
  313.   end
  314.  
  315.   def init_members
  316.     self.bitmap = nil
  317.     @offset = @offset_x = @offset_y = @buffer_x = @buffer_y = @opacity_var = 0
  318.     @switch = 0
  319.     @blink = false
  320.     @blink_rate = @opacity_rate = @offset_rate = THEO::Lights::DefaultRate
  321.     @op_count = @count = @char.light_start_count
  322.     @op_fade = 0
  323.     @ori_opacity = 255
  324.     @opacity_offset = 0
  325.     @show_light = true
  326.   end
  327.  
  328.   def setup_database(key)
  329.     le = $data_lights[key]
  330.     self.bitmap = Cache.picture(le.name)
  331.     self.tone = le.tone
  332.     @ori_opacity=self.opacity=le.fading_speed > 0 ? le.opacity/2.0 : le.opacity
  333.     self.blend_type = le.blend_type
  334.     @blink = le.blink
  335.     @blink_rate = le.blink_rate
  336.     @opacity_rate = le.opacity_rate
  337.     @opacity_var = le.opacity_var
  338.     @offset = le.offset
  339.     @offset_rate = le.offset_rate
  340.     self.zoom_x = self.zoom_y = le.zoom
  341.     @switch = le.switch
  342.     @buffer_x = le.buffer_x
  343.     @buffer_y = le.buffer_y
  344.     @op_fade = le.fading_speed > 0 ? @ori_opacity : 0
  345.     update_placement
  346.     check_blink
  347.     update_op_fade
  348.     update_opacity
  349.   end
  350.  
  351.   def check_blink
  352.     return if @blink_rate == 0 || !@blink
  353.     @show_light = !@show_light if (@count/@blink_rate) % 2 == 0
  354.   end
  355.  
  356.   def update
  357.     super
  358.     setup(@char.light_key) if light_changed?
  359.     @count += 1
  360.     update_offset
  361.     update_placement
  362.     update_visibility
  363.     update_blink
  364.     update_op_fade
  365.     update_opacity    
  366.   end
  367.  
  368.   def update_placement
  369.     self.x = @char.screen_x + @buffer_x + @offset_x
  370.     self.y = @char.screen_y + @buffer_y + @offset_y
  371.   end
  372.  
  373.   def update_offset
  374.     return unless refresh_count?(@offset_rate)
  375.     return if @offset <= 0
  376.     @offset_x = -@offset + rand(@offset*2)
  377.     @offset_y = -@offset + rand(@offset*2)
  378.   end
  379.  
  380.   def update_visibility
  381.     self.visible = !$game_switches[@switch] && @show_light
  382.   end
  383.  
  384.   def update_blink
  385.     return unless @blink
  386.     return unless refresh_count?(@blink_rate)
  387.     @show_light = !@show_light
  388.   end
  389.  
  390.   def update_opacity
  391.     self.opacity = @ori_opacity + @opacity_offset + @op_fade
  392.     return unless refresh_count?(@opacity_rate)
  393.     @opacity_offset = -@opacity_var + rand(@opacity_var*2)
  394.   end
  395.  
  396.   def update_op_fade
  397.     return unless $data_lights[@key].fading_speed > 0
  398.     @op_count += $data_lights[@key].fading_speed
  399.     @op_fade = Math.cos(Math.radian(@count)) * @ori_opacity
  400.   end
  401.  
  402.   def refresh_count?(rate)
  403.     return false if rate <= 0
  404.     return @count % rate == 0
  405.   end  
  406.  
  407.   def light_changed?
  408.     @key != @char.light_key
  409.   end
  410.  
  411. end
  412.  
  413. class Spriteset_Map
  414.  
  415.   alias theo_le_create_char create_characters
  416.   def create_characters
  417.     theo_le_create_char
  418.     @simple_lights = []
  419.     $game_map.events.values.each do |char|
  420.       @simple_lights.push(Theolized_Light_Effect.new(char,@viewport2))
  421.     end
  422.   end
  423.  
  424.   alias theo_le_update update
  425.   def update
  426.     theo_le_update
  427.     update_simple_lights
  428.   end
  429.  
  430.   def update_simple_lights
  431.     @simple_lights.each do |light|
  432.       light.update
  433.     end
  434.   end
  435.  
  436.   alias theo_sle_dispose_char dispose_characters
  437.   def dispose_characters
  438.     theo_sle_dispose_char
  439.     @simple_lights.each {|light| light.dispose}
  440.   end
  441.  
  442. end
Advertisement
Add Comment
Please, Sign In to add comment