VX Animated State Icons

Animated State Icons Version: 1.0
By: Racheal

Introduction
Ever wanted to display more state icons then there's space for? Then this script might help. Instead of the default row of icons, this script displays them all in one spot, cross-fading between each one.

Features
  • Can be applied every time a state is drawn or just where you want it to be
  • Customize the speed of the cross-fade and wait count between states
  • Optional: Display an icon for "normal" aka no state

Screenshots

Since the script is all about animation, it's a little hard to show with a stationary screenshot. So instead, here's a gif of approximately what it looks like:
Image

Script

  1. #==============================================================================
  2. # Animated State Icons
  3. # by: Racheal
  4. # Version 1.0
  5. # Created: 02/04/2010
  6. #==============================================================================
  7. # Displays states as one animated sprite instead of a row of icons
  8. #==============================================================================
  9. # Instructions:
  10. # * Insert in the Materials section
  11. #   (though it should work anywhere below Window_Base and above Main)
  12. # * Four constants in module AnimatedStates are editable; see comments below
  13. #==============================================================================
  14. # Compatibility:
  15. # * Alias 8 Window_Base functions: initialize, dispose, update,
  16. #                                  visible=, viewport=, x=, y=
  17. #                                  and draw_actor_state
  18. # * Overwrites nothing
  19. #==============================================================================
  20.  
  21. module AnimatedStates
  22.   #-------------------------------CUSTOMIZATION--------------------------------
  23.  
  24.   # icon index for "Normal" aka no status. Set it to a blank icon if you
  25.   # do not want an icon for Normal. To determine the icon index of an icon
  26.   # count left to right, starting from 0, in the icon set file.
  27.   # (There are 16 icons per row)
  28.   NORMAL_STATUS = 0
  29.  
  30.   # Amount opacity is changed per frame; higher number = faster cross fade
  31.   # Value must be between 1 and 255
  32.   OPACITY_STEP = 5
  33.  
  34.   # Number of frames current state is displayed before starting cross-fade
  35.   # into next state
  36.   WAIT = 60
  37.  
  38.   # If set to true, all draw_actor_states draw animated icons.
  39.   # If you want some state draws to be animated, and some to be the old
  40.   # stationary style, set this to false and call the function
  41.   # draw_animated_state(battler, x, y)
  42.   # instead of draw_actor_state(actor, x, y, width = 96)
  43.   # when you want to use animated state icons.
  44.   ANIMATE_ALWAYS = true
  45.  
  46.   #---------------------------END CUSTOMIZATION--------------------------------
  47. end
  48.  
  49. #==============================================================================
  50. # Sprite_States
  51. #==============================================================================
  52.  
  53. class Sprite_States < Sprite_Base
  54.   #--------------------------------------------------------------------------
  55.   # * Public Instance Variables
  56.   #--------------------------------------------------------------------------
  57.   attr_accessor :battler
  58.   #--------------------------------------------------------------------------
  59.   # * Object Initialization
  60.   #     viewport : viewport
  61.   #     battler  : battler (Game_Battler)
  62.   #--------------------------------------------------------------------------
  63.   def initialize(viewport, battler = nil)
  64.     super(viewport)
  65.     @battler = battler
  66.     @state_index = -2
  67.     @win_x = @win_y = 0
  68.     @wait_count = AnimatedStates::WAIT
  69.     self.bitmap = Cache.system("Iconset")
  70.     @next_state = Sprite_Base.new(viewport)
  71.     @next_state.bitmap = Cache.system("Iconset")
  72.     start_state
  73.     update
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # * Dispose
  77.   #--------------------------------------------------------------------------
  78.   def dispose
  79.     @battler = nil
  80.     @next_state.dispose
  81.     super
  82.   end
  83.   #--------------------------------------------------------------------------
  84.   # * Set Window Cordinates
  85.   #--------------------------------------------------------------------------
  86.   def set_window_cord (x, y)
  87.     self.x -= @win_x
  88.     self.y -= @win_y
  89.     @win_x = x
  90.     @win_y = y
  91.     self.x += @win_x
  92.     self.y += @win_y
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # * Frame Update
  96.   #--------------------------------------------------------------------------
  97.   def update
  98.     super
  99.     # Update Next State icons based on self
  100.     @next_state.x = self.x
  101.     @next_state.y = self.y
  102.     @next_state.ox = self.ox
  103.     @next_state.oy = self.oy
  104.     @next_state.z = self.z + 5
  105.     @next_state.viewport = self.viewport
  106.     @next_state.visible = self.visible
  107.     # Do not display if not battler
  108.     if @battler == nil
  109.       @state_index = -2
  110.       self.opacity = 0
  111.       @next_state.opacity = 0
  112.     else
  113.       return unless self.visible # Don't update if not visible
  114.       if @state_index != -2
  115.         if @next_state.opacity > 0
  116.           update_fade
  117.         else
  118.           if @wait_count == 0
  119.             update_state
  120.             @wait_count = AnimatedStates::WAIT
  121.           else
  122.             @wait_count -= 1
  123.           end
  124.         end
  125.       else # Set icon to current state, no fading
  126.         start_state
  127.       end
  128.     end
  129.   end
  130.   #--------------------------------------------------------------------------
  131.   # * Start State Display
  132.   #--------------------------------------------------------------------------
  133.   def start_state
  134.     if @battler == nil or @battler.states.size == 0 # Normal Status
  135.       set_current_state(AnimatedStates::NORMAL_STATUS)
  136.       @state_index = -1
  137.     else
  138.       set_current_state(@battler.states[0].icon_index)
  139.       @state_index = 0
  140.     end
  141.     set_next_state(AnimatedStates::NORMAL_STATUS)
  142.     self.opacity = 255
  143.     @next_state.opacity = 0
  144.   end
  145.   #--------------------------------------------------------------------------
  146.   # * Update State Display
  147.   #--------------------------------------------------------------------------
  148.   def update_state
  149.     return if @battler == nil
  150.     states = @battler.states
  151.     if states.size == 0
  152.       return if @state_index == -1
  153.       set_next_state(AnimatedStates::NORMAL_STATUS)
  154.       @state_index = -1
  155.     else
  156.       return if states.size == 1 and @state_index == 0 # Don't animate if only 1 status
  157.       @state_index += 1
  158.       @state_index = 0 if @state_index >= states.size
  159.       set_next_state(states[@state_index].icon_index)
  160.       @state_index = @state_index
  161.     end
  162.     update_fade
  163.   end
  164.   #--------------------------------------------------------------------------
  165.   # * Update Fading between states
  166.   #--------------------------------------------------------------------------
  167.   def update_fade
  168.     @next_state.opacity += AnimatedStates::OPACITY_STEP
  169.     self.opacity -= AnimatedStates::OPACITY_STEP
  170.     if @next_state.opacity == 255
  171.       self.src_rect = @next_state.src_rect
  172.       self.opacity = 255
  173.       @next_state.opacity = 0
  174.     end
  175.   end
  176.   #--------------------------------------------------------------------------
  177.   # * Set Current Icon
  178.   #--------------------------------------------------------------------------
  179.   def set_current_state(icon_index)
  180.     self.src_rect.set(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  181.   end
  182.   #--------------------------------------------------------------------------
  183.   # * Set Next Icon
  184.   #--------------------------------------------------------------------------
  185.   def set_next_state(icon_index)
  186.     @next_state.src_rect.set(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  187.   end
  188. end
  189.  
  190. #==============================================================================
  191. # Base Window Edit
  192. #==============================================================================
  193.  
  194. class Window_Base < Window    
  195.   #--------------------------------------------------------------------------
  196.   # * Object Initialization
  197.   #--------------------------------------------------------------------------
  198.   alias animstate_old_init initialize
  199.   def initialize(x, y, width, height)
  200.     @anim_states = []
  201.     animstate_old_init(x, y, width, height)
  202.   end
  203.   #--------------------------------------------------------------------------
  204.   # * Dispose
  205.   #--------------------------------------------------------------------------
  206.   alias animstate_old_dispose dispose
  207.   def dispose
  208.     animstate_old_dispose
  209.     for state in @anim_states
  210.       state.dispose if state != nil
  211.     end
  212.   end
  213.   #--------------------------------------------------------------------------
  214.   # * Set Visible
  215.   #--------------------------------------------------------------------------
  216.   alias animstate_old_visible visible=
  217.   def visible= (visibility)
  218.     animstate_old_visible(visibility)
  219.     update_all_state_sprites
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # * Set Viewport
  223.   #--------------------------------------------------------------------------
  224.   alias animstate_old_viewport viewport=
  225.   def viewport= (view)
  226.     animstate_old_viewport(view)
  227.     update_all_state_sprites
  228.   end
  229.   #--------------------------------------------------------------------------
  230.   # * Set X
  231.   #--------------------------------------------------------------------------
  232.   alias animstate_old_x x=
  233.   def x= (x)
  234.     animstate_old_x(x)
  235.     update_all_state_sprites
  236.   end
  237.   #--------------------------------------------------------------------------
  238.   # * Set Y
  239.   #--------------------------------------------------------------------------
  240.   alias animstate_old_y y=
  241.   def y= (y)
  242.     animstate_old_y(y)
  243.     update_all_state_sprites
  244.   end
  245.   #--------------------------------------------------------------------------
  246.   # * Update
  247.   #--------------------------------------------------------------------------
  248.   alias animstate_old_update update
  249.   def update
  250.     animstate_old_update
  251.     update_all_state_sprites
  252.   end
  253.   #--------------------------------------------------------------------------
  254.   # * Update All State Sprites
  255.   #--------------------------------------------------------------------------
  256.   def update_all_state_sprites
  257.     for i in 0...@anim_states.size
  258.       update_state_sprite(i)
  259.     end
  260.   end
  261.   #--------------------------------------------------------------------------
  262.   # * Update State Sprite
  263.   #--------------------------------------------------------------------------
  264.   def update_state_sprite(index)
  265.     state = @anim_states[index]
  266.     return if state == nil
  267.     state.viewport = self.viewport
  268.     if self.viewport != nil
  269.       state.visible = (self.visible and self.viewport.visible)
  270.     else
  271.       state.visible = self.visible
  272.     end
  273.     state.set_window_cord(self.x, self.y)
  274.     state.ox = self.ox
  275.     state.oy = self.oy
  276.     state.visible = state.visible and sprite_in_bounds?(state)
  277.     state.z = self.z + 5
  278.     state.update
  279.   end
  280.   #--------------------------------------------------------------------------
  281.   # * Sprites in Bounds?
  282.   #      determine is sprite is within the window
  283.   #--------------------------------------------------------------------------
  284.   def sprite_in_bounds?(sprite)
  285.     return false unless (sprite.x - sprite.ox) >= self.x
  286.     return false unless (sprite.y - sprite.oy) >= self.y
  287.     return false unless (sprite.x - sprite.ox) <= (self.x + self.width)
  288.     return false unless (sprite.y - sprite.oy) <= (self.y + self.height)
  289.     return true
  290.   end
  291.   #--------------------------------------------------------------------------
  292.   # * Draw Animated State
  293.   #--------------------------------------------------------------------------
  294.   def draw_animated_state(battler, x, y)
  295.     # Check if battler's state is already drawn
  296.     for i in 0...@anim_states.size
  297.       if @anim_states[i].battler.index == battler.index
  298.         @anim_states[i].x = x + self.x + 16
  299.         @anim_states[i].y = y + self.y + 16
  300.         return
  301.       end
  302.     end
  303.     viewp = self.viewport == nil ? Viewport.new(0, 0, 544, 416) : self.viewport
  304.     new_state = Sprite_States.new(viewp, battler)
  305.     new_state.x = x + 16
  306.     new_state.y = y + 16
  307.     @anim_states.push(new_state)
  308.     update_state_sprite(@anim_states.size - 1)
  309.   end
  310.   #--------------------------------------------------------------------------
  311.   # * Draw State
  312.   #--------------------------------------------------------------------------
  313.   alias animstate_old_draw_state draw_actor_state
  314.   def draw_actor_state(actor, x, y, width = 96)
  315.     if AnimatedStates::ANIMATE_ALWAYS
  316.       draw_animated_state(actor, x, y)
  317.     else
  318.       animstate_old_draw_state(actor, x, y, width)
  319.     end
  320.   end
  321. end
Expand to see the code.


Instructions

Copy-Paste above Main but below Window_Base.

Most of the cutomization options are explained in the script.

Compatibility

Doesn't overwrite anything so should be compatible with most anything.

Author's Notes

This is the first script I've released publically outside of a old tiny little thing, but I tried to make it as user friendly as I could. I spent a few hours testing and squashing bugs, but please let me know if you encounter any issues.

Terms and Conditions

Free for non-commercial use, though I do request you credit me.
If you want to use it for commercial use, please contact me via pm.
Nice idea, nice scripting, nice thread... thumbs up from me (which I think means something :p ).

The only things you could improve are the customization constants (in my opinion totally unneeded - everyone not satisfied with the default could easily find those values within the script anyway) and not making update_state_sprite() a seperate method, as it's only called in one place (if I'm not mistaken) and therefore doesn't need it's own definition.
As far as the thread goes, you don't need a spoiler around code tags... that's why they're shut by default ^^

Keep up the good work, it's definately rare to see well executed (as well as commented, for that matter) scripts in here!
I get a bug whenever I use F12 to return to the Title Menu... I added unless $@ to the end of all of your aliases,
but the error still persists...


EDIT: NVM. Was incompatibility with KGC_DrawFormatText... my bad.

Also, there seems to be a small bug if you apply states via events. Upon entering battle, your states don't
animate.

I am using Enu's Sideview Battle System 3.3d + the ATB 1.1i script, which could be the cause of the problem.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=70417