TheoAllen

Animation Over Pictures

Oct 27th, 2014
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.16 KB | None | 0 0
  1. class Game_Interpreter
  2.  
  3.   def anim_pic(pic_num, anim_id, mirror = false)
  4.     if $game_party.in_battle
  5.       pic = $game_troop.screen.pictures[pic_num]
  6.     else
  7.       pic = $game_map.screen.pictures[pic_num]
  8.     end
  9.     pic.anim_id = anim_id
  10.     pic.anim_mirror = mirror
  11.   end
  12.  
  13. end
  14.  
  15. class Game_Picture
  16.   attr_accessor :anim_id
  17.   attr_accessor :anim_mirror
  18.  
  19.   alias theo_animpic_init initialize
  20.   def initialize(number)
  21.     theo_animpic_init(number)
  22.     @anim_id = 0
  23.     @anim_mirror = false
  24.   end
  25.  
  26. end
  27.  
  28. class Sprite_Picture
  29.  
  30.   @@ani_checker = []
  31.   @@ani_spr_checker = []
  32.   @@_reference_count = {}
  33.  
  34.   alias theo_animpic_init initialize
  35.   def initialize(viewport, picture)
  36.     @use_sprite = true        
  37.     theo_animpic_init(viewport, picture)
  38.   end
  39.  
  40.   alias theo_animpic_dispose dispose
  41.   def dispose
  42.     theo_animpic_dispose
  43.     dispose_animation
  44.   end
  45.  
  46.   alias theo_animpic_update update
  47.   def update
  48.     theo_animpic_update
  49.     update_animation
  50.     @@ani_checker.clear
  51.     @@ani_spr_checker.clear
  52.     update_anim_flag
  53.   end
  54.  
  55.   def update_anim_flag
  56.     if @picture.anim_id > 0
  57.       start_animation($data_animations[@picture.anim_id], @picture.anim_mirror)
  58.       @picture.anim_id = 0
  59.     end
  60.   end
  61.  
  62.   def animation?
  63.     @animation != nil
  64.   end
  65.  
  66.   def start_animation(animation, mirror = false)
  67.     dispose_animation
  68.     @animation = animation
  69.     if @animation
  70.       @ani_mirror = mirror
  71.       set_animation_rate
  72.       @ani_duration = @animation.frame_max * @ani_rate + 1
  73.       load_animation_bitmap
  74.       make_animation_sprites
  75.       set_animation_origin
  76.     end
  77.   end
  78.  
  79.   def set_animation_rate
  80.     @ani_rate = 4     # Fixed value by default
  81.     @ani_rate = YEA::CORE::ANIMATION_RATE if $imported["YEA-CoreEngine"]
  82.   end
  83.  
  84.   def load_animation_bitmap
  85.     animation1_name = @animation.animation1_name
  86.     animation1_hue = @animation.animation1_hue
  87.     animation2_name = @animation.animation2_name
  88.     animation2_hue = @animation.animation2_hue
  89.     @ani_bitmap1 = Cache.animation(animation1_name, animation1_hue)
  90.     @ani_bitmap2 = Cache.animation(animation2_name, animation2_hue)
  91.     if @@_reference_count.include?(@ani_bitmap1)
  92.       @@_reference_count[@ani_bitmap1] += 1
  93.     else
  94.       @@_reference_count[@ani_bitmap1] = 1
  95.     end
  96.     if @@_reference_count.include?(@ani_bitmap2)
  97.       @@_reference_count[@ani_bitmap2] += 1
  98.     else
  99.       @@_reference_count[@ani_bitmap2] = 1
  100.     end
  101.     Graphics.frame_reset
  102.   end
  103.  
  104.   def make_animation_sprites
  105.     @ani_sprites = []
  106.     if @use_sprite && !@@ani_spr_checker.include?(@animation)
  107.       16.times do
  108.         sprite = ::Sprite.new(viewport)
  109.         sprite.visible = false
  110.         @ani_sprites.push(sprite)
  111.       end
  112.       if @animation.position == 3
  113.         @@ani_spr_checker.push(@animation)
  114.       end
  115.     end
  116.     @ani_duplicated = @@ani_checker.include?(@animation)
  117.     if !@ani_duplicated && @animation.position == 3
  118.       @@ani_checker.push(@animation)
  119.     end
  120.   end
  121.  
  122.   def set_animation_origin
  123.     if @animation.position == 3
  124.       if viewport == nil
  125.         @ani_ox = Graphics.width / 2
  126.         @ani_oy = Graphics.height / 2
  127.       else
  128.         @ani_ox = viewport.rect.width / 2
  129.         @ani_oy = viewport.rect.height / 2
  130.       end
  131.     else
  132.       @ani_ox = x - ox + width / 2
  133.       @ani_oy = y - oy + height / 2
  134.       if @animation.position == 0
  135.         @ani_oy -= height / 2
  136.       elsif @animation.position == 2
  137.         @ani_oy += height / 2
  138.       end
  139.     end
  140.   end
  141.  
  142.   def dispose_animation
  143.     if @ani_bitmap1
  144.       @@_reference_count[@ani_bitmap1] -= 1
  145.       if @@_reference_count[@ani_bitmap1] == 0
  146.         @ani_bitmap1.dispose
  147.       end
  148.     end
  149.     if @ani_bitmap2
  150.       @@_reference_count[@ani_bitmap2] -= 1
  151.       if @@_reference_count[@ani_bitmap2] == 0
  152.         @ani_bitmap2.dispose
  153.       end
  154.     end
  155.     if @ani_sprites
  156.       @ani_sprites.each {|sprite| sprite.dispose }
  157.       @ani_sprites = nil
  158.       @animation = nil
  159.     end
  160.     @ani_bitmap1 = nil
  161.     @ani_bitmap2 = nil
  162.   end
  163.  
  164.   def update_animation
  165.     return unless animation?
  166.     @ani_duration -= 1
  167.     if @ani_duration % @ani_rate == 0
  168.       if @ani_duration > 0
  169.         frame_index = @animation.frame_max
  170.         frame_index -= (@ani_duration + @ani_rate - 1) / @ani_rate
  171.         animation_set_sprites(@animation.frames[frame_index])
  172.         @animation.timings.each do |timing|
  173.           animation_process_timing(timing) if timing.frame == frame_index
  174.         end
  175.       else
  176.         end_animation
  177.       end
  178.     end
  179.   end
  180.  
  181.   def end_animation
  182.     dispose_animation
  183.   end
  184.  
  185.   def animation_set_sprites(frame)
  186.     cell_data = frame.cell_data
  187.     @ani_sprites.each_with_index do |sprite, i|
  188.       next unless sprite
  189.       pattern = cell_data[i, 0]
  190.       if !pattern || pattern < 0
  191.         sprite.visible = false
  192.         next
  193.       end
  194.       sprite.bitmap = pattern < 100 ? @ani_bitmap1 : @ani_bitmap2
  195.       sprite.visible = true
  196.       sprite.src_rect.set(pattern % 5 * 192,
  197.         pattern % 100 / 5 * 192, 192, 192)
  198.       if @ani_mirror
  199.         sprite.x = @ani_ox - cell_data[i, 1]
  200.         sprite.y = @ani_oy + cell_data[i, 2]
  201.         sprite.angle = (360 - cell_data[i, 4])
  202.         sprite.mirror = (cell_data[i, 5] == 0)
  203.       else
  204.         sprite.x = @ani_ox + cell_data[i, 1]
  205.         sprite.y = @ani_oy + cell_data[i, 2]
  206.         sprite.angle = cell_data[i, 4]
  207.         sprite.mirror = (cell_data[i, 5] == 1)
  208.       end
  209.       sprite.z = self.z + 300 + i
  210.       sprite.ox = 96
  211.       sprite.oy = 96
  212.       sprite.zoom_x = cell_data[i, 3] / 100.0
  213.       sprite.zoom_y = cell_data[i, 3] / 100.0
  214.       sprite.opacity = cell_data[i, 6] * self.opacity / 255.0
  215.       sprite.blend_type = cell_data[i, 7]
  216.     end
  217.   end
  218.  
  219.   def animation_process_timing(timing)
  220.     timing.se.play unless @ani_duplicated
  221.     case timing.flash_scope
  222.     when 1
  223.       self.flash(timing.flash_color, timing.flash_duration * @ani_rate)
  224.     when 2
  225.       if viewport && !@ani_duplicated
  226.         viewport.flash(timing.flash_color, timing.flash_duration * @ani_rate)
  227.       end
  228.     when 3
  229.       self.flash(nil, timing.flash_duration * @ani_rate)
  230.     end
  231.   end
  232. end
Advertisement
Add Comment
Please, Sign In to add comment