Advertisement
Guest User

AnimatedPokemonBitmap

a guest
Jan 15th, 2015
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.17 KB | None | 0 0
  1. #===============================================================================
  2. #  New animation methods for Pokemon sprites
  3. #    by Luka S.J.
  4. #
  5. #  supports both animated, and static sprites
  6. #  does not support the usage of GIFs
  7. #  any one frame of sprite needs to be of equal width and height
  8. #  all sprites need to be in 1*1 px resolution
  9. #  use my GIF to PNG converter to properly format your sprites
  10. #  
  11. #  allows the use of custom looping points
  12. #===============================================================================
  13.  
  14. class AnimatedPokemonBitmap
  15.   attr_reader :width
  16.   attr_reader :height
  17.   attr_reader :totalFrames
  18.   attr_reader :currentIndex
  19.   attr_accessor :speed
  20.  
  21.   def initialize(file)
  22.     raise "filename is nil" if file==nil
  23.     @width = 0
  24.     @height = 0
  25.     @frame = 0
  26.     @direction = +1
  27.     @totalFrames = 0
  28.     @currentIndex = 0
  29.     @speed = 1
  30.       # 0 - not moving at all
  31.       # 1 - normal speed
  32.       # 2 - medium speed
  33.       # 3 - slow speed
  34.     @bitmap=BitmapCache.load_bitmap(file)
  35.       # initializes full Pokemon bitmap
  36.     @width=@bitmap.height*2
  37.     @height=@bitmap.height*2
  38.    
  39.     @totalFrames=@bitmap.width/@bitmap.height
  40.       # calculates total number of frames
  41.     @loop_points=[0,@totalFrames]
  42.       # first value is start, second is end
  43.    
  44.     @actualBitmap=Bitmap.new(@width,@height)
  45.     @actualBitmap.clear
  46.     @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
  47.   end
  48.    
  49.   def length; @totalFrames; end
  50.   def disposed?; @actualBitmap.disposed?; end
  51.   def dispose; @actualBitmap.dispose; end
  52.   def copy; @actualBitmap.clone; end
  53.   def bitmap; @actualBitmap; end
  54.  
  55.   def reverse
  56.     if @direction>0
  57.       @direction=-1
  58.     elsif @direction<0
  59.       @direction=+1
  60.     end
  61.   end
  62.  
  63.   def setLoop(start, finish)
  64.     @loop_points=[start,finish]
  65.   end
  66.  
  67.   def toFrame(frame)
  68.     if frame.is_a?(String)
  69.       if frame=="last"
  70.         frame=@totalFrames-1
  71.       else
  72.         frame=0
  73.       end
  74.     end
  75.     frame=@totalFrames if frame>@totalFrames
  76.     frame=0 if frame<0
  77.     @currentIndex=frame
  78.     @actualBitmap.clear
  79.     @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
  80.   end
  81.  
  82.   def update
  83.     return false if @speed<1
  84.     case @speed
  85.     # frame skip
  86.     when 1
  87.       frames=1
  88.     when 2
  89.       frames=2
  90.     when 3
  91.       frames=3
  92.     end
  93.     @frame+=1
  94.    
  95.     if @frame>=frames
  96.       # processes animation speed
  97.       @currentIndex+=@direction
  98.       @currentIndex=@loop_points[0] if @currentIndex>=@loop_points[1]
  99.       @currentIndex=@loop_points[1]-1 if @currentIndex<@loop_points[0]
  100.       @frame=0
  101.     end
  102.     @actualBitmap.clear
  103.     @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
  104.       # updates the actual bitmap
  105.   end
  106.    
  107.   # returns bitmap to original state
  108.   def deanimate
  109.     @frame=0
  110.     @currentIndex=0
  111.     @actualBitmap.clear
  112.     @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
  113.   end
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement