Advertisement
Double_X

DoubleX RMVXA State Counters v1.00e

May 11th, 2015 (edited)
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 21.79 KB | None | 0 0
  1. #==============================================================================|
  2. #  ** Script Info                                                              |
  3. #------------------------------------------------------------------------------|
  4. #  * Script Name                                                               |
  5. #    DoubleX RMVXA State Counters                                              |
  6. #------------------------------------------------------------------------------|
  7. #  * Functions                                                                 |
  8. #    Displays the state icons with remaining turns on their battler sprites    |
  9. #------------------------------------------------------------------------------|
  10. #  * Terms Of Use                                                              |
  11. #    You shall keep this script's Script Info part's contents intact           |
  12. #    You shalln't claim that this script is written by anyone other than       |
  13. #    DoubleX or his aliases                                                    |
  14. #    None of the above applies to DoubleX or his aliases                       |
  15. #------------------------------------------------------------------------------|
  16. #  * Prerequisites                                                             |
  17. #    Abilities:                                                                |
  18. #    1. Some RGSS3 scripting proficiency to fully utilize this script          |
  19. #------------------------------------------------------------------------------|
  20. #  * Instructions                                                              |
  21. #    1. Open the script editor and put this script into an open slot between   |
  22. #       Materials and Main, save to take effect.                               |
  23. #------------------------------------------------------------------------------|
  24. #  * Links                                                                     |
  25. #    Script Usage 101:                                                         |
  26. #    1. forums.rpgmakerweb.com/index.php?/topic/32752-rmvxa-script-usage-101/  |
  27. #    2. rpgmakervxace.net/topic/27475-rmvxa-script-usage-101/                  |
  28. #    This script:                                                              |
  29. #    1. http://pastebin.com/pPvJCrmx                                           |
  30. #    Mentioned Patreon Supporters:                                             |
  31. #    https://www.patreon.com/posts/71738797                                    |
  32. #------------------------------------------------------------------------------|
  33. #  * Authors                                                                   |
  34. #    DoubleX                                                                   |
  35. #------------------------------------------------------------------------------|
  36. #  * Changelog                                                                 |
  37. #    v1.00e(GMT 1300 19-9-2015):                                               |
  38. #    1. Fixed lacking the spriteset viewport reader and iconset loader bug     |
  39. #    2. Further increased this script's compatibility                          |
  40. #    v1.00d(GMT 0800 16-9-2015):                                               |
  41. #    1. Fixed duplicate actor sprite creations at the start of battles bug     |
  42. #    2. Increased this script's compatibility and readability                  |
  43. #    v1.00c(GMT 1500 14-4-2015):                                               |
  44. #    1. Improved this script's robustness                                      |
  45. #    v1.00b(GMT 1100 11-4-2015):                                               |
  46. #    1. Fixed the state remaining turn display bug when a state's reapplied    |
  47. #    v1.00a(GMT 0900 11-4-2015):                                               |
  48. #    1. 1st version of this script finished                                    |
  49. #==============================================================================|
  50.  
  51. ($doublex_rmvxa ||= {})[:State_Counters] = "v1.00e"
  52.  
  53. #==============================================================================|
  54. #  ** Script Configurations                                                    |
  55. #     You only need to edit this part as it's about what this script does      |
  56. #------------------------------------------------------------------------------|
  57.  
  58. module DoubleX_RMVXA
  59.  
  60.   module State_Counters
  61.  
  62.       # Sets if this script needs to create the actor sprites at the start of
  63.       # battles
  64.       # This script won't display the actor sprites by itself
  65.       # It should be set as false if other custom scripts already do that
  66.       # It can't be changed once set
  67.       # Example: To use this script to create the actor sprites at the start of
  68.       #          battles, set this as true
  69.       CREATE_ACTOR_SPRITE = false
  70.  
  71.       # Sets if the state icons are displayed for actor sprites as well
  72.       # It only works with scripts supporting actor sprite displays in battles
  73.       # It's used at:
  74.       # 1. Game_Battler
  75.       #    - return nil unless enemy? || #{ACTOR_SPRITE} in
  76.       #      state_counters_battler_sprite
  77.       # 2. Sprite_Battler
  78.       #    - @state_counters = {} if @battler && (@battler.enemy? ||
  79.       #      #{ACTOR_SPRITE}) in initialize
  80.       #    - return unless @battler && (@battler.enemy? || #{ACTOR_SPRITE}) in
  81.       #      create_state_counter
  82.       # Example: To display the state icon for actor sprites as well, set this
  83.       #          as "true"
  84.       ACTOR_SPRITE = "false"
  85.  
  86.       # Sets the state icon xy offsets from its battler sprite
  87.       # It's used at:
  88.       # 1. State_Counter_Sprite
  89.       #    - xy_offset = #{XY_OFFSET} in update_pos
  90.       # The state priority index can be referneced by @index
  91.       # It must return an array of real numbers and should return an array of
  92.       # integers
  93.       # Example: To set the state icon xy offsets as the value of
  94.       #          variables with id a and b respectively, set this as
  95.       #          "[$game_variables[a], $game_variables[b]]"
  96.       XY_OFFSET = "[24 * (@index % 2 - 0.5), -24 * (@index / 2) - 20]"
  97.  
  98.       # Sets the state icon z position
  99.       # It's used at:
  100.       # 1. State_Counter_Sprite
  101.       #    - z_pos = #{Z} in update_pos
  102.       # It must return a non-negative real number and should return a
  103.       # non-negative integer
  104.       # Example: To set the state icon z position as the value of variable
  105.       #          with id x, set this as "$game_variables[x]"
  106.       Z = "125"
  107.  
  108.       # Sets the state remaining turn text color
  109.       # It's used at:
  110.       # 1. State_Counter_Sprite
  111.       #    - color = #{TEXT_COLOR} in update_text_font
  112.       # It must return a color
  113.       # Example: To set the state remaining turn text color's rgba values as r,
  114.       #          g, b and a, set this as "Color.new(r, g, b, a)"
  115.       TEXT_COLOR = "Color.new(0, 0, 0, 255)"
  116.  
  117.       # Sets the state remaining turn text size
  118.       # It's used at:
  119.       # 1. State_Counter_Sprite
  120.       #    - size = #{TEXT_SIZE} in update_text_font
  121.       # It must return a positive real number and should return a natural number
  122.       # Example: To set the state remaining turn text size as the value of
  123.       #          variable with id x, set this as "$game_variables[x]"
  124.       TEXT_SIZE = "16"
  125.  
  126.       # Sets the state remaining turn text x and y positions
  127.       # It's used at:
  128.       # 1. State_Counter_Sprite
  129.       #    - text_xy = #{TEXT_XY} in update_turn
  130.       # It must return an array of real numbers and should return an array of
  131.       # integers
  132.       # Example: To set the state remaining turn text x and y positions as the
  133.       #          value of variables with id a and b respectively, set this as
  134.       #          "[$game_variables[a], $game_variables[b]]"
  135.       TEXT_XY = "[12, 0]"
  136.  
  137. #==============================================================================|
  138. #  ** Script Implementations                                                   |
  139. #     You need not edit this part as it's about how this script works          |
  140. #------------------------------------------------------------------------------|
  141. #  * Script Support Info:                                                      |
  142. #    1. Prerequisites                                                          |
  143. #       - Some RGSS3 scripting proficiency to fully comprehend this script     |
  144. #    2. Method documentation                                                   |
  145. #       - The 1st part describes why this method's rewritten/aliased for       |
  146. #         rewritten/aliased methods or what the method does for new methods    |
  147. #       - The 2nd part describes what the arguments of the method are          |
  148. #       - The 3rd part informs which version rewritten, aliased or created this|
  149. #         method                                                               |
  150. #       - The 4th part informs whether the method's rewritten or new           |
  151. #       - The 5th part describes how this method works for new methods only,   |
  152. #         and describes the parts added, removed or rewritten for rewritten or |
  153. #         aliased methods only                                                 |
  154. #       Example:                                                               |
  155. # #--------------------------------------------------------------------------| |
  156. # #  Why rewrite/alias/What this method does                                 | |
  157. # #--------------------------------------------------------------------------| |
  158. # # *argv: What these variables are                                            |
  159. # # &argb: What this block is                                                  |
  160. # def def_name(*argv, &argb) # Version X+; Rewrite/New                         |
  161. #   # Added/Removed/Rewritten to do something/How this method works            |
  162. #   def_name_code                                                              |
  163. #   #                                                                          |
  164. # end # def_name                                                               |
  165. #------------------------------------------------------------------------------|
  166.  
  167.     GAME_BATTLER = %Q(
  168.   def state_counters_battler_sprite
  169.     return nil unless SceneManager.scene_is?(Scene_Battle)
  170.     return nil unless SceneManager.scene.spriteset
  171.     return nil unless enemy? || #{ACTOR_SPRITE}
  172.     SceneManager.scene.spriteset.battler_sprites.find { |s| s.battler == self }
  173.   end
  174.     )
  175.  
  176.     SPRITE_BATTLER = %Q(
  177.   alias initialize_state_counters initialize
  178.   def initialize(viewport, battler = nil, &argb)
  179.     initialize_state_counters(viewport, battler, &argb)
  180.     @state_counters = {}
  181.   end
  182.  
  183.   def create_state_counter(state, turn)
  184.     return unless @battler && (@battler.enemy? || #{ACTOR_SPRITE})
  185.     return if !@state_counters || @state_counters[state]
  186.     @state_counters[state] = State_Counter_Sprite.new(@battler, state, turn)
  187.   end
  188.     )
  189.  
  190.     STATE_COUNTER_SPRITE = %Q(
  191.   def update_pos
  192.     z_pos = #{Z}
  193.     self.z = z_pos if z != z_pos
  194.     dx = @battler.screen_x
  195.     dy = @battler.screen_y
  196.     xy_offset = #{XY_OFFSET}
  197.     return if @last_x == dx && @last_y == dy && @last_xy_offest == xy_offset
  198.     @last_x = dx
  199.     @last_y = dy
  200.     @last_xy_offest = xy_offset
  201.     self.x = dx + xy_offset[0]
  202.     self.y = dy + xy_offset[1]
  203.   end
  204.  
  205.   def update_turn(turn)
  206.     return if disposed? || bitmap.disposed? || !@bw || !@bh
  207.     update_text_font
  208.     bitmap.clear
  209.     return unless visible
  210.     icon_index = @state.icon_index
  211.     @rect ||= Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  212.     bitmap.blt(24 + @bw / 2 - 36, (@bh - 24) / 2, Cache.system("Iconset"),
  213.     @rect, 255)
  214.     return if turn <= 0 || @state.auto_removal_timing <= 0
  215.     text_xy = #{TEXT_XY}
  216.     bitmap.draw_text(text_xy[0], text_xy[1], @bw - 24, @bh, turn.to_s, 1)
  217.   end
  218.  
  219.   def update_text_font
  220.     color = #{TEXT_COLOR}
  221.     bitmap.font.out_color.set(color) if bitmap.font.out_color != color
  222.     size = #{TEXT_SIZE}
  223.     bitmap.font.size = size if bitmap.font.size != size
  224.   end
  225.     )
  226.  
  227.   end # State_Counters
  228.  
  229. end # DoubleX_RMVXA
  230.  
  231. class Game_BattlerBase # Edit
  232.  
  233.   alias erase_state_counters erase_state
  234.   def erase_state(state_id, &argb)
  235.     remove_state_counter(state_id) if @states.include?(state_id) # Added
  236.     erase_state_counters(state_id, &argb)
  237.   end # erase_state
  238.  
  239.   #----------------------------------------------------------------------------|
  240.   #  Removes the state icons and repositions those having larger indices       |
  241.   #----------------------------------------------------------------------------|
  242.   def remove_state_counter(state_id) # New
  243.     battler_sprite = state_counters_battler_sprite
  244.     return unless battler_sprite && battler_sprite.state_counters
  245.     index = if battler_sprite.state_counters[$data_states[state_id]]
  246.       battler_sprite.state_counters[$data_states[state_id]].index
  247.     else
  248.       nil
  249.     end
  250.     battler_sprite.dispose_state_counter($data_states[state_id])
  251.     reposition_state_counters(battler_sprite.state_counters, index) if index
  252.   end # remove_state_counter
  253.  
  254.   #----------------------------------------------------------------------------|
  255.   #  Repositions all the state icons on its battler sprite                     |
  256.   #----------------------------------------------------------------------------|
  257.   def reposition_state_counters(state_counters, index) # New
  258.     state_counters.each_value { |sprite|
  259.       next if sprite.index <= index
  260.       sprite.index -= 1
  261.       sprite.update_pos if sprite.visible
  262.     }
  263.   end # reposition_state_counters
  264.  
  265. end # Game_BattlerBase
  266.  
  267. class Game_Battler < Game_BattlerBase # Edit
  268.  
  269.   alias clear_states_counters clear_states
  270.   def clear_states(*argv, &argb)
  271.     clear_states_counters(*argv, &argb)
  272.     clear_state_counters # Added
  273.   end # clear_states
  274.  
  275.   alias reset_state_counts_state_counters reset_state_counts
  276.   def reset_state_counts(state_id, &argb)
  277.     reset_state_counts_state_counters(state_id, &argb)
  278.     reset_state_counter(state_id) if @states.include?(state_id) # Added
  279.   end # reset_state_counts
  280.  
  281.   alias update_state_turns_state_counters update_state_turns
  282.   def update_state_turns(*argv, &argb)
  283.     update_state_turns_state_counters(*argv, &argb)
  284.     update_state_counters # Added
  285.   end # update_state_turns
  286.  
  287.   #----------------------------------------------------------------------------|
  288.   #  Removes all state icons from their battler sprite                         |
  289.   #----------------------------------------------------------------------------|
  290.   def clear_state_counters # New
  291.     battler_sprite = state_counters_battler_sprite
  292.     return unless battler_sprite && battler_sprite.state_counters
  293.     battler_sprite.state_counters.each_key { |state|
  294.       battler_sprite.dispose_state_counter(state)
  295.     }
  296.   end # clear_state_counters
  297.  
  298.   #----------------------------------------------------------------------------|
  299.   #  Resets this state's icon and sort all state icons using their priorities  |
  300.   #----------------------------------------------------------------------------|
  301.   def reset_state_counter(state_id) # New
  302.     battler_sprite = state_counters_battler_sprite
  303.     return unless battler_sprite && battler_sprite.state_counters
  304.     state = $data_states[state_id]
  305.     turn = @state_turns[state_id]
  306.     if battler_sprite.state_counters[state]
  307.       return battler_sprite.state_counters[state].update_turn(turn)
  308.     end
  309.     battler_sprite.create_state_counter(state, turn)
  310.     sort_state_counters(battler_sprite.state_counters)
  311.   end # reset_state_counter
  312.  
  313.   #----------------------------------------------------------------------------|
  314.   #  Sort all state icons on their battler sprite using their priorities       |
  315.   #----------------------------------------------------------------------------|
  316.   def sort_state_counters(state_counters) # New
  317.     state_counters.each { |state, sprite|
  318.       sprite.index = states.index(state)
  319.       sprite.update_pos if sprite.visible
  320.     }
  321.   end # sort_state_counters
  322.  
  323.   #----------------------------------------------------------------------------|
  324.   #  Updates the remaining turn of the state icons on their battler sprite     |
  325.   #----------------------------------------------------------------------------|
  326.   def update_state_counters # New
  327.     battler_sprite = state_counters_battler_sprite
  328.     return unless battler_sprite && battler_sprite.state_counters
  329.     battler_sprite.state_counters.each { |state, sprite|
  330.       sprite.update_turn(@state_turns[state.id])
  331.     }
  332.   end # update_state_counters
  333.  
  334.   #----------------------------------------------------------------------------|
  335.   #  Returns the battler sprite used by this battler in battles                |
  336.   #----------------------------------------------------------------------------|
  337.   module_eval(DoubleX_RMVXA::State_Counters::GAME_BATTLER) # New
  338.  
  339. end # Game_Battler
  340.  
  341. if DoubleX_RMVXA::State_Counters::CREATE_ACTOR_SPRITE
  342.  
  343. class Spriteset_Battle # Edit
  344.  
  345.   alias create_actors_state_counters create_actors
  346.   def create_actors(*argv, &argb)
  347.     create_actors_state_counters(*argv, &argb)
  348.     # Added to insert actors into the actor sprites if it's not already done
  349.     @actor_sprites = $game_party.members.reverse.collect { |actor|
  350.       Sprite_Battler.new(@viewport1, actor)
  351.     }
  352.     #
  353.   end # create_actors
  354.  
  355. end # Spriteset_Battle
  356.  
  357. end # if DoubleX_RMVXA::State_Counters::CREATE_ACTOR_SPRITE
  358.  
  359. class Sprite_Battler < Sprite_Base # Edit
  360.  
  361.   #----------------------------------------------------------------------------|
  362.   #  New public instance variable                                              |
  363.   #----------------------------------------------------------------------------|
  364.   attr_reader :state_counters # The battler sprite state icon container
  365.  
  366.   #----------------------------------------------------------------------------|
  367.   #  Creates the battler sprite state icon container if the battler shows them |
  368.   #----------------------------------------------------------------------------|
  369.   module_eval(DoubleX_RMVXA::State_Counters::SPRITE_BATTLER) # Alias; New
  370.  
  371.   alias dispose_state_counters dispose
  372.   def dispose(*argv, &argb)
  373.     dispose_state_counters(*argv, &argb)
  374.     # Added
  375.     return unless @state_counters
  376.     @state_counters.each_key { |state| dispose_state_counter(state) }
  377.     #
  378.   end # dispose
  379.  
  380.   alias update_state_counters update
  381.   def update(*argv, &argb)
  382.     update_state_counters(*argv, &argb)
  383.     # Added
  384.     return unless @state_counters
  385.     @state_counters.each_value { |sprite| sprite.update }
  386.     #
  387.   end # dispose
  388.  
  389.   def dispose_state_counter(state) # New
  390.     return unless @state_counters[state]
  391.     @state_counters[state].dispose
  392.     @state_counters.delete(state)
  393.   end # dispose_state_counter
  394.  
  395. end # Sprite_Battler
  396.  
  397. class Spriteset_Battle # v1.00e+; Edit
  398.  
  399.   #----------------------------------------------------------------------------|
  400.   #  New public instance variable                                              |
  401.   #----------------------------------------------------------------------------|
  402.   attr_reader :viewport1 # Used by State_Counter_Sprite to read the 1st viewport
  403.  
  404. end # Spriteset_Battle
  405.  
  406. #------------------------------------------------------------------------------|
  407. #  * Handles the state icons with remaining turns on their battler sprites     |
  408. #------------------------------------------------------------------------------|
  409. class State_Counter_Sprite < Sprite # New
  410.  
  411.   #----------------------------------------------------------------------------|
  412.   #  New public instance variable                                              |
  413.   #----------------------------------------------------------------------------|
  414.   attr_accessor :index # The state index in the priority-sorted states
  415.  
  416.   #----------------------------------------------------------------------------|
  417.   #  New private instance variables                                            |
  418.   #----------------------------------------------------------------------------|
  419.   # @battler: The battler having this sprite
  420.   # @state: The state using this sprite
  421.  
  422.   def initialize(battler, state, turn)
  423.     super(SceneManager.scene.spriteset.viewport1)
  424.     @battler = battler
  425.     @state = state
  426.     @index = @battler.states.index(@state)
  427.     self.visible = @battler.alive?
  428.     create_state_bitmap
  429.     update_turn(turn)
  430.   end # initialize
  431.  
  432.   def create_state_bitmap
  433.     @bw = Graphics.width + 48
  434.     @bh = Font.default_size * 3
  435.     self.bitmap = Bitmap.new(@bw, @bh)
  436.     update_pos
  437.     self.ox = @bw / 2
  438.     self.oy = @bh / 2
  439.   end # create_state_bitmap
  440.  
  441.   #----------------------------------------------------------------------------|
  442.   #  Updates this state icon's display                                         |
  443.   #----------------------------------------------------------------------------|
  444.   def update
  445.     super
  446.     update_pos if self.visible = @battler.alive?
  447.   end # update
  448.  
  449.   def dispose
  450.     return if disposed?
  451.     bitmap.dispose unless bitmap.disposed?
  452.     super
  453.   end # dispose
  454.  
  455.   #----------------------------------------------------------------------------|
  456.   #  Updates the positions, state turns and text fonts                         |
  457.   #----------------------------------------------------------------------------|
  458.   module_eval(DoubleX_RMVXA::State_Counters::STATE_COUNTER_SPRITE)
  459.  
  460. end # State_Counter_Sprite
  461.  
  462. class Scene_Battle < Scene_Base # Edit
  463.  
  464.   #----------------------------------------------------------------------------|
  465.   #  New public instance variable                                              |
  466.   #----------------------------------------------------------------------------|
  467.   attr_reader :spriteset # Used by Game_Battler to read the battler sprite set
  468.  
  469. end # Scene_Battle
  470.  
  471. #------------------------------------------------------------------------------|
  472.  
  473. #==============================================================================|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement