Double_X

DoubleX RMVXA State Counters v1.00e

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