Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===============================================================================
- # New animation methods for Pokemon sprites
- # by Luka S.J.
- #
- # supports both animated, and static sprites
- # does not support the usage of GIFs
- # any one frame of sprite needs to be of equal width and height
- # all sprites need to be in 1*1 px resolution
- # use my GIF to PNG converter to properly format your sprites
- #
- # allows the use of custom looping points
- #===============================================================================
- class AnimatedPokemonBitmap
- attr_reader :width
- attr_reader :height
- attr_reader :totalFrames
- attr_reader :currentIndex
- attr_accessor :speed
- def initialize(file)
- raise "filename is nil" if file==nil
- @width = 0
- @height = 0
- @frame = 0
- @direction = +1
- @totalFrames = 0
- @currentIndex = 0
- @speed = 1
- # 0 - not moving at all
- # 1 - normal speed
- # 2 - medium speed
- # 3 - slow speed
- @bitmap=BitmapCache.load_bitmap(file)
- # initializes full Pokemon bitmap
- @width=@bitmap.height*2
- @height=@bitmap.height*2
- @totalFrames=@bitmap.width/@bitmap.height
- # calculates total number of frames
- @loop_points=[0,@totalFrames]
- # first value is start, second is end
- @actualBitmap=Bitmap.new(@width,@height)
- @actualBitmap.clear
- @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
- end
- def length; @totalFrames; end
- def disposed?; @actualBitmap.disposed?; end
- def dispose; @actualBitmap.dispose; end
- def copy; @actualBitmap.clone; end
- def bitmap; @actualBitmap; end
- def reverse
- if @direction>0
- @direction=-1
- elsif @direction<0
- @direction=+1
- end
- end
- def setLoop(start, finish)
- @loop_points=[start,finish]
- end
- def toFrame(frame)
- if frame.is_a?(String)
- if frame=="last"
- frame=@totalFrames-1
- else
- frame=0
- end
- end
- frame=@totalFrames if frame>@totalFrames
- frame=0 if frame<0
- @currentIndex=frame
- @actualBitmap.clear
- @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
- end
- def update
- return false if @speed<1
- case @speed
- # frame skip
- when 1
- frames=1
- when 2
- frames=2
- when 3
- frames=3
- end
- @frame+=1
- if @frame>=frames
- # processes animation speed
- @currentIndex+=@direction
- @currentIndex=@loop_points[0] if @currentIndex>=@loop_points[1]
- @currentIndex=@loop_points[1]-1 if @currentIndex<@loop_points[0]
- @frame=0
- end
- @actualBitmap.clear
- @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
- # updates the actual bitmap
- end
- # returns bitmap to original state
- def deanimate
- @frame=0
- @currentIndex=0
- @actualBitmap.clear
- @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/2),0,@width/2,@height/2))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement