Advertisement
Archeia

Yanfly Engine Ace - State Animations v1.01

Mar 30th, 2014
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.39 KB | None | 0 0
  1. #==============================================================================
  2. #
  3. # ▼ Yanfly Engine Ace - State Animations v1.01
  4. # -- Last Updated: 2014.03.22
  5. # -- Level: Normal
  6. # -- Requires: n/a
  7. # -- Special Thanks:
  8. #    SoulPour777 for the Bug Fix for V1.01
  9. #==============================================================================
  10.  
  11. $imported = {} if $imported.nil?
  12. $imported["YEA-StateAnimations"] = true
  13.  
  14. #==============================================================================
  15. # ▼ Updates
  16. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  17. # 2014.03.22 - Some of the animations have been using the same reference count,
  18. #              which results to cancelling the animation.
  19. # 2011.12.23 - Started Script and Finished.
  20. #
  21. #==============================================================================
  22. # ▼ Introduction
  23. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  24. # A missing feature from RPG Maker XP. Status effects had animations replaying
  25. # on them constantly to indicate that a user was affected by a state. Only the
  26. # state with the highest priority and possesses an animation will be played.
  27. #
  28. #==============================================================================
  29. # ▼ Instructions
  30. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  31. # To install this script, open up your script editor and copy/paste this script
  32. # to an open slot below ▼ Materials/素材 but above ▼ Main. Remember to save.
  33. #
  34. # -----------------------------------------------------------------------------
  35. # State Notetags - These notetags go in the states notebox in the database.
  36. # -----------------------------------------------------------------------------
  37. # <state ani: x>
  38. # Causes the status effect to play battle animation x repeatedly on the battler
  39. # if the battler is affected by this state and if this state is the highest
  40. # priority state with an animation.
  41. #
  42. #==============================================================================
  43. # ▼ Compatibility
  44. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  45. # This script is made strictly for RPG Maker VX Ace. It is highly unlikely that
  46. # it will run with RPG Maker VX without adjusting.
  47. #
  48. #==============================================================================
  49.  
  50. module YEA
  51.   module STATE_ANIMATION
  52.    
  53.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  54.     # - Adjust the state animation settings here. -
  55.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  56.     # These settings decide whether or not state animations will cause the
  57.     # screen to flash, play sound effects, and what kinds of zoom levels will
  58.     # be used on actors affected by states with animations.
  59.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  60.     PLAY_SOUND = false      # Play sounds for state animations?
  61.     PLAY_FLASH = false      # Use screen flash for state animations?
  62.    
  63.     PLAY_ACTOR = false       # Play animations on the actor?
  64.     ACTOR_ZOOM = 0.25       # Zoom level for animations on actors.
  65.    
  66.   end # STATE_ANIMATION
  67. end # YEA
  68.  
  69. #==============================================================================
  70. # ▼ Editting anything past this point may potentially result in causing
  71. # computer damage, incontinence, explosion of user's head, coma, death, and/or
  72. # halitosis so edit at your own risk.
  73. #==============================================================================
  74.  
  75. module YEA
  76.   module REGEXP
  77.   module STATE
  78.    
  79.     STATEANI = /<(?:STATE_ANIMATION|state ani|animation|ani):[ ](\d+)>/i
  80.    
  81.   end # STATE
  82.   end # REGEXP
  83. end # YEA
  84.  
  85. #==============================================================================
  86. # ■ DataManager
  87. #==============================================================================
  88.  
  89. module DataManager
  90.  
  91.   #--------------------------------------------------------------------------
  92.   # alias method: load_database
  93.   #--------------------------------------------------------------------------
  94.   class <<self; alias load_database_sani load_database; end
  95.   def self.load_database
  96.     load_database_sani
  97.     load_notetags_sani
  98.   end
  99.  
  100.   #--------------------------------------------------------------------------
  101.   # new method: load_notetags_sani
  102.   #--------------------------------------------------------------------------
  103.   def self.load_notetags_sani
  104.     for state in $data_states
  105.       next if state.nil?
  106.       state.load_notetags_sani
  107.     end
  108.   end
  109.  
  110. end # DataManager
  111.  
  112. #==============================================================================
  113. # ■ RPG::State
  114. #==============================================================================
  115.  
  116. class RPG::State < RPG::BaseItem
  117.  
  118.   #--------------------------------------------------------------------------
  119.   # public instance variables
  120.   #--------------------------------------------------------------------------
  121.   attr_accessor :state_animation
  122.  
  123.   #--------------------------------------------------------------------------
  124.   # common cache: load_notetags_sani
  125.   #--------------------------------------------------------------------------
  126.   def load_notetags_sani
  127.     @state_animation = 0
  128.     #---
  129.     self.note.split(/[\r\n]+/).each { |line|
  130.       case line
  131.       #---
  132.       when YEA::REGEXP::STATE::STATEANI
  133.         @state_animation = $1.to_i
  134.       end
  135.     } # self.note.split
  136.     #---
  137.   end
  138.  
  139. end # RPG::State
  140.  
  141. #==============================================================================
  142. # ■ Sprite_Battler
  143. #==============================================================================
  144.  
  145. class Sprite_Battler < Sprite_Base
  146.  
  147.   #--------------------------------------------------------------------------
  148.   # class variables
  149.   #--------------------------------------------------------------------------
  150.   @@state_ani_checker = []
  151.   @@state_ani_spr_checker = []
  152.   @@_reference_count = {}
  153.  
  154.   #--------------------------------------------------------------------------
  155.   # alias method: initialize
  156.   #--------------------------------------------------------------------------
  157.   alias sprite_battler_initialize_sani initialize
  158.   def initialize(viewport, battler = nil)
  159.     sprite_battler_initialize_sani(viewport, battler)
  160.     @state_ani_duration = 0
  161.   end
  162.  
  163.   #--------------------------------------------------------------------------
  164.   # alias method: dispose
  165.   #--------------------------------------------------------------------------
  166.   alias sprite_battler_dispose_sani dispose
  167.   def dispose
  168.     dispose_state_animation
  169.     sprite_battler_dispose_sani
  170.   end
  171.  
  172.   #--------------------------------------------------------------------------
  173.   # alias method: setup_new_effect
  174.   #--------------------------------------------------------------------------
  175.   alias sprite_battler_setup_new_effect_sani setup_new_effect
  176.   def setup_new_effect
  177.     sprite_battler_setup_new_effect_sani
  178.     setup_state_ani_effect
  179.   end
  180.  
  181.   #--------------------------------------------------------------------------
  182.   # new method: setup_state_ani_effect
  183.   #--------------------------------------------------------------------------
  184.   def setup_state_ani_effect
  185.     return if @battler.state_animation_id.nil?
  186.     if @battler.state_animation_id == 0
  187.       dispose_state_animation
  188.     else
  189.       animation = $data_animations[@battler.state_animation_id]
  190.       start_state_animation(animation)
  191.     end
  192.   end
  193.  
  194.   #--------------------------------------------------------------------------
  195.   # alias method: update
  196.   #--------------------------------------------------------------------------
  197.   alias sprite_battler_update_sani update
  198.   def update
  199.     sprite_battler_update_sani
  200.     update_state_animations
  201.   end
  202.  
  203.   #--------------------------------------------------------------------------
  204.   # new method: update_state_animations
  205.   #--------------------------------------------------------------------------
  206.   def update_state_animations
  207.     update_state_animation
  208.     @@state_ani_checker.clear
  209.     @@state_ani_spr_checker.clear
  210.   end
  211.  
  212.   #--------------------------------------------------------------------------
  213.   # new method: state_animation?
  214.   #--------------------------------------------------------------------------
  215.   def state_animation?
  216.     return !@state_animation.nil?
  217.   end
  218.  
  219.   #--------------------------------------------------------------------------
  220.   # new method: start_state_animation
  221.   #--------------------------------------------------------------------------
  222.   def start_state_animation(animation, mirror = false)
  223.     return if !@state_animation.nil? && @state_animation.id == animation.id
  224.     dispose_state_animation
  225.     @state_animation = animation
  226.     return if @state_animation.nil?
  227.     @state_ani_mirror = mirror
  228.     set_animation_rate
  229.     @state_ani_duration = @state_animation.frame_max * @ani_rate + 1
  230.     load_state_animation_bitmap
  231.     make_state_animation_sprites
  232.     set_state_animation_origin
  233.   end
  234.  
  235.   #--------------------------------------------------------------------------
  236.   # new method: load_state_animation_bitmap
  237.   #--------------------------------------------------------------------------
  238.   def load_state_animation_bitmap
  239.     animation1_name = @state_animation.animation1_name
  240.     animation1_hue = @state_animation.animation1_hue
  241.     animation2_name = @state_animation.animation2_name
  242.     animation2_hue = @state_animation.animation2_hue
  243.     @state_ani_bitmap1 = Cache.animation(animation1_name, animation1_hue)
  244.     @state_ani_bitmap2 = Cache.animation(animation2_name, animation2_hue)
  245.     if @@_reference_count.include?(@state_ani_bitmap1)
  246.       @@_reference_count[@state_ani_bitmap1] += 1
  247.     else
  248.       @@_reference_count[@state_ani_bitmap1] = 1
  249.     end
  250.     if @@_reference_count.include?(@ani_bitmap2)
  251.       @@_reference_count[@state_ani_bitmap2] += 1
  252.     else
  253.       @@_reference_count[@state_ani_bitmap2] = 1
  254.     end
  255.     Graphics.frame_reset
  256.   end
  257.  
  258.   #--------------------------------------------------------------------------
  259.   # new method: make_state_animation_sprites
  260.   #--------------------------------------------------------------------------
  261.   def make_state_animation_sprites
  262.     @state_ani_sprites = []
  263.     if @use_sprite && !@@state_ani_spr_checker.include?(@state_animation)
  264.       16.times do
  265.         sprite = ::Sprite.new(viewport)
  266.         sprite.visible = false
  267.         @state_ani_sprites.push(sprite)
  268.       end
  269.       if @state_animation.position == 3
  270.         @@state_ani_spr_checker.push(@animation)
  271.       end
  272.     end
  273.     @state_ani_duplicated = @@state_ani_checker.include?(@state_animation)
  274.     if !@state_ani_duplicated && @state_animation.position == 3
  275.       @@state_ani_checker.push(@state_animation)
  276.     end
  277.   end
  278.  
  279.   #--------------------------------------------------------------------------
  280.   # new method: set_state_animation_origin
  281.   #--------------------------------------------------------------------------
  282.   def set_state_animation_origin
  283.     if @state_animation.position == 3
  284.       if viewport == nil
  285.         @state_ani_ox = Graphics.width / 2
  286.         @state_ani_oy = Graphics.height / 2
  287.       else
  288.         @state_ani_ox = viewport.rect.width / 2
  289.         @state_ani_oy = viewport.rect.height / 2
  290.       end
  291.     else
  292.       @state_ani_ox = x - ox + width / 2
  293.       @state_ani_oy = y - oy + height / 2
  294.       if @state_animation.position == 0
  295.         @state_ani_oy -= height / 2
  296.       elsif @state_animation.position == 2
  297.         @state_ani_oy += height / 2
  298.       end
  299.     end
  300.   end
  301.  
  302.   #--------------------------------------------------------------------------
  303.   # new method: dispose_state_animation
  304.   #--------------------------------------------------------------------------
  305.   def dispose_state_animation
  306.     if @state_ani_bitmap1
  307.       @@_reference_count[@state_ani_bitmap1] -= 1
  308.       if @@_reference_count[@state_ani_bitmap1] == 0
  309.         @state_ani_bitmap1.dispose
  310.       end
  311.     end
  312.     if @state_ani_bitmap2
  313.       @@_reference_count[@state_ani_bitmap2] -= 1
  314.       if @@_reference_count[@state_ani_bitmap2] == 0
  315.         @state_ani_bitmap2.dispose
  316.       end
  317.     end
  318.     if @state_ani_sprites
  319.       @state_ani_sprites.each {|sprite| sprite.dispose }
  320.       @state_ani_sprites = nil
  321.       @state_animation = nil
  322.     end
  323.     @state_ani_bitmap1 = nil
  324.     @state_ani_bitmap2 = nil
  325.   end
  326.  
  327.   #--------------------------------------------------------------------------
  328.   # new method: update_state_animation
  329.   #--------------------------------------------------------------------------
  330.   def update_state_animation
  331.     return unless state_animation?
  332.     @state_ani_duration -= 1
  333.     if @state_ani_duration % @ani_rate == 0
  334.       if @state_ani_duration > 0
  335.         @state_frame_index = @state_animation.frame_max
  336.         change = (@state_ani_duration + @ani_rate - 1) / @ani_rate
  337.         @state_frame_index -= change
  338.         @state_animation.timings.each do |timing|
  339.           next unless timing.frame == @state_frame_index
  340.           state_animation_process_timing(timing)
  341.         end
  342.       else
  343.         @state_ani_duration = @state_animation.frame_max * @ani_rate + 1
  344.       end
  345.     end
  346.     return if @state_frame_index.nil?
  347.     state_animation_set_sprites(@state_animation.frames[@state_frame_index])
  348.     set_state_animation_origin
  349.   end
  350.  
  351.   #--------------------------------------------------------------------------
  352.   # new method: end_state_animation
  353.   #--------------------------------------------------------------------------
  354.   def end_state_animation
  355.     dispose_state_animation
  356.   end
  357.  
  358.   #--------------------------------------------------------------------------
  359.   # new method: state_animation_set_sprites
  360.   #--------------------------------------------------------------------------
  361.   def state_animation_set_sprites(frame)
  362.     return if @state_animation.nil?
  363.     return if frame.nil?
  364.     cell_data = frame.cell_data
  365.     @state_ani_sprites.each_with_index do |sprite, i|
  366.       next unless sprite
  367.       pattern = cell_data[i, 0]
  368.       if !pattern || pattern < 0
  369.         sprite.visible = false
  370.         next
  371.       end
  372.       sprite.bitmap = pattern < 100 ? @state_ani_bitmap1 : @state_ani_bitmap2
  373.       sprite.visible = true
  374.       sprite.src_rect.set(pattern % 5 * 192,
  375.         pattern % 100 / 5 * 192, 192, 192)
  376.       if @state_ani_mirror
  377.         sprite.x = @state_ani_ox - cell_data[i, 1]
  378.         sprite.y = @state_ani_oy + cell_data[i, 2]
  379.         sprite.angle = (360 - cell_data[i, 4])
  380.         sprite.mirror = (cell_data[i, 5] == 0)
  381.       else
  382.         sprite.x = @state_ani_ox + cell_data[i, 1]
  383.         sprite.y = @state_ani_oy + cell_data[i, 2]
  384.         sprite.angle = cell_data[i, 4]
  385.         sprite.mirror = (cell_data[i, 5] == 1)
  386.       end
  387.       sprite.z = self.z + 250 + i
  388.       sprite.ox = 96
  389.       sprite.oy = 96
  390.       sprite.zoom_x = cell_data[i, 3] / 100.0
  391.       sprite.zoom_y = cell_data[i, 3] / 100.0
  392.       if @battler.actor?
  393.         zoom = YEA::STATE_ANIMATION::ACTOR_ZOOM
  394.         sprite.zoom_x *= zoom
  395.         sprite.zoom_y *= zoom
  396.       end
  397.       sprite.opacity = cell_data[i, 6] * self.opacity / 255.0
  398.       sprite.blend_type = cell_data[i, 7]
  399.     end
  400.   end
  401.  
  402.   #--------------------------------------------------------------------------
  403.   # new method: state_animation_process_timing
  404.   #--------------------------------------------------------------------------
  405.   def state_animation_process_timing(timing)
  406.     timing.se.play if YEA::STATE_ANIMATION::PLAY_SOUND
  407.     case timing.flash_scope
  408.     when 1
  409.       self.flash(timing.flash_color, timing.flash_duration * @ani_rate)
  410.     when 2
  411.       return unless YEA::STATE_ANIMATION::PLAY_FLASH
  412.       if viewport && !@state_ani_duplicated
  413.         flash_amount = timing.flash_duration * @ani_rate
  414.         viewport.flash(timing.flash_color, flash_amount)
  415.       end
  416.     when 3
  417.       self.flash(nil, timing.flash_duration * @ani_rate)
  418.     end
  419.   end
  420.  
  421. end # Sprite_Battler
  422.  
  423. #==============================================================================
  424. # ■ Game_BattlerBase
  425. #==============================================================================
  426.  
  427. class Game_BattlerBase
  428.  
  429.   #--------------------------------------------------------------------------
  430.   # public instance variables
  431.   #--------------------------------------------------------------------------
  432.   attr_accessor :state_animation_id
  433.  
  434.   #--------------------------------------------------------------------------
  435.   # alias method: refresh
  436.   #--------------------------------------------------------------------------
  437.   alias game_battlerbase_refresh_sani refresh
  438.   def refresh
  439.     game_battlerbase_refresh_sani
  440.     reload_state_animation
  441.   end
  442.  
  443.   #--------------------------------------------------------------------------
  444.   # new method: reload_state_animation
  445.   #--------------------------------------------------------------------------
  446.   def reload_state_animation
  447.     @state_animation_id = 0
  448.     return if actor? && !YEA::STATE_ANIMATION::PLAY_ACTOR
  449.     for state in states
  450.       next unless state.state_animation > 0
  451.       @state_animation_id = state.state_animation
  452.       break
  453.     end
  454.   end
  455.  
  456. end # Game_BattlerBase
  457.  
  458. #==============================================================================
  459. #
  460. # ▼ End of File
  461. #
  462. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement