Skip to content

Instantly share code, notes, and snippets.

@themaxhero
Last active August 29, 2015 14:02
Show Gist options
  • Save themaxhero/2a356fe6a0609d8aa1a4 to your computer and use it in GitHub Desktop.
Save themaxhero/2a356fe6a0609d8aa1a4 to your computer and use it in GitHub Desktop.
RGSS Animation Class
#===============================================================================
# Util - Animation
#-------------------------------------------------------------------------------
=begin
License - MIT
Copyright (c) 2014 Marcelo Amancio de Lima Santos (maxhero)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=end
#===============================================================================
class Animation < Sprite
attr_accessor :index,
:size,
:frame_width,
:frame_height,
:type,
:rate,
:animating
#-----------------------------------------------------------------------------
#
#-----------------------------------------------------------------------------
def initialize(bitmap,size,frame_width,frame_height,rate,viewport = Viewport.new)
super(viewport)
@counter = 0
self.rate = rate
self.bitmap = bitmap
self.size = size
self.frame_width = frame_width
self.frame_height = frame_height
self.src_rect = Rect.new(0,0,frame_width,frame_height)
self.animating = false
self.index = 0
if self.frame_height == self.bitmap.height
self.type = :horiz_ani
elsif self.frame_width == self.bitmap.width
self.type = :vert_ani
else
self.type = :dual_ani
end
end
#-----------------------------------------------------------------------------
#
#-----------------------------------------------------------------------------
def start
self.animating = true
puts self.type
end
def force_frame(frame_id)
self.index = frame_id
puts "Forced frame: #{frame_id}"
update_frame
end
def stop
self.animating = false
end
#-----------------------------------------------------------------------------
#
#-----------------------------------------------------------------------------
def update
super
if (self.animating)
if @counter == self.rate
update_frame
self.index = ((self.index+1) % self.size)
end
@counter = (@counter+1) % (self.rate+1)
end
end
def update_frame
case self.type
when :horiz_ani
self.src_rect.x = self.index*self.frame_width
when :vert_ani
self.src_rect.y = self.index*self.frame_height
when :dual_ani
frames_per_line = (self.bitmap.width/self.frame_width)
rect = Rect.new(
self.index % frames_per_line * self.frame_width,
self.index / frames_per_line * self.frame_height,
self.frame_width,
self.frame_height
)
self.src_rect = rect
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment