khanhdu

MOG_Chain_Commands

Jun 8th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 20.28 KB | None | 0 0
  1. #===============================================================================
  2. # +++ MOG - Chain Commands (v1.4) +++
  3. #===============================================================================
  4. # By Moghunter                                                                
  5. # https://atelierrgss.wordpress.com/                  
  6. #===============================================================================
  7. # Sistema de sequência de botões para ativar switchs.
  8. #
  9. # Serão necessárias as seguintes imagens. (Graphics/System)
  10. #
  11. # Chain_Cursor.png
  12. # Chain_Command.png
  13. # Chain_Layout.png
  14. # Chain_Timer_Layout.png
  15. # Chain_Timer_Meter.png
  16. #
  17. #===============================================================================
  18. #
  19. # Para ativar o script use o comando abaixo.  (*Call Script)
  20. #
  21. # chain_commands(ID)
  22. #
  23. # ID - Id da switch.
  24. #
  25. #===============================================================================
  26.  
  27. #==============================================================================
  28. # ● Histórico (Version History)
  29. #==============================================================================
  30. # v 1.4 - Correção do crash aleatório.
  31. # v 1.3 - Melhoria no sistema de dispose
  32. # v 1.2 - Melhoria na codificação e correção de alguns glitches.
  33. # v 1.1 - Animação de fade ao sair da cena de chain.
  34. #       - Opção de definir a prioridade da hud na tela.
  35. #==============================================================================
  36.  
  37. module MOG_CHAIN_COMMANDS
  38.  #==============================================================================
  39.  # CHAIN_COMMAND = { SWITCH_ID => [COMMAND] }
  40.  #
  41.  # SWITCH_ID = ID da switch
  42.  # COMMANDS = Defina aqui a sequência de botões.
  43.  #            (Para fazer a sequência use os comandos abaixo)  
  44.  #  
  45.  # "Down" ,"Up" ,"Left" ,"Right" ,"Shift" ,"D" ,"S" ,"A" ,"Z" ,"X" ,"Q" ,"W"
  46.  #
  47.  # Exemplo de utilização
  48.  #
  49.  # CHAIN_SWITCH_COMMAND = {
  50.  # 25=>["Down","D","S","Right"],
  51.  # 59=>["Down","Up","Left","Right","Shift","D","S","A","Z","X","Q","W"],
  52.  # 80=>["Shift","D"]
  53.  # }
  54.  #==============================================================================
  55.  CHAIN_SWITCH_COMMAND = {
  56.  5=>["X","Right","Left","Z","Z"],
  57.  6=>["Left","Right","Left","Right","Left","Right","Q","Z","Up","A","S",
  58.       "Down","D","Z","Right","Up","Up","Z","W","Left","Down","D","A","W"],
  59.  8=>["Up","Down","Left","Right","Z"]
  60.  }
  61.  #Duração para colocar os comandos. (A duração é multiplicado pela quantidade
  62.  #de comandos) *60 = 1 sec
  63.  CHAIN_INPUT_DURATION = 30
  64.  #Som ao acertar.
  65.  CHAIN_RIGHT_SE = "Chime1"
  66.  #Som ao errar.
  67.  CHAIN_WRONG_SE = "Buzzer1"
  68.  #Switch que ativa o modo automático.
  69.  CHAIN_AUTOMATIC_MODE_SWITCH_ID = 20
  70.  #Definição da prioridade da hud na tela
  71.  CHAIN_HUD_Z = 300
  72. end
  73.  
  74.  
  75. #===============================================================================
  76. # ■ Chain Commands
  77. #===============================================================================
  78. class Chain_Commands
  79.    include MOG_CHAIN_COMMANDS
  80.  #--------------------------------------------------------------------------
  81.  # ● Initialize
  82.  #--------------------------------------------------------------------------      
  83.   def initialize
  84.       @action_id = $game_temp.chain_switch_id
  85.       @chain_command = CHAIN_SWITCH_COMMAND[@action_id]
  86.       @chain_command = ["?"] if @chain_command == nil  
  87.       duration = [[CHAIN_INPUT_DURATION, 1].max, 9999].min
  88.       @timer_max = duration * @chain_command.size
  89.       @timer = @timer_max
  90.       @slide_time = [[60 / duration, 10].max, 60].min
  91.       @change_time = 0
  92.       if $game_switches[CHAIN_AUTOMATIC_MODE_SWITCH_ID]
  93.          @auto = true
  94.       else
  95.          @auto = false
  96.       end
  97.       @com = 0  
  98.   end    
  99.  
  100.  #--------------------------------------------------------------------------
  101.  # ● Main
  102.  #--------------------------------------------------------------------------    
  103.  def main
  104.      dispose
  105.      @background_sprite = Sprite.new
  106.      @background_sprite.bitmap = SceneManager.background_bitmap2
  107.      create_chain_command
  108.      create_cusrsor
  109.      create_layout
  110.      create_meter
  111.      create_text
  112.      create_number
  113.      Graphics.transition
  114.      loop do
  115.           Graphics.update
  116.            Input.update
  117.            update
  118.            break if SceneManager.scene != self
  119.      end
  120.      dispose
  121.  end  
  122.      
  123.  #--------------------------------------------------------------------------
  124.  # ● Create Cursor
  125.  #--------------------------------------------------------------------------            
  126.  def create_cusrsor
  127.      @fy_time = 0
  128.      @fy = 0
  129.      @com_index = 0
  130.      @cursor = Sprite.new
  131.      @cursor.bitmap = Cache.system("Chain_Cursor")
  132.      @cursor.z = 4 + CHAIN_HUD_Z
  133.      @cursor_space = ((@bitmap_cw + 5) * @chain_command.size) / 2
  134.      if @chain_command.size <= 20
  135.         @cursor.x = (544 / 2) - @cursor_space + @cursor_space * @com_index
  136.      else  
  137.         @cursor.x = (544 / 2)
  138.      end  
  139.      @cursor.y = (416 / 2) + 30    
  140.  end
  141.  
  142.  #--------------------------------------------------------------------------
  143.  # ● Create Chain Command
  144.  #--------------------------------------------------------------------------      
  145.  def create_chain_command
  146.      @image = Cache.system("Chain_Command")    
  147.      width_max = ((@image.width / 13) + 5) * @chain_command.size
  148.      @bitmap = Bitmap.new(width_max,@image.height * 2)
  149.      @bitmap_cw = @image.width / 13
  150.      @bitmap_ch = @image.height  
  151.      index = 0
  152.      for i in @chain_command
  153.          command_list_check(i)
  154.          bitmap_src_rect = Rect.new(@com * @bitmap_cw, 0, @bitmap_cw, @bitmap_ch)
  155.          if index == 0
  156.             @bitmap.blt(index * (@bitmap_cw + 5) , 0, @image, bitmap_src_rect)
  157.          else
  158.             @bitmap.blt(index * (@bitmap_cw + 5) , @bitmap_ch, @image, bitmap_src_rect)
  159.          end
  160.          index += 1
  161.      end
  162.      @sprite = Sprite.new
  163.      @sprite.bitmap = @bitmap
  164.      if @chain_command.size <= 15
  165.         @sprite.x = (544 / 2) - ((@bitmap_cw + 5) * @chain_command.size) / 2
  166.         @new_x = 0
  167.      else  
  168.         @sprite.x = (544 / 2)
  169.         @new_x = @sprite.x
  170.      end  
  171.      @sprite.y = (416 / 2) + 30  - @bitmap_ch  - 15
  172.      @sprite.z = 3 + CHAIN_HUD_Z
  173.      @sprite.zoom_x = 1.5
  174.      @sprite.zoom_y = 1.5
  175.  end
  176.  
  177.  #--------------------------------------------------------------------------
  178.  # * create_layout
  179.  #--------------------------------------------------------------------------  
  180.  def create_layout
  181.      @back = Plane.new
  182.      @back.bitmap = Cache.system("Chain_Layout")
  183.      @back.z = 0
  184.      @layout = Sprite.new
  185.      @layout.bitmap = Cache.system("Chain_Timer_Layout")
  186.      @layout.z = 1 + CHAIN_HUD_Z
  187.      @layout.x = 160
  188.      @layout.y = 150
  189.   end
  190.  
  191.  #--------------------------------------------------------------------------
  192.  # * create_meter
  193.  #--------------------------------------------------------------------------  
  194.  def create_meter
  195.      @meter_flow = 0
  196.      @meter_image = Cache.system("Chain_Timer_Meter")
  197.      @meter_bitmap = Bitmap.new(@meter_image.width,@meter_image.height)
  198.      @meter_range = @meter_image.width / 3
  199.      @meter_width = @meter_range * @timer / @timer_max
  200.      @meter_height = @meter_image.height
  201.      @meter_src_rect = Rect.new(@meter_range, 0, @meter_width, @meter_height)
  202.      @meter_bitmap.blt(0,0, @meter_image, @meter_src_rect)
  203.      @meter_sprite = Sprite.new
  204.      @meter_sprite.bitmap = @meter_bitmap
  205.      @meter_sprite.z = 2 + CHAIN_HUD_Z
  206.      @meter_sprite.x = 220
  207.      @meter_sprite.y = 159
  208.      update_flow
  209.  end  
  210.  
  211.  #--------------------------------------------------------------------------
  212.  # ● Create Text
  213.  #--------------------------------------------------------------------------        
  214.  def create_text
  215.      @text = Sprite.new
  216.      @text.bitmap = Bitmap.new(200,32)
  217.      @text.z = 2 + CHAIN_HUD_Z
  218.      @text.bitmap.font.name = "Georgia"
  219.      @text.bitmap.font.size = 25
  220.      @text.bitmap.font.bold = true
  221.      @text.bitmap.font.italic = true
  222.      @text.bitmap.font.color.set(255, 255, 255,220)
  223.      @text.x = 230
  224.      @text.y = 100
  225.  end
  226.  
  227.  #--------------------------------------------------------------------------
  228.  # ● Create Number
  229.  #--------------------------------------------------------------------------        
  230.  def create_number
  231.      @combo = 0
  232.      @number = Sprite.new
  233.      @number.bitmap = Bitmap.new(200,64)
  234.      @number.z = 2 + CHAIN_HUD_Z
  235.      @number.bitmap.font.name = "Arial"
  236.      @number.bitmap.font.size = 24
  237.      @number.bitmap.font.bold = true
  238.      @number.bitmap.font.color.set(0, 255, 255,200)
  239.      @number.bitmap.draw_text(0, 0, 200, 32, "Ready",1)        
  240.      @number.x = 30
  241.      @number.y = 100
  242.  end
  243.  
  244.  #--------------------------------------------------------------------------
  245.  # ● Pre Dispose
  246.  #--------------------------------------------------------------------------      
  247.  def exit
  248.      loop do
  249.          Graphics.update
  250.          @sprite.x += 5
  251.          @layout.x -= 5
  252.          @meter_sprite.x -= 5
  253.          @text.x -= 2
  254.          @text.opacity -= 5
  255.          @sprite.opacity -= 5
  256.          @layout.opacity -= 5
  257.          @meter_sprite.opacity -= 5
  258.          @number.opacity -= 5
  259.          @back.opacity -= 5
  260.          @cursor.visible = false    
  261.          break if @sprite.opacity == 0
  262.      end
  263.      SceneManager.call(Scene_Map)
  264.  end
  265.    
  266.  #--------------------------------------------------------------------------
  267.  # ● Dispose
  268.  #--------------------------------------------------------------------------      
  269.  def dispose
  270.      return if @layout == nil
  271.      Graphics.freeze
  272.      @background_sprite.bitmap.dispose
  273.      @background_sprite.dispose
  274.      @bitmap.dispose
  275.      @sprite.bitmap.dispose
  276.      @sprite.dispose
  277.      @cursor.bitmap.dispose
  278.      @cursor.dispose  
  279.      @meter_image.dispose
  280.      @meter_bitmap.dispose
  281.      @meter_sprite.bitmap.dispose
  282.      @meter_sprite.dispose
  283.      @layout.bitmap.dispose  
  284.      @layout.dispose
  285.      @layout = nil
  286.      @back.bitmap.dispose
  287.      @back.dispose
  288.      @text.bitmap.dispose
  289.      @text.dispose
  290.      @number.bitmap.dispose
  291.      @number.dispose
  292.      @image.dispose
  293.  end
  294.  
  295.  #--------------------------------------------------------------------------
  296.  # ● Update
  297.  #--------------------------------------------------------------------------      
  298.  def update
  299.      update_command
  300.      update_cursor_slide
  301.      update_flow
  302.      update_change_time
  303.  end
  304.  
  305.  #--------------------------------------------------------------------------
  306.  # ● Change_Time
  307.  #--------------------------------------------------------------------------        
  308.  def update_change_time
  309.      return unless @auto
  310.      @change_time += 1
  311.      check_command(-1) if @change_time >= CHAIN_INPUT_DURATION - 1
  312.  end
  313.  
  314.  #--------------------------------------------------------------------------
  315.  # ● Update Flow
  316.  #--------------------------------------------------------------------------      
  317.  def update_flow
  318.      @timer -= 1
  319.      @meter_sprite.bitmap.clear
  320.      @meter_width = @meter_range * @timer / @timer_max
  321.      @meter_src_rect = Rect.new(@meter_flow, 0, @meter_width, @meter_height)
  322.      @meter_bitmap.blt(0,0, @meter_image, @meter_src_rect)  
  323.      @meter_flow += 20
  324.      @meter_flow = 0 if @meter_flow >= @meter_image.width - @meter_range      
  325.      wrong_command if @timer == 0 and @auto == false
  326.   end  
  327.    
  328.  #--------------------------------------------------------------------------
  329.  # ● Update Command
  330.  #--------------------------------------------------------------------------      
  331.  def update_command
  332.      return if @auto
  333.      if Input.trigger?(Input::X)
  334.         check_command(0)
  335.      elsif Input.trigger?(Input::Z)  
  336.         check_command(1)
  337.      elsif Input.trigger?(Input::Y)  
  338.         check_command(2)
  339.      elsif Input.trigger?(Input::A)    
  340.         check_command(3)
  341.      elsif Input.trigger?(Input::C)          
  342.         check_command(4)
  343.      elsif Input.trigger?(Input::B)        
  344.         check_command(5)
  345.      elsif Input.trigger?(Input::L)        
  346.         check_command(6)
  347.      elsif Input.trigger?(Input::R)        
  348.         check_command(7)        
  349.      elsif Input.trigger?(Input::RIGHT)      
  350.         check_command(8)
  351.      elsif Input.trigger?(Input::LEFT)
  352.         check_command(9)
  353.      elsif Input.trigger?(Input::DOWN)
  354.         check_command(10)
  355.      elsif Input.trigger?(Input::UP)  
  356.         check_command(11)
  357.      end  
  358.  end  
  359.    
  360.  #--------------------------------------------------------------------------
  361.  # ● command_list_check
  362.  #--------------------------------------------------------------------------      
  363.  def command_list_check(command)
  364.      case command
  365.          when "A"
  366.             @com = 0  
  367.          when "D"
  368.             @com = 1  
  369.          when "S"
  370.             @com = 2
  371.          when "Shift"
  372.             @com = 3
  373.          when "Z"
  374.             @com = 4
  375.          when "X"
  376.             @com = 5
  377.          when "Q"
  378.             @com = 6
  379.          when "W"
  380.             @com = 7            
  381.          when "Right"
  382.             @com = 8
  383.          when "Left"
  384.             @com = 9
  385.          when "Down"
  386.             @com = 10
  387.          when "Up"  
  388.             @com = 11
  389.          else  
  390.             @com = 12          
  391.      end
  392.  end  
  393.  
  394.  #--------------------------------------------------------------------------
  395.  # ● check_command
  396.  #--------------------------------------------------------------------------            
  397.  def check_command(com)
  398.      index = 0
  399.      if com != -1
  400.         right_input = false
  401.         for i in @chain_command
  402.            if index == @com_index
  403.               command_list_check(i)
  404.               right_input = true if @com == com
  405.            end  
  406.           index += 1  
  407.        end  
  408.      else  
  409.        command_list_check(@com_index)
  410.        @change_time = 0
  411.        right_input = true
  412.      end  
  413.      if right_input
  414.         refresh_number
  415.         next_command
  416.      else  
  417.         wrong_command
  418.      end  
  419.  end  
  420.    
  421.  #--------------------------------------------------------------------------
  422.  # ● Next Command
  423.  #--------------------------------------------------------------------------            
  424.  def next_command  
  425.      @com_index += 1  
  426.      Audio.se_play("Audio/SE/" + CHAIN_RIGHT_SE, 100, 100)
  427.      if @com_index == @chain_command.size
  428.         $game_switches[@action_id] = true
  429.         exit
  430.         $game_map.need_refresh = true
  431.      end  
  432.      refresh_command
  433.      refresh_text(0)
  434.  end    
  435.  
  436.  #--------------------------------------------------------------------------
  437.  # ● wrong_command
  438.  #--------------------------------------------------------------------------              
  439.  def wrong_command
  440.      Audio.se_play("Audio/SE/" + CHAIN_WRONG_SE, 100, 100)
  441.      refresh_text(1)
  442.      exit
  443.      $game_player.jump(0,0)
  444.  end
  445.    
  446.  #--------------------------------------------------------------------------
  447.  # ● Refresh Command
  448.  #--------------------------------------------------------------------------                
  449.  def refresh_command
  450.      @sprite.bitmap.clear
  451.      index = 0
  452.      for i in @chain_command
  453.          command_list_check(i)
  454.          bitmap_src_rect = Rect.new(@com * @bitmap_cw, 0, @bitmap_cw, @bitmap_ch)
  455.          if @com_index == index
  456.             @bitmap.blt(index * (@bitmap_cw + 5) , 0, @image, bitmap_src_rect)
  457.          else
  458.             @bitmap.blt(index * (@bitmap_cw + 5) , @bitmap_ch, @image, bitmap_src_rect)
  459.          end
  460.          index += 1
  461.        end
  462.      if @chain_command.size > 15  
  463.         @new_x = (544 / 2) - ((@bitmap_cw + 5) * @com_index)
  464.      else  
  465.         @cursor.x = (544 / 2) - @cursor_space + ((@bitmap_cw + 5) * @com_index)
  466.      end
  467.  end  
  468.  
  469.  #--------------------------------------------------------------------------
  470.  # ● Refresh Text
  471.  #--------------------------------------------------------------------------              
  472.  def refresh_text(type)
  473.      @text.bitmap.clear
  474.      if type == 0
  475.         if @com_index == @chain_command.size
  476.            @text.bitmap.font.color.set(55, 255, 55,220)
  477.            @text.bitmap.draw_text(0, 0, 200, 32, "Perfect!",1)              
  478.         else  
  479.            @text.bitmap.font.color.set(55, 155, 255,220)
  480.            @text.bitmap.draw_text(0, 0, 200, 32, "Success!",1)              
  481.         end
  482.      else
  483.         @text.bitmap.font.color.set(255, 155, 55,220)
  484.         if @timer == 0
  485.            @text.bitmap.draw_text(0, 0, 200, 32, "Time Limit!",1)  
  486.         else  
  487.            @text.bitmap.draw_text(0, 0, 200, 32, "Fail!",1)  
  488.         end
  489.      end  
  490.      @text.x = 230  
  491.      @text.opacity = 255
  492.  end
  493.  
  494.  #--------------------------------------------------------------------------
  495.  # ● Refresh Number
  496.  #--------------------------------------------------------------------------              
  497.  def refresh_number
  498.      @combo += 1
  499.      @number.bitmap.clear
  500.      @number.bitmap.font.size = 34
  501.      @number.bitmap.draw_text(0, 0, 200, 32, @combo.to_s,1)          
  502.      @number.opacity = 255
  503.      @number.zoom_x = 2
  504.      @number.zoom_y = 2
  505.  end
  506.  
  507.  #--------------------------------------------------------------------------
  508.  # ● Update Cursor Slide
  509.  #--------------------------------------------------------------------------          
  510.  def update_cursor_slide    
  511.      @sprite.zoom_x -= 0.1 if @sprite.zoom_x > 1
  512.      @sprite.zoom_y -= 0.1 if @sprite.zoom_y > 1
  513.      @text.x -= 2 if @text.x > 210
  514.      @text.opacity -= 5 if @text.opacity > 0
  515.      @sprite.x -= @slide_time if @sprite.x > @new_x and @chain_command.size > 15
  516.      if @number.zoom_x > 1
  517.         @number.zoom_x -= 0.1
  518.         @number.zoom_y -= 0.1
  519.      end
  520.      if @fy_time > 15
  521.         @fy += 1
  522.      elsif @fy_time > 0
  523.         @fy -= 1
  524.      else  
  525.         @fy = 0
  526.         @fy_time = 30
  527.      end  
  528.      @fy_time -= 1
  529.      @cursor.oy = @fy    
  530.  end  
  531. end
  532.  
  533. #==============================================================================
  534. # ■ Game Temp
  535. #==============================================================================
  536. class Game_Temp
  537.  
  538.  attr_accessor :chain_switch_id
  539.  
  540.  #--------------------------------------------------------------------------
  541.  # ● Initialize
  542.  #--------------------------------------------------------------------------        
  543.   alias mog_chain_commands_initialize initialize
  544.   def initialize
  545.       @chain_switch_id = 0
  546.       mog_chain_commands_initialize
  547.   end  
  548.    
  549. end
  550.  
  551. #==============================================================================
  552. # ■ Game_Interpreter
  553. #==============================================================================
  554. class Game_Interpreter
  555.  
  556.  #--------------------------------------------------------------------------
  557.  # ● Chain Commands
  558.  #--------------------------------------------------------------------------          
  559.   def chain_commands(switch_id = 0)
  560.       return if switch_id <= 0
  561.       $game_temp.chain_switch_id = switch_id
  562.       SceneManager.call(Chain_Commands)
  563.       wait(1)
  564.   end
  565.  
  566. end
  567.  
  568. #===============================================================================
  569. # ■ SceneManager
  570. #===============================================================================
  571. class << SceneManager
  572.   @background_bitmap2 = nil
  573.  
  574.   #--------------------------------------------------------------------------
  575.   # ● Snapshot For Background2
  576.   #--------------------------------------------------------------------------
  577.   def snapshot_for_background2
  578.       @background_bitmap2.dispose if @background_bitmap2
  579.       @background_bitmap2 = Graphics.snap_to_bitmap
  580.   end
  581.  
  582.   #--------------------------------------------------------------------------
  583.   # ● Background Bitmap2
  584.   #--------------------------------------------------------------------------
  585.   def background_bitmap2
  586.       @background_bitmap2
  587.   end
  588.  
  589. end
  590.  
  591. #===============================================================================
  592. # ■ Scene Map
  593. #===============================================================================
  594. class Scene_Map < Scene_Base
  595.  
  596.   #--------------------------------------------------------------------------
  597.   # ● Terminate
  598.   #--------------------------------------------------------------------------  
  599.   alias mog_chain_commands_terminate terminate
  600.   def terminate
  601.       SceneManager.snapshot_for_background2
  602.       mog_chain_commands_terminate      
  603.   end
  604.  
  605. end  
  606.  
  607. $mog_rgss3_chain_commands = true
Add Comment
Please, Sign In to add comment