khanhdu

Victor Engine - Loop Animation

Nov 9th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 40.51 KB | None | 0 0
  1. #==============================================================================
  2. # ** Victor Engine - Loop Animation
  3. #------------------------------------------------------------------------------
  4. # Author : Victor Sant
  5. #
  6. # Version History:
  7. #  v 1.00 - 2012.01.01 > First relase
  8. #  v 1.01 - 2012.01.07 > Fixed Bug with Comment Call
  9. #                      > Reordered the Regular Expression to fix a mismatch
  10. #  v 1.02 - 2012.01.15 > Compatibility with Target Arrow
  11. #  v 1.03 - 2012.03.11 > Compatibility with Animation Setting
  12. #  v 1.04 - 2013.02.13 > Compatibility with Basic Module 1.35
  13. #------------------------------------------------------------------------------
  14. #  This script allows to display looping and cycling animations, the
  15. # animation loops indefinitely, and if there's more than one they cycles
  16. # between them. They're used to display status effects or can be called to
  17. # display some visual effect.
  18. #------------------------------------------------------------------------------
  19. # Compatibility
  20. #   Requires the script 'Victor Engine - Basic Module' v 1.35 or higher
  21. #
  22. # * Alias methods (Default)
  23. #   class Game_Battler < Game_BattlerBase
  24. #     def initialize
  25. #
  26. #   class Game_Enemy < Game_Battler
  27. #     def initialize
  28. #
  29. #   class Game_CharacterBase
  30. #     def initialize
  31. #
  32. #   class Sprite_Base < Sprite
  33. #     def initialize(viewport = nil)
  34. #     def dispose
  35. #     def update
  36. #
  37. #   class Sprite_Character < Sprite_Base
  38. #     def initialize(viewport, character = nil)
  39. #     def setup_new_effect
  40. #
  41. #   class Sprite_Battler < Sprite_Base
  42. #     def initialize(viewport, battler = nil)
  43. #     def setup_new_effect
  44. #
  45. # * Alias methods (Basic Module)
  46. #   class Game_Interpreter
  47. #     def comment_call
  48. #
  49. #------------------------------------------------------------------------------
  50. # Instructions:
  51. #  To instal the script, open you script editor and paste this script on
  52. #  a new section bellow the Materials section. This script must also
  53. #  be bellow the scripts 'Victor Engine - Basic'
  54. #
  55. #------------------------------------------------------------------------------
  56. # Comment calls note tags:
  57. #  Tags to be used in events comment box, works like a script call.
  58. #
  59. #  <event loop animation i: x>
  60. #   Display a loop animation on the event.
  61. #     i : event ID
  62. #     x : loop animation ID
  63. #
  64. #  <event loop animation type i: x, y>
  65. #   Set the loop animation type for events, have effect only when used together
  66. #   with <event loop animation i: x>, otherwise have no effect, this tag is
  67. #   opitional
  68. #     i : event ID
  69. #     x : loop animation ID
  70. #     y : loop animation type
  71. #
  72. #  <event loop animation loop i: x, y>
  73. #   Set the loop animation loop numbers for events, have effect only when used
  74. #   together with <event loop animation i: x>, otherwise have no effect,
  75. #   this tag is opitional
  76. #     i : event ID
  77. #     x : loop animation ID
  78. #     y : loop animation loop times
  79. #
  80. #  <actor loop animation i: x>
  81. #   Display a loop animation on the actors, can be used in battles.
  82. #     i : actor index
  83. #     x : loop animation ID
  84. #
  85. #  <actor loop animation type i: x, y>
  86. #   Set the loop animation type for actors, have effect only when used together
  87. #   with <actor loop animation i: x>, otherwise have no effect, this tag
  88. #  is opitional
  89. #     i : actor index
  90. #     x : loop animation ID
  91. #     y : loop animation type
  92. #
  93. #  <actor loop animation loop i: x, y>
  94. #   Set the loop animation loop numbers for actors, have effect only when used
  95. #   together with <actor loop animation i: x>, otherwise have no effect,
  96. #   this tag is opitional
  97. #     i : actor index
  98. #     x : loop animation ID
  99. #     y : loop animation loop times
  100. #
  101. #  <vehicle loop animation i: x>
  102. #   Display a loop animation on the vehicles, can be used in battles.
  103. #   this tag is opitional
  104. #     i : vehicle type name
  105. #     x : loop animation ID
  106. #
  107. #  <vehicle loop animation type i: x, y>
  108. #   Set the loop animation type for vehicles, have effect only when used
  109. #   together with <vehicle loop animation i: x>, otherwise have no effect,
  110. #   this tag is opitional
  111. #     i : vehicle type name
  112. #     x : loop animation ID
  113. #     y : loop animation type
  114. #
  115. #  <vehicle loop animation loop i: x, y>
  116. #   Set the loop animation loop numbers for vehicles, have effect only when
  117. #   used together with <vehicle loop animation i: x>, otherwise have no
  118. #   effect, this tag is opitional
  119. #     i : vehicle type name
  120. #     x : loop animation ID
  121. #     y : loop animation loop times
  122. #
  123. #------------------------------------------------------------------------------
  124. # Comment boxes note tags:
  125. #   Tags to be used on events Comment boxes. They're different from the
  126. #   comment call, they're called always the even refresh.
  127. #
  128. #  <loop animation: i>
  129. #   Display a loop animation on the event.
  130. #     i : loop animation ID
  131. #
  132. #  <loop animation type: x, y>
  133. #   Set the loop animation type, have effect only when used together with
  134. #   <self loop animation id: x>, otherwise have no effect, this tag is opitional
  135. #     x : loop animation ID
  136. #     y : loop animation type
  137. #
  138. #  <loop animation loop: x, y>
  139. #   Set the loop animation loop numbers, have effect only when used
  140. #   together with <self loop animation id: x>, otherwise have no effect,
  141. #   this tag is opitional
  142. #     x : loop animation ID
  143. #     y : loop animation loop times
  144. #
  145. #------------------------------------------------------------------------------
  146. # Enemies note tags:
  147. #   Tags to be used on the Enemies note box in the database
  148. #
  149. #  <loop animation: i>
  150. #   Display a loop animation on the enemy.
  151. #     i : loop animation ID
  152. #
  153. #  <loop animation type: x, y>
  154. #   Set the loop animation type, have effect only when used together with
  155. #   <loop animation id: x>, otherwise have no effect, this tag is opitional
  156. #     x : loop animation ID
  157. #     y : loop animation type
  158. #
  159. #  <loop animation loop: x, y>
  160. #   Set the loop animation loop numbers, have effect only when used together
  161. #   with <loop animation id: x>, otherwise have no effect, this tag is opitional
  162. #     x : loop animation ID
  163. #     y : loop animation loop times
  164. #
  165. #------------------------------------------------------------------------------
  166. # States note tags:
  167. #   Tags to be used on the States note box in the database
  168. #
  169. #  <loop animation: i>
  170. #   Display a loop animation on the target of the state during battles
  171. #     i : loop animation ID
  172. #
  173. #  <loop animation type: x, y>
  174. #   Set the loop animation type, have effect only when used together with
  175. #   <loop animation: x>, otherwise have no effect, this tag is opitional
  176. #     x : loop animation ID
  177. #     y : loop animation type
  178. #
  179. #  <loop animation loop: x, y>
  180. #   Set the loop animation loop numbers, have effect only when used together
  181. #   with <loop animation: x>, otherwise have no effect, this tag is opitional
  182. #     x : loop animation ID
  183. #     y : loop animation loop times
  184. #
  185. #  <map loop animation: i>
  186. #   Display a loop animation on the target of the state on the map
  187. #     i : loop animation ID
  188. #
  189. #  <map loop animation type: x, y>
  190. #   Set the loop animation type, have effect only when used together with
  191. #   <map loop animation: x>, otherwise have no effect, this tag is opitional
  192. #     x : loop animation ID
  193. #     y : loop animation type
  194. #
  195. #  <map loop animation loop: x, y>
  196. #   Set the loop animation loop numbers, have effect only when used together
  197. #   with <map loop animation: x>, otherwise have no effect, this tag is
  198. #   opitional
  199. #     x : loop animation ID
  200. #     y : loop animation loop times
  201. #
  202. #------------------------------------------------------------------------------
  203. # Additional instructions:
  204. #
  205. #  The animation type is a identifier to decide the animation cycle. Loop
  206. #  animations only cycle thought animations of the same type. If two or more
  207. #  animations from different types are displayed on the same target.
  208. #  all animations from the different types will be displayed at same time.
  209. #  Avoid overdoing with this because it may cause FPS drop.
  210. #
  211. #==============================================================================
  212.  
  213. #==============================================================================
  214. # ** Victor Engine
  215. #------------------------------------------------------------------------------
  216. #   Setting module for the Victor Engine
  217. #==============================================================================
  218.  
  219. module Victor_Engine
  220.   #--------------------------------------------------------------------------
  221.   # * required
  222.   #   This method checks for the existance of the basic module and other
  223.   #   VE scripts required for this script to work, don't edit this
  224.   #--------------------------------------------------------------------------
  225.   def self.required(name, req, version, type = nil)
  226.     if !$imported[:ve_basic_module]
  227.       msg = "The script '%s' requires the script\n"
  228.       msg += "'VE - Basic Module' v%s or higher above it to work properly\n"
  229.       msg += "Go to http://victorenginescripts.wordpress.com/ to download this script."
  230.       msgbox(sprintf(msg, self.script_name(name), version))
  231.       exit
  232.     else
  233.       self.required_script(name, req, version, type)
  234.     end
  235.   end
  236.   #--------------------------------------------------------------------------
  237.   # * script_name
  238.   #   Get the script name base on the imported value, don't edit this
  239.   #--------------------------------------------------------------------------
  240.   def self.script_name(name, ext = "VE")
  241.     name = name.to_s.gsub("_", " ").upcase.split
  242.     name.collect! {|char| char == ext ? "#{char} -" : char.capitalize }
  243.     name.join(" ")
  244.   end
  245. end
  246.  
  247. $imported ||= {}
  248. $imported[:ve_loop_animation] = 1.04
  249. Victor_Engine.required(:ve_loop_animation, :ve_basic_module, 1.35, :above)
  250.  
  251. #==============================================================================
  252. # ** Game_Battler
  253. #------------------------------------------------------------------------------
  254. #  This class deals with battlers. It's used as a superclass of the Game_Actor
  255. # and Game_Enemy classes.
  256. #==============================================================================
  257.  
  258. class Game_Battler < Game_BattlerBase
  259.   #--------------------------------------------------------------------------
  260.   # * Public Instance Variables
  261.   #--------------------------------------------------------------------------
  262.   attr_reader   :add_animation
  263.   attr_reader   :remove_animation
  264.   #--------------------------------------------------------------------------
  265.   # * Alias method: initialize
  266.   #--------------------------------------------------------------------------
  267.   alias :initialize_ve_loop_animation :initialize
  268.   def initialize
  269.     initialize_ve_loop_animation
  270.     clear_add_loop_animation
  271.   end
  272.   #--------------------------------------------------------------------------
  273.   # * New method: add_loop_animation
  274.   #--------------------------------------------------------------------------
  275.   def add_loop_animation(anim, type, loop = 1)
  276.     @add_animation = {anim: anim, type: type, loop: loop}
  277.   end
  278.   #--------------------------------------------------------------------------
  279.   # * New method: remove_loop_animation
  280.   #--------------------------------------------------------------------------
  281.   def remove_loop_animation(anim, type)
  282.     @remove_animation = {anim: anim, type: type}
  283.   end
  284.   #--------------------------------------------------------------------------
  285.   # * New method: clear_add_loop_animation
  286.   #--------------------------------------------------------------------------
  287.   def clear_add_loop_animation
  288.     @add_animation = nil
  289.   end
  290.   #--------------------------------------------------------------------------
  291.   # * New method: clear_remove_loop_animation
  292.   #--------------------------------------------------------------------------
  293.   def clear_remove_loop_animation
  294.     @remove_animation = nil
  295.   end
  296. end
  297.  
  298.  
  299. #==============================================================================
  300. # ** Game_Enemy
  301. #------------------------------------------------------------------------------
  302. #  This class handles enemy characters. It's used within the Game_Troop class
  303. # ($game_troop).
  304. #==============================================================================
  305.  
  306. class Game_Enemy < Game_Battler
  307.   #--------------------------------------------------------------------------
  308.   # * Alias method: initialize
  309.   #--------------------------------------------------------------------------
  310.   alias :initialize_ge_ve_loop_animation :initialize
  311.   def initialize(index, enemy_id)
  312.     initialize_ge_ve_loop_animation(index, enemy_id)
  313.     refresh_loop_animations
  314.   end
  315.   #--------------------------------------------------------------------------
  316.   # * New method: refresh_loop_animations
  317.   #--------------------------------------------------------------------------
  318.   def refresh_loop_animations
  319.     note.scan(/<LOOP ANIMATION: (\d+)>/i) do
  320.       id   = $1
  321.       type = note =~ /<LOOP ANIMATION TYPE: #{id},\s*(\d+)>/i ? $1 : 0
  322.       loop = note =~ /<LOOP ANIMATION LOOP: #{id},\s*(\d+)>/i ? $1 : 1
  323.       add_loop_animation(id.to_i, type.to_i, loop.to_i)
  324.     end
  325.   end
  326. end
  327.  
  328. #==============================================================================
  329. # ** Game_CharacterBase
  330. #------------------------------------------------------------------------------
  331. #  This class deals with characters. Common to all characters, stores basic
  332. # data, such as coordinates and graphics. It's used as a superclass of the
  333. # Game_Character class.
  334. #==============================================================================
  335.  
  336. class Game_CharacterBase
  337.   #--------------------------------------------------------------------------
  338.   # * Public Instance Variables
  339.   #--------------------------------------------------------------------------
  340.   attr_reader   :add_animation
  341.   attr_reader   :remove_animation
  342.   #--------------------------------------------------------------------------
  343.   # * Alias method: initialize
  344.   #--------------------------------------------------------------------------
  345.   alias :initialize_ve_loop_animation :initialize
  346.   def initialize
  347.     initialize_ve_loop_animation
  348.     clear_add_loop_animation
  349.   end
  350.   #--------------------------------------------------------------------------
  351.   # * New method: add_loop_animation
  352.   #--------------------------------------------------------------------------
  353.   def add_loop_animation(anim, type, loop = 1)
  354.     @add_animation = {anim: anim, type: type, loop: loop}
  355.   end
  356.   #--------------------------------------------------------------------------
  357.   # * New method: remove_loop_animation
  358.   #--------------------------------------------------------------------------
  359.   def remove_loop_animation(anim, type)
  360.     @remove_animation = {anim: anim, type: type}
  361.   end
  362.   #--------------------------------------------------------------------------
  363.   # * New method: clear_add_loop_animation
  364.   #--------------------------------------------------------------------------
  365.   def clear_add_loop_animation
  366.     @add_animation = nil
  367.   end
  368.   #--------------------------------------------------------------------------
  369.   # * New method: clear_remove_loop_animation
  370.   #--------------------------------------------------------------------------
  371.   def clear_remove_loop_animation
  372.     @remove_animation = nil
  373.   end
  374. end
  375.  
  376. #==============================================================================
  377. # ** Game_Event
  378. #------------------------------------------------------------------------------
  379. #  This class deals with events. It handles functions including event page
  380. # switching via condition determinants, and running parallel process events.
  381. # It's used within the Game_Map class.
  382. #==============================================================================
  383.  
  384. class Game_Event < Game_Character
  385.   #--------------------------------------------------------------------------
  386.   # * Alias method: clear_starting_flag
  387.   #--------------------------------------------------------------------------
  388.   alias :clear_starting_flag_ve_loop_animation :clear_starting_flag
  389.   def clear_starting_flag
  390.     clear_starting_flag_ve_loop_animation
  391.     refresh_loop_animations if @page
  392.   end
  393.   #--------------------------------------------------------------------------
  394.   # * New method: refresh_loop_animations
  395.   #--------------------------------------------------------------------------
  396.   def refresh_loop_animations
  397.     note.scan(/<LOOP ANIMATION ID: (\d+)>/i) do
  398.       id   = $1
  399.       type = note =~ /<LOOP ANIMATION TYPE: #{id},\s*(\d+)>/i ? $1 : 0
  400.       loop = note =~ /<LOOP ANIMATION LOOP: #{id},\s*(\d+)>/i ? $1 : 1
  401.       add_loop_animation(id.to_i, type.to_i, loop.to_i)
  402.     end
  403.   end
  404. end
  405.  
  406. #==============================================================================
  407. # ** Game_Interpreter
  408. #------------------------------------------------------------------------------
  409. #  An interpreter for executing event commands. This class is used within the
  410. # Game_Map, Game_Troop, and Game_Event classes.
  411. #==============================================================================
  412.  
  413. class Game_Interpreter
  414.   #--------------------------------------------------------------------------
  415.   # * Alias method: comment_call
  416.   #--------------------------------------------------------------------------
  417.   alias :comment_call_ve_loop_animation :comment_call
  418.   def comment_call
  419.     call_event_loop_anim
  420.     call_actor_loop_anim
  421.     call_vehicle_loop_anim
  422.     call_enemy_loop_anim
  423.     comment_call_ve_loop_animation
  424.   end
  425.   #--------------------------------------------------------------------------
  426.   # * New method: call_event_loop_anim
  427.   #--------------------------------------------------------------------------
  428.   def call_event_loop_anim
  429.     note.scan(/<EVENT LOOP ANIMATION (\d+): (\d+)>/i) do |i, id|
  430.       event = $game_map.events[i.to_i]
  431.       regexp = "EVENT LOOP ANIMATION"
  432.       type   = note =~ /<#{regexp} TYPE #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  433.       loop   = note =~ /<#{regexp} LOOP #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  434.       event.add_loop_animation(id.to_i, type.to_i, loop.to_i)
  435.     end
  436.   end
  437.   #--------------------------------------------------------------------------
  438.   # * New method: call_actor_loop_anim
  439.   #--------------------------------------------------------------------------
  440.   def call_actor_loop_anim
  441.     note.scan(/<ACTOR LOOP ANIMATION (\d+): (\d+)>/i) do |i, id|
  442.       if $game_party.in_battle
  443.         actor = $game_party.battle_members[i.to_i]
  444.       else
  445.         actor = $game_map.actors[i.to_i]
  446.       end
  447.       regexp = "ACTOR LOOP ANIMATION"
  448.       type   = note =~ /<#{regexp} TYPE #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  449.       loop   = note =~ /<#{regexp} LOOP #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  450.       actor.add_loop_animation(id.to_i, type.to_i, loop.to_i)
  451.     end
  452.   end
  453.   #--------------------------------------------------------------------------
  454.   # * New method: call_vehicle_loop_anim
  455.   #--------------------------------------------------------------------------
  456.   def call_vehicle_loop_anim
  457.     note.scan(/<VEHICLE LOOP ANIMATION (\w+): (\d+)>/i) do |i, id|
  458.       vehicle = $game_map.vehicle(eval(":#{i}"))
  459.       regexp = "VEHICLE LOOP ANIMATION"
  460.       type   = note =~ /<#{regexp} TYPE #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  461.       loop   = note =~ /<#{regexp} LOOP #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  462.       vehicle.add_loop_animation(id.to_i, type.to_i, loop.to_i)
  463.     end
  464.   end
  465.   #--------------------------------------------------------------------------
  466.   # * New method: call_enemy_loop_anim
  467.   #--------------------------------------------------------------------------
  468.   def call_enemy_loop_anim
  469.     return if !$game_party.in_battle
  470.     note.scan(/<ENEMY LOOP ANIMATION (\d+): (\d+)>/i) do |i, id|
  471.       enemy = $game_troop.members[i.to_i]
  472.       regexp = "ENEMY LOOP ANIMATION"
  473.       type   = note =~ /<#{regexp} TYPE #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  474.       loop   = note =~ /<#{regexp} LOOP #{i}: #{id},\s*(\d+)>/i ? $1 : 1
  475.       enemy.add_loop_animation(id.to_i, type.to_i, loop.to_i)
  476.     end
  477.   end  
  478. end
  479.  
  480. #==============================================================================
  481. # ** Sprite_Base
  482. #------------------------------------------------------------------------------
  483. #  A sprite class with animation display processing added.
  484. #==============================================================================
  485.  
  486. class Sprite_Base < Sprite
  487.   #--------------------------------------------------------------------------
  488.   # * Public Instance Variables
  489.   #--------------------------------------------------------------------------
  490.   attr_accessor :loop_anim
  491.   #--------------------------------------------------------------------------
  492.   # * Class Variables
  493.   #--------------------------------------------------------------------------
  494.   @@loop_ani_checker = []
  495.   #--------------------------------------------------------------------------
  496.   # * Alias method: initialize
  497.   #--------------------------------------------------------------------------
  498.   alias :initialize_ve_loop_animation :initialize
  499.   def initialize(viewport = nil)
  500.     initialize_ve_loop_animation(viewport)
  501.     @loop_animation = {}
  502.     @loop_list = {}
  503.   end
  504.   #--------------------------------------------------------------------------
  505.   # * Alias method: dispose
  506.   #--------------------------------------------------------------------------
  507.   alias :dispose_ve_loop_animation :dispose
  508.   def dispose
  509.     dispose_ve_loop_animation
  510.     end_all_loop_anim
  511.   end
  512.   #--------------------------------------------------------------------------
  513.   # * Alias method: update
  514.   #--------------------------------------------------------------------------
  515.   alias :update_ve_loop_animation :update
  516.   def update
  517.     dispose_removed_loop_anim
  518.     update_ve_loop_animation
  519.     setup_loop_anim
  520.     update_all_loop_anim
  521.     end_all_loop_anim if any_loop_anim? && !sprite_visible
  522.   end
  523.   #--------------------------------------------------------------------------
  524.   # * New method: user
  525.   #--------------------------------------------------------------------------
  526.   def user
  527.     @battler ? @battler : @character
  528.   end
  529.   #--------------------------------------------------------------------------
  530.   # * New method: setup_loop_anim
  531.   #--------------------------------------------------------------------------
  532.   def setup_loop_anim
  533.     @loop_list.keys.each do |type|
  534.       if !loop_anim?(type) && @loop_list[type].size > 0
  535.         create_loop_anim(type)
  536.       elsif @loop_list[type].empty?
  537.         @loop_list.delete(type)
  538.       end
  539.     end
  540.   end
  541.   #--------------------------------------------------------------------------
  542.   # * New method: create_loop_anim
  543.   #--------------------------------------------------------------------------
  544.   def create_loop_anim(type)
  545.     return if !@loop_list[type]
  546.     item = @loop_list[type].next_item
  547.     anim = $data_animations[item.first]
  548.     start_loop_anim(anim, type)
  549.     @loop_animation[type].loop = item.last - 1
  550.   end
  551.   #--------------------------------------------------------------------------
  552.   # * New method: any_loop_anim?
  553.   #--------------------------------------------------------------------------
  554.   def any_loop_anim?
  555.     @loop_animation && @loop_animation.values.size > 0
  556.   end
  557.   #--------------------------------------------------------------------------
  558.   # * New method: loop_anim?
  559.   #--------------------------------------------------------------------------
  560.   def loop_anim?(type)
  561.     @loop_animation[type]
  562.   end
  563.   #--------------------------------------------------------------------------
  564.   # * New method: start_loop_anim
  565.   #--------------------------------------------------------------------------
  566.   def start_loop_anim(animation, type, mirror = false)
  567.     return if @loop_animation[type] && animation == @loop_animation[type].data
  568.     dispose_loop_anim(type) if @loop_animation[type]
  569.     @loop_animation[type] = Game_Animation.new(animation, mirror)
  570.     if @loop_animation[type]
  571.       load_loop_anim_bitmap(type)
  572.       make_loop_anim_sprites(type)
  573.       set_loop_anim_origin(type)
  574.     end
  575.   end
  576.   #--------------------------------------------------------------------------
  577.   # * New method: load_loop_anim_bitmap
  578.   #--------------------------------------------------------------------------
  579.   def load_loop_anim_bitmap(type)
  580.     animation  = @loop_animation[type]
  581.     anim1_name = animation.animation1_name
  582.     anim1_hue  = animation.animation1_hue
  583.     anim2_name = animation.animation2_name
  584.     anim2_hue  = animation.animation2_hue
  585.     animation.bitmap1 = Cache.animation(anim1_name, anim1_hue)
  586.     animation.bitmap2 = Cache.animation(anim2_name, anim2_hue)
  587.     if @@_reference_count.include?(animation.bitmap1)
  588.       @@_reference_count[animation.bitmap1] += 1
  589.     else
  590.       @@_reference_count[animation.bitmap1] = 1
  591.     end
  592.     if @@_reference_count.include?(animation.bitmap2)
  593.       @@_reference_count[animation.bitmap2] += 1
  594.     else
  595.       @@_reference_count[animation.bitmap2] = 1
  596.     end
  597.     Graphics.frame_reset
  598.   end
  599.   #--------------------------------------------------------------------------
  600.   # * New method: make_loop_anim_sprites
  601.   #--------------------------------------------------------------------------
  602.   def make_loop_anim_sprites(type)
  603.     animation = @loop_animation[type]
  604.     animation.duplicated = @@loop_ani_checker.include?(animation.data)
  605.     if @use_sprite && !(animation.duplicated  && animation.position == 3)
  606.       16.times do
  607.         sprite = ::Sprite.new(viewport)
  608.         sprite.visible = false
  609.         animation.sprites.push(sprite)
  610.       end
  611.     end
  612.     @@loop_ani_checker.push(animation.data) if !animation.duplicated
  613.   end
  614.   #--------------------------------------------------------------------------
  615.   # * New method: set_loop_anim_origin
  616.   #--------------------------------------------------------------------------
  617.   def set_loop_anim_origin(type)
  618.     animation = @loop_animation[type]
  619.     if animation.position == 3
  620.       update_animation_screen(animation)
  621.     else
  622.       update_animation_origin(animation)
  623.     end
  624.     animation.map_x = charset? ? $game_map.display_x : 0
  625.     animation.map_y = charset? ? $game_map.display_y : 0
  626.   end
  627.   #--------------------------------------------------------------------------
  628.   # * New method: update_animation_screen
  629.   #--------------------------------------------------------------------------
  630.   def update_animation_screen(animation)
  631.     if viewport == nil
  632.       animation.ox = Graphics.width  / 2
  633.       animation.oy = Graphics.height / 2
  634.     else
  635.       animation.ox = viewport.rect.width  / 2
  636.       animation.oy = viewport.rect.height / 2
  637.     end
  638.   end
  639.   #--------------------------------------------------------------------------
  640.   # * New method: update_animation_origin
  641.   #--------------------------------------------------------------------------
  642.   def update_animation_origin(animation)
  643.     animation.ox  = x - ox + width  / 2
  644.     animation.oy  = y - oy + height / 2
  645.     animation.oy -= height / 2 if animation.position == 0
  646.     animation.oy += height / 2 if animation.position == 2
  647.   end
  648.   #--------------------------------------------------------------------------
  649.   # * New method: dispose_loop_anim
  650.   #--------------------------------------------------------------------------
  651.   def dispose_loop_anim(type)
  652.     animation = @loop_animation[type]
  653.     if animation.bitmap1
  654.       @@_reference_count[animation.bitmap1] -= 1
  655.       animation.bitmap1.dispose if @@_reference_count[animation.bitmap1] == 0
  656.     end
  657.     if animation.bitmap2
  658.       @@_reference_count[animation.bitmap2] -= 1
  659.       animation.bitmap2.dispose if @@_reference_count[animation.bitmap2] == 0
  660.     end
  661.     animation.sprites.each {|sprite| sprite.dispose } if animation.sprites
  662.     @loop_animation.delete(type)
  663.   end
  664.   #--------------------------------------------------------------------------
  665.   # * New method: update_all_loop_anim
  666.   #--------------------------------------------------------------------------
  667.   def update_all_loop_anim
  668.     @loop_animation.keys.each {|type| update_loop_anim(type) }
  669.   end
  670.   #--------------------------------------------------------------------------
  671.   # * New method: update_loop_anim
  672.   #--------------------------------------------------------------------------
  673.   def update_loop_anim(type)
  674.     animation = @loop_animation[type]
  675.     animation.duration -= 1
  676.     update_animation_origin(animation)
  677.     if animation.duration > 0
  678.       update_loop_frames(animation)
  679.     elsif animation.duration <= 0 && animation.loop > 0
  680.       animation.loop -= 1
  681.       animation.duration = animation.frame_max * animation.rate + 1
  682.     elsif @loop_list.empty?
  683.       end_all_loop_anim
  684.     else
  685.       create_loop_anim(type)
  686.       animation          = @loop_animation[type]
  687.       animation.duration = animation.frame_max * animation.rate
  688.       update_loop_frames(animation)
  689.     end
  690.   end
  691.   #--------------------------------------------------------------------------
  692.   # * New method: update_loop_frames
  693.   #--------------------------------------------------------------------------
  694.   def update_loop_frames(animation)
  695.     frame = (animation.duration + animation.rate - 1) / animation.rate
  696.     index = animation.frame_max - frame
  697.     animation_set_sprites(animation, animation.frames[index])
  698.     @ani_duplicated = animation.duplicated
  699.     @ani_rate       = animation.rate
  700.     if animation.duration % animation.rate == 1
  701.       @@loop_ani_checker.delete(animation.data)
  702.       animation.timings.each do |timing|
  703.         animation_process_timing(timing) if timing.frame == index
  704.       end
  705.     end
  706.   end
  707.   #--------------------------------------------------------------------------
  708.   # * New method: dispose_removed_loop_anim
  709.   #--------------------------------------------------------------------------  
  710.   def dispose_removed_loop_anim
  711.     @loop_animation.keys.each do |type|
  712.       id = @loop_animation[type].id
  713.       if @loop_list[type] && !@loop_list[type].include?(id)
  714.         @loop_list[type].delete(id)
  715.       end
  716.     end
  717.   end
  718.   #--------------------------------------------------------------------------
  719.   # * New method: end_all_loop_anim
  720.   #--------------------------------------------------------------------------
  721.   def end_all_loop_anim
  722.     @loop_animation.keys.each {|type| dispose_loop_anim(type) }
  723.     @loop_list = {}
  724.   end
  725.   #--------------------------------------------------------------------------
  726.   # * New method: end_all_loop_anim
  727.   #--------------------------------------------------------------------------
  728.   def end_loop_anim(type)
  729.     return unless @loop_animation.keys.include?(type)
  730.     dispose_loop_anim(type)
  731.     @loop_list.delete(type)
  732.   end
  733.   #--------------------------------------------------------------------------
  734.   # * New method: loop_anim_set_sprites
  735.   #--------------------------------------------------------------------------
  736.   def loop_anim_set_sprites(animation, frame)
  737.     cell_data = frame.cell_data
  738.     animation.sprites.each_with_index do |sprite, i|
  739.       next unless sprite
  740.       pattern = cell_data[i, 0]
  741.       if !pattern || pattern < 0
  742.         sprite.visible = false
  743.         next
  744.       end
  745.       if animation.duration % animation.rate == 0
  746.         setup_loop_pattern(animation, pattern, sprite)
  747.       end
  748.       setup_loop_position(animation, cell_data, sprite, i)
  749.     end
  750.   end
  751.   #--------------------------------------------------------------------------
  752.   # * New method: setup_loop_pattern
  753.   #--------------------------------------------------------------------------
  754.   def setup_loop_pattern(animation, pattern, sprite)
  755.     sprite.bitmap  = pattern < 100 ? animation.bitmap1 : animation.bitmap2
  756.     sprite.visible = true
  757.     sprite.src_rect.set(pattern % 5 * 192, pattern % 100 / 5 * 192, 192, 192)
  758.   end
  759.   #--------------------------------------------------------------------------
  760.   # * New method: setup_loop_position
  761.   #--------------------------------------------------------------------------
  762.   def setup_loop_position(animation, cell_data, sprite, i)
  763.     if $imported[:ve_animation_setting]
  764.       setup_sprite_position(animation, cell_data, sprite, i)
  765.     else
  766.       if animation.mirror
  767.         sprite.x = animation.ox - cell_data[i, 1] * animation.zoom
  768.         sprite.y = animation.oy + cell_data[i, 2] * animation.zoom
  769.         sprite.angle  = 360 - cell_data[i, 4]
  770.         sprite.mirror = cell_data[i, 5] == 0
  771.       else
  772.         sprite.x = animation.ox + cell_data[i, 1] * animation.zoom
  773.         sprite.y = animation.oy + cell_data[i, 2] * animation.zoom
  774.         sprite.angle  = cell_data[i, 4]
  775.         sprite.mirror = cell_data[i, 5] == 1
  776.       end
  777.       sprite.z  = self.z + 300 + i
  778.       sprite.ox = 96
  779.       sprite.oy = 96
  780.       sprite.zoom_x     = cell_data[i, 3] / 100.0
  781.       sprite.zoom_y     = cell_data[i, 3] / 100.0
  782.       sprite.opacity    = cell_data[i, 6] * self.opacity / 255.0
  783.       sprite.blend_type = cell_data[i, 7]
  784.     end
  785.   end
  786.   #--------------------------------------------------------------------------
  787.   # * New method: add_loop_animation
  788.   #--------------------------------------------------------------------------
  789.   def add_loop_animation(animation)
  790.     if animation
  791.       type = animation[:type]
  792.       anim = animation[:anim]
  793.       loop = animation[:loop]
  794.       @loop_list[type] ||= []
  795.       @loop_list[type].push([anim, loop])
  796.       @loop_list[type].uniq!
  797.     end
  798.   end
  799.   #--------------------------------------------------------------------------
  800.   # * New method: remove_loop_animation
  801.   #--------------------------------------------------------------------------
  802.   def remove_loop_animation(animation)
  803.     if animation
  804.       type = animation[:type]
  805.       data = animation[:anim]
  806.       if @loop_list[type]
  807.         @loop_list[type].delete_if {|anim| anim.first == data }
  808.       end
  809.       @loop_list.delete(type) if @loop_list[type] && @loop_list[type].empty?
  810.     end
  811.   end
  812.   #--------------------------------------------------------------------------
  813.   # * New method: remove_state_animations
  814.   #--------------------------------------------------------------------------
  815.   def remove_state_animations(new_states, old_states, map)
  816.     regex = "#{map}\s*LOOP ANIMATION"
  817.     old_states.each do |state|
  818.       next if new_states.include?(state)
  819.       if state.note =~ /<#{regex}: (\d+)>/i
  820.         id   = $1.to_i
  821.         type = state.note =~ /<#{regex} TYPE: #{id},\s*(\d+)>/i ? $1.to_i : ""
  822.         anim = {anim: id, type: eval(":state#{type}")}
  823.         remove_loop_animation(anim)
  824.       end
  825.     end
  826.   end
  827.   #--------------------------------------------------------------------------
  828.   # * New method: add_state_animation
  829.   #--------------------------------------------------------------------------
  830.   def add_state_animation(new_states, old_states, map)
  831.     regex = "#{map}\s*LOOP ANIMATION"
  832.     new_states.each do |state|
  833.       next if old_states.include?(state)
  834.       if state.note =~ /<#{regex}: (\d+)>/i
  835.         id   = $1.to_i
  836.         type = state.note =~ /<#{regex} TYPE: #{id},\s*(\d+)>/i ? $1.to_i : ""
  837.         loop = state.note =~ /<#{regex} LOOP: #{id},\s*(\d+)>/i ? $1.to_i : 1
  838.         anim = {anim: id, type: eval(":state#{type}"), loop: loop}
  839.         add_loop_animation(anim)
  840.       end
  841.     end
  842.   end
  843.   #--------------------------------------------------------------------------
  844.   # * New method: sprite_visible
  845.   #--------------------------------------------------------------------------
  846.   def sprite_visible
  847.     self.visible && self.opacity > 0
  848.   end
  849. end
  850.  
  851. #==============================================================================
  852. # ** Sprite_Character
  853. #------------------------------------------------------------------------------
  854. #  This sprite is used to display characters. It observes a instance of the
  855. # Game_Character class and automatically changes sprite conditions.
  856. #==============================================================================
  857.  
  858. class Sprite_Character < Sprite_Base
  859.   #--------------------------------------------------------------------------
  860.   # * Alias method: initialize
  861.   #--------------------------------------------------------------------------
  862.   alias :initialize_sc_ve_loop_animation :initialize
  863.   def initialize(viewport, character = nil)
  864.     @actor_states = []
  865.     initialize_sc_ve_loop_animation(viewport, character)
  866.   end
  867.   #--------------------------------------------------------------------------
  868.   # * Alias method: setup_new_effect
  869.   #--------------------------------------------------------------------------
  870.   alias :setup_new_effect_ve_loop_animation :setup_new_effect
  871.   def setup_new_effect
  872.     setup_new_effect_ve_loop_animation
  873.     setup_new_loop_animation
  874.     setup_remove_loop_animation
  875.     setup_states_loop_animation
  876.   end
  877.   #--------------------------------------------------------------------------
  878.   # * New method: setup_new_loop_animation
  879.   #--------------------------------------------------------------------------
  880.   def setup_new_loop_animation
  881.     add_loop_animation(@character.add_animation)
  882.     @character.clear_add_loop_animation
  883.   end
  884.   #--------------------------------------------------------------------------
  885.   # * New method: setup_remove_loop_animation
  886.   #--------------------------------------------------------------------------
  887.   def setup_remove_loop_animation
  888.     remove_loop_animation(@character.remove_animation)
  889.     @character.clear_remove_loop_animation
  890.   end
  891.   #--------------------------------------------------------------------------
  892.   # * New method: setup_states_loop_animation
  893.   #--------------------------------------------------------------------------
  894.   def setup_states_loop_animation
  895.     return unless actor
  896.     if @actor_states != actor.states
  897.       remove_state_animations(actor.states, @actor_states, "MAP")
  898.       add_state_animation(actor.states, @actor_states, "MAP")
  899.       @actor_states = actor.states.dup
  900.     end
  901.   end
  902.   #--------------------------------------------------------------------------
  903.   # * New method: sprite_visible
  904.   #--------------------------------------------------------------------------
  905.   def sprite_visible
  906.     self.visible && (self.opacity > 0 || @effect_type == :blink)
  907.   end
  908. end
  909.  
  910. #==============================================================================
  911. # ** Sprite_Battler
  912. #------------------------------------------------------------------------------
  913. #  This sprite is used to display battlers. It observes a instance of the
  914. # Game_Battler class and automatically changes sprite conditions.
  915. #==============================================================================
  916.  
  917. class Sprite_Battler < Sprite_Base
  918.   #--------------------------------------------------------------------------
  919.   # * Alias method: initialize
  920.   #--------------------------------------------------------------------------
  921.   alias :initialize_sb_ve_loop_animation  :initialize
  922.   def initialize(viewport, battler = nil)
  923.     @battler_states = []
  924.     initialize_sb_ve_loop_animation(viewport, battler)
  925.   end
  926.   #--------------------------------------------------------------------------
  927.   # * Alias method: setup_new_effect
  928.   #--------------------------------------------------------------------------
  929.   alias :setup_new_effect_ve_loop_animation :setup_new_effect
  930.   def setup_new_effect
  931.     setup_new_effect_ve_loop_animation
  932.     setup_new_loop_animation
  933.     setup_remove_loop_animation
  934.     setup_states_loop_animation
  935.   end
  936.   #--------------------------------------------------------------------------
  937.   # * New method: setup_new_loop_animation
  938.   #--------------------------------------------------------------------------
  939.   def setup_new_loop_animation
  940.     add_loop_animation(@battler.add_animation)
  941.     @battler.clear_add_loop_animation
  942.   end
  943.   #--------------------------------------------------------------------------
  944.   # * New method: setup_remove_loop_animation
  945.   #--------------------------------------------------------------------------
  946.   def setup_remove_loop_animation
  947.     remove_loop_animation(@battler.remove_animation)
  948.     @battler.clear_remove_loop_animation
  949.   end
  950.   #--------------------------------------------------------------------------
  951.   # * New method: setup_states_loop_animation
  952.   #--------------------------------------------------------------------------
  953.   def setup_states_loop_animation
  954.     if @battler_states != @battler.states
  955.       remove_state_animations(@battler.states, @battler_states, "")
  956.       add_state_animation(@battler.states, @battler_states, "")
  957.       @battler_states = @battler.states.dup
  958.     end
  959.   end
  960.   #--------------------------------------------------------------------------
  961.   # * New method: sprite_visible
  962.   #--------------------------------------------------------------------------
  963.   def sprite_visible
  964.     @battler_visible && (self.opacity > 0 || @effect_type == :blink)
  965.   end
  966. end
Advertisement
Add Comment
Please, Sign In to add comment