molegato

Animated pictures

Oct 6th, 2013
2,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.97 KB | None | 0 0
  1. #==============================================================================
  2. # ANIMATED PICTURES
  3. # Author Molegato
  4. # Version 1.1
  5. #------------------------------------------------------------------------------
  6. # explanation
  7. #   This script allows you to use animated pictures as you'd use normal ones
  8. #   with the 'show picture' event block. For that you only need to set up
  9. #   an animation here on the config module, and then use an image named as
  10. #   the animation. That image doesn't need to be contained within the animation
  11. #   per se, so you can use a dummy one if you wish.
  12. # The animations are set as the examples below:
  13. #   'name' => [frame_delay,'loop_mode',['frame1','frame2','frame3'...]]
  14. # Frame delay: how many game-frames will take for an image to switch
  15. # Loop modes: how to behave at the end of the animation
  16. #   'loop': repeats the animation again and again
  17. #   'once': when finished, stays on the last frame
  18. #   anything else: the animation will stick to the first frame
  19. # Frames: you can use any image that is on the pictures folder, you can
  20. #   use them more than once, and reuse them in different animations
  21. #   The number of frames is entirely up to you, with a minimum of 1
  22. #==============================================================================
  23.  
  24. $imported = {} if $imported.nil?
  25. $imported['Molegato-Picture_anim'] = true
  26.  
  27. #==============================================================================
  28. # CONFIGURATION, YOU CAN TOUCH THIS
  29. #==============================================================================
  30. module ANIM_PICTURES
  31.  
  32.   #'animation_name'=> {frames_per_still, loop_mode, {frame,frame,frame...}}
  33.   ANIMS={
  34.       'circle1' => [2, 'loop', ['circle1','circle2','circle3','circle4','circle5','circle6']],
  35.       'slow_anim' => [25, 'loop', ['test_1','test_2']],
  36.       'once_anim' => [10, 'once', ['test_1','test_2','test_3','test_4']],
  37.   }
  38.  
  39. end
  40. #==============================================================================
  41. # END OF CONFIGURATION. EVERYTHING UNDER HERE IS A HELLISH MESS OF DEFICIENT
  42. # AMATEUR SCRIPTING. BE AWARE THAT MESSING WITH IT CAN RESULT IN DISASTER
  43. #==============================================================================
  44. #==============================================================================
  45. # ■ Sprite_Picture
  46. #==============================================================================
  47.  
  48. class Sprite_Picture < Sprite
  49.  
  50.   alias anim_initialize initialize
  51.   def initialize(viewport, picture)
  52.     anim_initialize(viewport, picture)
  53.     animation_setup
  54.   end
  55.  
  56.   def animation_setup
  57.     if (ANIM_PICTURES::ANIMS.include?(@picture.name))
  58.       @animation=@picture.name
  59.       @animated=true
  60.       @anim_speed=ANIM_PICTURES::ANIMS[@picture.name][0]
  61.       @loop_mode=ANIM_PICTURES::ANIMS[@picture.name][1]
  62.       @frames=ANIM_PICTURES::ANIMS[@picture.name][2]
  63.       @frame_max=@frames.size
  64.       @frame_count=0.0
  65.       @frame_images=[]
  66.       @frames.each do |frame|
  67.         new_bitmap=Cache.picture(frame)
  68.         @frame_images.push(new_bitmap)
  69.       end
  70.     end
  71.   end
  72.  
  73.   alias anim_update update
  74.   def update
  75.     if (@picture.name=="")
  76.       @animated=false
  77.     end
  78.    
  79.     if (@animated)
  80.       if (@picture.name!=@animation)
  81.         animation_setup
  82.       end
  83.       update_animation
  84.     end
  85.     anim_update
  86.   end
  87.  
  88.   def update_animation
  89.     @frame_count+=1.0/@anim_speed
  90.     if (@loop_mode=='loop') #loop
  91.       if (@frame_count>=@frame_max)
  92.         @frame_count=0.0
  93.       end
  94.     elsif (@loop_mode=='once') #once
  95.       if (@frame_count>=@frame_max)
  96.         @frame_count=@frame_max-1
  97.       end
  98.     else  #no valid loop mode
  99.       @frame_count=0.0
  100.     end
  101.   end
  102.  
  103.   def update_bitmap
  104.     if (@animated)
  105.       self.bitmap = @frame_images[@frame_count.to_i]
  106.     else
  107.       self.bitmap = Cache.picture(@picture.name)
  108.     end
  109.   end
  110.  
  111. end
  112.  
  113. class Game_Picture
  114.  
  115.   alias anim_erase erase
  116.   def erase
  117.     anim_erase
  118.     @name = ""
  119.     @origin = 0
  120.   end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment